API documentation
Base Classes
Base class for building ML models that are easy to deploy and integrate.
MLModel
Bases: ABC
Base class for ML model prediction code.
Source code in ml_base/ml_model.py
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
description: str
property
abstractmethod
Abstract property that returns a description of the model.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The description of the model. |
display_name: str
property
abstractmethod
Abstract property that returns a display name for the model.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The display name of the model. |
Note
This is a name for the model that looks good in user interfaces.
input_schema: BaseModel
property
abstractmethod
Property that returns the schema that is accepted by the predict() method.
Returns:
Type | Description |
---|---|
BaseModel
|
pydantic.BaseModel: The input schema of the model. |
Note
This property must return a subtype of pydantic.BaseModel.
output_schema: BaseModel
property
abstractmethod
Property returns the schema that is returned by the predict() method.
Returns:
Type | Description |
---|---|
BaseModel
|
pydantic.BaseModel: The output schema of the model. |
Note
This property must return a subtype of pydantic.BaseModel.
qualified_name: str
property
abstractmethod
Abstract property that returns the qualified name of the model.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The qualified name of the model. |
Warning
A qualified name is an unambiguous identifier for the model.
version: str
property
abstractmethod
Abstract property that returns the model's version as a string.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The version of the model. |
__init__()
abstractmethod
Create an MLModel instance by adding any deserialization and initialization code for the model.
Source code in ml_base/ml_model.py
91 92 93 94 |
|
__repr__()
Return a string representing the model object.
Source code in ml_base/ml_model.py
9 10 11 |
|
predict(data)
abstractmethod
Prediction with the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
BaseModel
|
data used by the model for making a prediction |
required |
Returns:
Name | Type | Description |
---|---|---|
object |
BaseModel
|
can be any python type |
Source code in ml_base/ml_model.py
96 97 98 99 100 101 102 103 104 105 106 107 |
|
MLModelException
Bases: Exception
Exception base class used to raise exceptions within MLModel derived classes.
Source code in ml_base/ml_model.py
110 111 112 113 114 115 |
|
__init__(*args)
Initialize MLModelException instance.
Source code in ml_base/ml_model.py
113 114 115 |
|
MLModelSchemaValidationException
Bases: MLModelException
Exception type used to raise schema validation exceptions within MLModel derived classes.
Source code in ml_base/ml_model.py
118 119 120 121 122 123 |
|
__init__(*args)
Initialize MLModelSchemaValidationException instance.
Source code in ml_base/ml_model.py
121 122 123 |
|
Base class for building decorators for MLModel objects.
MLModelDecorator
Bases: MLModel
Base class for ML model decorator code.
Note
The default behavior of the MLModelDecorator base class is to do nothing and to forward the method call to the model that is is wrapping. Any subtypes of MLModelDecorator that would like to add on to the behavior of the model needs to override the default implementations in the MLModelDecorator base class.
Source code in ml_base/decorator.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
description: str
property
Property that returns a description of the model.
Note
Unless this method is overridden, the implementation just returns the description property of the model that is being decorated.
display_name: str
property
Property that returns a display name for the model.
Note
Unless this method is overridden, the implementation just returns the display_name property of the model that is being decorated.
input_schema: BaseModel
property
Property that returns the schema that is accepted by the predict() method.
Note
Unless this method is overridden, the implementation just returns the input_schema property of the model that is being decorated.
output_schema: BaseModel
property
Property returns the schema that is returned by the predict() method.
Note
Unless this method is overridden, the implementation just returns the output_schema property of the model that is being decorated.
qualified_name: str
property
Property that returns the qualified name of the model.
Note
Unless this method is overridden, the implementation just returns the qualified_name property of the model that is being decorated.
version: str
property
Property that returns the model's version as a string.
Note
Unless this method is overridden, the implementation just returns the version property of the model that is being decorated.
__delattr__(name)
Delete an attribute.
Source code in ml_base/decorator.py
62 63 64 |
|
__getattr__(name)
Get an attribute.
Source code in ml_base/decorator.py
48 49 50 51 52 53 |
|
__init__(model=None, **kwargs)
Initialize MLModelDecorator instance.
Note
The MLModel parameter is optional and does not need to be provided at initialization of the decorator instance.
Note
This method receives the model instance and stores the reference.
Source code in ml_base/decorator.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
__repr__()
Return a string describing the decorator and the model that it is decorating.
Source code in ml_base/decorator.py
36 37 38 |
|
__setattr__(name, value)
Set an attribute.
Source code in ml_base/decorator.py
55 56 57 58 59 60 |
|
predict(data)
Predict with the model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
BaseModel
|
Data used by the model for making a prediction. |
required |
Returns:
Type | Description |
---|---|
BaseModel
|
python object -- can be any python type |
Note
Unless this method is overridden, the implementation just calls the predict method of the model that is being decorated and returns the result.
Source code in ml_base/decorator.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
set_model(model)
Set a model in the decorator instance.
Source code in ml_base/decorator.py
40 41 42 43 44 45 46 |
|
Utilities
Bases: object
Singleton class that instantiates and manages model objects.
Source code in ml_base/utilities/model_manager.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
__init__()
Construct ModelManager object.
Source code in ml_base/utilities/model_manager.py
22 23 24 25 26 |
|
__new__()
Create and return a new ModelManager instance, after instance is first created it will always be returned.
Source code in ml_base/utilities/model_manager.py
14 15 16 17 18 19 20 |
|
add_decorator(qualified_name, decorator)
Add a decorator to a model object by qualified name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
qualified_name |
str
|
The qualified name of the model to add decorator to. |
required |
decorator |
MLModelDecorator
|
MLModelDecorator instance to apply to model instance. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Raises:
Type | Description |
---|---|
ValueError
|
Raised if a model with the qualified name can't be found in the ModelManager singleton. |
Source code in ml_base/utilities/model_manager.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
add_model(model)
Add a model to the ModelManager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
MLModel
|
instance of MLModel |
required |
Source code in ml_base/utilities/model_manager.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
clear_instance()
classmethod
Clear singleton instance from class.
Source code in ml_base/utilities/model_manager.py
28 29 30 31 |
|
get_model(qualified_name)
Get a model object by qualified name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
qualified_name |
str
|
The qualified name of the model to be returned. |
required |
Returns:
Type | Description |
---|---|
MLModel
|
Model object |
Raises:
Type | Description |
---|---|
ValueError
|
Raised if a model with the qualified name can't be found in the ModelManager singleton. |
Source code in ml_base/utilities/model_manager.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
get_model_metadata(qualified_name)
Get model metadata by qualified name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
qualified_name |
str
|
Qualified name of the model for which to get metadata |
required |
Returns:
Type | Description |
---|---|
dict
|
Dictionary containing information about a model in the ModelManager singleton. |
Note
The dictionaries in the list returned by this method contain these keys:
- display_name
- qualified_name
- description
- version
- input_schema
- output_schema
Source code in ml_base/utilities/model_manager.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
get_models()
Get a list of models in the model manager singleton.
Returns:
Type | Description |
---|---|
List[dict]
|
List of dictionaries containing information about the model instances in the ModelManager singleton. |
Note
The dictionaries in the list returned by this method contain these keys:
- display_name
- qualified_name
- description
- version
Source code in ml_base/utilities/model_manager.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
load_model(class_path)
Import and instantiate an MLModel object from a class path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_path |
str
|
Class path to the model's MLModel class. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
Raised if the model is not a subtype of MLModel, or if a model with the same qualified name is already loaded in the ModelManager. |
Source code in ml_base/utilities/model_manager.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
remove_model(qualified_name)
Remove an MLModel object from the ModelManager singleton.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
qualified_name |
str
|
The qualified name of the model to be returned. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
Raised if a model with the qualified name can't be found in the ModelManager singleton. |
Source code in ml_base/utilities/model_manager.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|