hf-gradio
hf-gradio is an extension of the Hugging Face CLI, designed to streamline interaction with Gradio Spaces and Apps. It provides command-line tools for tasks like running, deploying, and managing Gradio applications on the Hugging Face Hub, alongside a Python API that extends `gradio_client.Client` with Hugging Face integration. The current version is 0.3.0, and it generally follows the release cadence of Gradio and Hugging Face Hub libraries, with frequent updates during its early development phase.
Warnings
- gotcha The `hf-gradio` library's primary interface is the `huggingface-cli gradio` command-line tool. While it offers a Python API via `hf_gradio.client.GradioClient`, most of its unique functionalities (like deploying, running, or managing spaces) are exposed through the CLI extension rather than direct Python functions.
- gotcha Interacting with private Gradio Spaces or performing authenticated operations (such as deploying new apps or managing existing ones) requires a valid Hugging Face API token. This token should be provided either via the `HF_TOKEN` environment variable or explicitly as the `hf_token` argument.
- breaking As a relatively new library (v0.3.0), `hf-gradio` is under active development. Early minor versions (0.x.x) may introduce breaking changes to the Python API or CLI arguments as the project matures and stabilizes its public interfaces.
Install
-
pip install hf-gradio
Imports
- GradioClient
from hf_gradio.client import GradioClient
Quickstart
import os
from hf_gradio.client import GradioClient
# Replace with your actual Hugging Face token if connecting to private spaces
# For public spaces, a token is often not strictly required, but good practice for rate limits.
hf_token = os.environ.get('HF_TOKEN', '')
try:
# Connect to a public Gradio Space (e.g., 'gradio/hello_world')
client = GradioClient(space_id="gradio/hello_world", hf_token=hf_token)
print(f"Connected to space: {client.src}")
# Make a prediction using the API name for the first function in the space
# For gradio/hello_world, the default API name is usually '/predict'
result = client.predict(
name="Gradio User",
api_name="/predict"
)
print(f"Prediction result: {result}")
except Exception as e:
print(f"An error occurred: {e}")
print("Make sure the Gradio Space ID is correct and accessible.")
print("If it's a private space, ensure HF_TOKEN environment variable is set or passed correctly.")