Build a Web Demo with Streamlit
Why Streamlit?
Streamlit turns a Python script into a web app, with no HTML or JavaScript. It is perfect for showing your model to friends, teachers or clients, and for your portfolio.
Install
bash
pip install streamlit
The App
Create a file named demo.py.
python
import numpy as np
import pandas as pd
import streamlit as st
import keras
from PIL import Image
CLASS_NAMES = ["T-shirt", "Trouser", "Pullover", "Dress", "Coat",
"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"]
@st.cache_resource # load the model only once
def load_model():
return keras.models.load_model("fashion_cnn.keras")
model = load_model()
st.title("Clothing Classifier")
st.write("Upload a picture of a clothing item and the model will guess what it is.")
file = st.file_uploader("Choose an image", type=["png", "jpg", "jpeg"])
if file is not None:
image = Image.open(file).convert("L").resize((28, 28))
st.image(image.resize((140, 140)), caption="Your image (as the model sees it)")
x = np.array(image).astype("float32").reshape(1, 28, 28, 1) / 255.0
probs = model.predict(x, verbose=0)[0]
top = int(np.argmax(probs))
st.success(f"Prediction: {CLASS_NAMES[top]} ({probs[top]:.0%})")
chart = pd.DataFrame({"class": CLASS_NAMES, "probability": probs}).set_index("class")
st.bar_chart(chart)
Run It
bash
streamlit run demo.py
Your browser opens at http://localhost:8501 with the working app.
Useful Streamlit Elements
| Function | Purpose |
|---|---|
st.title, st.write | Text on the page |
st.file_uploader | Upload images or files |
st.text_area | Enter text, for sentiment analysis demos |
st.slider, st.selectbox | Controls for settings |
st.bar_chart, st.image | Show results |
st.cache_resource | Keep the loaded model in memory |
Text Model Example
python
text = st.text_area("Write a movie review")
if st.button("Analyze") and text:
score = float(sentiment_model.predict(np.array([text]), verbose=0)[0][0])
st.write("Positive" if score > 0.5 else "Negative", f"({score:.2f})")
Share It
- Push the code,
requirements.txtand the model file to GitHub - Deploy for free on Streamlit Community Cloud or a similar hosting service
- Keep model files small, or download them at startup
Portfolio tip: A live demo link makes your project far more convincing than a notebook. Add one to every project you build in this course.
Conclusion
With about 30 lines you created a working AI web app. Use Streamlit to demonstrate your models quickly.
Quick Summary: Use st.file_uploader, load the model with cache_resource, predict and display the results.

