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.
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.
The objective of a linear regression model is to find a relationship between one or more features(independent variables) and a continuous target variable(dependent variable). When there is only feature it is called Uni-variate Linear Regression and if there are multiple features, it is called Multiple Linear Regression.
The linear regression model can be represented by the following equation
Y is the predicted value
θ0 is the bias term.
θ1,...,θn are the model parameters
x1, x2,...,xn are the feature values.
Easy to use.
Built in development server and debugger.
Integrated unit testing support.
RESTful request dispatching.
Extensively documented.
This project has four parts :
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.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.request.py — This uses requests module to call APIs defined in app.py and displays the returned value.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.
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.
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.
request.py — This uses requests module to call APIs defined in app.py and displays the returned value.
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.
scikit-learnpandasnumpyflask
scikit-learn
pandas
numpy
flask
Without much ado, let’s get started with the code. The complete project on github can be found here.
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.
Next I did some styling using CSS for the input button, login buttons and the background.
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.
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.
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.
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.
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.
Finally I used requests module to call APIs defined in app.py. It displays the returned sales value in the third month.
Run the web application using this command.
$ python app.py
Open http://127.0.0.1:5000/ in your web-browser, and the GUI as shown below should appear.
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.
towardsdatascience.com
www.coursera.org
towardsdatascience.com
www.kdnuggets.com
The corresponding source code can be found here.
github.com
If you want to keep updated with my latest articles and projects follow me on Medium. These are some of my contacts details:
Personal Website
Linkedin
Medium Profile
GitHub
Kaggle
Happy reading, happy learning and happy coding.