Develop Web Apps and Deploy ML Models using Streamlit: A Comprehensive Guide

In this comprehensive guide, we will explore the world of web app development and ML model deployment using Streamlit. Whether you are a data scientist, machine learning engineer, or web developer, this blog post is tailored to provide you with a step-by-step approach to building interactive web applications that leverage the power of ML models. The blog post covers essential topics such as data visualization, user interaction elements, and incorporating model predictions into the web app. Additionally, it offers insights on deploying the Streamlit app, hosting it on popular cloud platforms, and optimizing the app’s performance.

Whether you are new to web app development or looking to enhance your skills in deploying ML models, this blog post equips you with the knowledge and resources needed to create robust and engaging applications. By the end of the read, you will have gained a comprehensive understanding of Streamlit’s capabilities and be empowered to build your own web apps that showcase your ML models effectively.

Web apps vs Mobile apps

•Web apps are accessed through web browsers on desktop computers, laptops, tablets, and mobile devices. They are platform-independent and can run on various operating systems (e.g., Windows, macOS, Linux). •Mobile apps, on the other hand, are specifically designed for mobile devices and are installed directly on the device’s operating system, such as Android or iOS.

Pre-requisites
  • A github account
  • set up python environment with streamlit and pickle (execute pip install streamlit, pickle)
  • Install IDE like Spyder or VS Code to open the .py files involved and play with them
  • Files for this lab

Download the files from this link

Lab 1: Will help check the set up and get started

Open a terminal / anaconda prompt

Navigate to the folder where the lab files are saved

Execute streamlit run app.py

You should see a msg with a local URL where the app is running. Using any browser go to this localhost URL and see the app.py running.

See the code in IDE to understand how to implement user inputs in Streamlit

How to close this application: close the browser tab, go to terminal and use Ctrl + C to close it. If needed just close the terminal and open a new one for next labs.

Lab 2: Introduces markdown in Streamlit

Execute streamlit run app2.py

You should see a msg with a local URL where the app is running. Using any browser go to this localhost URL and see the app running.

See the code in IDE to understand how to display msgs using markdown Streamlit

Lab 3: Introduces various ways of accepting user inputs in Streamlit

Execute streamlit run demo_st_various_input_methods.py in a terminal

You should see a msg with a local URL where the app is running. Using any browser go to this localhost URL and see the app running.

See the code in IDE to understand how to different inputs methods are implemented using Streamlit

Lab 4: Introduces web app with ML model using Streamlit

Execute streamlit run app3.py in a terminal

You should see a msg with a local URL where the app is running. Using any browser go to this localhost URL and see the app running.

This app includes a simple linear regression model. On the left side, using the slider you can change the value of X_test. In the page, you can see the model intercept and coefficient. This is based on a toy dataset with just 5 rows. At the bottom of the page, the predicted value of y for this given X_test is shown.

Open the app3.py in IDE and examine. This script has three parts. One where model is fit, one where model is used to predict. The rest of the script deals with display the model properties, input element and the predicted value.

Am sure by now you are bursting with ideas for what all can be accomplished with these features. So let us take this up a notch.

Lab 5: The famous Iris dataset based model and hosted as web app using Streamlit

Execute streamlit run iris-ml-app.py in a terminal

You should see a msg with a local URL where the app is running. Using any browser go to this localhost URL and see the app running.

This app shows the four features as sliders on the left side, the page also lists the received feature values, the label classes, predicted probabilities associated with each class and final class prediction. Try changing the features using the sliders and observe the results change.

Lab 6: Deploy ML model as a web app on Streamlit cloud

Use the script demo_lin_reg_fit.py to train a model and save it as a pkl file. This can be done in several ways.

A simple way is to execute it using python demo_lin_reg_fit.py in a terminal. I have already included the pkl file from an earlier run. You can remove it and see it get created again.

Now execute pip freeze > requirements.txt to get a list of the libraries in your environment. You can use this as such or trim away libraries not needed for this run. That is optional but a good idea. For e.g. for executing the demo_lin_reg_webapp.py script, you can easily see the libraries needed like pickle, streamlit etc.

In your github account create a new repo, say streamlit. Upload the following files to this repo and commit: requirements.txt, the .pkl file and demo_lin_reg_webapp.py.

Go to a new browser tab and access share.streamlit.io. Log in using your github account. Once in streamlit site, click to create a new app.

There are just a few fields to fill up. Just click on the repo field and select the one where you uploaded the three files. Then leave branch as main. Then enter the name of the .py file which is demo_lin_reg_webapp.py. Initialize deploy. You should see a series of food images showing that the process is well underway and end up with the app running just like it ran in your local machine.

If you face errors, it is likely to be about library dependencies. One simple way is to use the requirements.txt file with all libraries you have in your environment. If that does not work for you just try with the min set of libraries needed to run demo_lin_reg_webapp.py. That should work.

What next?

Streamlit allows unlimited public apps. So you can try creating apps using the other files like iris.

Try using a decision tree algorithm, introduce a few sliders to control hyperparameters and see the tree shape and model performance.

Try using threshold parameter in a logistic regression and see how it affects the confusion matrix

Try to generate random data for a classification model and tweak the label class balance and see how it affects model metrics.

There are a ton of Streamlit and ML related features online. Enjoy surfing.

1 thought on “Develop Web Apps and Deploy ML Models using Streamlit: A Comprehensive Guide”

  1. Pingback: Experience “No Code” Machine Learning – Classification

Leave a Comment

Your email address will not be published. Required fields are marked *