@@ -10,13 +10,11 @@ A PHP client to call [Gradio](https://www.gradio.app) APIs.
1010- [x] HTTP and WS support
1111- [x] ` predict `
1212- [x] getConfig
13- - [ ] client options
14- - [ ] hf_token support
15- - [ ] viewApi
13+ - [x ] client options
14+ - [x ] hf_token support
15+ - [x ] viewApi
1616- [x] Finish event system
17- - [ ] Add tests
18- - [ ] Add more examples
19- - [ ] Add documentation
17+ - [x] Add tests
2018
2119## Installation
2220
@@ -28,13 +26,157 @@ composer require sergix44/gradio-client-php
2826
2927## Usage
3028
29+ ### Basic Usage
30+
3131``` php
3232use SergiX44\Gradio\Client;
3333
3434$client = new Client('https://my-special.hf.space');
3535
3636$result = $client->predict(['arg', 1, 2], apiName: 'myFunction');
3737
38+ $outputs = $result->getOutputs(); // returns array of all outputs
39+ $first = $result->getOutput(0); // returns a single output by index
40+ ```
41+
42+ ### Hugging Face Token Authentication
43+
44+ To access private Hugging Face Spaces, pass your HF token:
45+
46+ ``` php
47+ use SergiX44\Gradio\Client;
48+
49+ $client = new Client('https://my-private.hf.space', hfToken: 'hf_your_token_here');
50+
51+ $result = $client->predict(['hello'], apiName: 'chat');
52+ ```
53+
54+ The token is automatically sent as a Bearer token in the ` Authorization ` header on all HTTP and WebSocket requests.
55+
56+ ### Viewing Available API Endpoints
57+
58+ Use ` viewApi() ` to inspect the available API endpoints, their parameters, and return types:
59+
60+ ``` php
61+ use SergiX44\Gradio\Client;
62+
63+ $client = new Client('https://my-special.hf.space');
64+
65+ $apiInfo = $client->viewApi();
66+
67+ // Returns an array with 'named_endpoints' and 'unnamed_endpoints'
68+ // Each endpoint includes parameter and return type information
69+ print_r($apiInfo);
70+ ```
71+
72+ ### Client Options
73+
74+ You can pass custom HTTP client options (Guzzle options) to customize the underlying HTTP client:
75+
76+ ``` php
77+ use SergiX44\Gradio\Client;
78+
79+ $client = new Client('https://my-special.hf.space', httpClientOptions: [
80+ 'timeout' => 30,
81+ 'headers' => [
82+ 'X-Custom-Header' => 'value',
83+ ],
84+ 'proxy' => 'http://proxy.example.com:8080',
85+ ]);
86+ ```
87+
88+ ### Using Function Index
89+
90+ If you know the function index instead of the API name, you can use ` fnIndex ` :
91+
92+ ``` php
93+ $result = $client->predict(['input'], fnIndex: 0);
94+ ```
95+
96+ ### Raw Response
97+
98+ To get the raw decoded response instead of an ` Output ` DTO:
99+
100+ ``` php
101+ $result = $client->predict(['input'], apiName: 'myFunction', raw: true);
102+ // $result is an associative array
103+ ```
104+
105+ ### Event System
106+
107+ You can register callbacks for various events during the prediction process:
108+
109+ ``` php
110+ use SergiX44\Gradio\Client;
111+ use SergiX44\Gradio\DTO\Messages\Estimation;
112+ use SergiX44\Gradio\DTO\Messages\ProcessCompleted;
113+
114+ $client = new Client('https://my-special.hf.space');
115+
116+ // Called when a prediction is submitted
117+ $client->onSubmit(function (array $payload) {
118+ echo "Submitted!\n";
119+ });
120+
121+ // Called when queue position is estimated
122+ $client->onQueueEstimation(function (Estimation $estimation) {
123+ echo "Queue position: {$estimation->rank}\n";
124+ });
125+
126+ // Called when processing starts
127+ $client->onProcessStarts(function () {
128+ echo "Processing started\n";
129+ });
130+
131+ // Called when processing completes (success or failure)
132+ $client->onProcessCompleted(function (ProcessCompleted $message) {
133+ echo "Completed: " . ($message->success ? 'success' : 'failed') . "\n";
134+ });
135+
136+ // Called only on success
137+ $client->onProcessSuccess(function (ProcessCompleted $message) {
138+ $output = $message->output;
139+ });
140+
141+ // Called only on failure
142+ $client->onProcessFailed(function (ProcessCompleted $message) {
143+ echo "Failed!\n";
144+ });
145+
146+ // Called when the queue is full
147+ $client->onQueueFull(function () {
148+ echo "Queue is full!\n";
149+ });
150+
151+ // Called during streaming/generating
152+ $client->onProcessGenerating(function () {
153+ echo "Generating...\n";
154+ });
155+
156+ $result = $client->predict(['input'], apiName: 'myFunction');
157+ ```
158+
159+ ### Accessing Config
160+
161+ ``` php
162+ $config = $client->getConfig();
163+
164+ echo $config->version; // Gradio version
165+ echo $config->protocol; // 'sse_v3', 'ws', etc.
166+ echo $config->title; // App title
167+ ```
168+
169+ ### File Upload
170+
171+ You can pass file paths or resources as arguments, and they will be automatically encoded as base64:
172+
173+ ``` php
174+ // Using a file path
175+ $result = $client->predict(['/path/to/image.png'], apiName: 'classify');
176+
177+ // Using a resource
178+ $stream = fopen('/path/to/audio.mp3', 'r');
179+ $result = $client->predict([$stream], apiName: 'transcribe');
38180```
39181
40182## Testing
0 commit comments