Deploy Machine Learning model on Heroku

Naivedh Shah
Coding Blocks
Published in
5 min readNov 3, 2020

Do you want to show off your Machine Learning projects? If yes then this blog is for you. Today we will learn how to deploy a simple Machine Learning model on Heroku using GitHub as well as Heroku CLI.

Table of Content:

  • Prepare the Flask Web App
  • Deploy using GitHub
  • Deploy using Heroku CLI
  • End Notes

Prepare the flask Web App

To deploy the ML model we need to create 2 files. The first one is Procfile (no file extension) in this we will write “web: gunicorn app:app”. The web depicts that this is a web app and gunicorn is the server on which our app will run. The first app represents the file name from where the Web App should start. The second app represents the name of the app.

Procfile

The second file will be requirements.txt in this file we will add all the libraries that our Web App will use.

imports

As you can see I have imported flask and joblib we will add it to this file and as we need a server we will add gunicorn.

requirements.txt

Deploy using GitHub

Once you have created an account on GitHub you will find this on your screen. To follow along you can clone my GitHub repository.

GitHub

Click on New and select a unique Repository name for your project. Keep the Repository public so that it can be deployed. You can choose to initialize the repository with a README file if you wish to. The next step is to simply click on create repository.

Creating a repository

Now, you will see a screen as shown below. If you are experienced with Git you can follow the procedure shown else you can click on upload an existing file and upload all the files and commit.

Uploading Files

In the upcoming step, you will create an account on Heroku. Once the account is created click on New and then Create new app. Select a unique App name and click Create App.

Creating App

When the app is created you will find the below options on your screen. We will choose GitHub. You will then get a button to Connect to GitHub click on it.

Deployment options

Click on Search and connect to the repository you created on GitHub. Scroll and finally click on Deploy branch.

You can now click on Open app to visit you Web App.

Web App

Deploy using Heroku CLI

To deploy your ML Web App using CLI you will have to install Git and Node. Once installed open your command prompt and type the below command (it will take some time) to install Heroku CLI.

npm install -g heroku
Installing Heroku CLI

To check whether Heroku is installed or not use the below command.

heroku --version

In the next step instead of GitHub, we will select Heroku CLI on the Heroku website.

Deployment options

Now let’s get back to the command prompt and type the below command to log in to Heroku.

heroku login

The next step is to clone the project you created on Heroku and to do this we will use git clone. Before using this command move to a specific location where you want to create your project (mltestmodel is the name of my Heroku project you need to add the name of your project).

heroku git:clone -a mltestmodel

Once you have cloned the project on a specific location you can move all the required files into this new folder.

In the upcoming step, we will push the new changes to the Heroku remote project. To push all the changes we will use the below commands.

git add .
git commit -am "Initial commit"
git push heroku master
Output

You can visit the given link in the command prompt to view the Deployed Web App.

End Notes

Today we learned important ways to deploy a Machine Learning model on Heroku. For more blogs on Machine Learning and Data Science do follow me and let me know if there is any topic that you would like to know more about.

I would also like to express my gratitude to Coding Blocks for helping me gain knowledge in the field of Data Science.

Hey Readers, thank you for your time. If you liked the blog, don’t forget to appreciate it by a clap👏 and if in case you loved ❤ it, you can give 50 👏

Data Science Enthusiast | ML Enthusiast | TCS CA | Coding Blocks CA | Blogger | Community Member | Public Speaker

If you have any queries or suggestions feel free to contact me on

https://twitter.com/shah_naivedh

https://www.linkedin.com/in/naivedh-shah/

--

--

Naivedh Shah
Coding Blocks

Data Science Enthusiast | ML Enthusiast | TCS CA | Coding Blocks CA | Blogger | Community Member | Public Speaker