Core client functionality for connecting to Gradio applications, managing connections, and configuring client behavior including authentication, SSL verification, and custom headers.
Initialize a client connection to a Gradio application with comprehensive configuration options.
class Client:
def __init__(
self,
src: str,
hf_token: str | None = None,
max_workers: int = 40,
verbose: bool = True,
auth: tuple[str, str] | None = None,
httpx_kwargs: dict[str, Any] | None = None,
*,
headers: dict[str, str] | None = None,
download_files: str | Path | Literal[False] = DEFAULT_TEMP_DIR,
ssl_verify: bool = True,
analytics_enabled: bool = True,
):
"""
Initialize a Gradio client.
Parameters:
- src: Hugging Face Space name (e.g. "abidlabs/whisper") or full URL
- hf_token: Optional Hugging Face token for private Spaces
- max_workers: Maximum thread workers for concurrent requests
- verbose: Whether to print status messages
- auth: Basic authentication credentials as (username, password) tuple
- httpx_kwargs: Additional arguments for HTTP client (timeouts, proxies, etc.)
- headers: Additional headers to send with requests
- download_files: Directory for downloaded files or False to disable downloading
- ssl_verify: Whether to verify SSL certificates
- analytics_enabled: Whether to enable telemetry
"""Manage client connections and sessions with proper cleanup and reset capabilities.
def close(self) -> None:
"""
Close the client connection and cleanup resources.
Properly terminates all active connections and cleans up temporary files.
Should be called when the client is no longer needed.
"""
def reset_session(self) -> None:
"""
Reset the current session.
Clears the current session state and generates a new session hash.
Useful for starting fresh without creating a new client instance.
"""Inspect and view available API endpoints with detailed parameter information.
def view_api(
self,
all_endpoints: bool | None = None,
print_info: bool = True,
return_format: Literal["dict", "str"] | None = None,
) -> dict | str | None:
"""
Display information about available API endpoints.
Parameters:
- all_endpoints: If True, shows all endpoints. If False, only named endpoints.
If None, shows named endpoints or unnamed if no named exist.
- print_info: Whether to print the API information to console
- return_format: Format for returned information ("dict", "str", or None)
Returns:
API information as dict/string based on return_format, or None if no format specified
"""
def add_zero_gpu_headers(self, headers: dict[str, str]) -> dict[str, str]:
"""
Add Zero-GPU specific headers for quota tracking.
Adds x-ip-token header for Zero-GPU Spaces when called from within
a Gradio app's LocalContext. Must be called from inside a prediction
function, not during client instantiation.
Parameters:
- headers: Existing headers dictionary to modify
Returns:
Updated headers dictionary with Zero-GPU headers if available
"""from gradio_client import Client
# Connect to a public Space
client = Client("abidlabs/whisper-large-v2")
# Connect to a private Space
client = Client("username/private-space", hf_token="hf_...")
# Connect to any Gradio URL
client = Client("https://example.com/gradio-app")from gradio_client import Client
# Client with custom configuration
client = Client(
"abidlabs/whisper",
max_workers=10,
verbose=False,
headers={"Custom-Header": "value"},
download_files="/custom/download/path",
ssl_verify=False,
httpx_kwargs={
"timeout": 30.0,
"proxies": {"http://": "http://proxy:8080"}
}
)
# View available APIs
client.view_api()
# Clean up when done
client.close()from gradio_client import Client
# Basic authentication
client = Client(
"https://protected-app.com",
auth=("username", "password")
)
# Hugging Face token authentication
client = Client(
"username/private-space",
hf_token="hf_your_token_here"
)