Skip to content
Tutorials Center
My learning

Topics

Lesson 79 of 111 ยท Beyond the Basics

Model Demos with Streamlit and Gradio

Wrap a scikit-learn model in a web UI with Streamlit or Gradio: design the prediction function, cache the model, guard the inputs, and share the demo.

A trained model in a notebook helps nobody but you. A demo app lets a teammate, a customer or a hiring manager type in values, press a button and see a prediction. You do not need a front-end team for that. Two Python libraries turn a function into a web page: Streamlit, where you write a script that draws widgets from top to bottom, and Gradio, where you declare a function with its inputs and outputs and get a ready interface. Both are popular for model demos, internal tools and quick prototypes. For a production API that other programs call, see Serving a Model with FastAPI.

Neither library can run inside this tutorial's browser runner, because both start a web server. The code for them is shown read-only. But the part that decides whether your demo is good is not the UI code: it is the plain Python function behind it, and that part we can run here, along with a small stand-in that behaves like a Gradio interface.

From model to demo

Trained model A saved pipeline.
Predict function Plain values in, plain values out.
UI layer Streamlit or Gradio.
Share A link anyone can open.

Keep the function separate from the UI code.

1 / 2

Design the function first

Whatever the front end, start with one function that takes the values a user can enter and returns something easy to show. Four design rules make demos reliable.

  1. Plain values in and out. Numbers, strings, a dictionary of label confidences. No DataFrames or arrays across the boundary.
  2. Load the model once. Training or loading a model takes seconds, and a UI may call your function hundreds of times. Streamlit reruns the whole script on every interaction, so it offers @st.cache_resource to keep the model in memory. In plain Python the same idea is functools.lru_cache, which is what the example uses.
  3. Limit the inputs. A slider restricted to the range of the training data cannot produce a nonsense request. For free text and number boxes, validate in the function, and warn when a value is outside what the model has seen.
  4. Show uncertainty. A bar chart of class probabilities tells the user more than a bare label, and it keeps you honest about weak predictions.
Python The function behind the UI: load once, validate, explain

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Four calls trained the model only once. The slider limits are taken straight from the training data, so each widget is built from facts, not guesses. The first two clicks are classified confidently as setosa (0.98) and versicolor (0.78, with 0.21 left on virginica, a hint that this flower is near a class boundary). The impossible petal length of 15 cm still gets an answer, but with a warning in the result. The model is certain (a probability of 1.0 for virginica) about a flower far outside anything it saw, which is exactly why you warn: confidence from a model says nothing about whether the input resembles its training data.

Gradio: declare the interface

In Gradio, you give gr.Interface the function, a list of input components and an output component. The library builds the page, a Submit button, an API endpoint and an optional queue. The function above needs one small change: the Label output expects a dictionary mapping each class name to its confidence.

Python
# read-only: requires the gradio package
import gradio as gr

def classify(sepal_length, sepal_width, petal_length, petal_width):
    probabilities = load_model().predict_proba([[sepal_length, sepal_width, petal_length, petal_width]])[0]
    return {name: float(p) for name, p in zip(iris.target_names, probabilities)}

demo = gr.Interface(
    fn=classify,
    inputs=[
        gr.Slider(4.0, 8.0, value=5.8, label="Sepal length (cm)"),
        gr.Slider(2.0, 4.5, value=3.0, label="Sepal width (cm)"),
        gr.Slider(1.0, 7.0, value=4.0, label="Petal length (cm)"),
        gr.Slider(0.1, 2.5, value=1.2, label="Petal width (cm)"),
    ],
    outputs=gr.Label(num_top_classes=3),
    title="Iris classifier",
    examples=[[5.0, 3.4, 1.5, 0.2], [6.5, 3.0, 5.5, 2.0]],
)

if __name__ == "__main__":
    demo.launch()          # a local web page; demo.launch(share=True) makes a temporary public link

Because Gradio knows the types of the inputs and outputs, it can also expose the function as an API automatically, and it offers ready-made components for images, audio, files and text, which makes it the fastest route for image and text models.

The next example is a stand-in, written for this lesson, that mimics what gr.Interface does: it holds the function, describes the form, cleans each input (a real slider cannot leave its range) and shows the result as a label with bars.

