tutorial on deploying a simple machine learning model on Heroku or Streamlit

Deploying a Simple Machine Learning Model on Heroku Prerequisites Before diving into deployment, ensure you have: Basic knowledge of Python and machine learning Installed Python (preferably 3.6+) An active Heroku account Git installed on your

Written by: Elara Schmidt

Published on: October 21, 2025

Deploying a Simple Machine Learning Model on Heroku

Prerequisites

Before diving into deployment, ensure you have:

  • Basic knowledge of Python and machine learning
  • Installed Python (preferably 3.6+)
  • An active Heroku account
  • Git installed on your local machine

Step 1: Build a Simple ML Model

In this tutorial, we’ll create a straightforward machine learning model using the scikit-learn library. Let’s create a model that predicts the species of the Iris flower based on its features.

  1. Install dependencies: Open your terminal and install the required libraries.
pip install pandas scikit-learn flask
  1. Create the machine learning model:
# model.py
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import joblib

# Load dataset
iris = datasets.load_iris()
X = iris.data
y = iris.target

# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train the model
model = RandomForestClassifier()
model.fit(X_train, y_train)

# Save the model
joblib.dump(model, 'model.joblib')

Step 2: Create a Flask Application

Next, we will create a Flask app to serve our model.

  1. Set up the Flask application:
# app.py
from flask import Flask, request, jsonify
import joblib

app = Flask(__name__)
model = joblib.load('model.joblib')

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json(force=True)
    prediction = model.predict([data['features']])
    return jsonify({'prediction': int(prediction[0])})

if __name__ == '__main__':
    app.run(debug=True)

Step 3: Create a Requirements File

Heroku needs to know which packages your application depends on. Create a requirements.txt file.

Flask==2.0.1
joblib==1.0.1
pandas==1.2.4
scikit-learn==0.24.1

Step 4: Prepare for Deployment

  1. Create a Procfile: This tells Heroku how to run your app.
web: python app.py
  1. Initialize Git repository:
git init
  1. Commit your changes:
git add .
git commit -m "Initial commit"

Step 5: Deploy to Heroku

  1. Log in to Heroku from your terminal:
heroku login
  1. Create a new Heroku app:
heroku create my-ml-app
  1. Deploy the app:
git push heroku master
  1. Open the app in your browser:
heroku open

Step 6: Test Your API

Use a tool like Postman or CURL to send a JSON request to your prediction endpoint. The endpoint accepts JSON data in the following format:

{
    "features": [5.1, 3.5, 1.4, 0.2]
}

You can test with a CURL command:

curl -X POST https://<your-app-name>.herokuapp.com/predict -H "Content-Type: application/json" -d '{"features": [5.1, 3.5, 1.4, 0.2]}'

Deploying a Simple Machine Learning Model on Streamlit

Step 1: Setting Up the Environment

  1. Install Streamlit:
pip install streamlit
  1. Create a new Streamlit app:
# app.py
import streamlit as st
import joblib

# Load model
model = joblib.load('model.joblib')

st.title('Iris Flower Prediction App')

# Features input
sepal_length = st.number_input('Sepal Length')
sepal_width = st.number_input('Sepal Width')
petal_length = st.number_input('Petal Length')
petal_width = st.number_input('Petal Width')

if st.button('Predict'):
    features = [[sepal_length, sepal_width, petal_length, petal_width]]
    prediction = model.predict(features)
    st.success(f'The predicted Iris species is: {int(prediction[0])}')

Step 2: Create a Requirements File

Create a requirements.txt file for Streamlit, similar to Heroku.

streamlit==1.5.0
joblib==1.0.1
scikit-learn==0.24.1

Step 3: Prepare for Deployment

  1. Run the Streamlit app locally:
streamlit run app.py

Step 4: Deploy to Heroku

Follow the deployment steps similar to how you did for Flask. Just modify your Procfile accordingly to run Streamlit:

web: streamlit run app.py --server.port $PORT --server.address 0.0.0.0

Final Notes

By following these steps, you can deploy a simple machine learning model using either Flask on Heroku or Streamlit. Understanding the deployment process will reinforce your knowledge, allowing you to share your ML models with the world. Whether you’re serving a complex deep learning model or a simple ensemble classifier, familiarity with deployment tools expands your data science toolkit. Now go ahead, experiment, and create interactive applications that showcase your models!

Leave a Comment

Previous

crafting your path to data science: a six-month structured learning roadmap

Next

best tools and platforms for hosting a data science portfolio for free