Skip to content

Commit

Permalink
Issue #145: Added Export_Model_Version and Import_Model_Version noteb…
Browse files Browse the repository at this point in the history
…ooks
  • Loading branch information
amesar committed Oct 29, 2023
1 parent 5c0e8e1 commit 42a34f8
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 4 deletions.
2 changes: 2 additions & 0 deletions databricks_notebooks/single/Common.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,5 @@ def activate_unity_catalog(model_name):
if model_utils.is_unity_catalog_model(model_name):
mlflow.set_registry_uri("databricks-uc")
print("Unity Catalog mode activated")
else:
mlflow.set_registry_uri("databricks")
107 changes: 107 additions & 0 deletions databricks_notebooks/single/Export_Model_Version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Databricks notebook source
# MAGIC %md ### Export Model Version
# MAGIC
# MAGIC ##### Overview
# MAGIC * Export a model version and its run.
# MAGIC
# MAGIC ##### Widgets
# MAGIC * `1. Model` - Registered model name.
# MAGIC * `2. Version` - Model version.
# MAGIC * `3. Output directory` - Output directory.
# MAGIC * `4. Export version MLflow model` - Export a model version's "cached" registry MLflow model (clone of run's MLflow model).
# MAGIC * `5. Notebook formats` - Notebook formats to export.

# COMMAND ----------

# MAGIC %md ### Include setup

# COMMAND ----------

# MAGIC %run ./Common

# COMMAND ----------

# MAGIC %md ### Widget setup

# COMMAND ----------

dbutils.widgets.text("1. Model name", "")
model_name = dbutils.widgets.get("1. Model name")

dbutils.widgets.text("2. Model version", "")
version = dbutils.widgets.get("2. Model version")

dbutils.widgets.text("3. Output directory", "")
output_dir = dbutils.widgets.get("3. Output directory")

dbutils.widgets.dropdown("4. Export version MLflow model","no",["yes","no"])
export_version_model = dbutils.widgets.get("4. Export version MLflow model") == "yes"

notebook_formats = get_notebook_formats(5) # widget "7. Notebook formats"

print("model_name:", model_name)
print("version:", version)
print("output_dir:", output_dir)
print("export_version_model:", export_version_model)
print("notebook_formats:", notebook_formats)

# COMMAND ----------

assert_widget(model_name, "1. Model name")
assert_widget(model_name, "2. Model version")
assert_widget(output_dir, "3. Output directory")

# COMMAND ----------

# MAGIC %md ### Turn on Unity Catalog mode if necessary

# COMMAND ----------

activate_unity_catalog(model_name)

# COMMAND ----------

# MAGIC %md ### Display model UI link

# COMMAND ----------

display_registered_model_uri(model_name)

# COMMAND ----------

# MAGIC %md ### Export the model version

# COMMAND ----------

from mlflow_export_import.model_version.export_model_version import export_model_version

export_model_version(
model_name = model_name,
version = version,
output_dir = output_dir,
export_version_model = export_version_model,
notebook_formats = notebook_formats
)

# COMMAND ----------

# MAGIC %md ### Display exported files

# COMMAND ----------

import os
output_dir = mk_local_path(output_dir)
os.environ['OUTPUT_DIR'] = output_dir

# COMMAND ----------

# MAGIC %sh echo $OUTPUT_DIR

# COMMAND ----------

# MAGIC %sh ls -l $OUTPUT_DIR

# COMMAND ----------

# MAGIC %sh
# MAGIC cat $OUTPUT_DIR/model_version.json
132 changes: 132 additions & 0 deletions databricks_notebooks/single/Import_Model_Version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Databricks notebook source
# MAGIC %md ### Import Model Version
# MAGIC
# MAGIC ##### Overview
# MAGIC
# MAGIC * Import a registered model version.
# MAGIC * See notebook [Export_Model_Version]($Export_Model_Version).
# MAGIC
# MAGIC ##### Widgets
# MAGIC * `1. Input directory` - Input directory containing an exported model version.
# MAGIC * `2. Model name` - registered model where into which the version will be imported.
# MAGIC * `3. Destination experiment name` - contains imported run created for the model version.
# MAGIC * `4. Create model` - Create an empty registered model before creating model version.
# MAGIC * `5. Import stages and aliases` - Import stages and aliases.
# MAGIC * `6. Import metadata` - Import registered model and experiment metadata (description and tags).
# MAGIC * `7. Import source tags` - Import source information for registered model and its versions and tags destination object.

