Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 37 ยท Models and Demos

Loading Hub Models and Spaces

Use gr.load, gr.Interface.from_pipeline and the Hugging Face Hub to build demos from existing models, remix Spaces and understand inference providers, with a runnable registry stand-in.

You rarely need to write a model demo from scratch. Gradio has three shortcuts for turning something that already exists into an interface: wrap a transformers pipeline, load a model by name from the Hugging Face Hub, or load another Gradio app that is already running on a Space. All three produce a normal Gradio app that you can place inside your own layout.

The real Gradio, transformers and the Hub are not available on this site (they need a server, large model files and network access), so the library code is read-only. The runnable block builds a small registry of "pipelines" in plain Python to show the idea behind task-based loading.

From a pipeline

If you already have a Transformers pipeline, from_pipeline infers the right input and output components from its task:

Python
import gradio as gr
from transformers import pipeline

pipe = pipeline("text-classification", model="distilbert-base-uncased-finetuned-sst-2-english")
demo = gr.Interface.from_pipeline(pipe)
demo.launch()

On first run Transformers downloads the model weights (a few hundred megabytes) to a local cache. After that the page opens with a text box and a label output, with no fn written by you. This is a good way to try a model, but writing the function yourself (see wrapping a model) gives you control over inputs, defaults and error messages.

gr.load: a model or Space by name

gr.load takes a Hub repository name with a prefix:

Python
import gradio as gr

# A hosted model, run by an inference provider
demo = gr.load("models/Qwen/Qwen2.5-1.5B-Instruct", provider="auto")

# Another Gradio app, running on a Space
translator = gr.load("spaces/some-user/some-translation-space")

demo.launch()

For models/..., Gradio looks at the model's task tag, builds a matching interface and sends each request to a hosted inference service, so nothing is downloaded to your machine. You may need a Hugging Face access token (set the HF_TOKEN environment variable), and free quotas apply. For spaces/..., Gradio calls the other app's API and mirrors its interface, which means your app depends on that Space being online.

Approach Where the model runs Needs download Control
from_pipeline Your machine Yes High (you own the pipeline)
gr.load("models/...") Remote provider No Low (their defaults)
gr.load("spaces/...") Someone else's Space No Low (their interface)
Your own predict Your machine or an API you call Depends Full

Using a loaded app inside Blocks

A loaded app is a regular Gradio object. Inside a Blocks layout you can render it, or call it like a function:

Python
with gr.Blocks() as demo:
    gr.Markdown("# Translate then summarize")
    with gr.Tab("Translate"):
        translator.render()
    with gr.Tab("Notes"):
        gr.Markdown("Powered by a community Space.")

You can also keep your own interface and call the loaded one inside your function with translator("hello"). Be careful: the call uses the remote service's quota, adds network latency and can fail, so handle exceptions with a friendly message.

Dependencies and reproducibility

When a demo relies on a hub model, pin what you can: a revision for the model (pipeline(..., revision="abc123")), versions in requirements.txt and the Gradio version of the Space. Hub repositories can change or be removed, and a demo that worked last month can break because a default silently changed. Write down which model and revision a demo uses, in the page text, so visitors know what they are testing.

Stand-in: a task-based registry

Gradio's task inference is just a lookup: task name in, components out. Here is the same idea in plain Python, with a registry of toy "pipelines" so you can see how one function can route work to whatever the task is.

Python A tiny task registry (stand-in for pipeline())

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 first two tasks print results; translation prints an error listing the known tasks. Real Transformers does the same lookup against a much larger table, which is also why a typo in a task name gives a list of valid choices.

Python Choosing components from a task

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.

This prints one line per task, such as image-classification mapping to an Image input and a Label output. This is the heart of from_pipeline: a table from task to components.

When not to use shortcuts

Use shortcuts for exploration and quick sharing. Switch to your own function when you need custom preprocessing, authentication, logging, rate limits or a unified layout. Also, never put a private token in the code; read it from the environment (see auth and secrets).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does gr.Interface.from_pipeline do?

    It infers the input and output components from the pipeline task.
  2. 2. Where does gr.load("models/...") run the model?

    Requests are sent to a hosted inference service; no weights are downloaded.
  3. 3. Which environment variable commonly holds the Hub access token?

    HF_TOKEN is read by the Hugging Face libraries; keep it out of source code.
  4. 4. Why pin a model revision?

    Hub repositories can change; a fixed revision keeps results reproducible.

Summary

  • Interface.from_pipeline wraps a local pipeline; gr.load("models/...") uses a hosted model; gr.load("spaces/...") mirrors another app.
  • Loaded apps can be rendered inside Blocks or called like functions.
  • Handle remote failures and pin versions for reliability.
  • Shortcuts are great for exploring; write your own predict when you need control.

Next: Chatbots with ChatInterface