Skip to content

Cheatsheet: Build and share a 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)
from transformers import pipeline
import 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.

box = gr.Textbox(label="Your prompt:", placeholder="Once upon a time", lines=3)
gr.Interface(fn=predict, inputs=box, outputs="text").launch()
ComponentFor
gr.TextboxText in/out
gr.ImageImages
gr.AudioSound
gr.LabelClassification scores
gr.SliderNumbers

String shorthands ("text") give defaults; the classes give control. Components match the model’s input/output.

demo.launch(share=True) # temporary public URL (~72h), runs on your machine
MethodLifetimeRuns on
launch()While runningYour machine, local only
launch(share=True)~72h, while runningYour machine (tunneled)
Hugging Face SpacePermanentHub’s machine

A Space is a Git repo with:

app.py # your Gradio code
requirements.txt # dependencies

Push 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).

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.

  • 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.
  • 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.