Skip to main content
The Gradio Python client makes it easy to use any Gradio app as an API. You can call Gradio apps hosted on Hugging Face Spaces, your own servers, or anywhere else with just a few lines of Python code.

Installation

The lightweight gradio_client package can be installed from pip and works with Python 3.10 or higher:
If you already have a recent version of gradio, then gradio_client is included as a dependency.

Quick start

Here’s a simple example using the Whisper transcription Space:

Connecting to apps

Connect to Hugging Face Spaces

Connect to a Gradio app by passing the Space name to the Client constructor:

Connect to private Spaces

For private Spaces, pass your Hugging Face token:
You can get your HF token at https://huggingface.co/settings/tokens.

Connect to custom URLs

If your app is running on your own server, provide the full URL:

Connect with authentication

If the app requires username and password authentication:

Duplicate a Space for unlimited usage

While you can use any public Space as an API, you may get rate limited if you make too many requests. For unlimited usage, duplicate the Space to create a private copy:
If you’ve previously duplicated a Space, duplicate() will attach to the existing Space instead of creating a new one.
If the original Space uses GPUs, your duplicated Space will also use GPUs and your Hugging Face account will be billed. Your Space will automatically sleep after 1 hour of inactivity. You can customize the hardware using the hardware parameter.

Inspect API endpoints

Use view_api() to see available endpoints and their parameters:
Alternatively, click the “Use via API” link in the footer of any Gradio app to view the API page in your browser.

Make predictions

Basic prediction

Call .predict() with the appropriate arguments:

Multiple parameters

For endpoints with multiple parameters, use keyword arguments:

File inputs

For file or URL inputs, use handle_file():

Async operations

Submit jobs asynchronously

The .predict() method blocks until the operation completes. Use .submit() to run jobs in the background:

Add callbacks

Execute functions when jobs complete:

Check job status

Monitor job progress with .status():
The StatusUpdate object includes:
  • code: Status code (e.g., STARTING, PENDING, COMPLETE)
  • rank: Position in queue
  • queue_size: Total queue size
  • eta: Estimated completion time
  • success: Whether job completed successfully
  • time: When status was generated

Cancel jobs

Cancel queued jobs that haven’t started:

Generator endpoints

Some endpoints return multiple values over time. Access all outputs with .outputs():

Iterate over results

Use the job as an iterator to process results as they arrive:

Cancel iterative jobs

Cancel generator endpoints mid-stream:

Session state

The Python client automatically handles session state for you. When an endpoint uses gr.State, the state is stored internally and passed automatically in subsequent requests. Here’s an example with a stateful word counter:
You don’t need to manage state parameters manually - the client handles this automatically.

Next steps

JavaScript client

Use Gradio apps as APIs from JavaScript

LLM agents

Integrate Gradio apps with LLM agents