mlsteam_model_sdk.sdk.model

Main model SDK interface

Classes

MVPackage(model, predictor, manifest, env[, ...])

Model version package.

Model([api_client, default_puuid, ...])

Provides high-level model operations.

ModelCleanupPolicy(value)

Represents model file cleanup policies.

class mlsteam_model_sdk.sdk.model.MVPackage(model, predictor: Callable, manifest: dict, env: dict, encrypted: bool = False, decdir: Optional[TemporaryDirectory] = None)

Bases: object

Model version package.

A delegator to make model operations such as prediction.

__init__(model, predictor: Callable, manifest: dict, env: dict, encrypted: bool = False, decdir: Optional[TemporaryDirectory] = None) None
close()

Manually closes the package.

It tries to release the enclosed packaged resources. Nothing will happen if the package is closed twice.

property closed: bool

Indicates whether the packaged is closed.

property encrypted: bool

Indicates whether the package is encrypted.

get_model(index: int) dict

Returns information for an included model.

property models: List[dict]

Returns information for all included models.

predict(inputs, *args, **kwargs) Any

Makes model predictions.

Parameters:
  • inputs – model inputs

  • *args – custom arguments

  • **kwargs – custom arguments

Returns:

model outputs

class mlsteam_model_sdk.sdk.model.Model(api_client: Optional[ApiClient] = None, default_puuid: Optional[str] = None, default_project_name: Optional[str] = None, default_muuid: Optional[str] = None, default_model_name: Optional[str] = None, offline: bool = False)

Bases: object

Provides high-level model operations.

__init__(api_client: Optional[ApiClient] = None, default_puuid: Optional[str] = None, default_project_name: Optional[str] = None, default_muuid: Optional[str] = None, default_model_name: Optional[str] = None, offline: bool = False) None

Initilizes a model operator.

Default project is determined with the following precedence:
  • default_puuid [argument]

  • default_project_name [argument]

  • default_puuid [config]

  • default_project_name [config]

  • None

Default muuid is determined with the following precedence:
  • default_muuid [argument]

  • default_model_name [argument]

  • default_muuid [config]

  • default_model_name [config]

  • None

Parameters:
  • api_client – API client. Creates a new API client if it is not given and not in offline mode.

  • default_puuid – default project uuid

  • default_project_name – default project name

  • default_muuid – default model uuid

  • default_model_name – default model name

  • offline – offline mode. Only offline operations could be used in offline mode if api_client is not given.

delete_model(muuid: Optional[str] = None, model_name: Optional[str] = None)

Deletes all model version packages of a model from local registry.

This method could be used offline.

The model should be given and is determined in the same way as in get_model().

Caution: Deleting a locally imported model (with import_model_version()) by muuid will delete all other locally imported models as well, since they share the same muuid.

Parameters:
  • muuid – model uuid

  • model_name – model name

delete_model_version(vuuid: Optional[str] = None, version_name: Optional[str] = None, muuid: Optional[str] = None, model_name: Optional[str] = None, delete_all: bool = False)

Deletes model version package from local registry.

This method could be used offline.

It suffices to specify only vuuid to determine the model version. Otherwise, both the model and the model version should be given.

Parameters:
  • vuuid – model version uuid

  • version_name – model version name

  • muuid – model uuid

  • model_name – model name

  • delete_all – delete all matching model versions

Raises:

MultipleModelVersionsException – there are multiple matching model versions and delete_all is not set

download_model_version(vuuid: Optional[str] = None, version_name: Optional[str] = None, muuid: Optional[str] = None, model_name: Optional[str] = None, puuid: Optional[str] = None, overwrite: bool = False, logging: bool = False) str

Downloads and extracts model version files or packages.

The model version should be given and is determined in the same way as in get_model_version().

The model should be given and is determined in the same way as in get_model().

Parameters:
  • vuuid – model version uuid

  • version_name – model version name

  • muuid – model uuid

  • model_name – model name

  • puuid – optional, project uuid to use rather than the default project

  • overwrite – overwrite the existing downloaded files if there is any

  • logging – enable logging

Returns:

path of model version extraction directory

Raises:
  • ValueError – An error occurred determining the project, the model, or the model version.

  • FileExistsError – Model version exists and overwrite is not set.

get_model(muuid: Optional[str] = None, model_name: Optional[str] = None, puuid: Optional[str] = None) dict

Gets model info.

