Deploying a machine learning model with Azure Functions and Azure ML

Machine learning is a powerful tool that can be used to build predictive models and automate decision-making processes in a variety of applications. However, deploying these models can often be a challenging task. In this tutorial, we will explore how to deploy a machine learning model using Azure Functions and Azure ML.

What is Azure Functions?

Azure Functions is a serverless computing platform provided by Microsoft that allows developers to run small pieces of code in the cloud without worrying about infrastructure or server management. Azure Functions enables developers to create event-driven, scalable, and cost-effective solutions that can handle various tasks such as data processing, automation, and integration with other services.

Azure Functions can be integrated with various triggers such as HTTP requests, service bus messages, timer schedules, and many more. Developers can write their code using different languages such as C#, Java, JavaScript, and Python.

What is Azure ML?

Azure Machine Learning (Azure ML) is a cloud-based platform provided by Microsoft that enables developers to build, train, deploy, and manage machine learning models at scale.

Azure ML provides a variety of tools and services to support the machine learning lifecycle, including data preparation, model training, hyperparameter tuning, model selection, deployment, and management. Developers can use familiar programming languages and tools such as Python, Jupyter Notebook, and Visual Studio Code to create and run machine learning experiments.

Azure ML also enables developers to easily deploy machine learning models to production using various deployment targets such as Azure Kubernetes Service (AKS), Azure Functions, and Azure Container Instances (ACI).

Prerequisites

Before we proceed with this tutorial, we need to have the following prerequisites:

  • An Azure account
  • Basic knowledge of Python and machine learning
  • Azure Machine Learning SDK for Python installed

Step-by-Step Guide to Deploying a Machine Learning Model with Azure Functions and Azure ML

Now that we have our prerequisites set up, we can proceed with the following steps to deploy a machine learning model with Azure Functions and Azure ML:

Step 1: Create a Workspace in Azure ML

The first step is to create a workspace in Azure ML. A workspace is a container that holds all the assets related to your machine learning experiments, including datasets, models, scripts, and metadata.

To create a workspace, log in to Azure Portal and follow the steps below:

  1. Click on “Create a resource” from the Azure Portal dashboard.
  2. Select “AI + Machine Learning” from the “New” window.
  3. Click on “Machine Learning” from the options.
  4. Click on “Create” from the Azure Machine Learning service window.
  5. Fill in the required fields such as Subscription, Resource group, Workspace name, and region.
  6. Click “Review + create” and then “Create” to create the workspace.

After the workspace is successfully created, you will be redirected to the Azure Machine Learning workspace dashboard.

Step 2: Create a Machine Learning Model

The next step is to create a machine learning model using Azure ML. For this tutorial, we will use the Scikit-learn library to build a simple binary classification model that predicts whether a customer will purchase a product or not based on their age and income.

Create a new Python file named train.py and copy the following code:

import json
import joblib
import numpy as np
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier
from azureml.core import Run

# Get the experiment run context
run = Run.get_context()

# Generate a synthetic dataset
X, y = make_classification(n_samples=1000, n_features=2, n_informative=2,
                             n_redundant=0, n_classes=2, random_state=42)

# Split the data into training and testing sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

# Train a random forest classifier on the training set
clf = RandomForestClassifier(n_estimators=100, max_depth=2, random_state=0)
clf.fit(X_train, y_train)

# Evaluate the model performance on the testing set
accuracy = clf.score(X_test, y_test)
print(f"Accuracy: {accuracy:.2f}")

# Serialize the model and save it to disk
model_file = "model.pkl"
joblib.dump(value=clf, filename=model_file)

# Upload the serialized model as an artifact of the experiment run
run.upload_file(name=model_file, path_or_stream=model_file)

# Log the model performance and a JSON representation of the hyperparameters
run.log("Accuracy", np.float(accuracy))
run.log("Hyperparameters", json.dumps({"n_estimators": 100, "max_depth": 2, "random_state": 0}))

This code generates a synthetic dataset using the make_classification function, splits it into training and testing sets using the train_test_split function, trains a random forest classifier on the training set using the RandomForestClassifier class from Sci

kit-learn, evaluates the model performance on the testing set using the score method, saves the trained model to disk using the joblib module, and uploads the serialized model as an artifact of the experiment run using the upload_file method.

