How to Deploy Your Machine Learning Web App (Built Using Streamlit) on Heroku?

Deploying a machine learning web app built using Streamlit on Heroku involves a few steps. Here's a general overview of the process:

  1. Create a Heroku account: If you don't already have one, create a Heroku account at https://signup.heroku.com/.

  2. Install the Heroku CLI: The Heroku Command Line Interface (CLI) allows you to manage your Heroku applications directly from the terminal. You can download and install the Heroku CLI from the official website: https://devcenter.heroku.com/articles/heroku-cli.

  3. Create a new Heroku app: Use the Heroku CLI to create a new app on Heroku. Navigate to your project directory in the terminal and type the following command:

 

heroku create 

Replace <app-name> with a unique name for your app. This command will create a new app on Heroku and add a new Git remote to your project.

  1. Create a requirements.txt file: Heroku uses a requirements.txt file to know which Python packages to install. You can create this file using the following command:

 

pip freeze > requirements.txt

Make sure to run this command in your project directory.

  1. Create a Procfile: The Procfile is a text file that tells Heroku what command to run to start your web app. Create a new file called Procfile in your project directory and add the following line:

 

web: sh setup.sh && streamlit run app.py

This command runs a setup.sh script (if it exists) and then starts the Streamlit app.

  1. Create a setup.sh file: If your app has any setup requirements, you can create a setup.sh file to install the necessary dependencies. For example, if you're using a custom model file, you can add the following command to your setup.sh file:

 

mkdir -p ~/.streamlit/
echo "\
[general]\n\
email = \"your-email@domain.com\"\n\
" > ~/.streamlit/credentials.toml
echo "\
[server]\n\
headless = true\n\
enableCORS=false\n\
" > ~/.streamlit/config.toml

This creates a directory for Streamlit configuration files and sets some default values.

  1. Commit your changes: Use Git to commit your changes to your local repository:

 

git add .
git commit -m "Initial commit"
  1. Deploy your app: Finally, use Git to push your changes to the Heroku remote:

 

git push heroku master

This will deploy your app to Heroku. Once the deployment is complete, you can open your app in a web browser using the following command:

 

heroku open

Congratulations, you have successfully deployed your machine learning web app built using Streamlit on Heroku!

Submit Your Programming Assignment Details