Model Deployment using Flask and PyCaret

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?

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 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


  • 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

# 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

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

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

Create a Flask app and connect with the model

Running a Flask App


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 on September 26, 2020.

Data Science Student