AI Insights

8 Best Streamlit Machine Learning Web App Examples in 2024

May 31, 2022


article featured image

Streamlit is an open-source app framework for machine learning and data science projects. Following this article, you will know how to use Streamlit for data science projects, and examples of interesting Streamlit projects.

Introduction

There are many options for deploying a machine learning model. For instance, we can use frameworks like Django and Flask to deploy our models. But the problem is that we should have an idea of CSS, HTML, and Javascript for using these frameworks. 

The question arises: What should data engineers use to deploy their machine learning models to production? A simple answer to this question is Streamlit, which can help you deploy any machine learning model with ease and without worrying about the front end. 

But let’s first understand what Streamlit is and how we can use it to deploy our machine-learning model.

What is Streamlit?

Streamlit is an open-source framework in Python that helps us transform data science and machine learning scripts into interactive apps. It supports Python’s fundamental plotting libraries such as Seaborn, Pyplot, Matplotlib, GraphViz, Plotly, etc. Apart from this, Streamlit also has its native graphing library that consists of bar charts, area charts, line charts, and maps.

Streamlit has no support for multi-page applications. However, we can find many workarounds on the Streamlit support forum that provide implementations similar to multi-page applications.

Why Use Streamlit for Data Science Projects?

As we discussed earlier, the app creation process with Streamlit is as easy as writing Python scripts compared to using frameworks like Flask and Django. The data science and machine learning models need to be effectively deployed into production so that we can use them for practical decision-making. 

If the projects are not deployed as web applications, the impact of the models is severely limited, and we can not derive meaningful insights from them. Further, the stakeholders can access the models when deployed and play with them to figure out what works and what does not. Therefore, the best option for the data scientists is to use Streamlit for creating interactive web applications and deploying them into the production environment as they only need to:

  • Have a basic Python knowledge
  • Know how to write scripts for performing specific tasks
  • Build the Streamlit app by writing a few lines of code

Machine learning engineers and data scientists must follow the below design principles of Streamlit for creating web apps.

  • Embrace Scripting – We can build an app in a few lines of code.
  • Weave in Interaction – Instead of writing backend, defining routes, and handling HTTP requests, we can use widgets to interact with the Streamlit web apps.
  • Deploy Instantly – We can manage, share, and deploy our apps directly from Streamlit.

How to Use Streamlit?

Let’s dive into how to use Streamlit for creating web apps.

Install Streamlit

We need to know how to install and set up Streamlit on our machine. For installing Streamlit, we need to run the below command in our terminal.

pip install streamlit

We can check the version of Streamlit installed on our machine using the following command.

streamlit --version

Here’s the Streamlit version on my machine.

 

 

After the successful installation of Streamlit, we can test the library by running the below command.

streamlit hello

We can see that Streamlit’s Hello app will appear in our web browser in a new tab.

Streamlit’s Hello app

Streamlit’s Hello app

Run a Streamlit App

We can create a test Python script using Streamlit commands and run it using the below command.

streamlit run <test_script.py>

Development and Data Flow

Whenever we make changes in the Streamlit Python script, the app gives us two options:

  • Rerun
  • Always rerun

We can select the Always rerun option, so every time we make a change, it is reflected immediately in our web app. This feature makes our development flow easier and allows us to work seamlessly with Streamlit.

Streamlit also has a unique data flow. If anything gets updated on our screen, Streamlit returns us the entire Python script.

Display the Data

We can use Streamlit methods to display data types such as tables, dataframes, and arrays. For instance, we can use the method st.write(“Our string”) for writing a string.

Widgets and Layouts

Streamlit has many widgets available such as st.slider, st.selectbox, and st.checkbox etc. We can also set the layout of our app by arranging our widgets. For example, we can use the st.sidebar method to align data in the left panel.

Below is an example Streamlit Python script with a title and a Dataframe.

import pandas as pd
import streamlit as st

st.title("Hi Anber! Welcome to Streamlit!")

st.write("My first DataFrame")

st.write(
pd.DataFrame({
    'A': [1, 5, 9, 7],
    'B': [3, 2, 4, 8]
  })
)

We can use the following command to run our script.

streamlit run test_app.py

Here’s the output we get at http://localhost:8502/ after running the above command.

Streamlit

You can also check out Streamlit tutorial to better understand how to use Streamlit for creating web apps.

How and Where to Deploy Streamlit App?

You can go to the Streamlit website and create an app by selecting the New App option in the top right corner, as shown below.

anberarif's apps

A new window will pop up, and if you have linked your Github account, Streamlit will pull out the list of your repositories. You can choose the one and specify its file path.