# COMMAND ----------

# MAGIC %md ### Include setup

# COMMAND ----------

# MAGIC %run ./Common

# COMMAND ----------

# MAGIC %md ### Widget setup

# COMMAND ----------

dbutils.widgets.text("1. Input directory", "")
input_dir = dbutils.widgets.get("1. Input directory")

dbutils.widgets.text("2. Model name", "")
model_name = dbutils.widgets.get("2. Model name")

dbutils.widgets.text("3. Destination experiment name", "")
experiment_name = dbutils.widgets.get("3. Destination experiment name")

dbutils.widgets.dropdown("4. Create destination model","no",["yes","no"])
create_model = dbutils.widgets.get("4. Create destination model") == "yes"

dbutils.widgets.dropdown("5. Import stages and aliases","no",["yes","no"])
import_stages_and_aliases = dbutils.widgets.get("5. Import stages and aliases") == "yes"

dbutils.widgets.dropdown("6. Import metadata","no",["yes","no"])
import_metadata = dbutils.widgets.get("6. Import metadata") == "yes"

dbutils.widgets.dropdown("7. Import source tags","no",["yes","no"])
import_source_tags = dbutils.widgets.get("7. Import source tags") == "yes"

print("model_name:", model_name)
print("input_dir:", input_dir)
print("experiment_name:", experiment_name)
print("create_model:", create_model)
print("import_stages_and_aliases:", import_stages_and_aliases)
print("import_metadata:", import_metadata)
print("import_source_tags:", import_source_tags)

import os
os.environ["INPUT_DIR"] = mk_local_path(input_dir)

# COMMAND ----------

assert_widget(input_dir, "1. Input directory")
assert_widget(model_name, "2. Model version")
assert_widget(experiment_name, "3. Destination experiment name")

# COMMAND ----------

# MAGIC %md ### Turn on Unity Catalog mode if necessary

# COMMAND ----------

activate_unity_catalog(model_name)

# COMMAND ----------

# MAGIC %md ### Display model version files to be imported

# COMMAND ----------

# MAGIC %sh ls -l $INPUT_DIR

# COMMAND ----------

# MAGIC %sh cat $INPUT_DIR/model_version.json

# COMMAND ----------

# MAGIC %md ### Import model version

# COMMAND ----------

from mlflow_export_import.model_version.import_model_version import import_model_version

dst_vr = import_model_version(
input_dir = input_dir,
model_name =model_name,
experiment_name = experiment_name,
create_model = create_model,
import_stages_and_aliases = import_stages_and_aliases,
import_metadata = import_metadata,
import_source_tags = import_source_tags
)

# COMMAND ----------

dump_obj(dst_vr)

# COMMAND ----------

# MAGIC %md ### Display UI links

# COMMAND ----------

display_registered_model_uri(model_name)

# COMMAND ----------

display_registered_model_uri(model_name)

# COMMAND ----------

display_experiment_info(experiment_name)

# COMMAND ----------

run = mlflow_client.get_run(dst_vr.run_id)
exp = mlflow_client.get_experiment(run.info.experiment_id)
dump_obj(exp)
8 changes: 4 additions & 4 deletions databricks_notebooks/single/_README.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Databricks notebook source
# MAGIC %md ## MLflow Export Import - Single Notebooks
# MAGIC
# MAGIC Export and import one MLflow object and specify its destination name.
# MAGIC Export and import one MLflow object.
# MAGIC
# MAGIC **Notebooks**
# MAGIC * Run
Expand All @@ -14,8 +14,8 @@
# MAGIC * [Export_Registered_Model]($./Export_Registered_Model) - export a registered model, its versions and their runs.
# MAGIC * [Import_Registered_Model]($./Import_Registered_Model)
# MAGIC * Model Version
# MAGIC * [Export_Version]($./Export_Version) - export a model version and its run.
# MAGIC * [Import_Version]($./Import_Version) - import model version.
# MAGIC * [Export_Model_Version]($./Export_Model_Version) - export a model version and its run.
# MAGIC * [Import_Model_Version]($./Import_Model_Version) - import a model version.
# MAGIC * [Common]($./Common) - helper utility methods.
# MAGIC
# MAGIC **More information**
Expand All @@ -25,4 +25,4 @@

# COMMAND ----------

# MAGIC %md ##### Last updated: _2023-10-28_
# MAGIC %md ##### Last updated: _2023-10-29_

0 comments on commit 42a34f8

Please sign in to comment.