Gradio makes it incredibly simple to demonstrate machine learning projects. This guide explains how to leverage Gradio's integration with Hugging Face's Inference API to quickly demo models, and how to host custom demos using Hugging Face Spaces.
Hugging Face Hub Integration in Gradio
You can showcase models hosted on the Hub with just a few lines of code. Define a Gradio Interface specifying the model repository ID, a description, title, and example inputs. Then call .launch() to start the demo. For easy sharing, deploy the app on Hugging Face Spaces by creating a new Space with Gradio as the SDK and adding an app.py file.
Example: Story generation with GPT-2:
import gradio as gr
description = "Story generation with GPT-2"
title = "Generate your own story"
examples = [["Adventurer is approached by a mysterious stranger in the tavern for a new quest."]]
interface = gr.Interface.load("huggingface/pranavpsv/gpt2-genre-story-generator",
description=description,
examples=examples
)
interface.launch()
Try the live demo here. Under the hood, Gradio calls the Inference API, which supports Transformers, spaCy, SpeechBrain, and other frameworks. It works with various model types like image-to-text, speech-to-text, and text-to-speech.
Serving Custom Model Checkpoints with Gradio in Hugging Face Spaces
Even if your model isn't supported by the Inference API, you can still serve it by wrapping the inference logic in a Gradio Interface and deploying it on Spaces.
Mix and Match Models!
Using Gradio's Series, you can chain multiple models. For example, combine a French-to-English translator, a story generator, and an English-to-French translator to create a French story generator:
import gradio as gr
from gradio.mix import Series
description = "Generate your own D&D story!"
title = "French Story Generator using Opus MT and GPT-2"
translator_fr = gr.Interface.load("huggingface/Helsinki-NLP/opus-mt-fr-en")
story_gen = gr.Interface.load("huggingface/pranavpsv/gpt2-genre-story-generator")
translator_en = gr.Interface.load("huggingface/Helsinki-NLP/opus-mt-en-fr")
examples = [["L'aventurier est approché par un mystérieux étranger, pour une nouvelle quête."]]
Series(translator_fr, story_gen, translator_en, description=description,
title=title,
examples=examples, inputs=gr.inputs.Textbox(lines=10)).launch()
Check out the French Story Generator here.
Uploading Models to Spaces
To host your demos on Hugging Face Spaces, create a new Space and upload your files via drag-and-drop or Git. Build your first demo now at Hugging Face Spaces.