Model module#

Module with all classes and methods to manage the Machine Learning (ML) models deployed at MLOps.

MLOpsModel#

class mlops_codex.model.MLOpsModel(*, name: str, model_hash: str, group: str, login: str | None = None, password: str | None = None, group_token: str | None = None, url: str | None = None)[source]#

Bases: BaseMLOps

Class to manage Models deployed inside MLOps

Parameters:
  • login (str) – Login for authenticating with the client. You can also use the env variable MLOPS_USER to set this

  • password (str) – Password for authenticating with the client. You can also use the env variable MLOPS_PASSWORD to set this

  • model_hash (str) – Model id (hash) from the model you want to access

  • group (str) – Group the model is inserted.

  • group_token (str) – Token for executing the model (show when creating a group). It can be informed when getting the model or when running predictions, or using the env variable MLOPS_GROUP_TOKEN

  • url (str) – URL to MLOps Server. Default value is https://neomaril.datarisk.net/, use it to test your deployment first before changing to production. You can also use the env variable MLOPS_URL to set these

Raises:
delete()[source]#

Deletes the current model.

Raises:
disable()[source]#

Disables a model. It means that you won’t be able to perform some operations in the model Please, check with your team if you’re allowed to perform this operation

Raises:
execution_info(execution_id: str)[source]#

Show the model execution data in a better format

get_logs(*, start: str | None = None, end: str | None = None, routine: str | None = None, log_type: str | None = None)[source]#

Get the logs

Parameters:
  • start (Optional[str], default=None) – Date to start filter. At the format aaaa-mm-dd

  • end (Optional[str], default=None) – Date to end filter. At the format aaaa-mm-dd

  • routine (Optional[str], default=None) – Type of routine being executed, can assume values Host or Run

  • log_type (Optional[str], default=None) – Defines the type of the logs that are going to be filtered, can assume the values Ok, Error, Debug or Warning

Raises:

ServerError – Unexpected server error

Returns:

Logs list

Return type:

dict

health()[source]#

Get the model health state.

Raises:
host(operation)[source]#

Builds the model execution environment

Parameters:

operation (str) – The model operation type (Sync or Async)

Raises:

InputError – Some input parameters is invalid

host_monitoring(period: str)[source]#

Host the monitoring configuration

Parameters:

period (str) – The monitoring period (Day, Week, Month)

Raises:

InputError – Monitoring host error

host_monitoring_status(period: str)[source]#

Get the host status for the monitoring configuration

Parameters:

period (str) – The monitoring period (Day, Week, Month)

Raises:
info() None[source]#

Show the model data in a better format

register_monitoring(*, preprocess_reference: str, shap_reference: str, configuration_file: str | dict, preprocess_file: str | None = None, requirements_file: str | None = None, wait_complete: bool | None = False) str[source]#

Register the model monitoring configuration at the database

Parameters:
  • preprocess_reference (str) – Name of the preprocess reference

  • shap_reference (str) – Name of the preprocess function

  • configuration_file (str or dict) – Path of the configuration file in json format. It can also be a dict

  • preprocess_file (Optional[str], default=None) – Path of the preprocess script

  • requirements_file (Optional[str], default=None) – Path of the requirements file

  • wait_complete (bool, default=False) – If it is True, wait until the monitoring host is Deployed or Failed

Raises:

InputError – Invalid parameters for model creation

Returns:

Model id (hash)

Return type:

str

restart_model(wait_for_ready: bool = True)[source]#
Restart a model deployment process health state. The model will be restarted if the state is one of following states:
  • Deployed;

  • Disabled;

  • DisabledRecovery;

  • FailedRecovery.

Parameters:

wait_for_ready (bool, default=True) – If the model is being deployed, wait for it to be ready instead of failing the request

Raises:
set_token(group_token: str) None[source]#

Saves the group token for this model instance.

Parameters:

group_token (str) – Token for executing the model (show when creating a group). You can set this using the MLOPS_GROUP_TOKEN env variable

