Deploying Machine Learning Models on Heroku

Machine learning is a continuous process that involves Data extraction, cleaning, picking important features, model building, validation, and deployment to test out the model on unseen data.

Top Machine Learning and AI Courses Online

While the initial data engineering and model building phase is fairly a tedious process and requires a lot of time to be spent with Data, model deployment may seem simple, but it is a critical process and depends on the use case you want to target. You can cater the model to mobile users, websites, smart devices, or through any other IoT device. 

One can choose to integrate the model in the main application, include it in SDLC, or the cloud. There are various strategies to deploy and run the model in the cloud platform, which seems a better option for most of the cases because of the availability of tools such as Google Cloud Platform, Azure, Amazon Web Services, and Heroku.

While you can opt to expose the model in Pub/Sub way, API (Application Program Interface) or REST wrapper is more commonly used to deploy the model in production. As the model complexity increases, different teams are assigned to handle such situations commonly known as Machine Learning Engineers. With this much introduction, let’s look at how to deploy a machine learning model as an API on the Heroku platform.

Trending Machine Learning Skills

Enrol for the Machine Learning Course from the World’s top Universities. Earn Masters, Executive PGP, or Advanced Certificate Programs to fast-track your career.

What is Heroku?

Heroku is a Platform as a service tool that allows developers to host their serverless code. What this means is that one can develop scripts to serve one or the other for specific purposes. The Heroku platform is itself hosted on AWS (Amazon Web Services), which is an infrastructure as a service tool.

The Heroku is a free platform but limited to 500hrs uptime. The apps are hosted as a dyno which after inactivity of 30 minutes goes into sleep mode. This ensures that your app is not consuming all the free time during inactivity. The platform supports Ruby, Java, PHP, Python, Node, Go, Scala. Most Data Science beginners refer to this platform to have an experience of running and deploying a model in the cloud. 

Preparing the Model

Now that you are aware of this platform, let’s prepare the model for the same. When a machine learning model is trained, the corresponding parameters are stored in the memory itself. This model needs to be exported in a separate file so we can directly load this model, pass unseen data, and get the outputs.

Different model formats are usually practiced such as Pickle, job-lib which converts the Python Object Model into a bitstream, ONNX, PMML, or MOJO which is an export format and offers the model to be integrated into Java applications too. For simplicity, consider that we want to export the model via pickle then you can do it by:

import pickle

Pkl_Filename = “model.pkl”  

with open(Pkl_Filename, ‘wb’) as file:  

    pickle.dump(model_name, file)

The model is now stored in a separate file and ready to be used in integrated into an API.

The Server logic

For providing access to this model for predictions, we need a server code that can redirect and handle all client-side requests. Python supports web development frameworks and a famous one is Flask.

It is a minimalistic framework that allows to set up a server with few lines of code. As it is a minimal package, a lot of functionalities such as authentication and RESTful nature are not explicitly supported. These can be integrated with extensions.

Another option is to opt for the newly released framework FastAPI. It is much faster, scalable, well documented, and comes with a lot of integrated packages. For now, let’s continue with the flask to set up a simple prediction route. 

from flask import Flask

import pickle

app = Flask(__name__)

with open(Filename, ‘rb’) as file:

model = pickle.load(file)

@app.route(‘/predict’, methods = [‘GET’, ‘POST’])

def pred():

# implement the logic to get parameters either through query or payload

prediction = model.predict([parameters obtained])

return {‘result’: prediction}

This is a rough code to show how to proceed with the server logic. There are various strategies you can opt for better implementation. 

Check Out: Guide to Deploying ML Models Using Streamlit

Setting up Deployment Files

Heroku requires a list of all dependencies required by our application. This is called the requirements file. It is a text file listing all the external libraries the application uses. In this example, the file contents would contain:



numpy </p>



The last library, gunicorn allows us to set up the WSGI server implementation that forms the interface for the client and the server handling the HTTP traffic. Heroku also demands another file known as Procfile that is used to specify the entry point of the app. Consider that the server logic file is saved by the name, then the command to be put in this file is:

web: gunicorn main:app

“web” is the type of dyno we are deploying, “gunicorn” act as the mediator to pass the request to the server code “main” and search for “app” in “main”. The app handles all the routes here.

Final Deployment

All the preparations are done, and now it’s time to run the app in the cloud. Create an account if not on the Heroku, click on create an app, choose any region. After that connect your GitHub account, and choose the repo that contains these files: server code, model.pkl, requirements.txt, and Procfile.

After this simply hit deploy branch! If it’s successful, then visit the link generated and your app should be live. Now you can make requests to route and it should give out the predictions. Learn more about machine learning models.

Popular AI and ML Blogs & Free Courses


This was an introduction to what is Heroku, why it is required, and how to deploy a model with the help of Flask. There are a lot of hosting platforms that offer more advanced features such as Data Pipelines, streaming, but Heroku being the free platform is still a good choice for beginners who just want to have a taste of deployment.

If you’re interested to learn more about machine learning, check out IIIT-B & upGrad’s PG Diploma in Machine Learning & AI which is designed for working professionals and offers 450+ hours of rigorous training, 30+ case studies & assignments, IIIT-B Alumni status, 5+ practical hands-on capstone projects & job assistance with top firms.

What are the disadvantages of using the Heroku platform?

Heroku is not ideal for large enterprises since the platform's documentation is not extensive. Another issue is the read-only file system, which makes bulky file uploads such as films or high-resolution photographs difficult to handle. Heroku is a platform-as-a-service provider with a limited amount of customization. Heroku only accepts payments in USD and not in other currencies, such as EUR. For business owners who prefer to pay in their own currency, this might be a minor inconvenience. Moreover, within CRE (Common Runtime Environment), Heroku does not provide static IP addresses.

How is Heroku different from AWS?

The deployment procedure in AWS is not simple, and as a result, it is not particularly user-friendly in terms of complexity. Heroku provides a ready-to-use environment that is simple to operate. Heroku is more costly than AWS since it requires less human intervention. AWS can handle high or very high computational needs, whereas Heroku can handle modest computational demands. Heroku, unlike AWS, does not require regular infrastructure maintenance. AWS and Heroku are both lightning quick when it comes to deployment.

How long can I use Heroku at no cost?

Heroku accounts have a pool of free dyno hours that may be used on free apps. This replaces the 18-hour limit on free programmes, enabling them to function 24 hours a day if necessary. New accounts get 550 free dyno hours, with an additional 450 hours if you validate your identity with a credit card. Heroku provides a free plan to help you learn the technology and get started. Many Heroku Add-ons provide a free plan, as do Heroku Buttons and Buildpacks.

Want to share this article?

Lead the AI Driven Technological Revolution

Learn More

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Machine Learning Course

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks