Deploying Machine Learning Models with Heroku

https://miro.medium.com/max/1200/0*rdLXHfDU5dRruVkY

Original Source Here

What is Deployment?

For starters, deployment is the process of integrating a trained machine learning model into a production environment, usually intended to serve an end-user.

Deployment is typically the last stage in the development lifecycle of a machine learning product. A brief overview of the development lifecycle is depicted in the image below:

The development lifecycle of machine learning product (Image by author)

The “Model Deployment” stage above consists of a series of steps which are shown in the image below:

The model deployment stage (Image by author)

The three primary steps to deploy a machine learning model are as follows:

  • First, we develop the machine learning model. This includes gathering relevant data and analyzing it, generating features, selecting models, performing hyper-parameter tuning, and finally, evaluating the model.
  • Once we are confident with the predictions and the accuracy metrics, we integrate the model into a web application, such as Flask, Django, etc. This involves creating the front-end (or user interface) of the application for the user to interact and provide input, and then blending it with the back-end to feed the obtained data to the machine learning model to make predictions.
  • Lastly, we deploy the application to a server using hosting services like Heroku, Google Cloud, etc.

For the purpose of this tutorial, I will use Flask to build the web application. Moving ahead, I shall deploy the application on a Heroku server.

Step 1: Developing the Machine Learning Model

In this section, let’s train the machine learning model we intend to deploy. For simplicity and to not divert from the primary objective of this post, I will deploy a linear regression model.

#1 Generating Dummy Data Points

I will train a linear regression model on a set of dummy data points depicted in the scatter plot below:

Scatter plot of the dummy data points (Image by author)

The mapping from the independent variable to x to the dependent variable y is implemented below:

#2 Training the Model

Next, I will use scikit-learn to train a linear regression model. This is demonstrated below:

The regression line learned by the model is depicted below:

Scatter plot of the dummy data points with the regression line (Image by author)

#3 Saving the Model to a Pickle File

To deploy the trained model on the Heroku server, you should save it as a pickle file, as shown in the code block below:

Step 2: Creating a Web Application using Flask

Flask is a popular web framework used to build lightweight web applications in python. As explained above, the purpose of using Flask in this project is to build a web application that an end-user can interact with.

In this section, I will use the model pickle file created in Step 1 and integrate it into the web application. The front-end of the web application will allow the user to provide input to the model. This will be fetched and delivered to the model running at the back-end to make a prediction. Finally, we will retrieve the model’s prediction and display it to the user.

#1 Project Requirements

To build a web application in Flask, you should install the Flask library in python. Open the command line and type the following command to install Flask:

pip install Flask

#2 Web Application Workflow

Ignoring the technical implementation for a while, the step-wise expected workflow of the web application should be as follows (in layman’s terms):

The web application workflow (Image by author)
  • First, we display an HTML page with a form for the user to provide input. Moreover, the user should click the “predict” button to know the corresponding output.
  • Once the user clicks “predict”, the web app should fetch the input value and take it to the backend for further processing.
  • The next step is to compute the input features from the fetched value and provide them as input to the trained model to generate the output.
  • Lastly, the app should carry the predicted value to the HTML page and display it to the user.

#3 Implementation

To build this web application in Flask, we need to code two files and integrate them together. These are:

  1. app.py: This file contains the Flask APIs that interact with the web page. It is responsible for fetching the input value, computing the prediction after loading the model, and returning it to the HTML file.
  2. new.html: As the name indicates, this file contains the front-end of our web application which the user will see.

The current directory structure of the application is:

Linear_Regression_Model
├── templates
│ ├── new.html
├── app.py
└── model.pickle

The implementation for both the files is shown below:

The implementation of the web application (Image by author)
  • The app.py file defines two methods, new() and predict(). The new() method corresponds to the “/” URL of the app and returns the new.html web page.
  • The predict() method is used to compute the model’s prediction. As the user clicks the “predict” button on the web page, a POST request is sent to the predict() method.
  • Once the model has given its prediction, we render the new.html page again and send a prediction_statement which is displayed to the user.

#4 App Walkthrough

To execute the app, run python app.py in the project directory. Once the server starts running, head over to http://127.0.0.1:5000/ in any local browser to open the app.

The walkthrough of this app is shown below:

A walkthrough of the Flask web application (Gif by author)

As depicted in the gif above, the user gets a form to input a value. This is taken to the back-end for processing. Once the model makes its prediction, the predicted value is displayed to the user.

Step 3: Deploying the Application to a Heroku Server

Now that we have trained the machine learning model and integrated it into a web application, our final step is to deploy the application to a Heroku Server — a free cloud-as-a-service platform to deploy any web app.

Supported programming languages include Java, PHP, Python, Go, etc. Moreover, most data scientists use Heroku to gain hands-on experience deploying models on the cloud. However, before deploying an app, you should create an account on Heroku.

To push the code to Heroku servers and deploy, Heroku provides three different ways: Heroku git, GitHub, and the container registry.

The options to deploy code to Heroku servers (Image by author)

I will use Heroku Git in this post to deploy the model.

Now, let’s begin with the deployment process.

#1 Install requirements

In terms of package requirements, you should install the gunicorn package in python as follows:

pip install gunicorn

Next, you should install the Heroku Command Line Interface (Heroku-CLI). Depending upon your operating system, you can find the instructions here.

#2 Add requirements.txt and Procfile to the directory

Before pushing the code to deployment, you should specify the requirements of your projects in the requirements.txt file. The file for this project appears as follows:

Flask==2.1.0
Werkzeug==2.0.3
numpy==1.21.5
gunicorn==20.1.0
jinja2
Werkzeug
itsdangerous

Next, create a new file and name it Procfile, and add the following command:

web: gunicorn app:new

Essentially, the file lets the production environment know which function in the app file is the main method. Moreover, it also provides the command that the app will run while starting up. The app:new part signifies that the main file is app.py and within that, the new() method is the primary function.

After this step, your directory structure should appear as follows:

Linear_Regression_Model
├── templates
│ ├── new.html
├── app.py
├── model.pickle
├── requirements.txt
└── Procfile

#3 Create an app on Heroku Server

Navigate to your account dashboard and select Create new app.

The option to create a new app on the Heroku account dashboard (Image by author)

Next, type a name for your application and select Create app.

The field to input the app name to host on Heroku (Image by author)

Once you have created the app, you can begin the deployment process, which is demonstrated in the next step.

#4 Deploy the app to Heroku Server

First, select Heroku Git as the “Deployment Method”.

The options to deploy code to Heroku servers (Image by author)

Open the terminal on your local computer and log in to Heroku. Make sure you have installed the Heroku-CLI.

$ heroku login

Next, navigate to your project and initialize a git repository as follows:

$ cd my-project/
$ git init
$ heroku git:remote -a linearregressiontest

Now, commit your code to the repository and deploy it to Heroku using Git.

$ git add .
$ git commit -am "deployment step 1"
$ git push heroku master

If the deployment is successful, you should see the following logs in the command line:

Image depicting successful deployment (Image by author).

With this, your Machine Learning model has been deployed successfully! The application can be accessed at the following address:

https://your-app-name.herokuapp.com/

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: