Machine learning Model Deployment using Flask

machine-learning project in production.

Bharat Choudhary
DataDrivenInvestor

--

machine learning deployment using flask deep learning deployment using flask
Here it begins

In this article, we are going to discuss majorly machine learning model deployment using flask API but the code for model building and deployment using flask API will be available in my GitHub repository so that you can also try this in your own project.

I assume that you have some experience in machine learning or deep learning model building and wish to productionize model with flask API.

Introduction to Flask API

Flask is a lightweight Web Server Gateway Interface(WSGI) a micro-framework written in python. This means flask provides us with tools, libraries and technologies that allow us to build a web application. This web application can be some web pages, a blog, or our machine learning model prediction web application. Flask is an intermediate medium to connect our model with front end web page for prediction as shown in below image.

flask Api machine learning model
API Data Flow

Prerequisites

We assume that all of us have knowledge about model training in jupyter notebook. This post is aimed to only provide insights on deploying a machine learning model into production using Flask API.

Libraries that require in model Deployment:

pip install pickle-mixin
pip install Flask
  1. pickle: A native python library to save (serialize) and load (de-serialize) python objects as files on the disk.
  2. Flask: A python-based easy to use micro web framework.

Project Structure

This project has four major parts :

  1. iris.py — This contains code for our Machine Learning model to predict Iris Plant(classification) on training data in ‘iris.data’ file.
  2. app.py — This contains Flask APIs that receives Iris data input through GUI or API calls, computes the precited value based on our model and returns it.
  3. iris.pkl — This contains a pre-train model which is obtain after training and will use in app.py for prediction.
  4. templates — This folder contains the HTML template to allow the user to enter input features and displays the predicted output in a webpage.

Dataset

Pandas DataFrame

Model Training

First, we read data through CSV file and we have four input features and three possible output variable so we labelEncode them for training. After that, we use SVC(support vector classification) for model training and save the model(iri.pkl) using pickle. It may be different for your problem we took simple example for understanding.

FLask API

  • Here, we initialize our flask app by calling Flask(__name__) and load the model with pickle.
  • “@app.route(‘/’)” here we initialize the root directory of our flask app and in it we definite function that will be call itself at this root directory.
  • “@app.route(‘/predict’, methods=[‘POST’])” here we define home function where we get data from a frontend HTML page using request.form[‘ ’] and predict output, then send it back to an HTML page for prediction visualization.

By default, the Flask route responds to the GET requests. However, this preference can be altered by providing methods argument to route() decorator.

In order to demonstrate the use of POST method in URL routing, first let us create an HTML form and use the POST method to send form data to a URL.

The following script starts the flask server on localhost and default port (5000) making the URL: http://127.0.0.1:5000/

Just paste http://127.0.0.1:5000/ on browser and press enter to see the server working.

Html Template

templates folder
|
+-- home.html
|
+-- after.html
  1. home.html is the default root page that opens when we open a link in a browser. It contains form through which we provide input feature(total four) in text format and on clicking on submit button it calls home function for prediction in app.py.

2. after.html is the page calls by home function in ‘/predict’ directory and it displays the prediction of the model.

Hosting on a local system

Flask API

Model building and Deployment Flow Chart

Conclusion

Model Deployment is a critical part of any machine learning pipeline.

Flask is a simple web application framework that can be easily built. Hosting and sharing machine learning models can be really easy with Flask API.

For Complete Code:- Github

If you want to learn more about Model Deployment, I would like to call out this excellent playlist on model deployment using flask and docker on youtube by Krish Naik. This was the one that helped me a lot. Do check it out.

Thanks for the read. I wish to write more beginner-friendly posts in the future too, by the way, this is my first post on Medium.

Follow me up at Medium. As always, I welcome feedback and constructive criticism and can be reached on Linkedin.

Gain Access to Expert View — Subscribe to DDI Intel

--

--