What Gradio Is and Why It Matters
Gradio is the fastest way to create interactive demos for ML models. Five lines of Python creates a shareable web UI with inputs, outputs, and an automatically generated REST API.
The library is maintained by Hugging Face and is the standard for sharing model demos in the ML community. Every model on Hugging Face Spaces is a Gradio (or Streamlit) app.
gr.Interface: Simplest Path to a Demo
import gradio as gr
from transformers import pipeline
classifier = pipeline("sentiment-analysis")
def analyze(text: str) -> dict:
result = classifier(text)[0]
return {"label": result["label"], "confidence": round(result["score"], 3)}
demo = gr.Interface(
fn=analyze,
inputs=gr.Textbox(label="Input text", placeholder="Type something..."),
outputs=gr.JSON(label="Result"),
title="Sentiment Analyzer",
examples=["I love this product!", "This is terrible."],
)
demo.launch()
Gradio infers input/output types automatically. Pass a string → gets a Textbox. Pass an image → gets an Image upload component.
gr.Blocks: Custom Layouts
import gradio as gr
with gr.Blocks() as demo:
gr.Markdown("# Image Classifier")
with gr.Row():
image_input = gr.Image(type="pil", label="Upload image")
output_label = gr.Label(num_top_classes=5, label="Predictions")
with gr.Row():
classify_btn = gr.Button("Classify", variant="primary")
clear_btn = gr.ClearButton([image_input, output_label])
classify_btn.click(fn=classify_image, inputs=image_input, outputs=output_label)
demo.launch()
gr.ChatInterface for LLM Chatbots
import gradio as gr
from anthropic import Anthropic
client = Anthropic()
def chat(message: str, history: list) -> str:
messages = [{"role": h["role"], "content": h["content"]} for h in history]
messages.append({"role": "user", "content": message})
response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=1024,
messages=messages,
)
return response.content[0].text
demo = gr.ChatInterface(fn=chat, title="Claude Chat", type="messages")
demo.launch()
For streaming, return a generator from your chat function and Gradio handles the streaming UI automatically.
Auto-Generated API
Every Gradio app exposes a REST API automatically:
# Call your Gradio app's API
curl -X POST http://localhost:7860/api/predict -H "Content-Type: application/json" -d '{"data": ["Hello world"]}'
The Python client makes this even simpler:
from gradio_client import Client
client = Client("https://your-app.hf.space")
result = client.predict("Hello world", api_name="/predict")
Sharing Your Demo
# Temporary public URL (72 hours)
demo.launch(share=True)
# Deploy to Hugging Face Spaces (permanent, free)
# 1. Create a Space at huggingface.co/spaces
# 2. Push your app.py + requirements.txt to the Space repo
# 3. HF Spaces builds and hosts it automatically
Gradio vs Streamlit
Use Gradio when: you want to demo a specific ML model, you want an auto-generated API, you are deploying to Hugging Face Spaces, or you need the simplest possible UI. Use Streamlit when: you are building a full data app (dashboards, multi-page), you need custom layouts and complex state, or you are building an internal tool.
Resources: Gradio docs, GitHub, HF Spaces.