How to deploy a machine learning web app like Streamlit on Heroku?


By allowing intelligent decision-making and technology, Machine Learning (ML) has revolutionized several sectors. However, when ML models are made available to people through user-friendly web apps, their true value is unlocked. In this post, we'll go through a few straightforward procedures for deploying a web application for machine learning that was created with Streamlit on the Heroku cloud.

What is Heroku?

Heroku is a platform based on the cloud that gives programmers rapid and simple access to application deployment, management, and scaling. It offers a platform-as-a-service (PaaS) alternative that abstracts away the foundational framework and frees programmers from worrying about server administration or infrastructure configuration so they can concentrate on creating and delivering their apps.

Steps

The steps listed below are used to launch a machine-learning web app like Streamlit on Heroku −

  • Prepare your Streamlit app  Get ready with the Streamlit app: Use Streamlit to build your machine learning web application. Before moving further with deployment, ensure that it is operating properly on your local PC.

  • Establish a fresh Git repository  Start a new Git repository or make use of one that already exists in the project directory. Verify that Git has your project files saved.

  • Create a requirements.txt file  Make a requirements.txt document in your project directory, and include all the Python packages that your application needs on it. Add Streamlit and any additional libraries you use in this document.

  • Set a Procfile  Make the following addition to the document Procfile, which should have no extension, in the project directory:

web: sh setup.sh && streamlit run app.py
  • Add Setup.sh script  Make a document called setup.sh in your project directory and add the below-mentioned lines to it:

mkdir -p ~/.streamlit/
echo "\
[general]
\ email = "your-email@example.com"
\ " > ~/.streamlit/credentials.toml echo "\ [server]
\ headless = true
\ enableCORS=false
\ port = $PORT
\ " > ~/.streamlit/config.toml
  • Make a Heroku application  If you haven't previously, download the Heroku CLI, and then execute the following command in your project directory to build a new Heroku application:

heroku create
  • Configure Heroku  Set up the essential parameters for your Heroku app while configuring Heroku. The following commands are available:

heroku stack:set container
heroku git:remote -a <your-heroku-app-name>
  • Add your revisions  Setup.sh, Procfile, requirements.txt, and other project files should all be committed to Git:

git add .
git commit -m "Initial commit"
  • Deploy to Heroku  Upload your Git repository to Heroku to start the installation procedure.

git push heroku master
  • Look for deployment  Heroku will begin creating your app once it has received the code and packages from your Git repository. The process of setting up can take a few minutes to finish.

  • Browse your deployed app  After an effective launch, you can access your app by visiting the Heroku-provided URL or by using the command below:

heroku open

That’s All! Your Streamlit machine-learning web application needs to be set up on Heroku at this point. Make careful you substitute the correct name for your Heroku app in step 7 instead of your-heroku-app-name>.

Conclusion

Your Streamlit-built ML web app has been successfully launched on Heroku. simply adhering to the detailed instructions provided in this blog. Now that you have an easy-to-use interface, people may visit your ML web-based application and engage with your models. To maintain your user's interest in the newest features, don't forget to periodically update your app with any upcoming enhancements or upgrades and reinstall it on Heroku. A critical first step to democratizing machine learning and opening it up to a larger audience is the deployment of ML web applications. To maximize the possible benefits of your machine learning initiatives, continue learning about new ML models, creating fun applications, and deploying them on services like Heroku. Cheers to deployment!

Updated on: 31-Jul-2023

82 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements