Training data

Developing a high-performing machine learning model is a major accomplishment, but it‘s only half the battle. To provide real value, your model needs to be deployed to production so it can be accessed and consumed by other applications. Deploying models can be tricky, but thankfully services like Heroku make it much easier.

In this guide, I‘ll walk you through the process of deploying a machine learning model as a web service using Heroku. By the end, you‘ll be able to take any trained model, wrap it in a web app, and deploy it to the cloud so that it can be used to generate predictions on demand. Let‘s jump in!

Overview of the Model Deployment Process

Before we get into the nitty gritty, let‘s zoom out and look at the big picture of the deployment process:

  1. Training and exporting the model – This is where you develop your model using a framework like scikit-learn or TensorFlow and save it to disk in a format like pickle that can be loaded later.

  2. Setting up a web app – To serve your model, you‘ll need a web app that loads the saved model file, takes in input data, generates predictions, and returns the result. We‘ll use Flask for this.

  3. Configuring the deployment – To deploy the app, you need to specify its dependencies and provide a startup command. Heroku looks for these in specific files.

  4. Deploying to Heroku – With the app code and configuration in place, you‘re ready to create a Heroku app, push your code to it, and launch the app in the cloud.

The rest of this guide will go through each of these steps in detail, using a concrete example to illustrate the concepts. Our goal is to build and deploy a simple web service that predicts house prices based on the number of rooms, like this:

Example prediction UI

Let‘s start by training and saving the model itself.

Step 1: Training and Exporting the Model

For this example, we‘ll train a basic linear regression model to predict house prices based on the number of rooms. Here‘s the code to do that using scikit-learn:

from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([[1], [2], [3], [4], [5]]) y = np.array([100, 150, 200, 250, 300])

model = LinearRegression() model.fit(X, y)

This trains a simple model that predicts house price from the number of rooms. The next step is to save this trained model to a file using pickle:

import pickle

with open(‘model.pkl‘, ‘wb‘) as file: pickle.dump(model, file)

The model is now saved in a file called model.pkl, ready to be loaded into our web app. Pickle is a handy way to serialize Python objects like machine learning models to disk.

With the model trained and saved, let‘s move on to building the web app that will serve it.

Step 2: Setting Up a Web App with Flask

To make predictions with our model, we need to wrap it in a web application that can take in HTTP requests with input data, feed that data through the model, and return the result in the response. The Flask web framework makes this easy.

First, make sure you have Flask installed:

pip install flask

Now create a new Python file called app.py with the following code:

from flask import Flask, request, jsonify
import pickle

app = Flask(name)

with open(‘model.pkl‘, ‘rb‘) as file: model = pickle.load(file)

@app.route(‘/predict‘, methods=[‘POST‘]) def predict():

data = request.get_json(force=True)

# Make prediction using model
prediction = model.predict([[data[‘rooms‘]]])

# Return the result as JSON
return jsonify(result=prediction[0])

if name == ‘main‘:
app.run(port=5000, debug=True)

Let‘s break this down:

  • First we import the necessary modules, create a Flask app, and load the pickled model from disk.

  • We then define a /predict endpoint that accepts POST requests. This will be the URL that clients can send input data to in order to get predictions from our model.

  • In the predict() function, we extract the input data from the request assuming it‘s in JSON format, pass it into model.predict() to generate a prediction, and finally return the result as JSON.

This defines a simple but complete Flask application for serving our house price prediction model. You can test it locally by running python app.py and sending a POST request to http://localhost:5000/predict with a JSON body like {"rooms": 2}.

However, to make this model accessible to the world, we need to deploy the app to a public server. That‘s where Heroku comes in.

Step 3: Configuring the Heroku Deployment

Heroku makes it easy to deploy Python web applications, but it needs a few configuration files to understand how to run your app.

First, create a file called requirements.txt in your project directory and add the following:

flask
gunicorn
scikit-learn

This tells Heroku that your app depends on Flask, gunicorn (a production-grade web server), and scikit-learn. Heroku will install these packages into the app‘s environment during deployment.

Next, create a file called Procfile (without any extension) and add:

web: gunicorn app:app

This tells Heroku how to run your app. It says to use gunicorn as the web server and look for the app object in the app.py file.

With these configuration files in place, you‘re ready to deploy the app to Heroku.

Step 4: Deploying to Heroku

First, make sure you have a Heroku account (it‘s free) and have installed the Heroku CLI. Authenticating the CLI is easy:

heroku login

This will open a browser window where you can log in to your Heroku account.

Next, navigate to your app‘s directory in the terminal and create a new Heroku app:

heroku create my-app-name

Replace my-app-name with a unique name for your app. This will create a new empty application on Heroku and associate it with your local Git repository.

Now stage your code, commit it, and push it to Heroku:

git add .
git commit -m "Initial commit"  
git push heroku master

Heroku will detect that it‘s a Python app, install the dependencies from requirements.txt, and start the app using the command from Procfile.

Once the deployment is finished, you can open the app using:

heroku open

This will launch the app in a browser window. You can now send POST requests to the /predict endpoint at the public URL of your app (something like https://my-app-name.herokuapp.com/predict) to get house price predictions from your model!

Wrapping Up

Congratulations, you‘ve successfully deployed a machine learning model to production using Heroku! Let‘s recap the key steps:

  1. Train your model using a framework like scikit-learn and save it to disk using pickle.

  2. Create a Flask app that loads the model, accepts input data from HTTP requests, generates predictions, and returns the results as JSON.

  3. Configure the deployment by specifying your app‘s dependencies in requirements.txt and providing a startup command in Procfile.

  4. Create a new Heroku app and deploy your code to it using Git.

By following this process, you can turn any machine learning model into a production-grade web service that can be integrated into applications and systems. Heroku makes this easy by handling the infrastructure and providing a simple deployment workflow.

I encourage you to try deploying your own models using the steps outlined here. With a little practice, you‘ll be able to quickly and easily bring your ML projects to life and start generating real value from your models.

Thanks for reading, and happy deploying!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts