How To Easily Deploy Machine Learning Models Using Flask - by Abhinav Sagar - Towards Data Science
How To Easily Deploy Machine Learning Models Using Flask - by Abhinav Sagar - Towards Data Science
Stuck behind the paywall? Click here to read the full story with my Friend Link!
When a data scientist/machine learning engineer develops a machine learning model using
Scikit-Learn, TensorFlow, Keras, PyTorch etc, the ultimate goal is to make it available in
production. Often times when working on a machine learning project, we focus a lot on
Exploratory Data Analysis(EDA), Feature Engineering, tweaking with hyper-parameters etc.
But we tend to forget our main goal, which is to extract real value from the model predictions.
https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 1/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science
Deployment of machine learning models or putting models into production means making
your models available to the end users or systems. However, there is complexity in the
deployment of machine learning models. This post aims to make you get started with putting
your trained machine learning models into production using Flask API.
I will be using linear regression to predict the sales value in the third month using rate of
interest and sales of the first two months.
https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 2/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science
Why Flask?
Easy to use.
Extensively documented.
Project Structure
This project has four parts :
1. model.py — This contains code for the machine learning model to predict sales in the
third month based on the sales in the first two months.
2. app.py — This contains Flask APIs that receives sales details through GUI or API calls,
computes the predicted value based on our model and returns it.
3. request.py — This uses requests module to call APIs defined in app.py and displays the
returned value.
4. HTML/CSS — This contains the HTML template and CSS styling to allow user to enter
sales detail and displays the predicted sales in the third month.
https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 3/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science
2. pandas
3. numpy
4. flask
Let’s get started with making the front end using HTML for the user to input the values. There
are three fields which need to be filled by the user — rate of interest, sales in first month and
sales in second month.
https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 4/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science
Next I did some styling using CSS for the input button, login buttons and the background.
https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 5/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science
I created a custom sales dataset for this project which has four columns — rate of interest,
sales in first month, sales in second month and sales in third month.
https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 6/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science
Let’s now make a machine learning model to predict sales in the third month. First let’s deal
with missing values using Pandas. Missing Data can occur when no information is provided
for one or more items. I filled the rate column with zero and sales in first month with mean of
that column if the value was not provided. I used linear regression as the machine learning
algorithm.
Serializing/De-Serializing
In simple words serializing is a way to write a python object on the disk that can be transferred
anywhere and later de-serialized (read) back by a python script.
Serialization, De-Serialization
I converted the model which is in the form of a python object into a character stream using
pickling. The idea is that this character stream contains all the information necessary to
reconstruct the object in another python script.
https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 7/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science
The next part was to make an API which receives sales details through GUI and computes the
predicted sales value based on our model. For this I de- serialized the pickled model in the
form of python object. I set the main page using index.html . On submitting the form values
using POST request to /predict , we get the predicted sales value.
The results can be shown by making another POST request to /results. It receives JSON
inputs, uses the trained model to make a prediction and returns that prediction in JSON format
which can be accessed through the API endpoint.
https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 8/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science
Finally I used requests module to call APIs defined in app.py . It displays the returned sales
value in the third month.
Results
Run the web application using this command.
$ python app.py
https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 9/10
11/17/2020 How to Easily Deploy Machine Learning Models Using Flask | by Abhinav Sagar | Towards Data Science
Open http://127.0.0.1:5000/ in your web-browser, and the GUI as shown below should appear.
Conclusions
This article demonstrated a very simple way to deploy machine learning models. I used linear
regression to predict sales value in the third month using rate of interest and sales in first two
months. One can use the knowledge gained in this blog to make some cool models and take
them into production so that others can appreciate their work.
https://towardsdatascience.com/how-to-easily-deploy-machine-learning-models-using-flask-b95af8fe34d4 10/10