Course documentation

Integrations with the Hugging Face Hub

Join the Hugging Face community

to get started

# Integrations with the Hugging Face Hub

To make your life even easier, Gradio integrates directly with Hugging Face Hub and Hugging Face Spaces. You can load demos from the Hub and Spaces with only one line of code.

To start with, choose one of the thousands of models Hugging Face offers through the Hub, as described in [Chapter 4](/course/chapter4/2).

Using the special Interface.load() method, you pass "model/" (or, equivalently, "huggingface/") followed by the model name. For example, here is the code to build a demo for GPT-J, a large language model, add a couple of example inputs:

import gradio as gr

title = "GPT-J-6B"
description = "Gradio Demo for GPT-J 6B, a transformer model trained using Ben Wang's Mesh Transformer JAX. 'GPT-J' refers to the class of model, while '6B' represents the number of trainable parameters. To use it, simply add your text, or click one of the examples to load them. Read more at the links below."
article = "<p style='text-align: center'><a href='https://github.com/kingoflolz/mesh-transformer-jax' target='_blank'>GPT-J-6B: A 6 Billion Parameter Autoregressive Language Model</a></p>"
examples = [
["The tower is 324 metres (1,063 ft) tall,"],
["The Moon's orbit around Earth has"],
["The smooth Borealis basin in the Northern Hemisphere covers 40%"],
]
"huggingface/EleutherAI/gpt-j-6B",
inputs=gr.Textbox(lines=5, label="Input Text"),
title=title,
description=description,
article=article,
examples=examples,
enable_queue=True,
).launch()

The code above will produce the interface below:

Loading a model in this way uses Hugging Face’s Inference API, instead of loading the model in memory. This is ideal for huge models like GPT-J or T0pp which require lots of RAM.

To load any Space from the Hugging Face Hub and recreate it locally, you can pass spaces/ to the Interface, followed by the name of the Space.

Remember the demo from section 1 that removes the background of an image? Let’s load it from Hugging Face Spaces:

gr.Interface.load("spaces/abidlabs/remove-bg").launch()

One of the cool things about loading demos from the Hub or Spaces is that you customize them by overriding any of the parameters. Here, we add a title and get it to work with a webcam instead:

gr.Interface.load(
"spaces/abidlabs/remove-bg", inputs="webcam", title="Remove your webcam background!"
).launch()

Now that we’ve explored a few ways to integrate Gradio with the Hugging Face Hub, let’s take a look at some advanced features of the Interface class. That’s the topic of the next section!