
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:

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:

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.

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

Interface for deployed machine learning model with Streamlit
Image by Author
Model deployed: well done!

No comments:
Post a Comment