status()[source]#

Gets the status of the model.

Raises:

ModelError – Execution unavailable

Returns:

The model status

Return type:

str

wait_monitoring(period: str)[source]#

Wait for the monitoring configuration

Parameters:

(str) (period) –

wait_ready()[source]#

Waits the model to be with status different from Ready or Building

MLOpsModelClient#

class mlops_codex.model.MLOpsModelClient(*, login: str | None = None, password: str | None = None, url: str | None = None)[source]#

Bases: BaseMLOpsClient

Class for client to access MLOps and manage models

Parameters:
  • login (str) – Login for authenticating with the client. You can also use the env variable MLOPS_USER to set this

  • password (str) – Password for authenticating with the client. You can also use the env variable MLOPS_PASSWORD to set this

  • url (str) – URL to MLOps Server. Default value is https://neomaril.datarisk.net, use it to test your deployment first before changing to production. You can also use the env variable MLOPS_URL to set this

Raises:

Example

Example 1: Creation and managing a Synchronous Model

from mlops_codex.model import MLOpsModelClient
from mlops_codex.model import MLOpsModel

def new_sync_model(client, group, data_path):
    model = client.create_model('Model Example Sync',
                        'score',
                        data_path+'app.py',
                        data_path+'model.pkl',
                        data_path+'requirements.txt',
                        data_path+'schema.json',
                        group=group,
                        operation="Sync"
                        )

    model.register_monitoring('parse',
                    'get_shap',
                    configuration_file=data_path+'configuration.json',
                    preprocess_file=data_path+'preprocess.py',
                    requirements_file=data_path+'requirements.txt'
                    )

    return model.model_hash

client = MLOpsModelClient('123456')
client.create_group('ex_group', 'Group for example purpose')

data_path = './samples/syncModel/'

model_hash = new_sync_model(client, 'ex_group', data_path)

model_list = client.search_models()
print(model_list)

model = client.get_model(model_hash, 'ex_group')

print(model.health())

model.wait_ready()
model.predict(model.schema)

print(model.get_logs(routine='Run'))

Example 2: creation and deployment of a Asynchronous Model

from mlops_codex.model import MLOpsModelClient
from mlops_codex.model import MLOpsModel

def new_async_model(client, group, data_path):
    model = client.create_model('Teste notebook Async',
                    'score',
                    data_path+'app.py',
                    data_path+'model.pkl',
                    data_path+'requirements.txt',
                    group=group,
                    python_version='3.9',
                    operation="Async",
                    input_type='csv'
                    )

    return model.model_hash

def run_model(client, model_hash, data_path):
    model = client.get_model(model_hash, 'ex_group')

    execution = model.predict(data_path+'input.csv')

    return execution

client = MLOpsModelClient('123456')
client.create_group('ex_group', 'Group for example purpose')

data_path = './samples/asyncModel/'

model_hash = new_async_model(client, 'ex_group', data_path)

execution = run_model(client, model_hash, data_path)

execution.get_status()

execution.download_result()
create_model(*, model_name: str, model_reference: str, source_file: str, model_file: str, requirements_file: str, group: str, input_type: str, schema: str | dict | None = None, extra_files: list | None = None, env: str | None = None, python_version: str = '3.10', operation: str | None = 'Sync', wait_for_ready: bool = True) MLOpsModel[source]#

Deploy a new model to MLOps.

Parameters:
  • model_name (str) – The name of the model, in less than 32 characters

  • model_reference (str) – The name of the scoring function inside the source file

  • source_file (str) – Path of the source file. The file must have a scoring function that accepts two parameters: data (data for the request body of the model) and model_path (absolute path of where the file is located)

  • model_file (str) – Path of the model pkl file

  • requirements_file (str) – Path of the requirements file. The packages versions must be fixed eg: pandas==1.0

  • schema (Union[str, dict]) – Path to a JSON or XML file with a sample of the input for the entrypoint function. A dict with the sample input can be sending as well. Mandatory for Sync models

  • group (str) – Group the model is inserted.

  • extra_files (list, optional) – A optional list with additional files paths that should be uploaded. If the scoring function refer to this file they will be on the same folder as the source file

  • env (str, optional) – .env file to be used in your model environment. This will be encrypted in the server.

  • python_version (str, default="3.10") – Python version for the model environment. Available versions are 3.8, 3.9, 3.10.

  • operation (Optional[str], default="Sync") – Defines which kind operation is being executed (Sync or Async)

  • input_type (str) – The type of the input file that should be ‘json’, ‘csv’ or ‘parquet’

  • wait_for_ready (bool, optional) – Wait for model to be ready and returns a MLOpsModel instance with the new model

Raises:

InputError – Some input parameters is invalid

Returns:

Returns the new model, if wait_for_ready=True runs the deployment process synchronously. If it’s False, returns nothing after sending all the data to server and runs the deployment asynchronously

Return type:

MLOpsModel

get_logs(*, model_hash, start: str | None = None, end: str | None = None, routine: str | None = None, log_type: str | None = None)[source]#

Get the logs

Parameters:
  • model_hash (str) – Model id (hash)

  • start (Optional[str], default=None) – Date to start filter. At the format aaaa-mm-dd

  • end (Optional[str], default=None) – Date to end filter. At the format aaaa-mm-dd

  • routine (Optional[str], default=None) – Type of routine being executed, can assume values ‘Host’ (for deployment logs) or ‘Run’ (for execution logs)

  • log_type (Optional[str], default=None) – Defines the type of the logs that are going to be filtered, can assume the values ‘Ok’, ‘Error’, ‘Debug’ or ‘Warning’

Raises:

ServerError – Unexpected server error

Returns:

Logs list

Return type:

dict

get_model(model_hash: str, group: str, group_token: str | None = None) MLOpsModel[source]#

Acess a model using its id

Parameters:
  • model_hash (str) – Model id (hash) that needs to be acessed

  • group (str) – Group the model was inserted

  • group_token (Optional[str], default = None) – Token of the group being accessed

Raises:
Returns:

A MLOpsModel instance with the model hash from model_hash

Return type:

MLOpsModel

Example

>>> model.get_model(model_hash='M9c3af308c754ee7b96b2f4a273984414d40a33be90242908f9fc4aa28ba8ec4')
get_model_execution(*, model_hash: str, exec_id: str, group: str | None = None) MLOpsModel | None[source]#

Get an execution instace (Async model only).

Parameters:
  • model_hash (str) – Model id (hash)

  • exec_id (str) – Execution id

  • group (str, optional) – Group name, default value is None

Returns:

The new execution

Return type:

MLOpsExecution

Example

>>> model.get_model_execution(model_hash=,exec_id='1')
search_models(*, name: str | None = None, state: str | None = None, group: str | None = None, only_deployed: bool = False) list[source]#

Search for models using the name of the model

Parameters:
  • name (Optional[str], default=None) – Text that it’s expected to be on the model name. It runs similar to a LIKE query on SQL

  • state (Optional[str], default=None) – Text that it’s expected to be on the state. It runs similar to a LIKE query on SQL

  • group (Optional[str], default=None) – Text that it’s expected to be on the group name. It runs similar to a LIKE query on SQL

  • only_deployed (Optional[bool], default=False) – If it’s True, filter only models ready to be used (status == “Deployed”).

Raises:

ServerError – Unexpected server error

Returns:

A list with the models data, it can works like a filter depending on the arguments values

Return type:

list

Example

>>> client.search_models(group='ex_group', only_deployed=True)
status(model_hash: str, group: str)[source]#

Gets the status of the model with the hash equal to model_hash

Parameters:
  • model_hash (str) – Model id (hash) from the model being searched

  • group (str) – Group the model is inserted

Raises:

ModelError – Model unavailable

Returns:

The model status and a message if the status is ‘Failed’

Return type:

dict