How to Easily Deploy Machine Learning Models Using Flask
This post aims to make you get started with putting your trained machine learning models into production using Flask API.
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.
What is Linear Regression
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.
Hypothesis of Linear Regression
The linear regression model can be represented by the following equation
- Y is the predicted value
- θ₀ is the bias term.
- θ₁,…,θₙ are the model parameters
- x₁, x₂,…,xₙ 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.
Environment and tools
Where is the code?
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.
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.
Writing a simple Flask Web Application in 80 lines
Sample tutorial for getting started with flask
Deploying Machine Learning Models | Coursera
Learn Deploying Machine Learning Models from University of California San Diego. In this course we will learn about…
Simple way to deploy machine learning models to cloud
Deploy your first ML model to production with a simple tech stack
Overview of Different Approaches to Deploying Machine Learning Models in Production - KDnuggets
There are different approaches to putting models into productions, with benefits that can vary dependent on the…
Before You Go
The corresponding source code can be found here.
Sample end to end projects from data collection to putting models into production …
If you want to keep updated with my latest articles and projects follow me on Medium. These are some of my contacts details:
Bio: Abhinav Sagar is a senior year undergrad at VIT Vellore. He is interested in data science, machine learning and their applications to real-world problems.
Original. Reposted with permission.
- Building a Flask API to Automatically Extract Named Entities Using SpaCy
- Deploy your PyTorch model to Production
- The Hackathon Guide for Aspiring Data Scientists