The mlflow.models module provides an API for saving machine learning models in “flavors” that can be understood by different downstream tools.

The built-in flavors are:

For details, see MLflow Models.

class mlflow.models.FlavorBackend(config, **kwargs)

Bases: object

Abstract class for Flavor Backend. This class defines the API interface for local model deployment of MLflow model flavors.

Returns:True if this flavor has a build_image method defined for building a docker container capable of serving the model, False otherwise.

Check whether this flavor backend can be deployed in the current environment.

Returns:True if this flavor backend can be applied int he current environment.
predict(model_uri, input_path, output_path, content_type, json_format)

Generate predictions using a saved MLflow model referenced by the given URI. Input and output are read from and written to a file or stdin / stdout.

  • model_uri – URI pointing to the MLflow model to be used for scoring.
  • input_path – Path to the file with input data. If not specified, data is read from stdin.
  • output_path – Path to the file with output predictions. If not specified, data is written to stdout.
  • content_type – Specifies the input format. Can be one of {‘json’, ‘csv’}
  • json_format – Only applies if content_type == ‘json’. Specifies how is the input data encoded in json. Can be one of {‘split’, ‘records’} mirroring the behavior of Pandas orient attribute. The default is ‘split’ which expects dict like data: {'index' -> [index], 'columns' -> [columns], 'data' -> [values]}, where index is optional. For more information see “ pandas-docs/stable/reference/api/pandas.read_json.html”
serve(model_uri, port, host)

Serve saved MLflow model locally. :param model_uri: URI pointing to the MLflow model to be used for scoring. :param port: Port to deploy the model to. :param host: Host to use for the model deployment. Defaults to ‘localhost’.

class mlflow.models.Model(artifact_path=None, run_id=None, utc_time_created=None, flavors=None)

Bases: object

An MLflow Model that can support multiple model flavors. Provides APIs for implementing new Model flavors.

add_flavor(name, **params)

Add an entry for how to serve the model in a given format.

classmethod load(path)

Load a model from its YAML representation.

classmethod log(artifact_path, flavor, **kwargs)

Log model using supplied flavor module.

  • artifact_path – Run relative path identifying the model.
  • flavor – Flavor module to save the model with. The module must have the save_model function that will persist the model as a valid MLflow model.
  • kwargs – Extra args passed to the model flavor.

Write the model as a local YAML file.