@gradio/client 0.17.0 → 0.19.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 (69) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +10 -6
  3. package/dist/client.d.ts +18 -7
  4. package/dist/client.d.ts.map +1 -1
  5. package/dist/constants.d.ts +8 -2
  6. package/dist/constants.d.ts.map +1 -1
  7. package/dist/helpers/api_info.d.ts +22 -0
  8. package/dist/helpers/api_info.d.ts.map +1 -1
  9. package/dist/helpers/data.d.ts +2 -2
  10. package/dist/helpers/data.d.ts.map +1 -1
  11. package/dist/helpers/init_helpers.d.ts.map +1 -1
  12. package/dist/helpers/spaces.d.ts.map +1 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +275 -183
  16. package/dist/test/handlers.d.ts +3 -0
  17. package/dist/test/handlers.d.ts.map +1 -0
  18. package/dist/test/mock_eventsource.d.ts +2 -0
  19. package/dist/test/mock_eventsource.d.ts.map +1 -0
  20. package/dist/test/server.d.ts +2 -0
  21. package/dist/test/server.d.ts.map +1 -0
  22. package/dist/test/test_data.d.ts +76 -0
  23. package/dist/test/test_data.d.ts.map +1 -0
  24. package/dist/types.d.ts +23 -7
  25. package/dist/types.d.ts.map +1 -1
  26. package/dist/upload.d.ts +2 -2
  27. package/dist/upload.d.ts.map +1 -1
  28. package/dist/utils/duplicate.d.ts.map +1 -1
  29. package/dist/utils/predict.d.ts +1 -1
  30. package/dist/utils/predict.d.ts.map +1 -1
  31. package/dist/utils/stream.d.ts +2 -2
  32. package/dist/utils/stream.d.ts.map +1 -1
  33. package/dist/utils/submit.d.ts +1 -1
  34. package/dist/utils/submit.d.ts.map +1 -1
  35. package/dist/utils/upload_files.d.ts.map +1 -1
  36. package/dist/utils/view_api.d.ts.map +1 -1
  37. package/package.json +8 -2
  38. package/src/client.ts +53 -28
  39. package/src/constants.ts +9 -2
  40. package/src/helpers/api_info.ts +75 -4
  41. package/src/helpers/data.ts +46 -28
  42. package/src/helpers/init_helpers.ts +7 -11
  43. package/src/helpers/spaces.ts +8 -3
  44. package/src/index.ts +1 -1
  45. package/src/test/api_info.test.ts +567 -0
  46. package/src/test/data.test.ts +281 -0
  47. package/src/test/handlers.ts +438 -0
  48. package/src/test/init.test.ts +139 -0
  49. package/src/test/init_helpers.test.ts +94 -0
  50. package/src/test/mock_eventsource.ts +13 -0
  51. package/src/test/post_data.test.ts +45 -0
  52. package/src/test/server.ts +6 -0
  53. package/src/test/spaces.test.ts +145 -0
  54. package/src/test/stream.test.ts +79 -0
  55. package/src/test/test_data.ts +557 -0
  56. package/src/test/upload_files.test.ts +42 -0
  57. package/src/test/view_api.test.ts +53 -0
  58. package/src/types.ts +36 -17
  59. package/src/upload.ts +4 -8
  60. package/src/utils/duplicate.ts +20 -3
  61. package/src/utils/handle_blob.ts +1 -1
  62. package/src/utils/post_data.ts +1 -1
  63. package/src/utils/predict.ts +2 -2
  64. package/src/utils/stream.ts +30 -21
  65. package/src/utils/submit.ts +42 -19
  66. package/src/utils/upload_files.ts +11 -6
  67. package/src/utils/view_api.ts +4 -7
  68. package/vite.config.js +7 -0
  69. package/src/utils/client.node-test.ts +0 -173
