Gradio: The Fastest Way to Share ML Models
Gradio by Hugging Face lets you build interactive ML demos with minimal code. Upload an image, get predictions. Enter text, get translations. Any model, any interface, 5 lines of code.
Why Gradio
- 5 lines to create a web interface for any model
- Auto-generated API — every Gradio app gets a REST API
- Share links — public URL in one flag
- Hugging Face Spaces — free hosting
- 20+ input/output types — image, audio, video, text, file
The Free API
Create an Interface
import gradio as gr
def greet(name, intensity):
return "Hello, " + name + "!" * int(intensity)
demo = gr.Interface(
fn=greet,
inputs=["text", gr.Slider(value=1, minimum=1, maximum=10)],
outputs="text"
)
demo.launch() # Starts web server + API
Auto-Generated REST API
Every Gradio app automatically gets an API:
# Call the API
curl -X POST http://localhost:7860/api/predict \
-H "Content-Type: application/json" \
-d "{\"data\": [\"World\", 3]}"
# Response: {"data": ["Hello, World!!!"]}
# API docs at http://localhost:7860/api/
Image Classification
import gradio as gr
from transformers import pipeline
classifier = pipeline("image-classification")
def classify(image):
results = classifier(image)
return {r["label"]: r["score"] for r in results}
demo = gr.Interface(
fn=classify,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=5)
)
demo.launch(share=True) # Gets a public URL!
Chatbot
import gradio as gr
from openai import OpenAI
client = OpenAI()
def chat(message, history):
messages = [{"role": "system", "content": "You are helpful."}]
for h in history:
messages.append({"role": "user", "content": h[0]})
messages.append({"role": "assistant", "content": h[1]})
messages.append({"role": "user", "content": message})
response = client.chat.completions.create(model="gpt-4o", messages=messages)
return response.choices[0].message.content
demo = gr.ChatInterface(chat)
demo.launch()
Real-World Use Case
A researcher built a medical image classifier. Needed doctors to test it without installing Python. Gradio: 5 lines of code, deployed to Hugging Face Spaces. Doctors open a URL, upload X-rays, get predictions. 200 users in the first week.
Quick Start
pip install gradio
python -c "import gradio as gr; gr.Interface(lambda x: x, text, text).launch()"
Resources
Need data for your ML models? Check out my scraping tools on Apify or email spinov001@gmail.com for custom data pipelines.
Top comments (0)