Deploying ML Apps on Heroku

Archit Sharma
DeepKlarity
Published in
3 min readOct 8, 2020

This blog is a part of series Clean or Messy Classifier . Check out the entire series for detailed implementation of deploying a machine learning classifier into web app using various frameworks.

Heroku is a cloud-based Platform as a Service (PaaS) i.e. it provides a platform for developers to deploy, run, manage and scale applications without requiring any additional infrastructure or software required for such a job. It helps the developers to focus on the core product instead of worrying about DevOps. Heroku started off with just Ruby support but now is a polyglot platform i.e. supporting multiple languages.

Setting up Heroku

  1. Creating a Heroku account -Follow the link to signup and create a Heroku account and login.
  2. Heroku CLI -Heroku apps can be created, run, and maintained using two methods: Heroku CLI (Command Line Interface) along with git and others using the Heroku website and connecting GitHub repository.

Installing Heroku CLI :

In this article, we will be using the Heroku CLI along with the Git to deploy a flask based webserver on Heroku. The OS in use is Ubuntu. Heroku apps are linked to a git repository hence we have to install git as a prerequisite. Install Git.

Ubuntu 16+ : sudo snap install --classic heroku

macOS : brew tap heroku/brew && brew install heroku

Windows Installation link: 64-bit installer, 32- bit installer.

Requirements to deploy a Flask app on Heroku.

  1. App Folder/ Files
  2. Procfile
  3. Requirements.txt

Deploying Apps on Heroku

  1. Logging on Heroku: Connect the Heroku CLI to the Heroku account. Log in using the following command.
$ heroku login
Press any key and connect on the redirected link.

2. Creating a Heroku app: Heroku app can be created with or without the name. In case you leave the app name blank the Heroku assigns a random name to the app. The app name used here is mediumexample. This also creates a git repository for the app.

$ heroku create mediumexample.

3. Deploying Heroku app: As mentioned above the prerequisite for deploying the Heroku app are the app files itself, requirements.txt, Procfile.

requirements.txt: This file tells the Heroku server what all dependencies are required to run the mentioned app. package.json file does the same task for the frontend apps.

Procfile: This file directs Heroku from where to start running the app. It contains the source code file name along with the name of the app as mentioned in the code. Procfile is not required for frontend apps.

web: gunicorn main:app

Deploying: When all the mentioned files are ready and placed in a folder. We are good to go to deploy the app.

Open the terminal in the directory of the app :

$ cd Documents/mediumexample

Initialize it as a Git repository.

$ git init

Initialize this git repository as a remote Heroku git repository.

$ heroku git:remote -a mediumexample

Add all the files in the repository.

$ git add .

Commit the changes made to the repository

$ git commit -m “your message”

Push the files to be deployed

$ git push heroku master

Heroku automatically detects the app type and generates a URL which can be used anywhere, this marks that the app is deployed.

Note: To make sure the app is deployed without any errors using

heroku logs --tail 

This will display all the logs and errors if any while deploying the app. Heroku generates a specific type of error code for a particular type of issue. Check out this link for the list for errors.

Follow the link for the entire Github repository of Clean or Messy Classifier project.

That’s it for this article. Please comment and share if you face any issues or errors using this approach. If you have used an alternate approach please share.

--

--