mlflow.azureml
The mlflow.azureml
module provides an API for deploying MLflow models to Azure
Machine Learning.
-
mlflow.azureml.
build_image
(model_path, workspace, run_id=None, image_name=None, model_name=None, mlflow_home=None, description=None, tags=None, synchronous=True) Register an MLflow model with Azure ML and build an Azure ML ContainerImage for deployment. The resulting image can be deployed as a web service to Azure Container Instances (ACI) or Azure Kubernetes Service (AKS).
The resulting Azure ML ContainerImage will contain a webserver that processes model queries. For information about the input data formats accepted by this webserver, see the MLflow deployment tools documentation.
Parameters: - model_path – The path to MLflow model for which the image will be built. If a run id is specified, this is should be a run-relative path. Otherwise, it should be a local path.
- run_id – MLflow run ID.
- image_name – The name to assign the Azure Container Image that will be created. If unspecified, a unique image name will be generated.
- model_name – The name to assign the Azure Model will be created. If unspecified, a unique model name will be generated.
- workspace – The AzureML workspace in which to build the image. This is a azureml.core.Workspace object.
- mlflow_home – Path to a local copy of the MLflow GitHub repository. If specified, the image will install MLflow from this directory. Otherwise, it will install MLflow from pip.
- description – A string description to associate with the Azure Container Image and the Azure Model that will be created. For more information, see https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.image.container.containerimageconfig and https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.model.model?view=azure-ml-py#register.
- tags –
A collection of tags, represented as a dictionary of string key-value pairs, to associate with the Azure Container Image and the Azure Model that will be created. These tags will be added to a set of default tags that include the model path, the model run id (if specified), and more. For more information, see https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.image.container.containerimageconfig and https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.model.model?view=azure-ml-py#register.
- synchronous – If True, this method will block until the image creation procedure terminates before returning. If False, the method will return immediately, but the returned image will not be available until the asynchronous creation process completes. The azureml.core.Image.wait_for_creation() function can be used to wait for the creation process to complete.
Returns: A tuple containing the following elements in order: - An azureml.core.image.ContainerImage object containing metadata for the new image. - An azureml.core.model.Model object containing metadata for the new model.
>>> import mlflow.azureml >>> from azureml.core import Workspace >>> from azureml.core.webservice import AciWebservice, Webservice >>> >>> # Load or create an Azure ML Workspace >>> workspace_name = "<Name of your Azure ML workspace>" >>> subscription_id = "<Your Azure subscription ID>" >>> resource_group = "<Name of the Azure resource group in which to create Azure ML resources>" >>> location = "<Name of the Azure location (region) in which to create Azure ML resources>" >>> azure_workspace = Workspace.create(name=workspace_name, >>> subscription_id=subscription_id, >>> resource_group=resource_group, >>> location=location, >>> create_resource_group=True, >>> exist_okay=True) >>> >>> # Build an Azure ML Container Image for an MLflow model >>> azure_image, azure_model = mlflow.azureml.build_image( >>> model_path="<model_path>", >>> workspace=azure_workspace, >>> synchronous=True) >>> # If your image build failed, you can access build logs at the following URI: >>> print("Access the following URI for build logs: {}".format(azure_image.image_build_log_uri)) >>> >>> # Deploy the image to Azure Container Instances (ACI) for real-time serving >>> webservice_deployment_config = AciWebservice.deploy_configuration() >>> webservice = Webservice.deploy_from_image( >>> image=azure_image, workspace=azure_workspace, name="<deployment-name>") >>> webservice.wait_for_deployment()