The world’s leading publication for data science, AI, and ML professionals.

Deploy Your Python Machine Learning Models on Heroku in 3 steps

Illustrative tutorial to deploy a Flask web app to expose an ML model for public use.

Illustrative tutorial to deploy a ML model on a Flask Web App for public use.

Prerequisites:

  1. Heroku account (Sign up at https://www.heroku.com/ if you haven’t got an existing one)
  2. GIT installed on local PC (Install GIT at https://git-scm.com/downloads at no cost)
  3. A trained ML model (shall name it as model.pkl in this tutorial) developed in python.

Purpose of this tutorial

So you have a working ML model on your local development environment and you are stuck at the "Model Deployment" stage illustrated in the the ML lifecycle below:

Image by Author | Showcasing the ML life cycle
Image by Author | Showcasing the ML life cycle

Then you have come to the right place! The rest of the article shall be focusing on the Model Deployment step. The full source code is available at my GitHub at https://github.com/incubated-geek-cc/your-custom-app-name Feel free to fork it for your own use ☺

Note: The sample ML Model used in this tutorial is used to predict the likelihood which an individual will choose to seek mental healthcare treatment based on several features (Logistic Regression Model). Depending on the use-case of your ML Model, do tweak the parameters accordingly in the demo files below.

Step 1. Create a new app on Heroku

Image by Author | Step 1: Select "Create new app" at after logging into your Heroku account. Step 2: Input your desired web app name. Step 3. Select "Create app".
Image by Author | Step 1: Select "Create new app" at after logging into your Heroku account. Step 2: Input your desired web app name. Step 3. Select "Create app".

Step 2. Initiate Project Folder on Local PC

Contents inside each file (requirements.txt, runtime.txt, Procfile, app.py, X_test.json, index.html, run_app.bat):

Explanation and things to note:

  • requirements.txt – Contains all python dependencies to set up a Flask web app and load your ML Model. Advice: To avoid python library version conflicts on your local PC, it is advisable to carry out your development work in a python virtual environment.

To create a python virtual environment -(shall name it as .env in this tutorial), open a command prompt and run the following commands:

Important: Python package dependencies in requirements.txt ought to include the packages used to develop your ML model as well as Flask + gunicorn **** for web app deployment.

To automatically extract out the python dependencies, run the command: pip freeze > requirements.txt

  • runtime.txt – Specifies the Python version used to develop your model.

E.g. For python version 3.7.9, the file content shall be: python-3.7.9

For python version 3.7.2, the file content shall be: python-3.7.2

Important: Please note that this file is case sensitive and precise. It is crucial that "python-3.7.x" is typed in lowercase with no whitespace(s). The Heroku build will fail if specified otherwise e.g. "Python-3.7.9" (capital letter "P") or "python 3.7.9" (no hypen "-").

  • X_test.json – A JSON array of the input parameter names of your ML model.
  • index.html – Minimalistic user interface setup for users to select inputs and retrieve results of model output.

Important: Do note that the form input names found in index.html correspond to the features stated in X_test.json

To test the web app on localhost, proceed to run run_web.bat file and open up your browser to navigate to http://localhost:5000/. You should be able to view the following user interface:

Image by Author | Preview of index.html file when running Flask app from localhost via run_app.bat file
Image by Author | Preview of index.html file when running Flask app from localhost via run_app.bat file

Output the ML Model’s result by selecting the form inputs below:

Image by Author | Note that on the left, the predicted answer was "Yes" while on the right, the predicted outcome was "No", dependent on the user's selections.
Image by Author | Note that on the left, the predicted answer was "Yes" while on the right, the predicted outcome was "No", dependent on the user’s selections.

Step 3. Initiate GIT in the project folder

After ensuring that the web app runs smoothly on your local PC, proceed to right click and open up your Git Bash terminal in the project folder. Run the following commands:

Image by Author | The web browser shall open automatically and proceed to select "Log In" as shown above. On your Git Bash terminal, the heroku: Waiting for login... , Logging in... done shall appear.
Image by Author | The web browser shall open automatically and proceed to select "Log In" as shown above. On your Git Bash terminal, the heroku: Waiting for login… , Logging in… done shall appear.

Finally, open up another Git Bash Terminal (in the project root folder) and run the following commands:

Heroku shall proceed to build the app based on the python dependencies stated in requirements.txt file with the specific python version stated in runtime.txt.

Finally, all sources are built and the deployment is done! Congratulations!

(Ignore) You now have your Machine Learning Model available for public access at: https://your-custom-app-name.herokuapp.com/

Note: Due to changes in Heroku’s hosting platform the web app has not been migrated to https://your-custom-app-name.onrender.com/ instead. Please ignore the Heroku app above (configurations can stay the same)

All source code in this tutorial can be found over at my GitHub at: https://github.com/incubated-geek-cc/your-custom-app-name

Feel free to fork it and tweak it to suit your use-cases accordingly. Have fun! ☺


Related Articles