def forecast_power(model_name, model_stage): from mlflow.tracking.client import MlflowClient client = MlflowClient() model_version = client.get_latest_versions(model_name, stages=[model_stage])[0].version model_uri = "models:/{model_name}/{model_stage}".format( model_name=model_name, model_stage=model_stage) model = mlflow.pyfunc.load_model(model_uri) weather_data, past_power_output = get_weather_and_forecast() power_predictions = pd.DataFrame(model.predict(weather_data)) power_predictions.index = pd.to_datetime(weather_data.index) print(power_predictions) plot(model_name, model_stage, int(model_version), power_predictions, past_power_output)
from mlflow.tracking.client import MlflowClient client = MlflowClient() client.transition_model_version_stage( name=modelDetails.name, version=modelDetails.version, stage='Production', ) # COMMAND ---------- # MAGIC %md The MLflow Model Registry allows multiple model versions to share the same stage. When referencing a model by stage, the Model Registry will use the latest model version (the model version with the largest version ID). The `MlflowClient.get_latest_versions()` function fetches the latest model version for a given stage or set of stages. The following cell uses this function to print the latest version of the power forecasting model that is in the `Production` stage. # COMMAND ---------- latestVersionInfo = client.get_latest_versions(modelName, stages=["Production"]) latestVersion = latestVersionInfo[0].version print("The latest production version of the model '%s' is '%s'." % (modelName, latestVersion)) # COMMAND ---------- # MAGIC %md # MAGIC ## Next Steps # MAGIC # MAGIC [Model Serving]($./06-Model-Serving) # COMMAND ---------- # MAGIC %md-sandbox # MAGIC © 2020 Databricks, Inc. All rights reserved.<br/>
description="This model forecasts the wine quality based on the characteristics." ) client.update_model_version( name=model_name, version=model_version, description="This model version was built using sklearn." ) # COMMAND ---------- client.transition_model_version_stage( name=model_name, version=model_version, stage=stage, archive_existing_versions=True ) model_version_details = client.get_model_version( name=model_name, version=model_version, ) print("The current model stage is: '{stage}'".format(stage=model_version_details.current_stage)) latest_version_info = client.get_latest_versions(model_name, stages=[stage]) latest_production_version = latest_version_info[0].version print("The latest production version of the model '%s' is '%s'." % (model_name, latest_production_version)) # COMMAND ---------- dbutils.notebook.exit()
# COMMAND ---------- model_version_details = client.get_model_version( name=model_details.name, version=model_details.version, ) print("The current model stage is: '{stage}'".format(stage=model_version_details.current_stage)) # COMMAND ---------- # MAGIC %md The MLflow Model Registry allows multiple model versions to share the same stage. When referencing a model by stage, the Model Registry will use the latest model version (the model version with the largest version ID). The `MlflowClient.get_latest_versions()` function fetches the latest model version for a given stage or set of stages. The following cell uses this function to print the latest version of the power forecasting model that is in the `Production` stage. # COMMAND ---------- latest_version_info = client.get_latest_versions(model_name, stages=["Production"]) latest_production_version = latest_version_info[0].version print("The latest production version of the model '%s' is '%s'." % (model_name, latest_production_version)) # COMMAND ---------- # MAGIC %md # Integrate the model with the forecasting application # MAGIC # MAGIC Now that you have trained and registered a power forecasting model with the MLflow Model Registry, the next step is to integrate it with an application. This application fetches a weather forecast for the wind farm over the next five days and uses the model to produce power forecasts. For example purposes, the application consists of a simple `forecast_power()` function (defined below) that is executed within this notebook. In practice, you may want to execute this function as a recurring batch inference job using the Databricks Jobs service. # MAGIC # MAGIC The following section demonstrates how to load model versions from the MLflow Model Registry for use in applications. The **Forecast power output with the production model** section uses the **Production** model to forecast power output for the next five days. # COMMAND ---------- # MAGIC %md ## Load versions of the registered model # MAGIC