Skip to content

Model base

This module contains the base Model class

Model is the base model class. It contains a loading and downloading methods that are used by default to download your model into your Docker container and load it into your application.

To use a custom model class in your application, create a new module such as model_awesome.py in this package and write a custom class that overwrite _load_model, download_model or predict depending on your needs.

Model

Parent model class.

This class is given as an exemple, you should probably adapt it to your project. This class loads the model from a .pkl file. The model must have a predict function.

Source code in template_api/model/model_base.py
class Model:
    """Parent model class.

    This class is given as an exemple, you should probably adapt it to your project.
    This class loads the model from a .pkl file. The model must have a predict function.
    """

    def __init__(self):
        '''Init. model class'''
        self._model = None
        self._model_conf = None
        self._model_explainer = None
        self._loaded = False

    def is_model_loaded(self):
        """return the state of the model"""
        return self._loaded

    def loading(self, **kwargs):
        """load the model"""
        self._load_model(**kwargs)
        self._loaded = True

    def predict(self, *args, **kwargs):
        """Make a prediction thanks to the model"""
        return self._model.predict(*args, **kwargs)

    def explain_as_json(self, *args, **kwargs) -> Union[dict, list]:
        """Compute explanations about a prediction and return a JSON serializable object"""
        return self._model_explainer.explain_instance_as_json(*args, **kwargs)

    def explain_as_html(self, *args, **kwargs) -> str:
        """Compute explanations about a prediction and return an HTML report"""
        return self._model_explainer.explain_instance_as_html(*args, **kwargs)

    def _load_model(self, **kwargs) -> None:
        """Load a model from a file

        Returns:
            Tuple[Any, dict]: A tuple containing the model and a dict of metadata about it.
        """
        settings = ModelSettings(**kwargs)

        logger.info(f"Loading the model from {settings.model_path}")
        with settings.model_path.open("rb") as f:
            self._model = pickle.load(f)

        self._model_conf = {
            "model_path": settings.model_path.name,
            "model_name": settings.model_path.stem,
        }
        logger.info(f"Model loaded")

    @staticmethod
    def download_model(**kwargs) -> bool:
        """You should implement a download method to automatically download your model"""
        logger.info("The function download_model is empty. Implement it to automatically download your model.")
        return True

__init__()

Init. model class

Source code in template_api/model/model_base.py
def __init__(self):
    '''Init. model class'''
    self._model = None
    self._model_conf = None
    self._model_explainer = None
    self._loaded = False

download_model(**kwargs) staticmethod

You should implement a download method to automatically download your model

Source code in template_api/model/model_base.py
@staticmethod
def download_model(**kwargs) -> bool:
    """You should implement a download method to automatically download your model"""
    logger.info("The function download_model is empty. Implement it to automatically download your model.")
    return True

explain_as_html(*args, **kwargs)

Compute explanations about a prediction and return an HTML report

Source code in template_api/model/model_base.py
def explain_as_html(self, *args, **kwargs) -> str:
    """Compute explanations about a prediction and return an HTML report"""
    return self._model_explainer.explain_instance_as_html(*args, **kwargs)

explain_as_json(*args, **kwargs)

Compute explanations about a prediction and return a JSON serializable object

Source code in template_api/model/model_base.py
def explain_as_json(self, *args, **kwargs) -> Union[dict, list]:
    """Compute explanations about a prediction and return a JSON serializable object"""
    return self._model_explainer.explain_instance_as_json(*args, **kwargs)

is_model_loaded()

return the state of the model

Source code in template_api/model/model_base.py
def is_model_loaded(self):
    """return the state of the model"""
    return self._loaded

loading(**kwargs)

load the model

Source code in template_api/model/model_base.py
def loading(self, **kwargs):
    """load the model"""
    self._load_model(**kwargs)
    self._loaded = True

predict(*args, **kwargs)

Make a prediction thanks to the model

Source code in template_api/model/model_base.py
def predict(self, *args, **kwargs):
    """Make a prediction thanks to the model"""
    return self._model.predict(*args, **kwargs)

ModelSettings

Bases: BaseSettings

Download settings

This class is used for settings management purpose, have a look at the pydantic documentation for more details : https://pydantic-docs.helpmanual.io/usage/settings/

By default, it looks for environment variables (case insensitive) to set the settings if a variable is not found, it looks for a file name .env in your working directory where you can declare the values of the variables and finally it sets the values to the default ones you can see above.

Source code in template_api/model/model_base.py
class ModelSettings(BaseSettings):
    """Download settings

    This class is used for settings management purpose, have a look at the pydantic
    documentation for more details : https://pydantic-docs.helpmanual.io/usage/settings/

    By default, it looks for environment variables (case insensitive) to set the settings
    if a variable is not found, it looks for a file name .env in your working directory
    where you can declare the values of the variables and finally it sets the values
    to the default ones you can see above.
    """

    model_path: Path = DEFAULT_MODEL_PATH

    model_config = SettingsConfigDict(env_file=".env", extra='ignore', protected_namespaces=('settings', ))