Cheatsheet: Build and share a demo
The simplest demo
Section titled “The simplest demo”import gradio as gr
def greet(name): return "Hello " + name
gr.Interface(fn=greet, inputs="text", outputs="text").launch()fn: any Python function (calls your model in an AI app)inputs/outputs: the component types.launch(): renders inline (notebook) or serves at localhost:7860 (script)
Wrap a model
Section titled “Wrap a model”from transformers import pipelineimport gradio as gr
model = pipeline("text-generation")
def predict(prompt): return model(prompt)[0]["generated_text"]
gr.Interface(fn=predict, inputs="text", outputs="text").launch()Put your inference code in fn; Gradio builds the UI around it.
Customize components
Section titled “Customize components”box = gr.Textbox(label="Your prompt:", placeholder="Once upon a time", lines=3)gr.Interface(fn=predict, inputs=box, outputs="text").launch()| Component | For |
|---|---|
gr.Textbox | Text in/out |
gr.Image | Images |
gr.Audio | Sound |
gr.Label | Classification scores |
gr.Slider | Numbers |
String shorthands ("text") give defaults; the classes give control. Components match the model’s input/output.
Share it
Section titled “Share it”demo.launch(share=True) # temporary public URL (~72h), runs on your machine| Method | Lifetime | Runs on |
|---|---|---|
launch() | While running | Your machine, local only |
launch(share=True) | ~72h, while running | Your machine (tunneled) |
| Hugging Face Space | Permanent | Hub’s machine |
Publish to a Space (permanent)
Section titled “Publish to a Space (permanent)”A Space is a Git repo with:
app.py # your Gradio coderequirements.txt # dependenciesPush both to a Space repo on the Hub; it builds and hosts the demo free at a stable URL. Same Hub world as models and datasets (lesson 4).
When Interface is not enough
Section titled “When Interface is not enough”Use the Blocks API for custom layouts, tabs, multiple panels, or components that update each other. Interface covers most demos; reach for Blocks only when it does not fit.
Words to use precisely
Section titled “Words to use precisely”gr.Interface: the one-function demo wrapper (fn + inputs + outputs).- Component: a typed input or output widget (Textbox, Image, Audio, …).
- Space: a Hub-hosted Git repo (app.py + requirements.txt) serving a demo permanently.
- Blocks: the lower-level API for custom layouts.
Recommended further study
Section titled “Recommended further study”- Hugging Face LLM Course, Chapter 9: “Building and sharing demos.”
huggingface.co/learn/llm-course/chapter9. Released under Apache 2.0; this lesson mirrors its structure with original prose.