Python A stand-in for gr.Interface: inputs, clamping and output

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The form description lists four sliders with their limits. The two normal submissions produce confident labels (setosa at 98%, virginica at 91%). The third one is the interesting one. A user cannot type 99 into a slider, so the value arrives clamped to 7.0, and the prediction changes to a muddled versicolor at 71% with setosa at 27%, which is what a model gives for an unusual flower. A real framework protects you from impossible values in the same way, but only if you declare the limits.

Streamlit: a script that draws the page

Streamlit works differently. Your script is the page: every widget call both draws the widget and returns its current value, and Streamlit reruns the whole script from the top each time the user touches a widget. That is why caching matters.

Python
# read-only: requires the streamlit package; run with  streamlit run app.py
import streamlit as st
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression

iris = load_iris()

@st.cache_resource                     # runs once, then the model stays in memory
def load_model():
    return LogisticRegression(max_iter=500).fit(iris.data, iris.target)

st.title("Iris classifier")

col_left, col_right = st.columns(2)
sepal_length = col_left.slider("Sepal length (cm)", 4.0, 8.0, 5.8)
sepal_width = col_left.slider("Sepal width (cm)", 2.0, 4.5, 3.0)
petal_length = col_right.slider("Petal length (cm)", 1.0, 7.0, 4.0)
petal_width = col_right.slider("Petal width (cm)", 0.1, 2.5, 1.2)

probabilities = load_model().predict_proba([[sepal_length, sepal_width, petal_length, petal_width]])[0]
st.subheader(f"Prediction: {iris.target_names[probabilities.argmax()]}")
st.bar_chart(dict(zip(iris.target_names, probabilities)))

with st.expander("How was this model trained?"):
    st.write("Logistic regression on the 150-flower iris data set, no held-out test set (it is a demo).")

Streamlit also has st.file_uploader (for CSV files and images), st.sidebar, tabs, forms that wait for a button, and st.session_state for values that must survive reruns. It suits dashboards and multi-step tools where you want to control the layout.

Sharing a demo

  • Local use. streamlit run app.py or python app.py (Gradio) serves the page on your own machine, which is enough for a quick check.
  • Temporary public link. Gradio's launch(share=True) creates a short-lived public URL to your machine. It is great for showing a colleague, and not a way to host anything.
  • Hosted demos. Streamlit Community Cloud runs a Streamlit app straight from a Git repository. Hugging Face Spaces hosts both Streamlit and Gradio apps. Both read a requirements.txt file, so pin your library versions, scikit-learn included.
  • Your own server. Put the app in a Docker container and run it behind a reverse proxy, like any other web app.

A demo is not a production system. Anyone with the link can send it any input, so keep secrets and private data out of the repository, do not expose a model trained on confidential data, cap file sizes, and add authentication if the page is not meant to be public. For traffic from other programs, use a proper API with validation and monitoring.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a Streamlit app wrap model loading in @st.cache_resource?

    Streamlit reruns the script from the top whenever a widget changes. Caching keeps the loaded model in memory across those reruns.
  2. 2. What does gr.Interface need to build a working demo page?

    You give it the function plus a description of its inputs and outputs, and it builds the form, the submit button and an API from that.
  3. 3. A user drags a slider to a value the model has never seen in training. The model answers with 100% confidence. What should the demo do?

    Model confidence says nothing about whether an input resembles the training data. Declare input limits, and warn when a value is far outside what the model has seen.
  4. 4. Which is the best use of Gradio's share=True?

    share=True creates a temporary tunnel to your own machine. It is for short demos, not for hosting.

Summary

  • A demo app is a thin UI over a plain prediction function; design and test the function first.
  • Load the model once (@st.cache_resource in Streamlit, or any cache), limit and validate inputs, and show probabilities, not only labels.
  • Gradio declares the function with input and output components; Streamlit reruns a script that draws widgets.
  • Share with a temporary link, a hosted service such as Streamlit Community Cloud or Hugging Face Spaces, or your own container, and pin your dependencies.
  • A demo is not a secure production service: control the inputs and the data behind it.

Next: Saving Models and Deploying covers saving models and serving them through an API.