Tech With Galvan
Back to Articles
AIDec 30, 2026 • 5 min read

Streamlit + Hugging Face: Run ML Models in Your App

Run Hugging Face models in Streamlit — text classification, NER, summarization, and image generation. With caching, GPU support, and deployment tips.

Galvan
Galvan

Founder & Creator

Introduction

Hugging Face hosts 500k+ models — and you can run them in Streamlit with 5 lines of code. No training, no GPU required (for small models), no complex setup. This post shows how to build ML-powered Streamlit apps with the transformers library.

Setup

pip install streamlit transformers torch

For GPU: pip install torch --index-url https://download.pytorch.org/whl/cu121 (CUDA 12.1).

Step 1: Text classification (sentiment)

import streamlit as st
from transformers import pipeline

@st.cache_resource
def load_classifier():
    return pipeline("sentiment-analysis")

classifier = load_classifier()

st.title("Sentiment Classifier")
text = st.text_area("Enter text to analyze")

if text and st.button("Analyze"):
    result = classifier(text)[0]
    st.write(f"**Label:** {result['label']}")
    st.write(f"**Score:** {result['score']:.4f}")
    st.progress(result['score'])

@st.cache_resource is critical — loading the model takes 5-10s. Cache it so it loads once.

Step 2: Named Entity Recognition (NER)

@st.cache_resource
def load_ner():
    return pipeline("ner", aggregation_strategy="simple")

ner = load_ner()

text = st.text_area("Enter text")
if text:
    entities = ner(text)
    for e in entities:
        st.write(f"**{e['word']}** → {e['entity_group']} (score: {e['score']:.2f})")

Extracts people, organizations, locations, etc. from text.

Step 3: Text summarization

@st.cache_resource
def load_summarizer():
    return pipeline("summarization", model="facebook/bart-large-cnn")

summarizer = load_summarizer()

text = st.text_area("Paste a long article")
if text and st.button("Summarize"):
    with st.spinner("Summarizing..."):
        summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
    st.write(summary[0]["summary_text"])

Step 4: Translation

@st.cache_resource
def load_translator():
    return pipeline("translation_en_to_fr")

translator = load_translator()

text = st.text_input("English text")
if text:
    result = translator(text)[0]["translation_text"]
    st.write(f"French: {result}")

Step 5: Question answering

@st.cache_resource
def load_qa():
    return pipeline("question-answering")

qa = load_qa()

context = st.text_area("Context (paste an article)")
question = st.text_input("Question")
if context and question:
    result = qa(question=question, context=context)
    st.write(f"**Answer:** {result['answer']}")
    st.write(f"**Confidence:** {result['score']:.2f}")

Step 6: Image classification

from PIL import Image

@st.cache_resource
def load_image_classifier():
    return pipeline("image-classification")

classifier = load_image_classifier()

uploaded = st.file_uploader("Upload an image", type=["jpg", "png"])
if uploaded:
    image = Image.open(uploaded)
    st.image(image, width=300)
    results = classifier(image)
    for r in results[:5]:
        st.write(f"{r['label']}: {r['score']:.2%}")

Step 7: Text generation (LLM)

@st.cache_resource
def load_generator():
    return pipeline("text-generation", model="gpt2")

generator = load_generator()

prompt = st.text_input("Prompt")
if prompt:
    result = generator(prompt, max_length=100, num_return_sequences=1)
    st.write(result[0]["generated_text"])

For better quality, use larger models (but they need more RAM/GPU).

Model size guide

Model Size RAM needed Speed (CPU) Quality
distilbert (sentiment) 66M 1GB Fast (~0.1s) Good
bart-large-cnn (summarizer) 400M 2GB Medium (~2s) Good
gpt2 (generation) 124M 1GB Medium (~1s) OK
gpt2-large 774M 4GB Slow (~5s) Better
Llama 3.2 (1B) 1B 4GB Slow (~3s) Good

For CPU-only deployments, stick to models <500M. For GPU, you can run 7B+ models.

Deployment tips

Streamlit Community Cloud

  • Add transformers and torch to requirements.txt.
  • Use CPU-only torch to save space: torch==2.1.0+cpu (from PyTorch CPU index).
  • Cache models with @st.cache_resource — they download on first run (~minutes), then cached.
  • Free tier has 1GB RAM — use small models.

Docker

FROM python:3.11-slim
RUN pip install streamlit transformers torch --index-url https://download.pytorch.org/whl/cpu
COPY . /app
WORKDIR /app
CMD ["streamlit", "run", "app.py"]

GPU deployment

For large models, deploy on a GPU instance (AWS, GCP, or a VPS with GPU). Use device=0 in the pipeline:

pipeline("text-generation", model="meta-llama/Llama-3.2-1B", device=0)

Common mistakes

  • Not caching the pipeline — model loads on every rerun (5-10s each time). Always @st.cache_resource.
  • Using huge models on CPU — 7B models take 30s+ per inference on CPU. Use small models or GPU.
  • Not handling OOM — large models crash on small machines. Check RAM before loading.
  • Downloading on every deploy — models are cached in ~/.cache/huggingface. In Docker, use a volume or pre-download.
  • Ignoring the tokenizer — some pipelines need trust_remote_code=True for custom models.

Key Concepts

  • pipeline() = high-level API for common ML tasks
  • @st.cache_resource = load model once, reuse across reruns
  • Model size = trade-off between quality and speed/RAM
  • CPU vs GPU = GPU is 10-50x faster for inference
  • Hugging Face Hub = 500k+ pre-trained models

What to Try Next

  • Build a sentiment dashboard with a Hugging Face model (better accuracy than TextBlob/VADER).
  • Combine with RAG — use HF embeddings instead of Ollama.
  • Add auth for a gated ML tool.
  • Deploy with Docker on a GPU instance.

FAQ

Hugging Face vs OpenAI API?

HF = self-hosted, free, private, but you manage compute. OpenAI = managed, pay-per-token, best quality. Use HF for privacy/cost; OpenAI for quality/convenience.

Can I fine-tune models?

Yes — use Hugging Face’s Trainer API or peft for LoRA. Fine-tuning requires GPU and data. For most apps, pre-trained models + prompt engineering is enough.

How do I reduce model size?

Use quantization (bitsandbytes, gguf) or smaller variants (distilBERT instead of BERT). Quantized 7B models run on 4GB RAM. Check the model card for quantized versions.