Model deployment

Introduction to MLflow

Weston Bassler

Senior MLOps Engineer

ML lifecycle

Steps of ML Lifecycle

1 datacamp.com
Introduction to MLflow

Model versions and stages

  • Model versions

    • Version +1
  • Model stages

    • Staging
    • Production
    • Archived

Model versions and stages

Introduction to MLflow

Ways to deploy models

Load model

# MLflow flavor
mlflow.FLAVOR.load_model()

Serve model

# MLflow serve command-line
mlflow models serve
Introduction to MLflow

Models URI

Convention

models:/

Model version

models:/model_name/version

Model stage

models:/model_name/stage
Introduction to MLflow

Load models

# Import flavor
import mlflow.FLAVOR

# Load version mlflow.FLAVOR.load_model("models:/model_name/version")
# Load stage mlflow.FLAVOR.load_model("models:/model_name/stage")
Introduction to MLflow

Load models example

# Import flavor
import mlflow.sklearn


# Load Unicorn model in Staging model = mlflow.sklearn.load_model("models:/Unicorn/Staging")
# Print model model
LogisticRegression()
# Inference
model.predict(data)
Introduction to MLflow

Serving models

# Serve Unicorn model in Production stage
mlflow models serve -m "models:/Unicorn/Production"
2023/03/26 15:07:00 INFO mlflow.models.flavor_backend_registry: 
Selected backend for flavor 'python_function'
2023/03/26 15:07:00 INFO mlflow.pyfunc.backend: === Running command 'exec gunicorn 
--timeout=60 -b 127.0.0.1:5000 -w 1 ${GUNICORN_CMD_ARGS} -- 
mlflow.pyfunc.scoring_server.wsgi:app'
[2023-03-26 15:07:00 -0400] [86409] [INFO] Starting gunicorn 20.1.0
[2023-03-26 15:07:00 -0400] [86409] [INFO] Listening at: http://127.0.0.1:5000
[2023-03-26 15:07:00 -0400] [86409] [INFO] Using worker: sync
[2023-03-26 15:07:00 -0400] [86410] [INFO] Booting worker with pid: 86410
Introduction to MLflow

Invocations endpoint

1 Flaticon.com
Introduction to MLflow

CSV format

pandas_df.to_csv()

JSON format

{
  "dataframe_split": {
      "columns": ["R&D Spend", "Administration", "Marketing Spend", "State"],
      "data": [["165349.20", 136897.80, 471784.10, 1]]
  }
}
Introduction to MLflow

Model prediction

# Send payload to invocations endpoint
curl http://127.0.0.1:5000/invocations -H 'Content-Type: application/json' -d
{
  "dataframe_split": {
      "columns": ["R&D Spend", "Administration", "Marketing Spend", "State"],
      "data": [["165349.20", 136897.80, 471784.10, 1]]
  }
}
[[104055.1842384]]
Introduction to MLflow

Let's practice!

Introduction to MLflow

Preparing Video For Download...