How to Quickly Deploy Machine Learning Models with Streamlit

How to Quickly Deploy Machine Learning Models with Streamlit

How to Quickly Deploy Machine Learning Models with Streamlit
Image by Author | Ideogram

This article will navigate you through the deployment of a simple machine learning (ML) for regression using Streamlit. This novel platform streamlines and simplifies deploying artifacts like ML systems as Web services.

A Glimpse of the Model Being Deployed

The focus of this how-to article is to showcase the steps to have an ML model up and running in the cloud in a matter of minutes. Therefore, we’ll stick to building and deploying an extremely simple linear regression model that predicts house prices based on just one attribute: the house size in square feet.

This is the code we’ll take as starting point:

Some quick notes about the main() method above:

  • Streamlit is primarily used to define the user interface for interacting with the model once deployed.
  • The model is first trained by calling a previously defined method, after which an input field and a button will appear to let the user introduce a house size and obtain a price prediction.
  • Upon clicking on a button, Streamlit handles model inference and not only returns and displays the prediction, but also adds a Plotly interactive visualization of the prediction alongside the training data.
  • The 🏠 emoji was purely intended 😉

The code shown above should execute correctly even if you tried a Python notebook like Jupyter or Colab — provided it comes preceded with pip install instructions for installing the necessary dependencies — but this won’t allow you to interact with the deployed model: for this, we must venture a tiny bit into the wild!

Deploying the Model

To do the actual deployment, first copy and paste the above code into a .py file, created or uploaded into a GitHub repository of your own. In the same GitHub directory, you’ll also need a requirements.txt file containing these dependencies:

Make sure your target GitHub directory looks like this:

Required files in Github repository to deploy an ML model with Streamlit

We will deploy the model by linking these files with Streamlit Cloud app. Register on the website and click on Create App in the upper right corner. You should see a few options:

Creating a Streamlit App via GitHub to deploy our ML model

Let’s go for the first option: deploy a public app from Github.

Next, we’ll introduce the necessary elements to deploy our model as a Streamlit app: the Github repository URL, the branch and main file (the .py file we saved earlier), and an optional app URL from which anyone will be able to access. You’ll quickly notice that Streamlit makes this step easy by automatically figuring out part of this info.

Deploying an app with Streamlit

Click on deploy, cross your fingers, and hopefully that’s it! Your deployed model is now accessible. The resulting interface:

Interface for deployed ML model with Streamlit

Interface for deployed machine learning model with Streamlit
Image by Author

Model deployed: well done!

Discover Fast Machine Learning in Python!

Master Machine Learning With Python

Develop Your Own Models in Minutes

...with just a few lines of scikit-learn code

Learn how in my new Ebook:
Machine Learning Mastery With Python

Covers self-study tutorials and end-to-end projects like:
Loading data, visualization, modeling, tuning, and much more...

Finally Bring Machine Learning To
Your Own Projects

Skip the Academics. Just Results.

See What's Inside

2 Responses to How to Quickly Deploy Machine Learning Models with Streamlit

  1. Vinod Arya January 28, 2025 at 12:02 am #

    Thanks Ivan for such a wonderful article, contrast to other aviailable article, your article is precise and works seamlessly

    • James Carmichael January 28, 2025 at 6:27 am #

      Thank you for your feedback Vinod!

Leave a Reply

Machine Learning Mastery is part of Guiding Tech Media, a leading digital media publisher focused on helping people figure out technology. Visit our corporate website to learn more about our mission and team.