Machine learning Model Deployment using Flask and Heroku

Model building is only a part of your data science project. The trained machine learning model should be deployed as a service on the web to the general public or users. In this article we will learn how to deploy a machine learning model using Flask and Heroku.

Flask is a python web framework. Flask lets you develop web applications very easily. The “Hello world” app in flask is just seven lines of code but learning how to build a full-fledge web applications with any framework takes a lot of work. Flask is always a good choice for beginners because unlike Django flask is very pythonic. Flask is based on the Werkzeug WSGI toolkit and the Jinja2 template engine. Both are Pocco projects. (Pocoo was a loosely assembled team of Open Source developers working on some very popular Python projects)

Heroku is a cloud-based, platform-as-a-service (PaaS) that gives startup developers a powerful solution for building, deploying and operating applications. It is possible to do the deployment directly from popular tools like Git, GitHub, or Docker.

Major steps

  1. Creating the project directory and python environment.
  2. Training and saving the model
  3. Creating the app.py file where we set all the methods and requests
  4. Setting the template file and Running the app.py in the local machine
  5. Creating the Procfile and requirements.txt
  6. Using the Heroku account connect the GitHub repository

First you need to create a project directory and within it you need python environment. In windows command line you can do this as follows:

mkdir directory_name         # creating the project directory 
py -3 -m venv venv # creating new environment
venv\Scripts\activate # activating the environment

Now we have activated the python environment in the project directory. You can use the pip install command to install the necessary packages like NumPy, Pandas, SciKit learn etc.

In order to train and save the model we can create a new python file and say model.py. To create the model we use student marks data set. You can download the data set from the following link: https://github.com/thomasreji155/model_deployment-flask/blob/master/student-mat.csv

For the model building we use linear regression. Here our focus is not on prediction accuracy but on the deployment part. Following is the model.py file where we created and saved the mode. We use pickle to save the model to disk.

So, our modelling phase is over. Now its the time to use flask to create the web app. You can install flask using usual pip install command. First you need to create an app.py and an html form. In the html page there are a couple of placeholders for dynamic content enclosed in {{…}} sections. These represents the parts of the page that are variables, where the the values will only be known at runtime. The render_template() function takes the template file and variable list and returns the same template but with all placeholders replaced with actual values. Given below the codes in app.py where we

We already saved our machine learning model, so now we just need to load the model using pickle. In the above code the method attribute specifies the http request method that should be used when submitting the form to the server. For the prediction we use six variables, So we have to setup html form for getting the values for these features.

We need a two methods in the app.py, one index method to show the html form and one predict method for the output prediction. First we have to create a template directory inside the project directory. Now we need to set up the template html for our application simultaneously. The html <form> element is used as a container for the web form. I shall give a sample html form where you can create a form for getting the user input;

The action attribute of the form is used to tell the browser the URL that should be used when submitting the information the user entered in the form. We use names for each user input and this will be referred in the predict method. In the final line we used a place holder {{prediction_text}} for printing the output. When the user click the predict button it will show the index page where the prediction_text will be get replaced. Now we have to check the app in the local machine.

We have to create the a requirements.txt file in the project root folder using the following command: You can run the command in the terminal. The app will not be deployed correctly if it is not in the root folder.

pip freeze > requirements.txt

To create a Procfile in the project main directory. Make sure that the Procfile does not contain any extensions like .txt

web: gunicorn app:name 
# app represents the name of the python file that runs your application. name represents your app name.

The next process is to upload the files into your GitHub repository.

You have to create a Heroku account first then you can select create new app in the Heroku dashboard. You can pass an app name there and connect your GitHub repository. You just need to click the deploy main branch option and this will automatically show you the URL generated. And finally your app URL is ready now.

Thank you :)

Data Science Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store