The model should be given and is determined with the following precedence:
  • muuid

  • model_name

  • default model

Parameters:
  • muuid – model uuid

  • model_name – model name

  • puuid – optional, project uuid to use rather than the default project

Returns:

model info

Raises:

ValueError – An error occurred determining the project or the model.

get_model_version(vuuid: Optional[str] = None, version_name: Optional[str] = None, muuid: Optional[str] = None, model_name: Optional[str] = None, puuid: Optional[str] = None) dict

Gets model version info.

The model version should be given and is determined with the following precedence:
  • muuid

  • model_name

The model should be given and is determined in the same way as in get_model().

Parameters:
  • vuuid – model version uuid

  • version_name – model version name

  • muuid – model uuid

  • model_name – model name

  • puuid – optional, project uuid to use rather than the default project

Returns:

model version

Raises:

ValueError – An error occurred determining the project, the model, or the model version.

get_model_version_dir(vuuid: Optional[str] = None, version_name: Optional[str] = None, muuid: Optional[str] = None, model_name: Optional[str] = None) str

Gets model version storage path.

This method could be used offline. It assumes has been downloaded by download_model_version(). NOTE: It only supports plaintext or plaintext-packaged model versions.

It suffices to specify only vuuid to determine the model version. Otherwise, both the model and the model version should be given.

Parameters:
  • vuuid – model version uuid

  • version_name – model version name

  • muuid – model uuid

  • model_name – model name

Returns:

model version storage path

Raises:
  • ValueError – An error occurred determining the project or the model or an encrypted model version is specified.

  • MLSteamException – Illegal operation.

import_model_version(mv_package_file: PathLike, enckey_file: Optional[PathLike] = None, model_name: Optional[str] = None, version_name: Optional[str] = None, logging: bool = False) str

Imports a model version package from local files.

This method could be used offline.

enckey_file should be given if the package is encrypted.

When model_name or version_name is not given, the corresponding fields in the package manifest are used instead.

Parameters:
  • mv_package_file – model version package file path

  • enckey_file – enckey file path

  • model_name – model name to register

  • version_name – model version name to register

  • logging – enable logging

Returns:

local vuuid for the imported model version

Raises:

ValueError – An error occurred determining the the model name or the version name.

list_model_versions(muuid: Optional[str] = None, model_name: Optional[str] = None, puuid: Optional[str] = None) List[dict]

Lists model versions.

The model should be given and is determined in the same way as in get_model().

Parameters:
  • muuid – model uuid

  • model_name – model name

  • puuid – optional, project uuid to use rather than the default project

Returns:

model versions

Raises:

ValueError – An error occurred determining the project or the model.

list_models(puuid: Optional[str] = None) List[dict]

Lists models.

Parameters:

puuid – optional, project uuid to use rather than the default project

Returns:

models

Raises:

ValueError – An error occurred determining the project.

load_model_version(vuuid: Optional[str] = None, version_name: Optional[str] = None, muuid: Optional[str] = None, model_name: Optional[str] = None, *args, **kwargs) MVPackage

Loads model version package.

This method could be used offline. It assumes the model version is packaged and has been downloaded by download_model_version().

It suffices to specify only vuuid to determine the model version. Otherwise, both the model and the model version should be given.

NOTE: When an encrypted model version packaged gets loaded twice, all previous loded hook modules for that package will be wiped out to avoid module loading errors. That is, all previous returned model version packages for that module are no longer usable. Make sure not to keep and use more than one model version package for a certain model version at the same time.

Parameters:
  • vuuid – model version uuid

  • version_name – model version name

  • muuid – model uuid

  • model_name – model name

  • *args – custom arguments

  • **kwargs – custom arguments

Returns:

model version package

Raises:
  • ValueError – An error occurred determining the project or the model.

  • MLSteamException – Illegal operation.

to_muuid(model_name: str, puuid: Optional[str] = None) Optional[str]

Converts model name to model uuid.

It returns None in offline mode.

to_puuid(project_name: str) Optional[str]

Converts project name to project uuid.

It returns None in offline mode.

to_vuuid(version_name: str, muuid: Optional[str] = None, puuid: Optional[str] = None) str

Converts model version name to model version uuid.

class mlsteam_model_sdk.sdk.model.ModelCleanupPolicy(value)

Bases: Enum

Represents model file cleanup policies.

AFTER_LOAD = 'after-load'
NEVER = 'never'