Installation
The lightweightgradio_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 theClient constructor:
Connect to private Spaces
For private Spaces, pass your Hugging Face token: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: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
Useview_api() to see available endpoints and their parameters:
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, usehandle_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():
StatusUpdate object includes:
code: Status code (e.g.,STARTING,PENDING,COMPLETE)rank: Position in queuequeue_size: Total queue sizeeta: Estimated completion timesuccess: Whether job completed successfullytime: 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 usesgr.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