@gradio/client 0.17.0 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/client.d.ts +15 -4
  3. package/dist/client.d.ts.map +1 -1
  4. package/dist/constants.d.ts +8 -2
  5. package/dist/constants.d.ts.map +1 -1
  6. package/dist/helpers/data.d.ts +2 -2
  7. package/dist/helpers/data.d.ts.map +1 -1
  8. package/dist/helpers/init_helpers.d.ts.map +1 -1
  9. package/dist/helpers/spaces.d.ts.map +1 -1
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +219 -176
  13. package/dist/test/handlers.d.ts +3 -0
  14. package/dist/test/handlers.d.ts.map +1 -0
  15. package/dist/test/mock_eventsource.d.ts +2 -0
  16. package/dist/test/mock_eventsource.d.ts.map +1 -0
  17. package/dist/test/server.d.ts +2 -0
  18. package/dist/test/server.d.ts.map +1 -0
  19. package/dist/test/test_data.d.ts +76 -0
  20. package/dist/test/test_data.d.ts.map +1 -0
  21. package/dist/types.d.ts +4 -3
  22. package/dist/types.d.ts.map +1 -1
  23. package/dist/upload.d.ts +2 -2
  24. package/dist/upload.d.ts.map +1 -1
  25. package/dist/utils/duplicate.d.ts.map +1 -1
  26. package/dist/utils/stream.d.ts +1 -1
  27. package/dist/utils/stream.d.ts.map +1 -1
  28. package/dist/utils/submit.d.ts.map +1 -1
  29. package/dist/utils/upload_files.d.ts.map +1 -1
  30. package/dist/utils/view_api.d.ts.map +1 -1
  31. package/package.json +8 -2
  32. package/src/client.ts +50 -24
  33. package/src/constants.ts +9 -2
  34. package/src/helpers/api_info.ts +4 -4
  35. package/src/helpers/data.ts +46 -28
  36. package/src/helpers/init_helpers.ts +7 -11
  37. package/src/helpers/spaces.ts +8 -3
  38. package/src/index.ts +1 -1
  39. package/src/test/api_info.test.ts +456 -0
  40. package/src/test/data.test.ts +281 -0
  41. package/src/test/handlers.ts +438 -0
  42. package/src/test/init.test.ts +139 -0
  43. package/src/test/init_helpers.test.ts +94 -0
  44. package/src/test/mock_eventsource.ts +11 -0
  45. package/src/test/post_data.test.ts +45 -0
  46. package/src/test/server.ts +6 -0
  47. package/src/test/spaces.test.ts +145 -0
  48. package/src/test/stream.test.ts +67 -0
  49. package/src/test/test_data.ts +557 -0
  50. package/src/test/upload_files.test.ts +42 -0
  51. package/src/test/view_api.test.ts +53 -0
  52. package/src/types.ts +4 -3
  53. package/src/upload.ts +4 -8
  54. package/src/utils/duplicate.ts +20 -3
  55. package/src/utils/handle_blob.ts +1 -1
  56. package/src/utils/post_data.ts +1 -1
  57. package/src/utils/stream.ts +29 -20
  58. package/src/utils/submit.ts +23 -15
  59. package/src/utils/upload_files.ts +11 -6
  60. package/src/utils/view_api.ts +4 -7
  61. package/vite.config.js +7 -0
  62. package/src/utils/client.node-test.ts +0 -173
@@ -1,3 +1,8 @@
1
+ import {
2
+ RUNTIME_URL,
3
+ SLEEPTIME_URL,
4
+ SPACE_STATUS_ERROR_MSG
5
+ } from "../constants";
1
6
  import type { SpaceStatusCallback } from "../types";
2
7
 
3
8
  export async function check_space_status(
@@ -22,7 +27,7 @@ export async function check_space_status(
22
27
  status_callback({
23
28
  status: "error",
24
29
  load_status: "error",
25
- message: "Could not get space status",
30
+ message: SPACE_STATUS_ERROR_MSG,
26
31
  detail: "NOT_FOUND"
27
32
  });
28
33
  return;
@@ -121,7 +126,7 @@ export async function get_space_hardware(
121
126
 
122
127
  try {
123
128
  const res = await fetch(
124
- `https://huggingface.co/api/spaces/${space_id}/runtime`,
129
+ `https://huggingface.co/api/spaces/${space_id}/${RUNTIME_URL}`,
125
130
  { headers }
126
131
  );
127
132
 
@@ -154,7 +159,7 @@ export async function set_space_timeout(
154
159
 
155
160
  try {
156
161
  const res = await fetch(
157
- `https://huggingface.co/api/spaces/${space_id}/sleeptime`,
162
+ `https://huggingface.co/api/spaces/${space_id}/${SLEEPTIME_URL}`,
158
163
  {
159
164
  method: "POST",
160
165
  headers: { "Content-Type": "application/json", ...headers },
package/src/index.ts CHANGED
@@ -14,4 +14,4 @@ export type {
14
14
 
15
15
  // todo: remove in @gradio/client v1.0
16
16
  export { client } from "./client";
17
- export { duplicate } from "./utils/duplicate";
17
+ export { duplicate_space as duplicate } from "./client";
@@ -0,0 +1,456 @@
1
+ import { QUEUE_FULL_MSG, SPACE_METADATA_ERROR_MSG } from "../constants";
2
+ import { beforeAll, afterEach, afterAll, it, expect, describe } from "vitest";
3
+ import {
4
+ handle_message,
5
+ get_description,
6
+ get_type,
7
+ process_endpoint
8
+ } from "../helpers/api_info";
9
+ import { initialise_server } from "./server";
10
+
11
+ const server = initialise_server();
12
+
13
+ beforeAll(() => server.listen());
14
+ afterEach(() => server.resetHandlers());
15
+ afterAll(() => server.close());
16
+
17
+ describe("handle_message", () => {
18
+ it("should return type 'data' when msg is 'send_data'", () => {
19
+ const data = { msg: "send_data" };
20
+ const last_status = "pending";
21
+ const result = handle_message(data, last_status);
22
+ expect(result).toEqual({ type: "data" });
23
+ });
24
+
25
+ it("should return type 'hash' when msg is 'send_hash'", () => {
26
+ const data = { msg: "send_hash" };
27
+ const last_status = "pending";
28
+ const result = handle_message(data, last_status);
29
+ expect(result).toEqual({ type: "hash" });
30
+ });
31
+
32
+ it("should return type 'update' with queue full message when msg is 'queue_full'", () => {
33
+ const data = { msg: "queue_full", code: 500, success: false };
34
+ const last_status = "pending";
35
+ const result = handle_message(data, last_status);
36
+ expect(result).toEqual({
37
+ type: "update",
38
+ status: {
39
+ queue: true,
40
+ message: QUEUE_FULL_MSG,
41
+ stage: "error",
42
+ code: 500,
43
+ success: false
44
+ }
45
+ });
46
+ });
47
+
48
+ it("should return type 'heartbeat' when msg is 'heartbeat'", () => {
49
+ const data = { msg: "heartbeat" };
50
+ const last_status = "pending";
51
+ const result = handle_message(data, last_status);
52
+ expect(result).toEqual({ type: "heartbeat" });
53
+ });
54
+
55
+ it("should return type 'unexpected_error' with error message when msg is 'unexpected_error'", () => {
56
+ const data = { msg: "unexpected_error", message: "Something went wrong" };
57
+ const last_status = "pending";
58
+ const result = handle_message(data, last_status);
59
+ expect(result).toEqual({
60
+ type: "unexpected_error",
61
+ status: {
62
+ queue: true,
63
+ message: "Something went wrong",
64
+ stage: "error",
65
+ success: false
66
+ }
67
+ });
68
+ });
69
+
70
+ it("should return type 'update' with estimation status when msg is 'estimation'", () => {
71
+ const data = {
72
+ msg: "estimation",
73
+ code: 200,
74
+ queue_size: 10,
75
+ rank: 5,
76
+ rank_eta: 60,
77
+ success: true
78
+ };
79
+ const last_status = "pending";
80
+ const result = handle_message(data, last_status);
81
+ expect(result).toEqual({
82
+ type: "update",
83
+ status: {
84
+ queue: true,
85
+ stage: "pending",
86
+ code: 200,
87
+ size: 10,
88
+ position: 5,
89
+ eta: 60,
90
+ success: true
91
+ }
92
+ });
93
+ });
94
+
95
+ it("should return type 'update' with progress status when msg is 'progress'", () => {
96
+ const data = {
97
+ msg: "progress",
98
+ code: 200,
99
+ progress_data: { current: 50, total: 100 },
100
+ success: true
101
+ };
102
+ const last_status = "pending";
103
+ const result = handle_message(data, last_status);
104
+ expect(result).toEqual({
105
+ type: "update",
106
+ status: {
107
+ queue: true,
108
+ stage: "pending",
109
+ code: 200,
110
+ progress_data: { current: 50, total: 100 },
111
+ success: true
112
+ }
113
+ });
114
+ });
115
+
116
+ it("should return type 'log' with the provided data when msg is 'log'", () => {
117
+ const data = { msg: "log", log_data: "Some log message" };
118
+ const last_status = "pending";
119
+ const result = handle_message(data, last_status);
120
+ expect(result).toEqual({
121
+ type: "log",
122
+ data: { msg: "log", log_data: "Some log message" }
123
+ });
124
+ });
125
+
126
+ it("should return type 'generating' with generating status when msg is 'process_generating' and success is true", () => {
127
+ const data = {
128
+ msg: "process_generating",
129
+ success: true,
130
+ code: 200,
131
+ progress_data: { current: 50, total: 100 },
132
+ average_duration: 120,
133
+ output: { result: "Some result" }
134
+ };
135
+ const last_status = "pending";
136
+ const result = handle_message(data, last_status);
137
+ expect(result).toEqual({
138
+ type: "generating",
139
+ status: {
140
+ queue: true,
141
+ message: null,
142
+ stage: "generating",
143
+ code: 200,
144
+ progress_data: { current: 50, total: 100 },
145
+ eta: 120
146
+ },
147
+ data: { result: "Some result" }
148
+ });
149
+ });
150
+
151
+ it("should return type 'update' with error status when msg is 'process_generating' and success is false", () => {
152
+ const data = {
153
+ msg: "process_generating",
154
+ success: false,
155
+ code: 500,
156
+ progress_data: { current: 50, total: 100 },
157
+ average_duration: 120,
158
+ output: { error: "Error" }
159
+ };
160
+ const last_status = "pending";
161
+ const result = handle_message(data, last_status);
162
+
163
+ expect(result).toEqual({
164
+ type: "generating",
165
+ data: null,
166
+ status: {
167
+ eta: 120,
168
+ queue: true,
169
+ message: "Error",
170
+ stage: "error",
171
+ code: 500,
172
+ progress_data: { current: 50, total: 100 }
173
+ }
174
+ });
175
+ });
176
+
177
+ it("should return type 'complete' with success status when msg is 'process_completed' and success is true", () => {
178
+ const data = {
179
+ msg: "process_completed",
180
+ success: true,
181
+ code: 200,
182
+ progress_data: { current: 100, total: 100 },
183
+ output: { result: "Some result" }
184
+ };
185
+ const last_status = "pending";
186
+ const result = handle_message(data, last_status);
187
+ expect(result).toEqual({
188
+ type: "complete",
189
+ status: {
190
+ queue: true,
191
+ message: undefined,
192
+ stage: "complete",
193
+ code: 200,
194
+ progress_data: { current: 100, total: 100 }
195
+ },
196
+ data: { result: "Some result" }
197
+ });
198
+ });
199
+
200
+ it("should return type 'update' with error status when msg is 'process_completed' and success is false", () => {
201
+ const data = {
202
+ msg: "process_completed",
203
+ success: false,
204
+ code: 500,
205
+ progress_data: { current: 100, total: 100 },
206
+ output: { error: "Some error message" }
207
+ };
208
+ const last_status = "pending";
209
+ const result = handle_message(data, last_status);
210
+ expect(result).toEqual({
211
+ type: "update",
212
+ status: {
213
+ queue: true,
214
+ message: "Some error message",
215
+ stage: "error",
216
+ code: 500,
217
+ success: false
218
+ }
219
+ });
220
+ });
221
+
222
+ it("should return type 'update' with pending status when msg is 'process_starts'", () => {
223
+ const data = {
224
+ msg: "process_starts",
225
+ code: 200,
226
+ rank: 5,
227
+ success: true,
228
+ eta: 60
229
+ };
230
+ const last_status = "pending";
231
+ const result = handle_message(data, last_status);
232
+ expect(result).toEqual({
233
+ type: "update",
234
+ status: {
235
+ queue: true,
236
+ stage: "pending",
237
+ code: 200,
238
+ size: 5,
239
+ position: 0,
240
+ success: true,
241
+ eta: 60
242
+ }
243
+ });
244
+ });
245
+
246
+ it("should return type 'none' with error status when msg is unknown", () => {
247
+ const data = { msg: "unknown" };
248
+ const last_status = "pending";
249
+ const result = handle_message(data, last_status);
250
+ expect(result).toEqual({
251
+ type: "none",
252
+ status: { stage: "error", queue: true }
253
+ });
254
+ });
255
+ });
256
+
257
+ describe("get_description", () => {
258
+ it("should return 'array of [file, label] tuples' when serializer is 'GallerySerializable'", () => {
259
+ const type = { type: "string", description: "param description" };
260
+ const serializer = "GallerySerializable";
261
+ const result = get_description(type, serializer);
262
+ expect(result).toEqual("array of [file, label] tuples");
263
+ });
264
+
265
+ it("should return 'array of strings' when serializer is 'ListStringSerializable'", () => {
266
+ const type = { type: "string", description: "param description" };
267
+ const serializer = "ListStringSerializable";
268
+ const result = get_description(type, serializer);
269
+ expect(result).toEqual("array of strings");
270
+ });
271
+
272
+ it("should return 'array of files or single file' when serializer is 'FileSerializable'", () => {
273
+ const type = { type: "string", description: "param description" };
274
+ const serializer = "FileSerializable";
275
+ const result = get_description(type, serializer);
276
+ expect(result).toEqual("array of files or single file");
277
+ });
278
+
279
+ it("should return the type's description when serializer is not 'GallerySerializable', 'ListStringSerializable', or 'FileSerializable'", () => {
280
+ const type = { type: "string", description: "param description" };
281
+ const serializer = "SomeOtherSerializer";
282
+ const result = get_description(type, serializer);
283
+ expect(result).toEqual(type.description);
284
+ });
285
+ });
286
+
287
+ describe("get_type", () => {
288
+ it("should return 'string' when type is 'string'", () => {
289
+ const type = { type: "string", description: "param description" };
290
+ const component = "Component";
291
+ const serializer = "Serializer";
292
+ const signature_type = "parameter";
293
+ const result = get_type(type, component, serializer, signature_type);
294
+ expect(result).toEqual("string");
295
+ });
296
+
297
+ it("should return 'boolean' when type is 'boolean'", () => {
298
+ const type = { type: "boolean", description: "param description" };
299
+ const component = "Component";
300
+ const serializer = "Serializer";
301
+ const signature_type = "parameter";
302
+ const result = get_type(type, component, serializer, signature_type);
303
+ expect(result).toEqual("boolean");
304
+ });
305
+
306
+ it("should return 'number' when type is 'number'", () => {
307
+ const type = { type: "number", description: "param description" };
308
+ const component = "Component";
309
+ const serializer = "Serializer";
310
+ const signature_type = "parameter";
311
+ const result = get_type(type, component, serializer, signature_type);
312
+ expect(result).toEqual("number");
313
+ });
314
+
315
+ it("should return 'any' when serializer is 'JSONSerializable'", () => {
316
+ const type = { type: "any", description: "param description" };
317
+ const component = "Component";
318
+ const serializer = "JSONSerializable";
319
+ const signature_type = "parameter";
320
+ const result = get_type(type, component, serializer, signature_type);
321
+ expect(result).toEqual("any");
322
+ });
323
+
324
+ it("should return 'any' when serializer is 'StringSerializable'", () => {
325
+ const type = { type: "any", description: "param description" };
326
+ const component = "Component";
327
+ const serializer = "StringSerializable";
328
+ const signature_type = "parameter";
329
+ const result = get_type(type, component, serializer, signature_type);
330
+ expect(result).toEqual("any");
331
+ });
332
+
333
+ it("should return 'string[]' when serializer is 'ListStringSerializable'", () => {
334
+ const type = { type: "any", description: "param description" };
335
+ const component = "Component";
336
+ const serializer = "ListStringSerializable";
337
+ const signature_type = "parameter";
338
+ const result = get_type(type, component, serializer, signature_type);
339
+ expect(result).toEqual("string[]");
340
+ });
341
+
342
+ it("should return 'Blob | File | Buffer' when component is 'Image' and signature_type is 'parameter'", () => {
343
+ const type = { type: "any", description: "param description" };
344
+ const component = "Image";
345
+ const serializer = "Serializer";
346
+ const signature_type = "parameter";
347
+ const result = get_type(type, component, serializer, signature_type);
348
+ expect(result).toEqual("Blob | File | Buffer");
349
+ });
350
+
351
+ it("should return 'string' when component is 'Image' and signature_type is 'return'", () => {
352
+ const type = { type: "string", description: "param description" };
353
+ const component = "Image";
354
+ const serializer = "Serializer";
355
+ const signature_type = "return";
356
+ const result = get_type(type, component, serializer, signature_type);
357
+ expect(result).toEqual("string");
358
+ });
359
+
360
+ it("should return '(Blob | File | Buffer)[]' when serializer is 'FileSerializable' and type is an array and signature_type is 'parameter'", () => {
361
+ const type = { type: "array", description: "param description" };
362
+ const component = "Component";
363
+ const serializer = "FileSerializable";
364
+ const signature_type = "parameter";
365
+ const result = get_type(type, component, serializer, signature_type);
366
+ expect(result).toEqual("(Blob | File | Buffer)[]");
367
+ });
368
+
369
+ it("should return 'Blob | File | Buffer' when serializer is 'FileSerializable' and type is not an array and signature_type is 'return'", () => {
370
+ const type = { type: "any", description: "param description" };
371
+ const component = "Component";
372
+ const serializer = "FileSerializable";
373
+ const signature_type = "return";
374
+ const result = get_type(type, component, serializer, signature_type);
375
+ expect(result).toEqual(
376
+ "{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}"
377
+ );
378
+ });
379
+
380
+ it("should return a FileData object when serializer is 'FileSerializable' and type is not an array and signature_type is 'return'", () => {
381
+ const type = { type: "any", description: "param description" };
382
+ const component = "Component";
383
+ const serializer = "FileSerializable";
384
+ const signature_type = "return";
385
+ const result = get_type(type, component, serializer, signature_type);
386
+ expect(result).toEqual(
387
+ "{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}"
388
+ );
389
+ });
390
+
391
+ it("should return '[(Blob | File | Buffer), (string | null)][]' when serializer is 'GallerySerializable' and signature_type is 'parameter'", () => {
392
+ const type = { type: "any", description: "param description" };
393
+ const component = "Component";
394
+ const serializer = "GallerySerializable";
395
+ const signature_type = "parameter";
396
+ const result = get_type(type, component, serializer, signature_type);
397
+ expect(result).toEqual("[(Blob | File | Buffer), (string | null)][]");
398
+ });
399
+
400
+ it("should return a FileData object when serializer is 'GallerySerializable' and signature_type is 'return'", () => {
401
+ const type = { type: "any", description: "param description" };
402
+ const component = "Component";
403
+ const serializer = "GallerySerializable";
404
+ const signature_type = "return";
405
+ const result = get_type(type, component, serializer, signature_type);
406
+ expect(result).toEqual(
407
+ "[{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}, (string | null))][]"
408
+ );
409
+ });
410
+ });
411
+
412
+ describe("process_endpoint", () => {
413
+ it("should return space_id, host, ws_protocol, and http_protocol when app_reference is a valid space name", async () => {
414
+ const app_reference = "hmb/hello_world";
415
+ const host = "hmb-hello-world.hf.space";
416
+
417
+ const hf_token = "hf_token";
418
+ const expected = {
419
+ space_id: app_reference,
420
+ host,
421
+ ws_protocol: "wss",
422
+ http_protocol: "https:"
423
+ };
424
+
425
+ const result = await process_endpoint(app_reference, hf_token);
426
+ expect(result).toEqual(expected);
427
+ });
428
+
429
+ it("should throw an error when fetching space metadata fails", async () => {
430
+ const app_reference = "hmb/bye_world";
431
+ const hf_token = "hf_token";
432
+
433
+ try {
434
+ await process_endpoint(app_reference, hf_token);
435
+ } catch (error) {
436
+ expect(error.message).toEqual(
437
+ SPACE_METADATA_ERROR_MSG + "Unexpected end of JSON input"
438
+ );
439
+ }
440
+ });
441
+
442
+ it("should return the correct data when app_reference is a valid space domain", async () => {
443
+ const app_reference = "hmb/hello_world";
444
+ const host = "hmb-hello-world.hf.space";
445
+
446
+ const expected = {
447
+ space_id: app_reference,
448
+ host,
449
+ ws_protocol: "wss",
450
+ http_protocol: "https:"
451
+ };
452
+
453
+ const result = await process_endpoint("hmb/hello_world");
454
+ expect(result).toEqual(expected);
455
+ });
456
+ });