Hugging Face, End to EndLesson 29 of 75

Contents

Hugging Face, End to End

Lesson 29 of 75. Sign in to keep your place.

Module 4 · Spaces: putting something in front of people

Forty lines from nothing to a public URL

9 min read

The whole thing

Create a Space on the Hub (New → Space, SDK: Gradio), and put two files in it.

requirements.txt:

Text
transformers==4.46.2
torch==2.5.1

app.py:

Python
import gradio as gr
from transformers import pipeline

clf = pipeline(
    "text-classification",
    model="cardiffnlp/twitter-roberta-base-sentiment-latest",
)

def classify(text):
    if not text.strip():
        return {}
    scores = clf(text, top_k=None)[0] if isinstance(clf(text, top_k=None)[0], dict) \
             else clf(text, top_k=None)
    return {d["label"]: float(d["score"]) for d in scores}

demo = gr.Interface(
    fn=classify,
    inputs=gr.Textbox(lines=4, label="Text", placeholder="Paste a review"),
    outputs=gr.Label(num_top_classes=3, label="Sentiment"),
    title="Sentiment, three ways",
    description="Roberta trained on tweets. English only. Not calibrated.",
    examples=[
        ["The delivery was late but the food was excellent."],
        ["Fine, I suppose, if you enjoy waiting."],
    ],
    flagging_mode="never",
)

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

Commit both. The Space builds, installs, starts, and gives you a public URL that works on a phone. On free CPU hardware the first build takes a few minutes, most of it downloading torch.

What gr.Interface is doing

It inspects the function and the component types, builds a form and a results panel, wires the submit handler, handles errors, and exposes a REST endpoint and a queue behind the scenes. You describe inputs and outputs; it builds the application.

The components you will reach for most:

  • gr.Textbox, gr.Number, gr.Slider, gr.Dropdown, gr.Checkbox: inputs.
  • gr.Image, gr.Audio, gr.Video, gr.File: media, with upload and record widgets built in.
  • gr.Label: a dictionary of label to score, rendered as bars.
  • gr.Dataframe, gr.JSON, gr.Markdown, gr.Gallery, gr.Plot: outputs.

Passing a bare string like "text" instead of gr.Textbox() works and is common in tutorials. Use the component object once you want a label, a placeholder or a line count, which is immediately.

Load the model once, outside the function

In the script above, pipeline(...) runs at import, once, when the Space starts. Put it inside classify instead and every single request reloads the weights: turning a 200 ms response into a 30-second one and, on a busy Space, exhausting memory. This is the most common performance bug in beginner Spaces and it is invisible when you test alone.

The details that make it usable by strangers

Examples. examples=[...] renders clickable rows. A visitor who does not know what to type will click one; a visitor faced with an empty box leaves. This single argument changes how many people actually try your Space.

An honest description. “English only. Not calibrated.” in the description costs nothing and prevents somebody deploying your demo against Hindi support tickets. Every limitation you know belongs on the page, not in your head.

Empty-input handling. The if not text.strip() guard stops the first visitor seeing a red traceback box. Gradio surfaces exceptions in the UI, which is good for you and alarming for them.

flagging_mode="never". Flagging writes user submissions to a file on a container that gets destroyed. Unless you have set up storage and said so in a privacy note, turn it off rather than silently collecting text people typed.

Running it locally first

Everything above runs on your own machine:

Bash
pip install gradio
python app.py          # http://127.0.0.1:7860

Debug locally where the loop is seconds, and push when it works. demo.launch(share=True) gives a temporary public link that tunnels to your laptop: useful for showing somebody something for an hour, and not a deployment, since it dies with the process.

The free tier, precisely

Two vCPUs, 16 GB RAM, public, and it sleeps after a period of inactivity, waking on the next visit with a cold start of anything from seconds to a couple of minutes depending on what has to be downloaded. There is no time limit and no card required. For an embedding model, a classifier, an ONNX pipeline or a small Whisper model, this is a free permanent deployment.

Do this now

Ship a Space today, however small. The gap between a notebook and a URL you can send to somebody is the largest gap in this course, and it is forty lines wide. Everything after this lesson is refinement.

Every lesson here is open. Your progress is recorded once you pass the Image Generation, in Practice exam.

Check your understanding

A Space responds in 200 ms when the developer tests it and takes 30 seconds per request once a few people use it at once. What is the most likely cause?

Pick the one you would defend. Your answer stays private.

Next lesson · 9 minBlocks, when Interface is no longer enough

Stuck on something?Ask Arjun, your AI tutor

How to cite: Addaly (2026) ‘Forty lines from nothing to a public URL’, in Hugging Face, End to End. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/hugging-face/gradio-in-forty-lines (Accessed: 13 October 2026).