Build with PyCaret : Deploy on Microsoft Azure Platform | by MA Raza, Ph.D. | Towards Data Science

In this story, I have developed a working tutorial on deploying a model trained with pycaret library on Microsoft's Azure Cloud Platform. In my previous article Build with PyCaret: Deploy on Google Cloud Platform, we had learned how to deploy the model on Google cloud. We will use the same example in this tutorial and deploy the model on Microsoft Azure platform.
We learn to deploy model trained with pycaret to Microsoft Azure Platform. pycaret has support to deploy a trained model on AWS but not with GCP or Azure at the moment. I followed the similar code-practices as used in library to deploy and load model with AWS to deploy the model on Microsoft's Azure Platform.
PyCaret is an open source, low-code machine learning library in Python that allows you to go from preparing your data to deploying your model within seconds in your choice of notebook environment.source
PyCaret is an autoML framework for citizen data scientists term used in its official documentation and homepage. This is relatively new library released few months ago for public use and still under active development. After going through some source code, I realized the current public release lacks support for deployment of trained/finalized models to Google and Azure Cloud Platforms. Although, It has support for deploying on Amazon web services.
Microsoft Azure is another very popular framework for cloud services and targeting different market than Google and AWS. Having already huge customer base, Azure has captured reasonable market share. In my opinion, for citizen data scientists, probably Microsoft Azure is best starting point. Let us learn how can we deploy a model on Microsoft Azure.
For this tutorial, we will use the Regression Tutorial (REG101) — Level Beginner for model training.
!pip install pycaret
We need to mount the google drive to read the data in colab environment. Below is the simplest way to mount it. You will be asked to enter the token generated by your access procedure. Here is the link to the article about mounting gdrive
We will save models locally on Google drive for this tutorial.
from google.colab import drivedrive.mount('/content/drive')Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
Let us create a directory to save models locally.
# Create directory on google drive to save models locally. You can use temp paths.import osmodel_dir = '/content/drive/My Drive/azure_deploy_model/'os.makedirs(model_dir, exist_ok=True)
You can download the data from the original source found here and load it using pandas (Learn How) or you can use PyCaret’s data respository to load the data using the get_data() function (This will require internet connection).
from pycaret.datasets import get_datadataset = get_data('diamond')
#check the shape of datadataset.shape(6000, 8)data = dataset.sample(frac=0.9, random_state=786).reset_index(drop=True)data_unseen = dataset.drop(data.index).reset_index(drop=True)print('Data for Modeling: ' + str(data.shape))print('Unseen Data For Predictions: ' + str(data_unseen.shape))Data for Modeling: (5400, 8)Unseen Data For Predictions: (600, 8)
Let us setup modelling pipeline using pycaret’s setup module.
from pycaret.regression import *exp_reg101 = setup(data = data, target = 'Price', session_id=123)Setup Successfully Completed!
For this tutorial, we model the data using Light GBM from many options implemented in pycaret. You can choose any model of your choice but that is not focus of this tutorial.
lightgbm = create_model('lightgbm')
Let us train the model also called a tuning the model in pycaret’s terminologies.
tuned_lightgbm = tune_model('lightgbm')
Below are the plots to see the residual errors for the model
plot_model(tuned_lightgbm)
Prediction Error Plot Let use plot the prediction errors vs true values of target.
plot_model(tuned_lightgbm, plot = 'error')
feature importance is very informative plot to see the contribution of each feature in the model.
plot_model(tuned_lightgbm, plot='feature')
Another way to analyze the performance of models is to use the evaluate_model() function which displays a user interface for all of the available plots for a given model. It internally uses the plot_model() function.
evaluate_model(tuned_lightgbm)interactive(children=(ToggleButtons(description='Plot Type:', icons=('',), options=(('Hyperparameters', 'param...
predict_model(tuned_lightgbm);
final_lightgbm = finalize_model(tuned_lightgbm)#Final Light Gradient Boosting Machine parameters for deploymentprint(final_lightgbm)LGBMRegressor(boosting_type='gbdt', class_weight=None, colsample_bytree=1.0,              importance_type='split', learning_rate=0.4, max_depth=10,              min_child_samples=20, min_child_weight=0.001, min_split_gain=0.9,              n_estimators=90, n_jobs=-1, num_leaves=10, objective=None,              random_state=123, reg_alpha=0.9, reg_lambda=0.2, silent=True,              subsample=1.0, subsample_for_bin=200000, subsample_freq=0)predict_model(final_lightgbm)
unseen_predictions = predict_model(final_lightgbm, data=data_unseen)unseen_predictions.head()
The Label column is added onto the data_unseen set. Label is the predicted value using the final_lightgbm model. If you want predictions to be rounded, you can use round parameter inside predict_model().
Let us first save the model locally
model_dirmodel_name = 'Final_lightgbm_model''/content/drive/My Drive/azure_deploy_model/'# Saving model to google drivesave_model(final_lightgbm, model_dir + model_name)Transformation Pipeline and Model Successfully Saved
To load a saved model at a future date in the same or an alternative environment, we would use PyCaret’s load_model() function and then easily apply the saved model on new unseen data for prediction.
saved_final_lightgbm = load_model(model_dir + model_name)Transformation Pipeline and Model Successfully Loaded
Once the model is loaded in the environment, you can simply use it to predict on any new data using the same predict_model() function. Below we have applied the loaded model to predict the same data_unseen that we used in section 13 above.
new_prediction = predict_model(saved_final_lightgbm, data=data_unseen)new_prediction.head()
Notice that the results of unseen_predictions and new_prediction are identical.
Microsoft Azure is one the biggest cloud providers and machine learning on cloud as a service. In my previous article, I have covered of deploying model to Google Cloud Platform. Library has already support for Amazon web services deployment.
Once, we have the trained model the next task is to deploy it to serve the clients. There are various deployment options available however in this section I focus on deploying it on Microsoft Azure Platform. I try to use a similar approach as followed in pycaret library for deploying on AWS.
The pre-requisite to deploy the ml model models on Azure cloud are
Familiarity with Microsoft Azure Platform
Microsoft Azure account
Basic understanding of storage containers and its command line tool
A Final Trained Model with pycaret
Read the Guide on Quickstart: Manage blobs with Python v12 SDK
import os, uuidfrom azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
AZURE_STORAGE_CONNECTION_STRING is the authentication string to connect to the Azure storage blob. You can set this string in your environment and can use any one time. Another option is that you enter your string whenever a connection is needed.
Microsoft Azure storage container is equivalent to Google Cloud Storage Bucket
# add the connection string in environment# Linux! export AZURE_STORAGE_CONNECTION_STRING="<yourconnectionstring>"# After you add the environment variable, restart any running programs that will need to read the environment variable. For example, restart your development environment or editor before continuing.# Retrieve the connection string for use with the application. The storage# connection string is stored in an environment variable on the machine# running the application called AZURE_STORAGE_CONNECTION_STRING. If the environment variable is# created after the application is launched in a console or with Visual Studio,# the shell or application needs to be closed and reloaded to take the# environment variable into account.connect_str_env = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
You can enter your connection string in google colab using below form
## Enter connection string when running in google colabconnect_str = 'Enter Connection String Here' #@param {type:"string"}print(connect_str)
To create the connection to blob container, use below command to create service client object.
# Create the BlobServiceClient object which will be used to create a container clientblob_service_client = BlobServiceClient.from_connection_string(connect_str)
I have written/collected below utilities to upload/download data from blobs.
def create_container(container_name):  # Create the container  container_client = blob_service_client.create_container(container_name)  return container_clientdef upload_blob(container_name, source_file_name, destination_blob_name):  # Create a blob client using the local file name as the name for the blob  blob_client = blob_service_client.get_blob_client(container=container_name, blob=destination_blob_name)  print("\nUploading to Azure Storage as blob:\n\t" + source_file_name)  # Upload the created file  with open(source_file_name, "rb") as data:      blob_client.upload_blob(data)def download_blob(container_name, source_blob_name, destination_file_name):  # Download the blob to a local file  print("\nDownloading blob to \n\t" + destination_file_name)  # Create a blob client using the local file name as the name for the blob  blob_client = blob_service_client.get_blob_client(container=container_name, blob=source_blob_name)  if destination_file_name is not None:         with open(destination_file_name, "wb") as download_file:          download_file.write(blob_client.download_blob().readall())        print(            "Blob {} downloaded to {}.".format(                source_blob_name, destination_file_name            )        )
Using above utilities, use below code to upload the model to Azure-container. Before uploading, we create the container if not done before.
Containers can be created using GUI too.
# Create a unique name for the containercontainer_name = "pycaret" + str(uuid.uuid4())container_client = create_container(container_name)# Save Model Local/google drive and upload to Azuremodel_name_azure = 'lightgbm-reg101-azure'save_model(final_lightgbm, model_name= model_dir + model_name_azure, verbose=False)model_src = model_dir + model_name_azure +'.pkl'model_dst = str(model_name)+'.pkl'upload_blob(CLOUD_PROJECT, bucket_name, model_src, model_dst)File /content/drive/My Drive/azure_deploy_model/lightgbm-reg101-azure.pkl uploaded to Final_lightgbm_model.pkl.
Once your model is upload to Azure, you can download anytime to perform the predictions. I follow a simple flow, where model is downloaded locally or in google drive first and then using load_model function to load the model.
print("\nListing blobs...")# List the blobs in the containerblob_list = container_client.list_blobs()for blob in blob_list:    print("\t" + blob.name)outfile_name = model_dir + 'lightgbm-reg101-azure-downloaded'model_azure_src = str(model_name)+'.pkl'download_blob(container_name, model_azure_src, outfile_name + '.pkl')
Use the recently downloaded model from Azure to perform the prediction.
# Loading the model for predictionsazure_final_lightgbm = load_model(outfile_name)Transformation Pipeline and Model Successfully Loaded# Predictions from deployed modelnew_prediction_azure = predict_model(azure_final_lightgbm, data=data_unseen)new_prediction_azure.head()
Follow the below Google colaboratory notebook to reproduce and practice this guide.
In this tutorial, we learned how to deploy a model to Microsoft Azure when trained with pycaret library.
The main objective was to use built-in utils of pycaret to deploy and load a model from Azure. Below are some highlights
Mount google drive for saving models
Train Regression model with pycaret
Saving and loading trained/finalized model Microsoft Azure
Deploying trained/finalized model to Microsoft’s Azure container
Using Azure deployed model to perform predictions