Mannequin Administration with MLflow, Azure, and Docker | by Sabrine Bendimerad | Sep, 2024

You may clone this folder to search out all the mandatory scripts for this tutorial.

To host the MLflow server, we begin by making a Docker container utilizing a Dockerfile. Right here’s an instance configuration:

# Use Miniconda as the bottom picture
FROM continuumio/miniconda3

# Set surroundings variables
ENV DEBIAN_FRONTEND=noninteractive

# Set up essential packages
RUN apt-get replace -y &&
apt-get set up -y --no-install-recommends curl apt-transport-https gnupg2 unixodbc-dev

# Add Microsoft SQL Server ODBC Driver 18 repository and set up
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - &&
curl https://packages.microsoft.com/config/debian/11/prod.record > /and so forth/apt/sources.record.d/mssql-release.record &&
apt-get replace &&
ACCEPT_EULA=Y apt-get set up -y msodbcsql18 mssql-tools18

# Add mssql-tools to PATH
RUN echo 'export PATH="$PATH:/choose/mssql-tools18/bin"' >> ~/.bash_profile &&
echo 'export PATH="$PATH:/choose/mssql-tools18/bin"' >> ~/.bashrc

# outline default server env variables
ENV MLFLOW_SERVER_HOST 0.0.0.0
ENV MLFLOW_SERVER_PORT 5000
ENV MLFLOW_SERVER_WORKERS 1

# Set the working listing
WORKDIR /app

# Copy the present listing contents into the container at /app
COPY . /app

# Set up Python dependencies laid out in necessities.txt
RUN pip set up --no-cache-dir -r necessities.txt

# Make sure that the launch.sh script is executable
RUN chmod +x /app/launch.sh

# Expose port 5000 for MLflow
EXPOSE 5000

# Set the entrypoint to run the launch.sh script
ENTRYPOINT ["/app/launch.sh"]

This Dockerfile creates a container that runs an MLflow server. It installs essential instruments, together with the Microsoft SQL Server ODBC driver, units up the surroundings, and installs Python dependencies. It then copies our recordsdata within the app folder into the container, exposes port 5000 (obligatory for MlFlow), and runs a launch.sh script to begin the MLflow server.

The launch.sh incorporates solely the command that launches the mlflow server.

  • Construct the Docker Picture in the identical listing the place your Dockerfile is :
docker construct . -t mlflowserver
# in case your are on mac, use :
# docker construct - platform=linux/amd64 -t mlflowserver:newest .

Run the Docker container:

docker run -it -p 5000:5000 mlflowserver

After working these instructions, the MLflow server begins domestically, and you’ll entry the MLflow UI by navigating to http://localhost:5000. This confirms the server is efficiently deployed in your native machine. Nevertheless, at this stage, when you can log experiments to MLflow, not one of the outcomes, artifacts, or metadata shall be saved within the SQL database or artifact retailer, as these haven’t been configured but. Moreover, the URL is barely accessible domestically, that means your information science workforce can not entry it remotely.

By the creator

Begin by creating an Azure account and grabbing your Subscription ID from the Azure Portal.

To deploy your MLflow server and make it accessible to your workforce, observe these simplified steps:

  • Clone the Repository: Clone this folder to your native machine.
  • Run the Deployment Script: Execute the deploy.sh script as a shell script. Make sure that to replace the Subscription ID variable within the script earlier than working it.

Whereas Azure provides a graphical interface for establishing sources, this information simplifies the method through the use of the deploy.sh script to automate all the pieces with a single command.

Right here’s a breakdown of what deploy.sh script does step-by-step:

1.Login and Set Subscription: First, log into your Azure account and set the right subscription the place all of your sources shall be deployed (retrieve the subscription ID from the Azure Portal).

az login az account set --subscription $SUBSCRIPTION_ID

2.Create a Useful resource Group: Create a Useful resource Group to arrange all of the sources you’ll deploy for MLflow.

az group create --name $RG_NAME --location <location>

3.Set Up Azure SQL Database: Create an Azure SQL Server and an SQL Database the place MLflow will retailer all experiment metadata.

az sql server create 
--name $SQL_SERVER_NAME
--resource-group $RG_NAME
--location $RG_LOCATION
--admin-user $SQL_ADMIN_USER
--admin-password $SQL_ADMIN_PASSWORD

az sql db create
--resource-group $RG_NAME
--server $SQL_SERVER_NAME
--name $SQL_DATABASE_NAME
--service-objective S0

