Don’t just train, but also deploy: a step-by-step guide
Building and deploying data-driven intelligent systems at scale has been of great interest lately to organizations operating in the machine learning space. This has not only led to the rapid evolution of efficient and accurate systems but has also had a profound contribution in improving the overall end-user experience by using intelligence-embedded products.
Due to the various benefits associated with intelligence-embedded software, managing machine learning models in the deployment stage has become an essential skill for machine learning engineers today. This, sometimes, can even require separate teams to release and manage the product in deployment.
Therefore, this post will provide a detailed overview of how you can take your trained model to deployment. Specifically, I will demonstrate the use of Flask to create a web application and then use Heroku to deploy the machine learning model to the cloud.
The highlight of the article is as follows:
What is Deployment?
Step 1: Developing the Machine Learning Model
Step 2: Creating a Web Application using Flask
Step 3: Deploying the Application to a Heroku Server
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 “Model Deployment” stage above consists of a series of steps which are shown in the image below:
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.
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:
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:
#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:
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):
- 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.
To build this web application in Flask, we need to code two files and integrate them together. These are:
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.
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:
│ ├── new.html
The implementation for both the files is shown below:
app.pyfile defines two methods,
new()method corresponds to the
“/”URL of the app and returns 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
- Once the model has given its prediction, we render the
new.htmlpage again and send a
prediction_statementwhich 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:
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.
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.
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:
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:
│ ├── new.html
#3 Create an app on Heroku Server
Navigate to your account dashboard and select
Create new app.
Next, type a name for your application and select
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
Heroku Git as the “Deployment Method”.
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:
With this, your Machine Learning model has been deployed successfully! The application can be accessed at the following address: