Build with PyCaret: Deploy on Google Cloud 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 on Google Cloud Platform. This tutorial can be used to deploy any machine learning model(s) with slight modifications.
We will learn how to deploy models trained with pycaret to Google Cloud Platform. As you might know, pycaret already has support to deploy the trained models on AWS but not with GCP or Azure at the moment. I followed the similar code-practices as used in the library for deploying models on AWS and added essential utilities for GCP deploying.
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].
A PyCaret is an autoML framework for citizen data scientists term used in its official documentation. PyCaret is relatively a new library released a 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 the deployment of trained/finalized models to Google Cloud Platform. Although, It has support only for deploying on Amazon web services.
Keeping in a view wide use of GCP as cloud service, I have given a try to add a tutorial on deploying the trained model on google cloud using PyCaret’s way.
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/write 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/gcp_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 repository 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 prepare a modeling pipeline using PyCaret’s setup module.
from pycaret.regression import *exp_reg101 = setup(data = data, target = 'Price', session_id=123)Setup Succesfully 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 the focus of this tutorial.
lightgbm = create_model('lightgbm')
Let us train the model also called 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)
Let us plot the prediction errors vs true values of target.
plot_model(tuned_lightgbm, plot = 'error')
feature importance is a 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. The 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/gcp_deploy_model/'# Saving model to google drivesave_model(final_lightgbm, model_dir + model_name)Transformation Pipeline and Model Succesfully 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 Sucessfully Loaded
Once the model is loaded in the environment, you can simply use it to predict 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 the section 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.
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 Google Cloud AI platform. I try to use a similar approach as followed in pycaret library for deploying on AWS.
The pre-requisites to deploy the machine learning models on google cloud are
Familiarity with Google Cloud Projects
Basic understanding of storage buckets and it gsutil command-line tool
Basic Understanding of gcloud command-line tool to interact with Google Cloud
A Final Trained Model with PyCaret
Read the Guide
from google.colab import authauth.authenticate_user()
Define google cloud project, target bucket, and set the cloud_project Environment variable. Create a google cloud project if not created earlier. Follow the above guide for more information on it.
# GCP project name, Change the name based on your own GCP project.CLOUD_PROJECT = 'gcpessentials-rz' # GCP project namebucket_name = 'pycaret-reg101-rz' # bucket name for storage of your modelBUCKET = 'gs://' + CLOUD_PROJECT + '-{}'.format(bucket_name)# Set the gcloud consol to $CLOUD_PROJECT Environment Variable for your Desired Project)!gcloud config set project $CLOUD_PROJECTUpdated property [core/project].
We define some utility functions to create a google cloud storage bucket, uploading a blob to storage bucket, and downloading the blob from the storage bucket.
The codes are taken from Google official documentation and modified slightly based on our requirements here.
from google.cloud import storagedef create_bucket(project_name, bucket_name):    """Creates a new bucket."""    # bucket_name = "your-new-bucket-name"    storage_client = storage.Client(project_name)    buckets = storage_client.list_buckets()    if bucket_name not in buckets:      bucket = storage_client.create_bucket(bucket_name)      print("Bucket {} created".format(bucket.name))    else:      raise FileExistsError('{} already exists'.format(bucket_name))def upload_blob(project_name, bucket_name, source_file_name, destination_blob_name):    """Uploads a file to the bucket."""    # bucket_name = "your-bucket-name"    # source_file_name = "local/path/to/file"    # destination_blob_name = "storage-object-name"    storage_client = storage.Client(project_name)    bucket = storage_client.bucket(bucket_name)    blob = bucket.blob(destination_blob_name)    blob.upload_from_filename(source_file_name)    print(        "File {} uploaded to {}.".format(            source_file_name, destination_blob_name        )    )def download_blob(project_name, bucket_name, source_blob_name, destination_file_name):    """Downloads a blob from the bucket."""    # bucket_name = "your-bucket-name"    # source_blob_name = "storage-object-name"    # destination_file_name = "local/path/to/file"    storage_client = storage.Client(project_name)    bucket = storage_client.bucket(bucket_name)    blob = bucket.blob(source_blob_name)        if destination_file_name is not None:       blob.download_to_filename(destination_file_name)      print(          "Blob {} downloaded to {}.".format(              source_blob_name, destination_file_name          )      )          return blob
Using the above utilities, use the below code to upload the model to the GCP storage bucket.
# Create Bucketcreate_bucket(CLOUD_PROJECT, bucket_name)Bucket pycaret-reg101-rz created
Upload the model to google cloud storage bucket
# Save Model Local/google drive and upload to GCPmodel_name_gcp = 'lightgbm-reg101-gcp'save_model(final_lightgbm, model_name= model_dir + model_name_gcp, verbose=False)model_src = model_dir + model_name_gcp +'.pkl'model_dst = str(model_name)+'.pkl'upload_blob(CLOUD_PROJECT, bucket_name, model_src, model_dst)File /content/drive/My Drive/gcp_deploy_model/lightgbm-reg101-gcp.pkl uploaded to Final_lightgbm_model.pkl.
Once your model is upload to GCP, you can download anytime to perform the predictions. I follow a simple flow, where the model is downloaded locally or in google drive first, and then using the load_model function, predictions can be generated.
outfile_name = model_dir + 'lightgbm-reg101-gcp-downloaded'model_gcp_src = str(model_name)+'.pkl'model_downloaded = download_blob(CLOUD_PROJECT, bucket_name, model_gcp_src, outfile_name + '.pkl')Blob Final_lightgbm_model.pkl downloaded to /content/drive/My Drive/gcp_deploy_model/lightgbm-reg101-gcp-downloaded.pkl.os.listdir(model_dir)['Final_lightgbm_model.pkl', 'lightgbm-reg101-gcp.pkl', 'lightgbm-reg101-gcp-downloaded.pkl']
Use the recently downloaded model from GCP to perform the prediction.
# Loading the model for predictionsgcp_final_lightgbm = load_model(outfile_name)Transformation Pipeline and Model Sucessfully Loaded# Predictions from deployed modelnew_prediction_gcp = predict_model(gcp_final_lightgbm, data=data_unseen)new_prediction_gcp.head()
Follow the below Google colaboratory notebook to reproduce and practice this guide.
In this tutorial, we learned how to deploy the model to GCP when trained with pycaret library.
The main objective was to use built-in utils of pycaret to deploy and load the model from GCP. Below are some highlights
Mount google drive for saving models
A train Regression model with pycaret
Saving and loading the trained/finalized model google drive
Deploying trained/finalized model to GCP Bucket
Using the GCP deployed model to perform predictions PyCaret’s way