@@ -0,0 +1,567 @@
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
+ map_data_to_params
9
+ } from "../helpers/api_info";
10
+ import { initialise_server } from "./server";
11
+ import { transformed_api_info } from "./test_data";
12
+
13
+ const server = initialise_server();
14
+
15
+ beforeAll(() => server.listen());
16
+ afterEach(() => server.resetHandlers());
17
+ afterAll(() => server.close());
18
+
19
+ describe("handle_message", () => {
20
+ it("should return type 'data' when msg is 'send_data'", () => {
21
+ const data = { msg: "send_data" };
22
+ const last_status = "pending";
23
+ const result = handle_message(data, last_status);
24
+ expect(result).toEqual({ type: "data" });
25
+ });
26
+
27
+ it("should return type 'hash' when msg is 'send_hash'", () => {
28
+ const data = { msg: "send_hash" };
29
+ const last_status = "pending";
30
+ const result = handle_message(data, last_status);
31
+ expect(result).toEqual({ type: "hash" });
32
+ });
33
+
34
+ it("should return type 'update' with queue full message when msg is 'queue_full'", () => {
35
+ const data = { msg: "queue_full", code: 500, success: false };
36
+ const last_status = "pending";
37
+ const result = handle_message(data, last_status);
38
+ expect(result).toEqual({
39
+ type: "update",
40
+ status: {
41
+ queue: true,
42
+ message: QUEUE_FULL_MSG,
43
+ stage: "error",
44
+ code: 500,
45
+ success: false
46
+ }
47
+ });
48
+ });
49
+
50
+ it("should return type 'heartbeat' when msg is 'heartbeat'", () => {
51
+ const data = { msg: "heartbeat" };
52
+ const last_status = "pending";
53
+ const result = handle_message(data, last_status);
54
+ expect(result).toEqual({ type: "heartbeat" });
55
+ });
56
+
57
+ it("should return type 'unexpected_error' with error message when msg is 'unexpected_error'", () => {
58
+ const data = { msg: "unexpected_error", message: "Something went wrong" };
59
+ const last_status = "pending";
60
+ const result = handle_message(data, last_status);
61
+ expect(result).toEqual({
62
+ type: "unexpected_error",
63
+ status: {
64
+ queue: true,
65
+ message: "Something went wrong",
66
+ stage: "error",
67
+ success: false
68
+ }
69
+ });
70
+ });
71
+
72
+ it("should return type 'update' with estimation status when msg is 'estimation'", () => {
73
+ const data = {
74
+ msg: "estimation",
75
+ code: 200,
76
+ queue_size: 10,
77
+ rank: 5,
78
+ rank_eta: 60,
79
+ success: true
80
+ };
81
+ const last_status = "pending";
82
+ const result = handle_message(data, last_status);
83
+ expect(result).toEqual({
84
+ type: "update",
85
+ status: {
86
+ queue: true,
87
+ stage: "pending",
88
+ code: 200,
89
+ size: 10,
90
+ position: 5,
91
+ eta: 60,
92
+ success: true
93
+ }
94
+ });
95
+ });
96
+
97
+ it("should return type 'update' with progress status when msg is 'progress'", () => {
98
+ const data = {
99
+ msg: "progress",
100
+ code: 200,
101
+ progress_data: { current: 50, total: 100 },
102
+ success: true
103
+ };
104
+ const last_status = "pending";
105
+ const result = handle_message(data, last_status);
106
+ expect(result).toEqual({
107
+ type: "update",
108
+ status: {
109
+ queue: true,
110
+ stage: "pending",
111
+ code: 200,
112
+ progress_data: { current: 50, total: 100 },
113
+ success: true
114
+ }
115
+ });
116
+ });
117
+
118
+ it("should return type 'log' with the provided data when msg is 'log'", () => {
119
+ const data = { msg: "log", log_data: "Some log message" };
120
+ const last_status = "pending";
121
+ const result = handle_message(data, last_status);
122
+ expect(result).toEqual({
123
+ type: "log",
124
+ data: { msg: "log", log_data: "Some log message" }
125
+ });
126
+ });
127
+
128
+ it("should return type 'generating' with generating status when msg is 'process_generating' and success is true", () => {
129
+ const data = {
130
+ msg: "process_generating",
131
+ success: true,
132
+ code: 200,
133
+ progress_data: { current: 50, total: 100 },
134
+ average_duration: 120,
135
+ output: { result: "Some result" }
136
+ };
137
+ const last_status = "pending";
138
+ const result = handle_message(data, last_status);
139
+ expect(result).toEqual({
140
+ type: "generating",
141
+ status: {
142
+ queue: true,
143
+ message: null,
144
+ stage: "generating",
145
+ code: 200,
146
+ progress_data: { current: 50, total: 100 },
147
+ eta: 120
148
+ },
149
+ data: { result: "Some result" }
150
+ });
151
+ });
152
+
153
+ it("should return type 'update' with error status when msg is 'process_generating' and success is false", () => {
154
+ const data = {
155
+ msg: "process_generating",
156
+ success: false,
157
+ code: 500,
158
+ progress_data: { current: 50, total: 100 },
159
+ average_duration: 120,
160
+ output: { error: "Error" }
161
+ };
162
+ const last_status = "pending";
163
+ const result = handle_message(data, last_status);
164
+
165
+ expect(result).toEqual({
166
+ type: "generating",
167
+ data: null,
168
+ status: {
169
+ eta: 120,
170
+ queue: true,
171
+ message: "Error",
172
+ stage: "error",
173
+ code: 500,
174
+ progress_data: { current: 50, total: 100 }
175
+ }
176
+ });
177
+ });
178
+
179
+ it("should return type 'complete' with success status when msg is 'process_completed' and success is true", () => {
180
+ const data = {
181
+ msg: "process_completed",
182
+ success: true,
183
+ code: 200,
184
+ progress_data: { current: 100, total: 100 },
185
+ output: { result: "Some result" }
186
+ };
187
+ const last_status = "pending";
188
+ const result = handle_message(data, last_status);
189
+ expect(result).toEqual({
190
+ type: "complete",
191
+ status: {
192
+ queue: true,
193
+ message: undefined,
194
+ stage: "complete",
195
+ code: 200,
196
+ progress_data: { current: 100, total: 100 }
197
+ },
198
+ data: { result: "Some result" }
199
+ });
200
+ });
201
+
202
+ it("should return type 'update' with error status when msg is 'process_completed' and success is false", () => {
203
+ const data = {
204
+ msg: "process_completed",
205
+ success: false,
206
+ code: 500,
207
+ progress_data: { current: 100, total: 100 },
208
+ output: { error: "Some error message" }
209
+ };
210
+ const last_status = "pending";
211
+ const result = handle_message(data, last_status);
212
+ expect(result).toEqual({
213
+ type: "update",
214
+ status: {
215
+ queue: true,
216
+ message: "Some error message",
217
+ stage: "error",
218
+ code: 500,
219
+ success: false
220
+ }
221
+ });
222
+ });
223
+
224
+ it("should return type 'update' with pending status when msg is 'process_starts'", () => {
225
+ const data = {
226
+ msg: "process_starts",
227
+ code: 200,
228
+ rank: 5,
229
+ success: true,
230
+ eta: 60
231
+ };
232
+ const last_status = "pending";
233
+ const result = handle_message(data, last_status);
234
+ expect(result).toEqual({
235
+ type: "update",
236
+ status: {
237
+ queue: true,
238
+ stage: "pending",
239
+ code: 200,
240
+ size: 5,
241
+ position: 0,
242
+ success: true,
243
+ eta: 60
244
+ }
245
+ });
246
+ });
247
+
248
+ it("should return type 'none' with error status when msg is unknown", () => {
249
+ const data = { msg: "unknown" };
250
+ const last_status = "pending";
251
+ const result = handle_message(data, last_status);
252
+ expect(result).toEqual({
253
+ type: "none",
254
+ status: { stage: "error", queue: true }
255
+ });
256
+ });
257
+ });
258
+
259
+ describe("get_description", () => {
260
+ it("should return 'array of [file, label] tuples' when serializer is 'GallerySerializable'", () => {
261
+ const type = { type: "string", description: "param description" };
262
+ const serializer = "GallerySerializable";
263
+ const result = get_description(type, serializer);
264
+ expect(result).toEqual("array of [file, label] tuples");
265
+ });
266
+
267
+ it("should return 'array of strings' when serializer is 'ListStringSerializable'", () => {
268
+ const type = { type: "string", description: "param description" };
269
+ const serializer = "ListStringSerializable";
270
+ const result = get_description(type, serializer);
271
+ expect(result).toEqual("array of strings");
272
+ });
273
+
274
+ it("should return 'array of files or single file' when serializer is 'FileSerializable'", () => {
275
+ const type = { type: "string", description: "param description" };
276
+ const serializer = "FileSerializable";
277
+ const result = get_description(type, serializer);
278
+ expect(result).toEqual("array of files or single file");
279
+ });
280
+
281
+ it("should return the type's description when serializer is not 'GallerySerializable', 'ListStringSerializable', or 'FileSerializable'", () => {
282
+ const type = { type: "string", description: "param description" };
283
+ const serializer = "SomeOtherSerializer";
284
+ const result = get_description(type, serializer);
285
+ expect(result).toEqual(type.description);
286
+ });
287
+ });
288
+
289
+ describe("get_type", () => {
290
+ it("should return 'string' when type is 'string'", () => {
291
+ const type = { type: "string", description: "param description" };
292
+ const component = "Component";
293
+ const serializer = "Serializer";
294
+ const signature_type = "parameter";
295
+ const result = get_type(type, component, serializer, signature_type);
296
+ expect(result).toEqual("string");
297
+ });
298
+
299
+ it("should return 'boolean' when type is 'boolean'", () => {
300
+ const type = { type: "boolean", description: "param description" };
301
+ const component = "Component";
302
+ const serializer = "Serializer";
303
+ const signature_type = "parameter";
304
+ const result = get_type(type, component, serializer, signature_type);
305
+ expect(result).toEqual("boolean");
306
+ });
307
+
308
+ it("should return 'number' when type is 'number'", () => {
309
+ const type = { type: "number", description: "param description" };
310
+ const component = "Component";
311
+ const serializer = "Serializer";
312
+ const signature_type = "parameter";
313
+ const result = get_type(type, component, serializer, signature_type);
314
+ expect(result).toEqual("number");
315
+ });
316
+
317
+ it("should return 'any' when serializer is 'JSONSerializable'", () => {
318
+ const type = { type: "any", description: "param description" };
319
+ const component = "Component";
320
+ const serializer = "JSONSerializable";
321
+ const signature_type = "parameter";
322
+ const result = get_type(type, component, serializer, signature_type);
323
+ expect(result).toEqual("any");
324
+ });
325
+
326
+ it("should return 'any' when serializer is 'StringSerializable'", () => {
327
+ const type = { type: "any", description: "param description" };
328
+ const component = "Component";
329
+ const serializer = "StringSerializable";
330
+ const signature_type = "parameter";
331
+ const result = get_type(type, component, serializer, signature_type);
332
+ expect(result).toEqual("any");
333
+ });
334
+
335
+ it("should return 'string[]' when serializer is 'ListStringSerializable'", () => {
336
+ const type = { type: "any", description: "param description" };
337
+ const component = "Component";
338
+ const serializer = "ListStringSerializable";
339
+ const signature_type = "parameter";
340
+ const result = get_type(type, component, serializer, signature_type);
341
+ expect(result).toEqual("string[]");
342
+ });
343
+
344
+ it("should return 'Blob | File | Buffer' when component is 'Image' and signature_type is 'parameter'", () => {
345
+ const type = { type: "any", description: "param description" };
346
+ const component = "Image";
347
+ const serializer = "Serializer";
348
+ const signature_type = "parameter";
349
+ const result = get_type(type, component, serializer, signature_type);
350
+ expect(result).toEqual("Blob | File | Buffer");
351
+ });
352
+
353
+ it("should return 'string' when component is 'Image' and signature_type is 'return'", () => {
354
+ const type = { type: "string", description: "param description" };
355
+ const component = "Image";
356
+ const serializer = "Serializer";
357
+ const signature_type = "return";
358
+ const result = get_type(type, component, serializer, signature_type);
359
+ expect(result).toEqual("string");
360
+ });
361
+
362
+ it("should return '(Blob | File | Buffer)[]' when serializer is 'FileSerializable' and type is an array and signature_type is 'parameter'", () => {
363
+ const type = { type: "array", description: "param description" };
364
+ const component = "Component";
365
+ const serializer = "FileSerializable";
366
+ const signature_type = "parameter";
367
+ const result = get_type(type, component, serializer, signature_type);
368
+ expect(result).toEqual("(Blob | File | Buffer)[]");
369
+ });
370
+
371
+ it("should return 'Blob | File | Buffer' when serializer is 'FileSerializable' and type is not an array and signature_type is 'return'", () => {
372
+ const type = { type: "any", description: "param description" };
373
+ const component = "Component";
374
+ const serializer = "FileSerializable";
375
+ const signature_type = "return";
376
+ const result = get_type(type, component, serializer, signature_type);
377
+ expect(result).toEqual(
378
+ "{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}"
379
+ );
380
+ });
381
+
382
+ it("should return a FileData object when serializer is 'FileSerializable' and type is not an array and signature_type is 'return'", () => {
383
+ const type = { type: "any", description: "param description" };
384
+ const component = "Component";
385
+ const serializer = "FileSerializable";
386
+ const signature_type = "return";
387
+ const result = get_type(type, component, serializer, signature_type);
388
+ expect(result).toEqual(
389
+ "{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}"
390
+ );
391
+ });
392
+
393
+ it("should return '[(Blob | File | Buffer), (string | null)][]' when serializer is 'GallerySerializable' and signature_type is 'parameter'", () => {
394
+ const type = { type: "any", description: "param description" };
395
+ const component = "Component";
396
+ const serializer = "GallerySerializable";
397
+ const signature_type = "parameter";
398
+ const result = get_type(type, component, serializer, signature_type);
399
+ expect(result).toEqual("[(Blob | File | Buffer), (string | null)][]");
400
+ });
401
+
402
+ it("should return a FileData object when serializer is 'GallerySerializable' and signature_type is 'return'", () => {
403
+ const type = { type: "any", description: "param description" };
404
+ const component = "Component";
405
+ const serializer = "GallerySerializable";
406
+ const signature_type = "return";
407
+ const result = get_type(type, component, serializer, signature_type);
408
+ expect(result).toEqual(
409
+ "[{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}, (string | null))][]"
410
+ );
411
+ });
412
+ });
413
+
414
+ describe("process_endpoint", () => {
415
+ it("should return space_id, host, ws_protocol, and http_protocol when app_reference is a valid space name", async () => {
416
+ const app_reference = "hmb/hello_world";
417
+ const host = "hmb-hello-world.hf.space";
418
+
419
+ const hf_token = "hf_token";
420
+ const expected = {
421
+ space_id: app_reference,
422
+ host,
423
+ ws_protocol: "wss",
424
+ http_protocol: "https:"
425
+ };
426
+
427
+ const result = await process_endpoint(app_reference, hf_token);
428
+ expect(result).toEqual(expected);
429
+ });
430
+
431
+ it("should throw an error when fetching space metadata fails", async () => {
432
+ const app_reference = "hmb/bye_world";
433
+ const hf_token = "hf_token";
434
+
435
+ try {
436
+ await process_endpoint(app_reference, hf_token);
437
+ } catch (error) {
438
+ expect(error.message).toEqual(
439
+ SPACE_METADATA_ERROR_MSG + "Unexpected end of JSON input"
440
+ );
441
+ }
442
+ });
443
+
444
+ it("should return the correct data when app_reference is a valid space domain", async () => {
445
+ const app_reference = "hmb/hello_world";
446
+ const host = "hmb-hello-world.hf.space";
447
+
448
+ const expected = {
449
+ space_id: app_reference,
450
+ host,
451
+ ws_protocol: "wss",
452
+ http_protocol: "https:"
453
+ };
454
+
455
+ const result = await process_endpoint("hmb/hello_world");
456
+ expect(result).toEqual(expected);
457
+ });
458
+ });
459
+
460
+ describe("map_data_params", () => {
461
+ let test_data = transformed_api_info;
462
+
463
+ test_data.named_endpoints["/predict"].parameters = [
464
+ {
465
+ parameter_name: "param1",
466
+ parameter_has_default: false,
467
+ label: "",
468
+ component: "",
469
+ serializer: "",
470
+ python_type: {
471
+ type: "",
472
+ description: ""
473
+ },
474
+ type: {
475
+ type: "",
476
+ description: ""
477
+ }
478
+ },
479
+ {
480
+ parameter_name: "param2",
481
+ parameter_has_default: false,
482
+ label: "",
483
+ type: {
484
+ type: "",
485
+ description: ""
486
+ },
487
+ component: "",
488
+ serializer: "",
489
+ python_type: {
490
+ type: "",
491
+ description: ""
492
+ }
493
+ },
494
+ {
495
+ parameter_name: "param3",
496
+ parameter_has_default: true,
497
+ parameter_default: 3,
498
+ label: "",
499
+ type: {
500
+ type: "",
501
+ description: ""
502
+ },
503
+ component: "",
504
+ serializer: "",
505
+ python_type: {
506
+ type: "",
507
+ description: ""
508
+ }
509
+ }
510
+ ];
511
+
512
+ it("should return an array of data when data is an array", () => {
513
+ const data = [1, 2];
514
+
515
+ const result = map_data_to_params(data, transformed_api_info);
516
+ expect(result).toEqual(data);
517
+ });
518
+
519
+ it("should return the data when too many arguments are provided for the endpoint", () => {
520
+ const data = [1, 2, 3, 4];
521
+
522
+ const result = map_data_to_params(data, transformed_api_info);
523
+ expect(result).toEqual(data);
524
+ });
525
+
526
+ it("should return an array of resolved data when data is an object", () => {
527
+ const data = {
528
+ param1: 1,
529
+ param2: 2,
530
+ param3: 3
531
+ };
532
+
533
+ const result = map_data_to_params(data, transformed_api_info);
534
+ expect(result).toEqual([1, 2, 3]);
535
+ });
536
+
537
+ it("should use the default value when a keyword argument is not provided and has a default value", () => {
538
+ const data = {
539
+ param1: 1,
540
+ param2: 2
541
+ };
542
+
543
+ const result = map_data_to_params(data, transformed_api_info);
544
+ expect(result).toEqual([1, 2, 3]);
545
+ });
546
+
547
+ it("should throw an error when an invalid keyword argument is provided", () => {
548
+ const data = {
549
+ param1: 1,
550
+ param2: 2,
551
+ param3: 3,
552
+ param4: 4
553
+ };
554
+
555
+ expect(() => map_data_to_params(data, transformed_api_info)).toThrowError(
556
+ "Parameter `param4` is not a valid keyword argument. Please refer to the API for usage."
557
+ );
558
+ });
559
+
560
+ it("should throw an error when no value is provided for a required parameter", () => {
561
+ const data = {};
562
+
563
+ expect(() => map_data_to_params(data, transformed_api_info)).toThrowError(
564
+ "No value provided for required parameter: param1"
565
+ );
566
+ });
567
+ });