Gradio Client

2.4.0 · active · verified Thu Apr 09

Gradio Client is a Python library designed for programmatically interacting with deployed Gradio applications. It allows developers to make API calls to Gradio-hosted machine learning models or other Gradio apps without needing the UI. The current stable version is 2.4.0. It's actively developed as part of the broader Gradio ecosystem, with frequent updates corresponding to Gradio's release cycle.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a `Client` for a Gradio application and make synchronous API calls using `client.predict()` and `client.submit().result()`. It includes basic error handling and shows how to pass a Hugging Face token for private spaces using an environment variable.

import os
from gradio_client import Client

# Replace with your Gradio app URL or Hugging Face Space ID
# For private Spaces, set the HF_TOKEN environment variable or pass hf_token directly.
SPACE_URL = os.environ.get('GRADIO_SPACE_URL', 'https://hf.space/gradio/calculator')
HF_TOKEN = os.environ.get('HF_TOKEN', '') # Optional: for private Hugging Face Spaces

client = Client(SPACE_URL, hf_token=HF_TOKEN)

# Example for a simple calculator app with 'add' function taking two numbers
try:
    # Use client.predict for synchronous calls
    result = client.predict(2, 3, api_name='/add')
    print(f"Result of 2 + 3: {result}")

    # Use client.submit for asynchronous calls or streaming output (not shown here)
    job = client.submit(10, 5, api_name='/subtract')
    print(f"Result of 10 - 5 (via job): {job.result()}")
except Exception as e:
    print(f"Error interacting with Gradio app: {e}")
    print("Please ensure the Gradio app is running and the API name is correct.")

view raw JSON →