Gradio Spaces can host your Hugging Face models, turning them into interactive web demos with zero infrastructure management.
Let’s get a simple text generation model running.
First, create a new Space on Hugging Face. You can name it anything, like your-username/my-awesome-text-gen. Choose the "Gradio" SDK.
import gradio as gr
from transformers import pipeline
# Load a pre-trained text generation model
generator = pipeline("text-generation", model="gpt2")
def generate_text(prompt, max_length=50):
"""Generates text based on a prompt."""
results = generator(prompt, max_length=max_length, num_return_sequences=1)
return results[0]['generated_text']
# Create the Gradio interface
interface = gr.Interface(
fn=generate_text,
inputs=gr.Textbox(label="Enter your prompt:"),
outputs=gr.Textbox(label="Generated text:"),
title="Simple Text Generator",
description="Enter a prompt and let GPT-2 generate some text."
)
# Launch the interface
interface.launch()
This Python script is the heart of your Gradio Space.
import gradio as grandfrom transformers import pipeline: These import the necessary libraries.gradiofor the UI, andtransformersto easily load and use Hugging Face models.generator = pipeline("text-generation", model="gpt2"): This line downloads and loads thegpt2model from Hugging Face. Thepipelinefunction is a high-level abstraction that handles tokenization, model inference, and decoding for you.def generate_text(prompt, max_length=50): This is your core logic function. It takes apromptstring and an optionalmax_lengthinteger. It then calls the loadedgeneratorto produce text.results[0]['generated_text']: Thepipelinereturns a list of dictionaries. We’re taking the first result ([0]) and extracting the generated text under the key'generated_text'.interface = gr.Interface(...): This is where you define the user interface.fn=generate_text: This tells Gradio which Python function to call when a user interacts with the UI.inputs=gr.Textbox(label="Enter your prompt:"): This defines the input component. Here, it’s a text box with a label.outputs=gr.Textbox(label="Generated text:"): This defines the output component, also a text box.titleanddescription: These are metadata for your app.
interface.launch(): This starts the Gradio web server. When hosted on Hugging Face Spaces, this function is intercepted by the Space runner to serve your app.
To deploy this, you’ll need a requirements.txt file in the same directory as your Python script. For this example, it would look like this:
gradio
transformers
torch
Or, if you prefer TensorFlow:
gradio
transformers
tensorflow
Once you’ve created your Space on Hugging Face and pushed these two files (app.py and requirements.txt) to the repository, your Gradio demo will automatically build and become accessible at the Space’s URL.
The pipeline function is incredibly versatile. You can replace "text-generation" and "gpt2" with other task names and model identifiers from the Hugging Face Hub (e.g., "sentiment-analysis", "distilbert-base-uncased-finetuned-sst-2-english"). Gradio’s Interface can often automatically infer the correct input and output components based on the function’s signature and the model’s expected inputs/outputs, but explicit definitions like gr.Textbox give you more control.
The magic behind Gradio Spaces is that the Hugging Face platform watches your repository. When it detects changes, it automatically rebuilds your environment based on requirements.txt and restarts your app.py script. You don’t need to provision servers, configure Docker, or manage any cloud infrastructure.
When you use interface.launch() locally, it spins up a web server on your machine, usually accessible at http://127.0.0.1:7860. The share=True argument to launch() (which you don’t use in Spaces) creates a temporary public URL that expires after 72 hours, useful for quick sharing without a full deployment.
Most people don’t realize how granularly you can control the UI. Beyond basic Textbox components, Gradio offers Image, Audio, Video, Dropdown, Slider, Checkbox, Radio, and many more. You can also create complex layouts using gr.Blocks for more sophisticated applications where a simple gr.Interface isn’t enough. For instance, you could have multiple input fields, conditional logic that shows/hides components, and custom CSS.
The next step is typically exploring how to handle larger models or models requiring specific hardware like GPUs, which involves configuring the Space’s hardware settings on Hugging Face.