Model Deployment using Flask and PyCaret

Sagar Jiyani
5 min readSep 26, 2020

A study found that 90% of Machine Learning models is cooked up by the Students, Practitioners and Data Scientists never make it into the Production. If you’re preparing your Machine Learning or Data Science Portfolio, you need to stop obsessing over training models for higher accuracy or precision and start deploying them. Deployment is also a skill recruiters actively look for when hiring ML Engineers or Data Scientists.

So in this blog I just want to show you basically how to train a model using pycaret and deploy it with the flask app, so it would be better if you follow my GitHub repository. This tutorial is based on the Deployment so I’m not going to more focus on data and the score of that model.

What is Deployment?

In a typical machine learning and deep learning project, we usually start by defining the problem statement followed by data collection and preparation, understanding of the data, and model building, right?

But, in the end, we want our model to be available for the end-users so that they can make use of it. Model Deployment is one of the last stages of any machine learning project and can be a little tricky. How do you get your machine learning model to your client/stakeholder? What are the different things you need to take care of when putting your model into production? And how can you even begin to deploy a model?

Here comes the role of Flask.

What is Flask?

Flask is a web application framework written in Python. It has multiple modules that make it easier for a web developer to write applications without having to worry about the details like protocol management, thread management, etc.

Flask gives is a variety of choices for developing web applications and it gives us the necessary tools and libraries that allow us to build a web application.

In this tutorial, we will lean on the resourcefulness of Flask to help us deploy our own machine learning model. You’ll love working with Flask!

# Installing Flask from cmd 
pip install flask

What is PyCaret?

PyCaret is an open source low-code machine learning library in Python that aims to reduce the hypothesis to insights cycle time in a ML experiment. It enables data scientists to perform end-to-end experiments quickly and efficiently.

In comparison with the other open source machine learning libraries, PyCaret is an alternate low-code library that can be used to perform complex machine learning tasks with only few lines of code.

PyCaret is simple and easy to use. All the operations performed in PyCaret are automatically stored in a custom Pipeline that is fully orchestrated for deployment. PyCaret is essentially a Python wrapper around several machine learning libraries and frameworks such as scikit-learn, XGBoost, Microsoft LightGBM, spaCy and many more.

# Installation of pycarey from cmd
pip install pycaret

Understanding the Problem Statement

I’ll work with a House Price Dataset which is taken from the kaggle. It is a Regression problem and the goal is to predict the house price. The Original dataset has 81 features but I have reduced it to only 16 features to make it easy for deployment. You will find out the dataset and code on my GitHub Repository. This tutorial is based on the Deployment so I’m not going to more focus on data and the score of that model.

Tasks:

  • Train and validate model and develop a machine learning pipeline for deployment.
  • Build a basic HTML front-end with an input form for independent variables
  • Build a back-end of the web application using a Flask Framework.

Build a ML Model using PyCaret

First of all we have to do data pre-processing, feature engineering, feature selection, label encoding, train-test split and so on. PyCaret automates all this steps by just initializing the setup function with necessary parameters.

# Initialize the setup function 
from pycaret.regression import *
r2 = setup(data, target = 'charges')

After comparing the different models, we have to create a best performing model. In this tutorial the best performing model is catboost. So we can create a model using PyCaret by just running a command like,

# Creating a Model
model = create_model('catboost')

We can also hypertune that particular model to get the better score, for that we have to use command like

# Hyperparameter tuning the model 
tuned_model = tune_model(model)

Then we can evaluate the model by simply running a command like

# Evaluate the model
evaluate_model(tuned_model)

We can save the model as a pickle file so that later we can use that model to deploy in website

# Saving a model 
catboost = save_model(tune_model, 'final_model')

Create Interactive Webpage

Generally, the front-end of web applications are built using HTML which is not the focus of this article. We have used a simple HTML template and a CSS style sheet to design an input form. Here’s the HTML snippet of the front-end page of our web application. Here is the basic structure of HTML code, you will need to add the form elements in the further code. If you are using my GitHub repository then you don’t need to do anything.

<html>
<head>
<title>Demo page</title>
</head>
<body>
<form action="predict" method="POST">
<!-- All the input boxes will be added here-->
</form>
<!-- Show the Predicted Value -->
{{ predicted_text }}
</body>
</html>

Create a Flask app and connect with the model

The back-end of a web application is developed using a Flask framework. For beginner’s it is intuitive to consider Flask as a library that you can import just like any other library in Python. See the sample code snippet of our back-end written using a Flask framework in Python.

Running a Flask App

Open command prompt and go to the project directory where app.py file is present. Then run the command

python app.py

You will get the Screen like this, then open the browser and go for http://localhost:8080

If all things are working well then you will get the screen like this,

Originally published at https://sgjiyani.wordpress.com on September 26, 2020.

--

--