Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 38 ยท Deployment and Spaces

Gradio Demos and Hugging Face Spaces

Turn a Transformers pipeline into a shareable web demo with Gradio and host it free on Hugging Face Spaces: app files, requirements, secrets, hardware, caching and limits.

Before building an API, you often just want people to try the model: a product manager, a teammate, a customer. Gradio is a Python library that turns a function into a web page with input widgets and an output panel. Spaces is Hugging Face's hosting for such apps: push a Git repo, and it builds and serves the demo at a public (or private) URL.

This lesson shows both, then runs a tiny stand-in that demonstrates how a function becomes an interface and how a Space is configured.

Gradio and the Hub are not available on this site, so the real app and Space files are read-only. The two runnable examples are small Python stand-ins for the ideas (function to interface, and checking Space metadata); they are not the Gradio library.

A demo in ten lines

Python
# app.py
import gradio as gr
from transformers import pipeline

classifier = pipeline("sentiment-analysis")           # loaded once, at startup

def analyze(text: str) -> dict:
    result = classifier(text, truncation=True)[0]
    # gr.Label shows {label: confidence}
    return {result["label"]: float(result["score"])}

demo = gr.Interface(
    fn=analyze,
    inputs=gr.Textbox(lines=4, label="Your text"),
    outputs=gr.Label(label="Sentiment"),
    examples=["I love this phone.", "The delivery was late and the box was crushed."],
    title="Sentiment demo",
)

if __name__ == "__main__":
    demo.launch()

Run python app.py and Gradio prints a local URL. The pattern is always the same: a function, a list of input components, a list of output components. Gradio inspects each component to build the form and convert values (images to arrays, audio to files, text to strings).

Three things worth knowing:

  • gr.Interface is the one-function shortcut. gr.Blocks gives full layout control (tabs, rows, buttons wired to different functions, state).
  • gr.ChatInterface wraps a fn(message, history) for chatbots, with streaming if the function yields partial text.
  • Every demo also exposes an API. The bottom of the page has a "Use via API" link, and gradio_client can call it from Python, so a demo doubles as a quick prototype backend.

How a function becomes an interface

The core idea is small: describe inputs and outputs as data, then generate the form and wire the call. This stand-in builds a text-mode "interface" from a function and a list of component specs, and validates the input before the function runs.

Python Stand-in: function to interface

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The real library adds a web server, many component types, queueing, authentication and a JavaScript front end, but its structure matches: components parse raw browser values into Python values, the function runs, an output component turns the result into something displayable.

Hosting on Spaces

A Space is a Git repository on the Hub with an app file. The README.md starts with YAML that tells the platform how to run it:

YAML
---
title: Sentiment Demo
emoji: ๐Ÿ™‚
colorFrom: indigo
colorTo: blue
sdk: gradio
sdk_version: 5.0.0
app_file: app.py
pinned: false
license: apache-2.0
---

Push app.py, requirements.txt (for example transformers and torch) and this README, and the Space builds and starts. Other SDKs are available too: streamlit, docker (any web app, including a FastAPI service from the previous lesson) and static (plain HTML).

Python Stand-in: check a Space's metadata

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Things that bite on Spaces

  • Hardware. The free tier is CPU-only with limited memory and sleeps when idle. A small model is fine; a 7B model needs paid GPU hardware (T4, A10, A100...) or a quantized build (see quantization fundamentals). ZeroGPU Spaces share GPUs and give them to a function only while it runs.
  • Cold starts. After sleeping, the Space reinstalls nothing but must reload the model; load at module level, not inside the function, so each request does not reload it.
  • Secrets. Put tokens in the Space's Settings, Variables and secrets and read them with os.environ. Never commit a token to the repo, even a private one.
  • Persistent storage. The Space's disk is reset on restart. Use a dataset repo or external storage if you must keep user data, and tell users what you store.
  • Pin versions. An unpinned requirements.txt can break a working demo the next time it rebuilds.
  • Model access. If the model repo is private or gated, the Space needs a read token as a secret.
  • Abuse and cost. Public demos are crawled and hit by bots. Limit input size, use the queue (demo.queue(max_size=20)), and avoid calling paid APIs from a public Space without a quota.
flowchart LR
  A["app.py + requirements.txt + README.md"] --> B["git push to the Space repo"]
  B --> C["Build the environment"]
  C --> D["Run app, load model once"]
  D --> E["Public URL + API"]

A Space is just a Git repo that the Hub knows how to run.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In gr.Interface, what do the inputs and outputs arguments describe?

    Components handle parsing, validation and display around your function.
  2. 2. Where should the pipeline be created in app.py?

    Loading once avoids reloading weights on every click.
  3. 3. How should a Space read an API token?

    Secrets are injected as environment variables and are not part of the repo.
  4. 4. Which Space SDK would you pick to host a FastAPI service?

    The docker SDK runs any containerised web app.
  5. 5. Why pin versions in requirements.txt?

    Each rebuild reinstalls dependencies; pinning keeps behaviour stable.

Summary

  • Gradio turns a function plus input and output components into a web UI (and an API); Blocks and ChatInterface cover custom layouts and chatbots.
  • A Space is a Git repo with an app file, requirements.txt and README metadata (sdk, app_file).
  • Load the model once, store tokens as secrets, pin dependencies, and limit input and queue size.
  • Free CPU Spaces suit small models; bigger ones need GPU hardware or quantization.

Next: Optimized inference: ONNX, Optimum and compiled models