Deploy Your ML/DL Streamlit App on Heroku: A Step-by-Step Guide
Introduction
If you‘ve built a machine learning or deep learning application using the popular Streamlit framework, the next step is to deploy it so users can access it via the web. Heroku is a great platform for deploying Streamlit apps for free. In this step-by-step guide, we‘ll walk through the process of deploying a demo Streamlit app that predicts house prices on Heroku. By the end, you‘ll be able to deploy your own ML-powered Streamlit apps for the world to use.
But first, what exactly is Streamlit and why is it so useful for deploying ML apps? Streamlit is an open-source Python library that makes it easy to create web apps for machine learning and data science. With just a few lines of code, you can create an interactive app that lets users enter input, run your ML models, and view the predictions and visualizations. No web development experience is required.
Some key benefits of Streamlit for deploying ML apps:
- Simple to use and learn
- Fast development process
- Supports a wide range of ML libraries
- Interactive widgets and visualizations
- Easy theming and customization
Now that we know why Streamlit is so powerful, let‘s get started deploying an app!
The Demo App: House Price Prediction
For this tutorial, we‘ll be deploying a simple Streamlit app that predicts house prices in Bangalore, India based on the following inputs:
- Number of bedrooms
- Area in square feet
- Number of bathrooms
- Number of balconies
The app uses a linear regression model trained on historical house price data. When a user enters the details of a house, the model predicts the estimated price.
Here‘s what the app looks like:
[Include screenshot of app]You can find the full code for the app on GitHub: [link to repo]
The app consists of a single Python file, app.py, which contains the Streamlit app code. It loads the pre-trained linear regression model and uses it to make predictions based on user input.
Preparing the App for Deployment
Before we can deploy the app on Heroku, we need to set up our environment and make sure the app runs locally. Follow these steps:
-
Create a new folder for the app and navigate to it in your terminal
-
Create a new virtual environment and activate it
python3 -m venv env
source env/bin/activate
- Install the required libraries
pip install streamlit scikit-learn pandas numpy
-
Download the
app.pyfile and the trained model file into the folder -
Test the app locally by running:
streamlit run app.py
You should see the app open in a new browser window. Try entering some details and make sure the price prediction works.
Setting Up Heroku
Now that our app is working locally, let‘s get it deployed on Heroku. First we need to set up a few accounts and tools.
-
Sign up for a free Heroku account at https://signup.heroku.com/
-
Install the Heroku CLI (command line interface) by following the instructions for your operating system: https://devcenter.heroku.com/articles/heroku-cli
-
Install Git, which is the version control system we‘ll use to push the code to Heroku. You can download it from https://git-scm.com/downloads
Once you have these set up, open your terminal and log in to your Heroku account:
heroku login
This will open a browser window where you can enter your login details.
Deploying to Heroku
We‘re finally ready to deploy the app! Follow these steps:
- Initialize a new Git repository in the app folder:
git init
- Create 3 new files in the folder:
-
requirements.txt: This tells Heroku which Python libraries to install. Create it using:pip freeze > requirements.txt -
Procfile: This tells Heroku the command to launch the app. Create it and add the following line:web: sh setup.sh && streamlit run app.py -
setup.sh: This is a shell script that sets up the Streamlit configuration. Create it and add:mkdir -p ~/.streamlit/ echo "[server] headless = true port = $PORT enableCORS = false " > ~/.streamlit/config.toml
-
Use Git to commit the code:
git add . git commit -m "Initial commit" -
Create a new Heroku app:
heroku create my-house-price-predictorReplace
my-house-price-predictorwith a unique name for your app. -
Set Heroku to use the right Python runtime:
heroku buildpacks:set heroku/python -
Push the code to Heroku:
git push heroku masterThis will start the deployment process, which may take a minute or two.
-
Once it‘s finished, you can launch the app using:
heroku openThis will open the app in your browser. You can also find the public URL for your app in the Heroku dashboard.
Congratulations, your Streamlit app is now live on the web for anyone to use!
Updating a Deployed App
If you make changes to your app, you can re-deploy it by committing the changes with Git and pushing to Heroku again:
git add .
git commit -m "Update model"
git push heroku master
I recommend using Git branches to manage different versions of your app. You can create a new branch for each major update:
git checkout -b new-feature
# Make changes and commit
git push heroku new-feature:master
This deploys the new-feature branch to Heroku‘s master branch.
Monitoring and Performance
Heroku provides some basic monitoring and analytics for your app. You can view logs, set up alerts, and check metrics like response time and memory usage in the Heroku dashboard.
For Streamlit apps in particular, you can also set up usage analytics by modifying the ~/.streamlit/config.toml file:
[browser]
serverAddress = "https://your-app-name.herokuapp.com"
gatherUsageStats = true
This will give you insights into how many people are using your app, what pages they visit, and more.
As your app scales, you may hit the limits of the free tier of Heroku. Paid tiers give you more resources and better performance. You can also optimize your Streamlit app‘s performance with techniques like caching, using smaller models, and loading data efficiently. Streamlit has a guide on performance optimization: https://docs.streamlit.io/en/stable/deploy_streamlit_app.html#performance-tips
Alternatives to Heroku
While Heroku is a great choice for deploying Streamlit apps, it‘s not the only option. Here are a few alternatives:
- Google Cloud Run
- AWS Elastic Beanstalk
- Azure App Service
- Digital Ocean App Platform
- Python Anywhere
- Streamlit Sharing
Each platform has its own pricing, features, and deployment process. Some, like Streamlit Sharing, are designed specifically for Streamlit apps. Do your research to find the best fit for your needs.
Conclusion
Congratulations, you now know how to deploy a Streamlit app on Heroku! We covered a lot in this guide:
- What Streamlit is and why it‘s great for ML apps
- How to prepare a Streamlit app for deployment
- Setting up a Heroku account and tools
- Deploying to Heroku step-by-step
- Updating and monitoring a deployed app
- Alternative deployment options
Armed with this knowledge, you can now confidently build and share Streamlit apps powered by machine learning with the world. The deployment process may seem daunting at first, but with practice it becomes a smooth workflow.
I encourage you to keep building and deploying Streamlit apps. The more you do it, the easier it becomes. Plus, there‘s no better feeling than sending a link to your ML app to friends and colleagues!
If you have any issues with the deployment process, refer back to this guide or check the official Streamlit and Heroku docs. The Streamlit community is also a great resource – you can ask questions on the forums or Slack.
Thanks for following along, and happy deploying! Let me know in the comments what awesome Streamlit apps you end up sharing with the world.