Machine Learning Model Deployment as APIs (flask)

Lekha Priya
5 min readApr 28, 2022

There is a process of solving any machine learning model that involves aspects ranging from collecting data to evaluating the model and that is exactly what is called a Machine Learning Pipeline.

When we are asked to predict whether a particular person will take a loan or not or whether an email is a spam or not, the general steps involved would be:

  1. Data collection: The first step is to acquire the dataset that is required for this particular prediction.
  2. Data analysis: After we have acquired the data, the next step is to analyze it. We identify the various data features and the variables present in it.
  3. Exploratory Data Analysis: After the analysis of the data, we start inferencing meaningful insights from it which requires performing the exploratory data analysis and treat issues like missing values, outliers, categorical variables and so on.
  4. Feature Engineering: After EDA, we analyze different features present in the data and shortlist the best fit features for model training.
  5. Model training: After we have performed all the above steps, the next step is to identify the apt algorithm that gives us the best metrics.
  6. Evaluation: After the model is trained, we evaluate it on a test set to analyze how well the model has performed.

Now that we have followed the ML Pipeline and trained the model and evaluated it, what next? How do we convert the model into a product and extract real values from model prediction? We cannot use jupyter notebook or Google colab for the same nor can we train the model every time we need the predictions, so what do we do? Answer is we deploy the model.

What is Model Deployment?

Making the model built available for end users or systems to use as a product that performs desired tasks is called Deployment of the model or in other terms productionizing the Machine Learning model.

In order to deploy a model, we need to follow the software architecture of deployment that has required components to be installed before deployment.

In this article we are focusing on deploying a machine learning model using Flask Rest API for which we need to first understand VCS.

Version Control System:

Until now we had only one contributor who is writing the code for the model but in real time there is usually more than one person contributing to the code and under this scenario if one person makes some changes to the code, how to make sure others working on the code do not make the same changes again? That’s where Version Control System comes into picture.

Version Control system keeps a track of all the changes done in the project like history of all the files created in the project, revert back to stable versions irrespective of number of changes made and collaborates as a team without affecting anyone’s work by merging all the changes to the stable version. This leads us to a very famous Version Control System called GIT and GIT HUB.

GIT is a version control system that has all the properties we have discussed above.

Github is a social code hosting platform. On Github we can look into open source codes and can suggest changes too. It offers the functionality of hosting websites directly from repository. Here we shall not discuss the terminologies and installation of Github as https://git-scm.com/book/en/v2/Getting-Started-Installing-Git helps us do so.

Now that we have learnt about Deployment, Version Control System and Github, the next main thing is to deploy the model using Flask Rest API. Before implementing and deploying the model using flask API, let us understand each term individually.

API

API stands for Application program Interface. It is a set of protocols and tools that help in building the software. They act as a medium of communication between the software components. Every time you send a message, look at a tweet, you are using an API.

REST

REST is an acronym for Representational State Transfer. REST is the set of rules that are followed while creating API to ease the communication. A system is called Restful if it adheres to the given constrain.

WEB FRAMEWORK-FLASK

A web-framework is a software framework that is used to support the development of web application by providing us with reusable codes and extensions for common operations.

There are a lot of frameworks available like:

  1. Flask
  2. Django
  3. Tornado
  4. Pyramid.

Choosing the right web framework for deployment is very important. For our model, we will use Flask.

Before we start implementing flask we need to perform certain tasks that is a pre-requisite to deployment for us to have a error free resolution.

  1. Packaging of ML Model: To save it in a particular format using Docker
  2. Build Application Skeleton: To get the capability of giving the input and getting the output through a URL
  3. Test the application: To check if the task performed is working or not
  4. Package the application: To make sure there are no version issues
  5. Design a user-interface: To serve the customer with the product output

Implementation of Flask

To install Flask. Run the following commands:

  1. conda install flask
  2. pip install flask

After installing flask we import flask and create a flask application object that contains the data and post that we look at methods of the application. That is done using Postman, which is a platform for API development.
So the different types of requests by Flask are:

  • GET: Used to request data from specified sources.
  • POST: Used to send data to the server to create or update a resource.
  • PUT: Used to send data to a server to create or update a resource.
  • DELETE: Used to delete the specified resource.

It’s time to deploy a real-time model using the Flask API. I’m using the telecom churn model the is already trained and can be found at my Github Repository.

The steps followed to build the classification model are as follows:

  • Data Acquisition
  • Data Pre-Processing
  • Feature Engineering
  • Train the Model
  • Save the Model

The steps followed to deploy the model areas below:

  • Create an API using Flask
from flask import Flask, jsonify,  request, render_template
from sklearn.externals import joblib
import numpy as np
app = Flask(__name__)
model_load = joblib.load("./models/rf_model.pkl")
@app.route('/')
def home():
return render_template('index.html')
@app.route("/predict", methods=['POST'])
def predict():
if (request.method == 'POST'):
int_features = [x for x in request.form.values()]
final_features = [np.array(int_features)]
output = model_load.predict(final_features).tolist()
return render_template('index.html', prediction_text='Churn Output {}'.format(output))
else :
return render_template('index.html')
@app.route("/predict_api", methods=['POST', 'GET'])
def predict_api():
print(" request.method :",request.method)
if (request.method == 'POST'):
data = request.get_json()
return jsonify(model_load.predict([np.array(list(data.values()))]).tolist())
else:
return render_template('index.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
  • Test API in Postman
import requests
data = {
"total_ic_mou_8": 5.49,
"total_rech_amt_diff": -206,
"total_og_mou_8": 0,
"arpu_8": 656.3919999999999,
"roam_ic_mou_8": 0,
"roam_og_mou_8": 0,
"std_ic_mou_8": 0,
"av_rech_amt_data_8": 708.893089087563,
"std_og_mou_8": 0
}
url = "http://127.0.0.1:5000/predict_api"
response = requests.post(url, json=data)
print("Churn: "+ str(response.json()))
  • Create a Webpage

http://localhost:8888/view/index.html

I hope you enjoyed reading this article as much as I enjoyed writing it. If you have anything to discuss, feel free to get in touch with me through my Linkedin profile. Do not forget to share feedback:)

--

--

Lekha Priya

AI/ML solutions architect operating as trusted advisor of customers ensuring they get most of the cloud at every stage adopting machine learning across org’s