#!/usr/bin/env python """08 -- drive a running gradio app programmatically with gradio_client. Every gradio app is also an HTTP API, which makes it scriptable from CI. Start a template in one shell, then run this in another: python 01_hello_interface.py python 08_api_client.py http://127.0.0.1:7860/ gradio_client fetches the app's schema from the server itself, so nothing beyond the app's own host and port is needed. Endpoint names are NOT stable across gradio majors: gradio 4 names the single gr.Interface function "/predict", while gradio 5+ derives the name from the function instead ("/greet" for 01_hello_interface.py). Rather than hardcode either, discover the endpoint from the schema. """ import sys from gradio_client import Client url = sys.argv[1] if len(sys.argv) > 1 else "http://127.0.0.1:7860/" client = Client(url, verbose=False) print(f"connected to {url}") print("--- declared endpoints ---") client.view_api(print_info=True) api = client.view_api(return_format="dict") or {} named = list((api.get("named_endpoints") or {}).keys()) if not named: print("no named endpoints; give the target function an api_name=") sys.exit(1) # Prefer a greeting-style endpoint if present, else just take the first. endpoint = next((n for n in named if "greet" in n or "predict" in n), named[0]) print(f"--- calling {endpoint} ---") result = client.predict("RHEL", 3, api_name=endpoint) print(result)