4.Configure SQL Server Firewall: Permit entry to the SQL Server from different Azure companies by making a firewall rule.

az sql server firewall-rule create 
--resource-group $RG_NAME
--server $SQL_SERVER_NAME
--name AllowAllAzureIPs
--start-ip-address 0.0.0.0
--end-ip-address 0.0.0.0

5.Create Azure Storage Account: Arrange an Azure Storage Account and a Blob Container to retailer artifacts (e.g., fashions, experiment outcomes).

az storage account create 
--resource-group $RG_NAME
--location $RG_LOCATION
--name $STORAGE_ACCOUNT_NAME
--sku Standard_LRS

az storage container create
--name $STORAGE_CONTAINER_NAME
--account-name $STORAGE_ACCOUNT_NAME

6.Create Azure Container Registry (ACR): Create an Azure Container Registry (ACR) to retailer the Docker picture of your MLflow server.

az acr create 
--name $ACR_NAME
--resource-group $RG_NAME
--sku Fundamental
--admin-enabled true

7.Construct and Push Docker Picture to ACR: Construct your Docker picture for the MLflow server and push it to the Azure Container Registry. For that, you want first to retrieve the ACR Username and Password and to log into ACR.

export ACR_USERNAME=$(az acr credential present --name $ACR_NAME --query "username" --output tsv)
export ACR_PASSWORD=$(az acr credential present --name $ACR_NAME --query "passwords[0].worth" --output tsv)

docker login $ACR_NAME.azurecr.io
--username "$ACR_USERNAME"
--password "$ACR_PASSWORD"

# Push the pictures
docker tag $DOCKER_IMAGE_NAME $ACR_NAME.azurecr.io/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG
docker push $ACR_NAME.azurecr.io/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG

8.Create App Service Plan: Arrange an App Service Plan to host your MLflow server on Azure.

az appservice plan create 
--name $ASP_NAME
--resource-group $RG_NAME
--sku B1
--is-linux
--location $RG_LOCATION

9.Deploy Internet App with MLflow Container: Create a Internet App that makes use of your Docker picture from ACR to deploy the MLflow server.

az webapp create 
--resource-group $RG_NAME
--plan $ASP_NAME
--name $WEB_APP_NAME
--deployment-container-image-name $ACR_NAME.azurecr.io/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG

10.Configure Internet App to Use Container Registry: Arrange your Internet App to tug the MLflow Docker picture from ACR, and configure surroundings variables.

az webapp config container set 
--name $WEB_APP_NAME
--resource-group $RG_NAME
--docker-custom-image-name $ACR_NAME.azurecr.io/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG
--docker-registry-server-url https://$ACR_NAME.azurecr.io
--docker-registry-server-user $ACR_USERNAME
--docker-registry-server-password $ACR_PASSWORD
--enable-app-service-storage true

az webapp config appsettings set
--resource-group $RG_NAME
--name $WEB_APP_NAME
--settings WEBSITES_PORT=$MLFLOW_PORT

az webapp log config
--name $WEB_APP_NAME
--resource-group $RG_NAME
--docker-container-logging filesystem

11.Set Internet App Setting Variables: Set the mandatory surroundings variables for MLflow, akin to storage entry, SQL backend, and port settings.


echo "Retrive artifact, entry key, connection string"
export STORAGE_ACCESS_KEY=$(az storage account keys record --resource-group $RG_NAME --account-name $STORAGE_ACCOUNT_NAME --query "[0].worth" --output tsv)
export STORAGE_CONNECTION_STRING=`az storage account show-connection-string --resource-group $RG_NAME --name $STORAGE_ACCOUNT_NAME --output tsv`
export STORAGE_ARTIFACT_ROOT="https://$STORAGE_ACCOUNT_NAME.blob.core.home windows.internet/$STORAGE_CONTAINER_NAME"

#Setting surroundings variables for artifacts and database
az webapp config appsettings set
--resource-group $RG_NAME
--name $WEB_APP_NAME
--settings AZURE_STORAGE_CONNECTION_STRING=$STORAGE_CONNECTION_STRING
az webapp config appsettings set
--resource-group $RG_NAME
--name $WEB_APP_NAME
--settings BACKEND_STORE_URI=$BACKEND_STORE_URI
az webapp config appsettings set
--resource-group $RG_NAME
--name $WEB_APP_NAME
--settings MLFLOW_SERVER_DEFAULT_ARTIFACT_ROOT=$STORAGE_ARTIFACT_ROOT

