-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #145: Added Export_Model_Version and Import_Model_Version noteb…
…ooks
- Loading branch information
amesar
committed
Oct 29, 2023
1 parent
5c0e8e1
commit 42a34f8
Showing
4 changed files
with
245 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters