Model Manager - In-memory model loading and caching for serving.
Handles loading trained models into memory, caching for performance, and provides a unified interface for predictions.
Classes
Manage loaded models for serving predictions.
Provides thread-safe model loading, caching, and prediction interface with automatic LRU eviction when cache is full.
Constructor
__init__( self, max_models: int = 10, )
Parameters
max_models
int
= 10
Maximum number of models to keep in memory. Older models are evicted when limit is reached.
python
>>> from tuiml.serving import ModelManager
>>> manager = ModelManager(max_models=5)
>>> manager.load("classifier", "model.pkl")
>>> predictions = manager.predict("classifier", X)
>>> manager.list_models()
['classifier']
Methods
load
(self, model_id: str, path: Union[str, Path], metadata: Optional[Dict]=None) -> Dict[str, Any]
load
(self, model_id: str, path: Union[str, Path], metadata: Optional[Dict]=None) -> Dict[str, Any]
Load a model into memory.
Parameters
model_id
str
Unique identifier for the model.
path
str or Path
Path to the saved model file.
metadata
dict
Additional metadata to associate with the model.
Returns
dict
Model info including class name, parameters, and load time.
Raises
FileNotFoundError
If the model file does not exist.
predict
(self, model_id: str, X: Union[np.ndarray, List]) -> np.ndarray
predict
(self, model_id: str, X: Union[np.ndarray, List]) -> np.ndarray
Make predictions using a loaded model.
Parameters
model_id
str
Identifier of the model to use.
X
array-like
Input features for prediction.
Returns
ndarray
Model predictions.
Raises
KeyError
If model is not loaded.
ValueError
If input format is invalid.
predict_proba
(self, model_id: str, X: Union[np.ndarray, List]) -> np.ndarray
predict_proba
(self, model_id: str, X: Union[np.ndarray, List]) -> np.ndarray
Get prediction probabilities using a loaded model.
Parameters
model_id
str
Identifier of the model to use.
X
array-like
Input features for prediction.
Returns
ndarray
Class probabilities with shape (n_samples, n_classes).
Raises
KeyError
If model is not loaded.
NotImplementedError
If model doesn't support probability predictions.