#Setting surroundings variables for the overall context
az webapp config appsettings set
--resource-group $RG_NAME
--name $WEB_APP_NAME
--settings MLFLOW_SERVER_PORT=$MLFLOW_PORT
az webapp config appsettings set
--resource-group $RG_NAME
--name $WEB_APP_NAME
--settings MLFLOW_SERVER_HOST=$MLFLOW_HOST
az webapp config appsettings set
--resource-group $RG_NAME
--name $WEB_APP_NAME
--settings MLFLOW_SERVER_FILE_STORE=$MLFLOW_FILESTORE
az webapp config appsettings set
--resource-group $RG_NAME
--name $WEB_APP_NAME
--settings MLFLOW_SERVER_WORKERS=$MLFLOW_WORKERS

As soon as the deploy.sh script has accomplished, you may confirm that each one your Azure companies have been created by checking the Azure portal.

By the creator

Go to the App Companies part to retrieve the URL of your MLflow net utility.

By the creator

Your MLflow Monitoring URL ought to now be reside and able to obtain experiments out of your information science workforce.

By the creator

Right here’s a Python script demonstrating log an experiment utilizing MLflow with a easy scikit-learn mannequin, akin to logistic regression. Be certain that you replace the script along with your MLflow monitoring URI:

import os
import mlflow
import pandas as pd
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
import joblib

# Load Iris dataset
iris = load_iris()

# Break up dataset into X options and Goal variable
X = pd.DataFrame(information = iris["data"], columns= iris["feature_names"])
y = pd.Sequence(information = iris["target"], title="goal")

# Break up our coaching set and our check set
X_train, X_test, y_train, y_test = train_test_split(X, y)

# Set your variables in your surroundings
EXPERIMENT_NAME="experiment1"

# Set monitoring URI to your Heroku utility
mlflow.set_tracking_uri("set your mlflow monitoring URI")
# mlflow.set_tracking_uri("http://localhost:5000")

# Set experiment's information
mlflow.set_experiment(EXPERIMENT_NAME)

# Get our experiment information
experiment = mlflow.get_experiment_by_name(EXPERIMENT_NAME)

# Name mlflow autolog
mlflow.sklearn.autolog()
with open("check.txt", "w") as f:
f.write("hiya world!")

with mlflow.start_run(experiment_id = experiment.experiment_id):
# Specified Parameters
c = 0.1

# Instanciate and match the mannequin
lr = LogisticRegression(C=c)
lr.match(X_train.values, y_train.values)

# Retailer metrics
predicted_qualities = lr.predict(X_test.values)
accuracy = lr.rating(X_test.values, y_test.values)

# Print outcomes
print("LogisticRegression mannequin")
print("Accuracy: {}".format(accuracy))

# Log Metric
mlflow.log_metric("Accuracy", accuracy)

# Log Param
mlflow.log_param("C", c)
mlflow.log_artifact('check.txt')

By working this script, you must be capable to log your fashions, metrics, and artifacts to MLflow. Artifacts shall be saved in Azure Blob Storage, whereas metadata shall be saved within the Azure SQL Database.

1Check MLflow Monitoring: Go to your MLflow monitoring URL to search out your experiment, run names, and all related metrics and mannequin parameters

By the creator
By the creator

Verify MLflow Artifacts: Entry the artifacts within the MLflow UI and confirm their presence in Azure Blob Storage

By the creator
By the creator

You and your workforce can now submit experiments to MLflow, observe them through the monitoring URI, and retrieve mannequin info or recordsdata from Azure Storage. Within the subsequent tutorial, we’ll discover create an API to learn fashions saved in Azure Storage.

You’ve efficiently arrange MLflow with Azure for monitoring and managing your machine studying experiments. Remember the fact that relying in your laptop and working system, you would possibly encounter some points with Docker, MLflow, or Azure companies. In case you run into bother, don’t hesitate to succeed in out for assist.

Subsequent, we’ll discover use MLflow fashions saved in Azure Blob Storage to create an API, finishing the automation workflow.

Thanks for studying!

Be aware: Some components of this text had been initially written in French and translated into English with the help of ChatGPT.

In case you discovered this text informative and useful, please don’t hesitate to 👏 and observe me on Medium | LinkedIn.