To run this code as an experiment in Azure ML, we need to create an experiment and a run configuration.

Create a new Python file named create_experiment.py and copy the following code:

from azureml.core import Experiment, Environment, ScriptRunConfig
from azureml.core.conda_dependencies import CondaDependencies

# Define the name and workspace of the experiment
experiment_name = "purchase-prediction-experiment"
workspace_name = "ml-workspace"

# Create an Azure ML experiment
experiment = Experiment(workspace=workspace_name, name=experiment_name)

# Create a new environment and add dependencies to it
env = Environment("purchase-prediction-env")
cd = CondaDependencies.create(conda_packages=["scikit-learn", "joblib"])
env.python.conda_dependencies = cd

# Create a ScriptRunConfig that references the training script and the new environment
src = ScriptRunConfig(source_directory=".",
                      script="train.py",
                      environment=env)

# Submit the experiment run to Azure ML
run = experiment.submit(src)

This code creates an experiment object using the Experiment class from Azure ML, creates a new environment object using the Environment class from Azure ML, adds the necessary dependencies to it using the CondaDependencies class, creates a script run configuration object using the ScriptRunConfig class from Azure ML, and submits the experiment run to Azure ML using the submit method.

To run this code, execute the following terminal command:

python create_experiment.py

This will create an experiment with the name purchase-prediction-experiment in the Azure ML workspace named ml-workspace, and run the train.py script inside a new environment named purchase-prediction-env.

After the experiment run is completed, you can view the run details and the generated artifacts in the Azure ML workspace portal.

Step 3: Deploy the Machine Learning Model with Azure Functions

The final step is to deploy the machine learning model using Azure Functions. For this tutorial, we will use Python and Visual Studio Code to create an Azure Functions app that loads the serialized model and deploys it as a REST API.

Follow these steps to create an Azure Functions app:

  1. Open Visual Studio Code and create a new folder named azure-functions-app.
  2. Open the terminal and navigate to the azure-functions-app folder.
  3. Execute the following terminal commands to create a new Python virtual environment and activate it:
python -m venv .venv
source .venv/bin/activate
  1. Execute the following terminal command to install the Azure Functions Core Tools:
npm install -g azure-functions-core-tools@3 --unsafe-perm true
  1. Execute the following terminal command to create a new Azure Functions project:
func init --worker-runtime python
  1. Choose the HTTP trigger template and enter predict as the name of the function.
  2. Navigate to the predict folder and open the __init__.py file.

  3. Replace the contents of the file with the following code:

import logging
import json
import joblib
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    # Load the serialized model from disk
    model_file = "model.pkl"
    clf = joblib.load(model_file)

    # Get the request body and convert it to a JSON object
    req_body = req.get_body()
    data = json.loads(req_body)

    # Extract the features from the JSON object and make a prediction
    age = data.get("age")
    income = data.get("income")
    features = [[age, income]]
    prediction = clf.predict(features)[0]

    # Return the prediction as a JSON response
    response = {"prediction": prediction}
    return func.HttpResponse(json.dumps(response))

This code defines a new Azure Functions endpoint named predict, loads the serialized model from disk using the joblib module, gets the request body and converts it to a JSON object using the get_body and json.loads methods, extracts the features from the JSON object, makes a prediction usin

g the predict method of the trained model, and returns the prediction as a JSON response using the json.dumps method.

  1. Save the __init__.py file and navigate back to the azure-functions-app folder.
  2. Execute the following terminal command to create a new virtual environment for the Azure Functions app and install the necessary packages:

python -m venv .venv
source .venv/bin/activate
pip install azure-functions
pip install joblib==1.0.1 scikit-learn==0.24.2
  1. Execute the following terminal command to start the Azure Functions app:
func start

This will start the Azure Functions app and open a local endpoint at `http://localhost:7071/api/predict`.

  1. Test the endpoint by sending a POST request to `http://localhost:7071/api/predict` with the following JSON payload:
{
    "age": 30,
    "income": 50000
}

The response should be a JSON object with a prediction field that indicates whether the customer is likely to purchase the product or not.

Congratulations!

You have successfully deployed a machine learning model with Azure Functions and Azure ML. You can now use this process to deploy your own machine learning models and automate decision-making processes in your applications.

Related Post