Deploy Machine Learning model with Flask and Heroku

Sagar Dubey
Analytics Vidhya
Published in
7 min readOct 23, 2019

Building a machine learning project is one thing but what matters at the end is how you show your project to the world. Documenting your whole project on GitHub is another thing but deploying your deep learning model as a web application is totally different gameball.

In order for Machine Learning Engineers to succeed at work, they need to build services such that other teams can use or a product where people can use it directly. Essentially, the goal is to provide a model as a service and for that there is a concept called API. API is the way for computer systems to talk to each other over Internet Protocols. They act as an agent which takes information from the user to the server and then again from server to the user giving back the answer. Flask provides that capability. Flask will act as an API between your model and the HTML file. In this article, we’ll be focusing on deploying the model rather than building it.

First thing first you need to have flask installed in your local machine. It can be easily done by running the command

pip install flask
or
conda install flask

After having it installed let’s build a simple Flask application.

Successful run of hello-world.py

Going over the important lines that are necessary for building a Flask app.

app=Flask(__name__)

Here, we are assigning Flask constructor to an variable app, which we need to run all the processes. You can also give your static file location.

Coming to the second line,

@app.route('/',methods=['GET','POST'])

app.route() is a decorator in Python. In Flask, each function is going to be triggered when you go to a specific page. Here, all the traffic on this URL will invoke my main() function.

You just made your first application in Flask. Isn’t it simple? Using Flask, we can now wrap our Deep Learning Model or Machine Learning Model and serve them as a Web API.

Let’s move on to make our Machine Learning model

Data Cleaning

You cannot go straight from raw text data to fitting a Machine Learning model. Data Cleaning plays a vital role while dealing with text data. When you clean your data it reduces noises and similar kinds of a word which will reduce the computation matrix hence reducing the computation time.

Removal of Stopwords

Stopwords are common words that do not add value to your model so you want to remove those words which will help in reducing the computation matrix. Some common stopwords are as — I,we,because,of,the etc

Even when you’re going to your dataset and find out that some words are repetitive you can add those custom words in your stopwords list as well.

Removal of Special Character

You don’t want hyphen and dot in your dataset so it is much better if you remove any special character which you think is irrelevant. In this, I removed all the characters which are non-alphabets.

Lemmatising/Stemming

Both of this technique is used to help us to achieve the root form of the sentence. It is used to normalize the text. If your text contains Play and Playing then our model will think that these are two different words but we know that they have the same importance and can be reduced as one. Then, It comes to which to use Stemming or Lemmatising. The main difference between the two is Stemming removes the suffix part from the word whereas Lemmatising goes back to its root word.

Vectorizing the text

We need to convert our data such that our model can understand i.e. Vectors and for that, we are using Tfidf Vectorizer, an embedding technique that takes account of the importance of each word in a document. For more about TF-IDF, you can refer to Wikipedia.

So we have converted our text data such that our machine can understand. We converted each word into a vector using tf-idf. In this tutorial, I’ve used Naive Bayes to train our model.

vectorizer = TfidfVectorizer()X = vectorizer.fit_transform(df['Processed'])X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=2019)model = MultinomialNB()model.fit(X_train,y_train)model.score(X_test,y_test)

In the end, I pickle my model as well as the vectorizer. This is very important that you pickle your vectorizer because if you don’t, it won’t give you the desired result.

We have now finished building our machine learning model. Up to this point, you will find a lot of articles but a few on how to deploy it so let’s move on to the next part of the article.

As we already know how to make a Flask Application let’s build an application which will take Message as an input and gives you back whether it is a Spam or Ham(Not Spam).

Our model is ready to get deployed now we have to create an API so let’s Go!!

When you’re creating API there are few things that you’ve to be careful about-

  1. Getting the request data from the user
  2. Pre-Process it according to your needs so that your model can understand.
  3. return the response. Use jsonify if you’re returning a JSON object. As a standard, body are sent across as JSON format. Here I’m just returning Plain Text but it is advisable to use JSON.

First thing First, we load our saved model and vectorizer which we will use later in the code. as you can see app.route(‘/’) will route to my main function which will return my main.html page that is my home page. If the user hits the submit button it will route to my /predict which takes two methods i.e. GET and POST which will trigger my predict function. I’m requesting the data from the server and using my saved vectorizer converting my text data into vectors so that the model can understand and predict for the output.

Voila!! you just made an API which will take text input and classify it as SPAM or HAM.

Here it is run locally but what if I want whole world to see my work? How can we do that? There are a lot of platforms in the market where you can deploy your code. Google App Engine, Heroku, Firebase, AWS EC2 to name a few. In this tutorial, we will use Heroku to deploy our code. It has very cheap pricing as compared to others(most of the time the free dyno plan is enough).

Folder Structure

It is very important to structure your code and files. So that it will be easier to navigate

spam.csv
app.py
model.py
requirements.txt
Procfile
templates/
main.html
runtime

You can add as many templates as you want for each page and the CSS file for the same in the new folder.

Heroku

Heroku is platform as a service (PaaS) that enables developers to build, run and operate application entirely on cloud rather than doing locally on your machine. In this project we will deploy using heroku git. There are other methods as well to deploy.

In order to deploy our model using heroku git we need to install git as well as heroku CLI. You can refer these links to install Git and Heroku CLI.

Before you deploy your code you need to create an account on Heroku.

heroku login

Creating a Heroku app

To deploy your Git project you need to create a Heroku app.

heroku apps:create unique-name-of-your-app

Requirement File

This is the first entry point for the program. It will install all the dependencies which are required to run your Code. requirements.txt will tell heroku that this project will require all these libraries to successfully run the application.

Procfile

Heroku requires Procfile to be present in your app root directory. It will tell Heroku how to run the application. Make sure it is a simple file with no extension. Procfile.txt is not valid. The part to the left of the colon is the process type and the part on the right is the command to run to start that process. In this, we can tell on which port the code should be deployed and you can start and stop these processes.

Procfile

This file tells heroku we want to use the web process with the command gunicorn and the app name.

Deploy to Heroku

Make sure Procfile and requirement.txt file are present in your app root directory.

$ git init
$ heroku git:remote -a unique-name-of-your-app

You can now commit files in the repo and push it to master branch

$ git add .
$ git commit -am "make it better"
$ git push heroku master

For more, you can refer to Heroku documentation.

Hurray! You just deployed your first app. This is a very basic API but you’ll be needing knowledge of JavaScript if you’re building a more complex web application.

Thank you very much for reading this post. I hope it will make things much more clearer than before. Please suggest in the comment section how I can improve on this post.

Cheers! and Happy Learning!! You can find the source code of this blog here.

--

--