Stop 14 · Deploying ML Models & MLOpsTopics · 10/11

Demo Apps With Streamlit or Gradio

Part of the Machine Learning Roadmap

In short

A recruiter won't clone your repo, set up a virtual environment and run a notebook. They will click a link and try your model for 30 seconds. Streamlit and Gradio turn your Python model into that link in under an hour.

Streamlit and Gradio turn a Python script into an interactive web app in minutes, with inputs, buttons and charts. A live demo makes your project easy for recruiters and teammates to try, and Hugging Face Spaces hosts it for free.

The jaadu offers a tray of small sweet samples to passers-by at the front of a bright mithai shop while curious people reach for a piece.

Think of it like this

The sample counter at the mithai shop

A sweet shop can describe its kaju katli in words, or put a small plate out front for people to taste. The tasting plate sells far more. A demo app is your project's tasting plate: people type an input, press a button and see your model's answer straight away.

Watch it happen

Streamlit vs Gradio

Streamlit

  • Script reruns top to bottom on every interaction.
  • Great for dashboards with charts, tables and several inputs.
  • Free hosting on Streamlit Community Cloud or Hugging Face Spaces.

Gradio

  • Wraps a single predict function with inputs and outputs.
  • Built-in components for images, audio, text and chat.
  • Native on Hugging Face Spaces, with a shareable link even from Colab.

Use Gradio for a quick 'input in, prediction out' model demo. Use Streamlit when you want a small app with charts and explanations around the model.

The big ideas

What you need to know

Load the saved model once

Save your pipeline with joblib and load it at app start. In Streamlit, wrap loading in @st.cache_resource so it isn't reloaded on every click.

Inputs match features

Each widget maps to one feature your model expects, with sensible ranges and defaults.

Deploy for free

Push app.py and requirements.txt to a Hugging Face Space or Streamlit Community Cloud and you get a public URL.

Explain the output

Show the probability, not just a label, and a line on what the model can't do. It builds trust.

See it in code

Walk through the code

python
1# app.py  (run with: streamlit run app.py)2import joblib3import pandas as pd4import streamlit as st5 6@st.cache_resource7def load_model():8    return joblib.load("loan_model.joblib")      # the saved scikit-learn Pipeline9 10model = load_model()11 12st.title("Loan default risk")13income = st.number_input("Monthly income (₹)", min_value=0, value=45000, step=1000)14age = st.slider("Age", 18, 70, 30)15loan = st.number_input("Loan amount (₹)", min_value=0, value=300000, step=10000)16city = st.selectbox("City", ["Mumbai", "Delhi", "Bengaluru", "Lucknow", "Other"])17job = st.radio("Employment", ["salaried", "self-employed"])18 19if st.button("Check risk"):20    row = pd.DataFrame([{"income": income, "age": age, "loan_amount": loan,21                         "city": city, "employment_type": job}])22    risk = model.predict_proba(row)[0, 1]23    st.metric("Default probability", f"{risk:.0%}")24    st.caption("Demo model trained on sample data. Not for real lending decisions.")

1/4cache_resource loads the model once and reuses it, instead of reading the file on every rerun.

A Streamlit front end for the loan Pipeline saved in the scikit-learn pipelines lesson.

Save yourself the pain

Common mistakes

Oops

Training the model inside the app every time it starts.

Do this instead

Train in a notebook or script, save the Pipeline, and only load it in the app.

Oops

Forgetting requirements.txt, or leaving versions unpinned.

Do this instead

Pin scikit-learn and other key versions so the deployed app loads the saved model without errors.

Your mission

Put your model online

~60 min

Quick check

Why use @st.cache_resource around model loading in Streamlit?

Go deeper

  1. 1Build a Gradio interface for your image or text model with gr.Interface.
  2. 2Build a Streamlit app with sliders for a tabular model's input features.
  3. 3Deploy one of them on Hugging Face Spaces and link it in your README.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge