Skip to content

Commit e2271e2

Browse files
pngwngradio-pr-bothannahblair
authored
documentation for @gradio/client (#8483)
* fix param name * format * docs * add changeset * Update client/js/README.md Co-authored-by: Hannah <[email protected]> * add changeset * Apply suggestions from code review Co-authored-by: Hannah <[email protected]> --------- Co-authored-by: gradio-pr-bot <[email protected]> Co-authored-by: Hannah <[email protected]>
1 parent f8ebace commit e2271e2

File tree

3 files changed

+61
-45
lines changed

3 files changed

+61
-45
lines changed

.changeset/plain-mice-help.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@gradio/client": patch
3+
"gradio": patch
4+
---
5+
6+
feat:documentation for @gradio/client

client/js/README.md

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,19 @@ const submission = app.submit("/predict", { name: "Chewbacca" });
154154

155155
The `submit` method accepts the same [`endpoint`](#endpoint) and [`payload`](#payload) arguments as `predict`.
156156

157-
The `submit` method does not return a promise and should not be awaited, instead it returns an object with a `on`, `off`, and `cancel` methods.
157+
The `submit` method does not return a promise and should not be awaited, instead it returns an async iterator with a `cancel` method.
158158

159-
##### `on`
159+
##### Accessing values
160160

161-
The `on` method allows you to subscribe to events related to the submitted API request. There are two types of event that can be subscribed to: `"data"` updates and `"status"` updates.
161+
Iterating the submission allows you to access the events related to the submitted API request. There are two types of events that can be listened for: `"data"` updates and `"status"` updates. By default only the `"data"` event is reported, but you can listen for the `"status"` event by manually passing the events you care about when instantiating the client:
162+
163+
```ts
164+
import { Client } from "@gradio/client";
165+
166+
const app = await Client.connect("user/space-name", {
167+
events: ["data", "status"]
168+
});
169+
```
162170

163171
`"data"` updates are issued when the API computes a value, the callback provided as the second argument will be called when such a value is sent to the client. The shape of the data depends on the way the API itself is constructed. This event may fire more than once if that endpoint supports emmitting new values over time.
164172

@@ -187,49 +195,26 @@ interface Status {
187195
}
188196
```
189197

190-
Usage of these subscribe callback looks like this:
198+
Usage looks like this:
191199

192200
```ts
193201
import { Client } from "@gradio/client";
194202
195203
const app = await Client.connect("user/space-name");
196204
const submission = app
197205
.submit("/predict", { name: "Chewbacca" })
198-
.on("data", (data) => console.log(data))
199-
.on("status", (status: Status) => console.log(status));
200-
```
201-
202-
##### `off`
203-
204-
The `off` method unsubscribes from a specific event of the submitted job and works similarly to `document.removeEventListener`; both the event name and the original callback must be passed in to successfully unsubscribe:
205-
206-
```ts
207-
import { Client } from "@gradio/client";
208206
209-
const app = await Client.connect("user/space-name");
210-
const handle_data = (data) => console.log(data);
211-
212-
const submission = app.submit("/predict", { name: "Chewbacca" }).on("data", handle_data);
207+
for await (const msg of submission) {
208+
if (msg.type === "data") {
209+
console.log(msg.data);
210+
}
213211
214-
// later
215-
submission.off("/predict", handle_data);
212+
if (msg.type === "status") {
213+
console.log(msg);
214+
}
215+
}
216216
```
217217

218-
##### `destroy`
219-
220-
The `destroy` method will remove all subscriptions to a job, regardless of whether or not they are `"data"` or `"status"` events. This is a convenience method for when you do not want to unsubscribe use the `off` method.
221-
222-
```js
223-
import { Client } from "@gradio/client";
224-
225-
const app = await Client.connect("user/space-name");
226-
const handle_data = (data) => console.log(data);
227-
228-
const submission = app.submit("/predict", { name: "Chewbacca" }).on("data", handle_data);
229-
230-
// later
231-
submission.destroy();
232-
```
233218

234219
##### `cancel`
235220

@@ -241,7 +226,7 @@ import { Client } from "@gradio/client";
241226
const app = await Client.connect("user/space-name");
242227
const submission = app
243228
.submit("/predict", { name: "Chewbacca" })
244-
.on("data", (data) => console.log(data));
229+
245230

246231
// later
247232

guides/08_gradio-clients-and-lite/02_getting-started-with-the-js-client.md

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const response = await fetch(
8484
const audio_file = await response.blob();
8585

8686
const app = await Client.duplicate("abidlabs/whisper", { hf_token: "hf_..." });
87-
const transcription = app.predict("/predict", [audio_file]);
87+
const transcription = await app.predict("/predict", [audio_file]);
8888
```
8989

9090
If you have previously duplicated a Space, re-running `Client.duplicate` will _not_ create a new Space. Instead, the client will attach to the previously-created Space. So it is safe to re-run the `Client.duplicate` method multiple times with the same space.
@@ -202,7 +202,7 @@ const result = await app.predict("/predict", [audio_file]);
202202

203203
## Using events
204204

205-
If the API you are working with can return results over time, or you wish to access information about the status of a job, you can use the event interface for more flexibility. This is especially useful for iterative endpoints or generator endpoints that will produce a series of values over time as discreet responses.
205+
If the API you are working with can return results over time, or you wish to access information about the status of a job, you can use the iterable interface for more flexibility. This is especially useful for iterative endpoints or generator endpoints that will produce a series of values over time as discreet responses.
206206

207207
```js
208208
import { Client } from "@gradio/client";
@@ -218,12 +218,27 @@ function log_result(payload) {
218218
const app = await Client.connect("abidlabs/en2fr");
219219
const job = app.submit("/predict", ["Hello"]);
220220

221-
job.on("data", log_result);
221+
for await (const message of job) {
222+
log_result(message);
223+
}
222224
```
223225

224226
## Status
225227

226-
The event interface also allows you to get the status of the running job by listening to the `"status"` event. This returns an object with the following attributes: `status` (a human readbale status of the current job, `"pending" | "generating" | "complete" | "error"`), `code` (the detailed gradio code for the job), `position` (the current position of this job in the queue), `queue_size` (the total queue size), `eta` (estimated time this job will complete), `success` (a boolean representing whether the job completed successfully), and `time` ( as `Date` object detailing the time that the status was generated).
228+
The event interface also allows you to get the status of the running job by instantiating the client with the `events` options passing `status` and `data` as an array:
229+
230+
231+
```ts
232+
import { Client } from "@gradio/client";
233+
234+
const app = await Client.connect("abidlabs/en2fr", {
235+
events: ["status", "data"]
236+
});
237+
```
238+
239+
This ensures that status messages are also reported to the client.
240+
241+
`status`es are returned as an object with the following attributes: `status` (a human readbale status of the current job, `"pending" | "generating" | "complete" | "error"`), `code` (the detailed gradio code for the job), `position` (the current position of this job in the queue), `queue_size` (the total queue size), `eta` (estimated time this job will complete), `success` (a boolean representing whether the job completed successfully), and `time` ( as `Date` object detailing the time that the status was generated).
227242

228243
```js
229244
import { Client } from "@gradio/client";
@@ -234,10 +249,16 @@ function log_status(status) {
234249
);
235250
}
236251

237-
const app = await Client.connect("abidlabs/en2fr");
252+
const app = await Client.connect("abidlabs/en2fr", {
253+
events: ["status", "data"]
254+
});
238255
const job = app.submit("/predict", ["Hello"]);
239256

240-
job.on("status", log_status);
257+
for await (const message of job) {
258+
if (message.type === "status") {
259+
log_status(message);
260+
}
261+
}
241262
```
242263

243264
## Cancelling Jobs
@@ -259,15 +280,17 @@ If the first job has started processing, then it will not be canceled but the cl
259280

260281
## Generator Endpoints
261282

262-
Some Gradio API endpoints do not return a single value, rather they return a series of values. You can listen for these values in real time using the event interface:
283+
Some Gradio API endpoints do not return a single value, rather they return a series of values. You can listen for these values in real time using the iterable interface:
263284

264285
```js
265286
import { Client } from "@gradio/client";
266287

267288
const app = await Client.connect("gradio/count_generator");
268289
const job = app.submit(0, [9]);
269290

270-
job.on("data", (data) => console.log(data));
291+
for await (const message of job) {
292+
console.log(message.data);
293+
}
271294
```
272295

273296
This will log out the values as they are generated by the endpoint.
@@ -280,7 +303,9 @@ import { Client } from "@gradio/client";
280303
const app = await Client.connect("gradio/count_generator");
281304
const job = app.submit(0, [9]);
282305

283-
job.on("data", (data) => console.log(data));
306+
for await (const message of job) {
307+
console.log(message.data);
308+
}
284309

285310
setTimeout(() => {
286311
job.cancel();

0 commit comments

Comments
 (0)