0
0
MLOpsdevops~7 mins

MLflow Model Registry in MLOps - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you train machine learning models, you need a way to save, organize, and track different versions. MLflow Model Registry helps you manage these models so you can easily find, update, and deploy them without confusion.
When you want to keep track of multiple versions of a machine learning model.
When you need to share models with your team in a clear and organized way.
When you want to move a model from testing to production safely.
When you want to add descriptions or tags to models for better understanding.
When you want to automate deployment by linking model stages like 'Staging' and 'Production'.
Commands
This command starts a local server to serve version 1 of the registered model named 'my-model'. It lets you test the model by sending data to port 1234.
Terminal
mlflow models serve -m models:/my-model/1 --no-conda -p 1234
Expected OutputExpected
2024/06/01 12:00:00 INFO mlflow.pyfunc.scoring_server: Starting server with command: ['mlflow', 'models', 'serve', '-m', 'models:/my-model/1', '--no-conda', '-p', '1234'] 2024/06/01 12:00:01 INFO mlflow.pyfunc.scoring_server: Listening on port 1234
-m - Specifies the model URI to serve
--no-conda - Avoids creating a new conda environment for serving
-p - Sets the port number for the server
This command lists all registered models in the MLflow Model Registry so you can see what models are available.
Terminal
mlflow models list
Expected OutputExpected
Name my-model another-model
This command moves version 1 of 'my-model' to the 'Production' stage, marking it as ready for real use.
Terminal
mlflow models transition-stage --model-name my-model --version 1 --stage Production
Expected OutputExpected
Model version '1' of 'my-model' transitioned to stage 'Production'.
--model-name - Specifies the name of the model
--version - Specifies the version number of the model
--stage - Specifies the new stage for the model version
This command deletes version 1 of 'my-model' from the registry if it is no longer needed.
Terminal
mlflow models delete --model-name my-model --version 1
Expected OutputExpected
Model version '1' of 'my-model' deleted.
--model-name - Specifies the name of the model
--version - Specifies the version number to delete
Key Concept

If you remember nothing else from MLflow Model Registry, remember: it helps you organize and control your machine learning models by version and stage for safe and easy deployment.

Code Example
MLOps
import mlflow
from mlflow.tracking import MlflowClient

# Initialize client
client = MlflowClient()

# Register a new model version
model_uri = "runs:/1234567890abcdef/model"
model_name = "my-model"

# Create a new model version
version = client.create_model_version(name=model_name, source=model_uri, run_id=None)
print(f"Registered model version: {version.version}")

# Transition model version to staging
client.transition_model_version_stage(name=model_name, version=version.version, stage="Staging")
print(f"Model version {version.version} moved to Staging")

# Add a description
client.update_model_version(name=model_name, version=version.version, description="Initial staging version")
print("Description added to model version")
OutputSuccess
Common Mistakes
Trying to serve a model version that does not exist in the registry.
The server will fail because the model version is not found.
Use 'mlflow models list' to check available models and versions before serving.
Transitioning a model version to 'Production' without testing it first.
This can cause unstable or untested models to be used in real applications.
Test the model in 'Staging' before moving it to 'Production'.
Deleting a model version that is currently in use or needed for rollback.
You lose the ability to revert to that model version if problems occur.
Only delete model versions after confirming they are no longer needed.
Summary
Use MLflow Model Registry to save and organize different versions of machine learning models.
Use commands to list models, serve specific versions, and move models between stages like Staging and Production.
Always test models before moving them to Production and avoid deleting versions that might be needed later.