Deploy an app

After filling all fields, you can hit the Deploy! Button. You will see the app will be up and running after a couple of seconds.

Before deploying the app, make sure your app code and dependencies are on Github, as Streamlit cloud launches the apps directly from the Github repository.

Best Streamlit Machine Learning Projects in 2024

Below are examples of interesting Streamlit Projects that we can take inspiration from to develop something new and exciting.

Sentiment Analyzer Tool

Sentiments are feelings that can be positive or negative. They play a significant role in our lives, so it is essential to analyze them correctly. For instance, sentiment analysis helps product managers to understand customer emotions and enhance the customer experience in their marketing campaigns.

The sentiment analyzer tool is a web app that uses the Streamlit library to analyze users’ sentiments on a platform like the web. Given below is the screenshot of this app. 

Sentiment Analyzer based on text analysis

You can also use the Github repo link to try out this project. Before running the app on your machine, you need to install the following libraries:

  • Streamlit
  • Numpy
  • Matplotlib
  • Python 3
  • Scikit-Learn

Visualize Pathologies in Ultrasound Images

This project uses OpenCV and Streamlit for detecting pathologies in ultrasound images. The end product is a Streamlit application that captures the image and the mask generated from the model as input. As a result, it provides the location of the pathology with mask and bounding box coordinates. As shown below, the visualizations highlight the tumor location and provide its intensity in our area of interest.

Visualize Pathologies in Ultrasound Images

Predict Infrastructure Needs of Africa

The team working on this project developed an interactive Streamlit Dashboard to predict the infrastructure needs of Africa. It built multiple models for Africa’s future infrastructure needs. The team also achieved the objective of providing recommendations regarding networks and verification approaches for scaling out to other countries. Here’s the dashboard of this project.

Predict Infrastructure Needs of Africa

The Streamlit dashboard gives visualizations and predictions for the objectives shown in the screenshot below.

Predict Infrastructure Needs of Africa

FastAPI Model Serving

It is a web app that uses FastAPI and Streamlit for serving a PyTorch model. When developing APIs that serve machine learning models, it can be helpful to have both a backend (with API documentation) and a frontend (for users to test the functionality). 

FastAPI Model Serving

This project uses Streamlit for the frontend service and an image semantic segmentation model using FastAPI for the backend service. You can find out the details of this project using this link.

Object Detection App

This project uses Streamlit to build an Object detection app. We can upload an image in this app and perform object detection. We can also adjust the confidence threshold and see the change in the predictions. Given below is the screenshot of the app.

Object Detection App

You can find out the details of this project using this link.

Covid-19 Data Explorer

This project uses Streamlit to create a dashboard for the Covid-19 spread. We can see the spread of Covid-19 in selected countries of Europe. Below is the screenshot of this app that illustrates that we can also switch between linear and log scales.

For instance, I selected Germany to see the Covid-19 spread in it.

Covid-19 Data Explorer

You can find out the details of this project using this link.

RenewCastApp

It is a web app that uses Streamlit and sktime to provide forecasts for renewable energy generation in European countries. Sktime is a great tool for time series forecasting and comes with several forecasters or forecasting algorithms that share a common interface. Below is the screenshot of the app that shows the total energy generation in Estonia.

RenewCastApp

You can find out the details of this project using this link.

Crop Recommender

Crop Recommendation is a significant feature of precision agriculture. This project uses Streamlit to develop an application that assists farmers in achieving the following objectives:

  • Increase in the agricultural productivity
  • Maximizing the water resource efficiency
  • Preventing soil degradation in cultivated land
  • Reducing the chemical use in crop production

Crop Recommender

You can find out the details of this project using this link.

Conclusion

In this article, we discussed that Streamlit is a great Python library for creating interactive web applications. We can go to the Streamlit website to deploy our apps. Model deployment with Streamlit is far easier than with frameworks like Django and Flask. But before deploying our apps on Streamlit, we have to make sure our app code is on Github. Apart from this, I hope you gained an idea of the use of Streamlit to develop applications and dashboards for achieving valuable objectives.

Ready to test your skills?

If you’re interested in collaborating, apply to join an Omdena project at: https://www.omdena.com/projects

media card
Ace Your Next Interview: How AI-Powered Mock Interviews are Revolutionizing Job Preparation
media card
Using Satellite Imagery to Detect and Assess the Damage of Armyworms in Farming
media card
UK WellnessTech Company GoodBoost and Omdena Deploy Web App to Gamify Exercises for MSK Conditions
media card
Shoreline Change Prediction using Satellite Imagery