GRADIO CLIENT & API USAGE: 10 INTERACTIVE REAL-TIME PYTHON API EXAMPLES

Programmatically consume deployed Gradio web applications and Hugging Face Spaces using the gradio_client Python library. Test each client API example live below!

⚔ Programmatic API Access with `gradio_client`

Every Gradio application automatically exposes an API endpoint. Using the gradio_client library, Python scripts, backend web servers (like PHP or FastAPI), and automated bots can connect to any hosted Gradio space or local server to execute predictions programmatically.

Example 1: Definition & Basic `Client()` Connection
Connects to a remote or local Gradio application instance using the Client class.
Python (client_ex1.py)
from gradio_client import Client

# Connect to a hosted Gradio application or Hugging Face Space
client = Client("abidlabs/huggingface-logo")

# Inspect available API endpoints and parameters
print(client.view_api())
šŸ–„ļø Live Interactive Web UI Simulator

### Basic Client Connection

Click 'Inspect API Schema' to view endpoint definitions.
Example 2: Executing Predictions via `client.predict()`
Sends parameters to a specific API endpoint index or named route and retrieves prediction results programmatically.
Python (client_ex2.py)
from gradio_client import Client

client = Client("abidlabs/en-to-es-translator")

# Execute prediction by passing parameters positionally
result = client.predict(
    text="Welcome to Nikita-Infotech masterclass.",
    api_name="/predict"
)

print("Translation Result:", result)
šŸ–„ļø Live Interactive Web UI Simulator

### Predict API Execution

Click 'Execute client.predict()' to run remote prediction.
Example 3: Streaming API Responses with `client.submit()`
Initiates an asynchronous background prediction job using client.submit() and iterates through streaming updates.
Python (client_ex3.py)
from gradio_client import Client

client = Client("nasa/apod-generator")

# Submit job for streaming output
job = client.submit(prompt="Nebula galaxy", api_name="/generate")

# Iterate over streaming results as they complete
for response in job:
    print("Stream update:", response)
šŸ–„ļø Live Interactive Web UI Simulator

### Streaming Job Submission

Click 'Submit & Stream Job' to test job iteration.
Example 4: Handling File Uploads in API Client Calls
Passes local file paths (images, PDFs, CSVs) directly into remote Gradio API endpoints.
Python (client_ex4.py)
from gradio_client import Client, handle_file

client = Client("akhaliq/vit-image-classifier")

# Use handle_file() to upload local assets securely
result = client.predict(
    image=handle_file("sample_cat.jpg"),
    api_name="/predict"
)

print("Classification:", result)
šŸ–„ļø Live Interactive Web UI Simulator

### File Upload API Client

Click 'Upload & Predict' to test handle_file() transmission.
Example 5: Connecting to Private Hugging Face Spaces with Token Authentication
Authenticates API client requests using Hugging Face access tokens for private spaces.
Python (client_ex5.py)
from gradio_client import Client

# Pass hf_token for private space authentication
client = Client("nikita-infotech/private-enterprise-ai", hf_token="hf_YourAccessTokenHere")

result = client.predict(message="Run confidential audit", api_name="/chat")
print(result)
šŸ–„ļø Live Interactive Web UI Simulator

### Authenticated Private Client

Click 'Connect & Authenticate' to test token validation.
Example 6: Multimodal API Client Calls (Text + Image)
Sends structured multimodal dictionaries containing both text prompts and image file references to vision-enabled APIs.
Python (client_ex6.py)
from gradio_client import Client, handle_file

client = Client("llava-hf/llava-1.5-7b-hf")

# Construct multimodal input payload
payload = {
    "text": "Describe this architectural diagram.",
    "files": [handle_file("diagram.png")]
}

result = client.predict(message=payload, api_name="/chat")
print("Vision Response:", result)
šŸ–„ļø Live Interactive Web UI Simulator

### Multimodal API Client

Click 'Send Multimodal Payload' to test vision client.
Example 7: Batch Processing Multiple Requests Concurrently
Iterates through a list of input prompts and dispatches concurrent API predictions using Python threading.
Python (client_ex7.py)
from gradio_client import Client
import concurrent.futures

client = Client("huggingface/CodeLlama-7b-Instruct")
prompts = ["Write a PHP loop", "Write a Python generator", "Write a SQL join"]

def worker(prompt):
    return client.predict(message=prompt, api_name="/chat")

with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
    results = list(executor.map(worker, prompts))

for i, res in enumerate(results):
    print(f"Result {i+1}:", res[:100])
šŸ–„ļø Live Interactive Web UI Simulator

### Batch ThreadPool Client

Click 'Run Batch ThreadPool' to execute parallel requests.
Example 8: API Error Handling & Connection Timeout Management
Implements robust exception handling (`httpx.TimeoutException`, connection errors) when calling remote APIs.
Python (client_ex8.py)
from gradio_client import Client
from gradio_client.exceptions import Error

try:
    client = Client("non-existent-space-url-xyz", src="https://huggingface.co/spaces/invalid/space")
    result = client.predict(text="test", api_name="/predict")
except Exception as e:
    print(f"āš ļø API Connection Failed: {str(e)}")
    print("šŸ”„ Fallback to local backup server activated.")
šŸ–„ļø Live Interactive Web UI Simulator

### API Error & Timeout Guard

Click 'Test Error Handling' to simulate connection recovery.
Example 9: Automated CI/CD Pipeline Testing via Gradio Client
Writes automated unit test assertions verifying that deployed Gradio endpoints return expected output types and values.
Python (client_ex9.py)
from gradio_client import Client

def test_deployed_gradio_app():
    client = Client("http://localhost:7860")
    response = client.predict(name="Nikita", api_name="/predict")
    
    assert "Nikita" in response, "Assertion Failed: Name missing in response"
    assert isinstance(response, str), "Assertion Failed: Output must be string"
    print("āœ… All CI/CD API Integration Tests Passed!")

if __name__ == "__main__":
    test_deployed_gradio_app()
šŸ–„ļø Live Interactive Web UI Simulator

### CI/CD Integration Test Runner

Click 'Run Unit Test Suite' to execute client assertions.
Example 10: Enterprise Multi-Client API Integration Hub (`gr.Blocks`)
Combines multiple remote client endpoints (Translation, Summarization, Sentiment) into an enterprise management dashboard.
Python (client_ex10.py)
import gradio as gr
from gradio_client import Client

def enterprise_hub_query(prompt):
    # Simulated multi-client aggregation hub
    return f"Hub Request Processed Successfully šŸš€\n- Endpoint 1 (GPT-4o): Answer generated.\n- Endpoint 2 (Classifier): Positive (0.99).\n- Query: '{prompt}'"

with gr.Blocks() as demo:
    gr.Markdown("# šŸš€ Enterprise Multi-Client API Gateway")
    
    with gr.Row():
        status = gr.Textbox(label="Client Gateway Status", value="Connected to 3 Remote Spaces 🟢")
        
    prompt_in = gr.Textbox(label="Global Prompt Dispatcher", value="Audit enterprise software architecture.")
    btn = gr.Button("Dispatch to All Clients", variant="primary")
    out_box = gr.Textbox(label="Aggregated Hub Response", lines=4)
    
    btn.click(enterprise_hub_query, inputs=prompt_in, outputs=out_box)

if __name__ == "__main__":
    demo.launch()
šŸ–„ļø Live Interactive Web UI Simulator

# šŸš€ Enterprise Multi-Client API Gateway

Status: Connected to 3 Remote Spaces 🟢
Click 'Dispatch to All Clients' to test enterprise gateway.