
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import streamlit as st import pandas as pd import numpy as np from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error, r2_score import plotly.express as px # Synthetic generation of data examples for training the model def generate_house_data(n_samples=100): np.random.seed(42) size = np.random.normal(1500, 500, n_samples) price = size * 100 + np.random.normal(0, 10000, n_samples) return pd.DataFrame({'size_sqft': size, 'price': price}) # Function for instantiating and training linear regression model def train_model(): df = generate_house_data() # Train-test data splitting X = df[['size_sqft']] y = df['price'] X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Train the model model = LinearRegression() model.fit(X_train, y_train) return model # Streamlit User Interface for Deployed Model def main(): st.title('🏠 Simple House Pricing Predictor') st.write('Introduce the house size to predict its sale price') # Train model model = train_model() # User input size = st.number_input('House size (square feet)', min_value=500, max_value=5000, value=1500) if st.button('Predict price'): # Perform prediction prediction = model.predict([[size]]) # Show result st.success(f'Estimated price: ${prediction[0]:,.2f}') # Visualization df = generate_house_data() fig = px.scatter(df, x='size_sqft', y='price', title='Size vs Price Relationship') fig.add_scatter(x=[size], y=[prediction[0]], mode='markers', marker=dict(size=15, color='red'), name='Prediction') st.plotly_chart(fig) if __name__ == '__main__': main() |
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:
|
1 2 3 4 5 |
streamlit==1.29.0 pandas==2.1.4 numpy==1.26.2 scikit-learn==1.3.2 plotly==5.18.0 |
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!







Thanks Ivan for such a wonderful article, contrast to other aviailable article, your article is precise and works seamlessly
Thank you for your feedback Vinod!