Gradio: Create Interactive ML Demos That Anyone Can Use in 5 Lines
Gradio lets you wrap any Python function in a web UI with automatic type inference, and every app doubles as a REST API - deploy to Hugging Face Spaces for free in minutes.
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.
Team workspace
Ship faster with chat, meetings, and projects in one place — Zlyqor.
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.
Practical deep-dives on LLMs, developer tools, and AI engineering. No filler. Unsubscribe any time.
// written byFIG. AUTH-01
530
Mahmudul Haque Qudrati
CEO & ML Engineer
CEO and ML Engineer at Pristren. Builds AI-powered software for teams and writes about machine learning, LLMs, developer tools, and practical AI applications.
Open Code Review – An AI-powered code review CLI tool: A Practical Overview
Open Code Review is an open-source CLI tool from Alibaba that uses AI to review code changes. It runs locally, supports multiple LLMs, and costs about $0.01 per review. Here's a practical breakdown.