dbutils.widgets.text('1_registry_secret_scope', '') dbutils.widgets.text('2_registry_secret_key_prefix', '') scope = str(dbutils.widgets.get('1_registry_secret_scope')) key = str(dbutils.widgets.get('2_registry_secret_key_prefix')) registry_uri = 'databricks://' + scope + ':' + key if scope and key else None
# Log a model to MLflow Tracking from mlflow.tracking.artifact_utils import get_artifact_uri with mlflow.start_run() as new_run: mlflow.pyfunc.log_model( python_model=SampleModel(), artifact_path=artifact_path, ) run1_id = new_run.info.run_id source = get_artifact_uri(run_id=run1_id, artifact_path=artifact_path)
# Instantiate an MlflowClient pointing to the local tracking server and a remote registry server from mlflow.tracking.client import MlflowClient client = MlflowClient(tracking_uri=None, registry_uri=registry_uri) model = client.create_registered_model(model1_name) client.create_model_version(name=model1_name, source=source, run_id=run1_id)
import os from six.moves import urllib from mlflow.store.artifact.dbfs_artifact_repo import DbfsRestArtifactRepository version = client.get_latest_versions(model3_name, ['Staging'])[0].version uri = client.get_model_version_download_uri(model3_name, version) path = urllib.parse.urlparse(uri).path local_path = DbfsRestArtifactRepository(f'dbfs://{scope}:{key}@databricks{path}').download_artifacts('') os.listdir(local_path)
def delete_version_tmp_files(version): import posixpath location = posixpath.dirname(version.source) if registry_uri == 'databricks': dbutils.fs.rm(location, recurse=True) else: from mlflow.utils.databricks_utils import get_databricks_host_creds from mlflow.utils.rest_utils import http_request import json response = http_request( host_creds=get_databricks_host_creds(registry_uri), endpoint='/api/2.0/dbfs/delete', method='POST', json=json.loads('{"path": "%s", "recursive": "true"}' % (location)) ) def archive_and_delete(name): try: client.transition_model_version_stage(name, 1, 'Archived') finally: client.delete_registered_model(name)
Remote Model Registry example
This notebook shows how to log models to the MLflow tracking server from the current workspace, and register the models into Model Registry in a different workspace.
Setup
a. Create a secret scope:
databricks secrets create-scope --scope <scope>
.b. Pick a unique name for the target workspace, which we'll refer to as
<prefix>
. Then create three secrets:databricks secrets put --scope <scope> --key <prefix>-host
. Enter the hostname of the model registry workspace.databricks secrets put --scope <scope> --key <prefix>-token
. Enter the access token from the model registry workspace.databricks secrets put --scope <scope> --key <prefix>-workspace-id
. Enter the workspace ID for the Model Registry workspace. This can be found in the URL of any page in the workspace.Before you run this notebook, enter the secret scope and key prefix corresponding to the Model Registry workspace in the notebook parameter fields at the top of this notebook.