Downloading ML Models From Fabric Model Registry

Downloading ML models from Fabric model registry using UI and mlflow API

In previous blog posts, I discussed loading models from the Fabric model registry and migrating models to Fabric. To conclude this series, this blog post will demonstrate how to download models from Fabric both programmatically and through the user interface. Downloading models enables you to export them from Fabric for inferencing on other platforms or for local inferencing during development while model endpoints are not yet available in Fabric.

Using UI

In the workspace, select the model you want to download and then select the version you want to download. In the top ribbon "Download model version". Note that this will download the individual model as a zip file.

This is a quick option if you need to download one or two models. To export many models from a workspace, we can use the mlflow API and download programmatically.

Programmatically

Get Registered Models

Use the below code to get a list of all the models that are registered in the current workspace:


import mlflow
import pandas as pd
from mlflow import MlflowClient

client = MlflowClient()

#Get a list of models in the workspace
models_list = client.search_registered_models()
models_dict_list = [dict(model) for model in models_list]

# Return a dataframe showing the models
models_df = pd.DataFrame(models_dict_list)
models_df

You can use the above pandas dataframe to recursively download the models and save them to the Files section of the lakehouse. Note that the code below will download the entire packaged MLModel. Refer to mlflow API if you want to download just the serialized model. I have also included some error handling.

from pathlib import Path
import mlflow
## Author : Sandeep Pawar  | fabric.guru  | ver:1.0  | Aug 1, 2023

## provide the path in the lakehouse where models should be saved
base_path_download = Path("/lakehouse/default/Files/")

for model in models_df['name']:
    print(model)

    # Create the directory, overwrite if it exists
    model_path = base_path_download / model
    model_path.mkdir(parents=True, exist_ok=True)

    try:
        # download the latest model
        mlflow.artifacts.download_artifacts(artifact_uri=f"models:/{model}/latest", dst_path=str(model_path))
        print("Downloaded & saved ", model)
    except Exception as e:
        print(f">>> Could not download model {model}: {str(e)}")

Output:

The models will be saved to individual folders in the Files section of the lakehouse. You can then download it to your local machine.

💡
Above code will download the latest models from the registry. If you need to download specific versions, change the code accordingly.

Did you find this article valuable?

Support Sandeep Pawar by becoming a sponsor. Any amount is appreciated!