@gradio/client 0.1.4 → 0.2.1

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # @gradio/client
2
2
 
3
+ ## 0.2.1
4
+
5
+ ### Features
6
+
7
+ - [#5173](https://github.com/gradio-app/gradio/pull/5173) [`730f0c1d`](https://github.com/gradio-app/gradio/commit/730f0c1d54792eb11359e40c9f2326e8a6e39203) - Ensure gradio client works as expected for functions that return nothing. Thanks [@raymondtri](https://github.com/raymondtri)!
8
+
9
+ ## 0.2.0
10
+
11
+ ### Features
12
+
13
+ - [#5133](https://github.com/gradio-app/gradio/pull/5133) [`61129052`](https://github.com/gradio-app/gradio/commit/61129052ed1391a75c825c891d57fa0ad6c09fc8) - Update dependency esbuild to ^0.19.0. Thanks [@renovate](https://github.com/apps/renovate)!
14
+ - [#5035](https://github.com/gradio-app/gradio/pull/5035) [`8b4eb8ca`](https://github.com/gradio-app/gradio/commit/8b4eb8cac9ea07bde31b44e2006ca2b7b5f4de36) - JS Client: Fixes cannot read properties of null (reading 'is_file'). Thanks [@raymondtri](https://github.com/raymondtri)!
15
+
16
+ ### Fixes
17
+
18
+ - [#5075](https://github.com/gradio-app/gradio/pull/5075) [`67265a58`](https://github.com/gradio-app/gradio/commit/67265a58027ef1f9e4c0eb849a532f72eaebde48) - Allow supporting >1000 files in `gr.File()` and `gr.UploadButton()`. Thanks [@abidlabs](https://github.com/abidlabs)!
19
+
3
20
  ## 0.1.4
4
21
 
5
22
  ### Patch Changes
@@ -40,4 +57,4 @@
40
57
 
41
58
  - [#3692](https://github.com/gradio-app/gradio/pull/3692) [`48e8b113`](https://github.com/gradio-app/gradio/commit/48e8b113f4b55e461d9da4f153bf72aeb4adf0f1) Thanks [@pngwn](https://github.com/pngwn)! - Ensure client works in node, create ESM bundle and generate typescript declaration files.
42
59
 
43
- - [#3605](https://github.com/gradio-app/gradio/pull/3605) [`ae4277a9`](https://github.com/gradio-app/gradio/commit/ae4277a9a83d49bdadfe523b0739ba988128e73b) Thanks [@pngwn](https://github.com/pngwn)! - Update readme.
60
+ - [#3605](https://github.com/gradio-app/gradio/pull/3605) [`ae4277a9`](https://github.com/gradio-app/gradio/commit/ae4277a9a83d49bdadfe523b0739ba988128e73b) Thanks [@pngwn](https://github.com/pngwn)! - Update readme.
package/README.md CHANGED
@@ -65,8 +65,8 @@ Applications hosted on Hugging Face spaces can be in a number of different state
65
65
  import { client, type SpaceStatus } from "@gradio/client";
66
66
 
67
67
  const app = await client("user/space-name", {
68
- // The space_status parameter does not need to be manually annotated, this is just for illustration.
69
- space_status: (space_status: SpaceStatus) => console.log(space_status),
68
+ // The space_status parameter does not need to be manually annotated, this is just for illustration.
69
+ space_status: (space_status: SpaceStatus) => console.log(space_status)
70
70
  });
71
71
  ```
72
72
 
@@ -157,22 +157,22 @@ The status payload look like this:
157
157
 
158
158
  ```ts
159
159
  interface Status {
160
- queue: boolean;
161
- code?: string;
162
- success?: boolean;
163
- stage: "pending" | "error" | "complete" | "generating";
164
- size?: number;
165
- position?: number;
166
- eta?: number;
167
- message?: string;
168
- progress_data?: Array<{
169
- progress: number | null;
170
- index: number | null;
171
- length: number | null;
172
- unit: string | null;
173
- desc: string | null;
174
- }>;
175
- time?: Date;
160
+ queue: boolean;
161
+ code?: string;
162
+ success?: boolean;
163
+ stage: "pending" | "error" | "complete" | "generating";
164
+ size?: number;
165
+ position?: number;
166
+ eta?: number;
167
+ message?: string;
168
+ progress_data?: Array<{
169
+ progress: number | null;
170
+ index: number | null;
171
+ length: number | null;
172
+ unit: string | null;
173
+ desc: string | null;
174
+ }>;
175
+ time?: Date;
176
176
  }
177
177
  ```
178
178
 
@@ -183,9 +183,9 @@ import { client } from "@gradio/client";
183
183
 
184
184
  const app = await client("user/space-name");
185
185
  const submission = app
186
- .submit("/predict", payload)
187
- .on("data", (data) => console.log(data))
188
- .on("status", (status: Status) => console.log(status));
186
+ .submit("/predict", payload)
187
+ .on("data", (data) => console.log(data))
188
+ .on("status", (status: Status) => console.log(status));
189
189
  ```
190
190
 
191
191
  ##### `off`
@@ -229,8 +229,8 @@ import { client } from "@gradio/client";
229
229
 
230
230
  const app = await client("user/space-name");
231
231
  const submission = app
232
- .submit("/predict", payload)
233
- .on("data", (data) => console.log(data));
232
+ .submit("/predict", payload)
233
+ .on("data", (data) => console.log(data));
234
234
 
235
235
  // later
236
236
 
@@ -271,7 +271,7 @@ The duplicate function will attempt to duplicate the space that is referenced an
271
271
  import { duplicate } from "@gradio/client";
272
272
 
273
273
  const app = await duplicate("user/space-name", {
274
- hf_token: "hf_...",
274
+ hf_token: "hf_..."
275
275
  });
276
276
  ```
277
277
 
@@ -295,8 +295,8 @@ This is an optional property specific to `duplicate`'s options object and will d
295
295
  import { duplicate } from "@gradio/client";
296
296
 
297
297
  const app = await duplicate("user/space-name", {
298
- hf_token: "hf_...",
299
- private: true,
298
+ hf_token: "hf_...",
299
+ private: true
300
300
  });
301
301
  ```
302
302
 
@@ -308,9 +308,9 @@ This is an optional property specific to `duplicate`'s options object and will s
308
308
  import { duplicate } from "@gradio/client";
309
309
 
310
310
  const app = await duplicate("user/space-name", {
311
- hf_token: "hf_...",
312
- private: true,
313
- timeout: 5,
311
+ hf_token: "hf_...",
312
+ private: true,
313
+ timeout: 5
314
314
  });
315
315
  ```
316
316
 
@@ -332,8 +332,8 @@ Possible hardware options are:
332
332
  import { duplicate } from "@gradio/client";
333
333
 
334
334
  const app = await duplicate("user/space-name", {
335
- hf_token: "hf_...",
336
- private: true,
337
- hardware: "a10g-small",
335
+ hf_token: "hf_...",
336
+ private: true,
337
+ hardware: "a10g-small"
338
338
  });
339
339
  ```
package/dist/client.d.ts CHANGED
@@ -20,27 +20,25 @@ export declare function duplicate(app_reference: string, options: {
20
20
  hf_token: `hf_${string}`;
21
21
  private?: boolean;
22
22
  status_callback: SpaceStatusCallback;
23
- hardware?: typeof hardware_types[number];
23
+ hardware?: (typeof hardware_types)[number];
24
24
  timeout?: number;
25
25
  }): Promise<client_return>;
26
- /**
27
- * We need to inject a customized fetch implementation for the Wasm version.
28
- */
29
- export declare function api_factory(fetch_implementation: typeof fetch): {
26
+ interface Client {
30
27
  post_data: (url: string, body: unknown, token?: `hf_${string}`) => Promise<[PostResponse, number]>;
31
- upload_files: (root: string, files: Array<File>, token?: `hf_${string}`) => Promise<UploadResponse>;
32
- client: (app_reference: string, options?: {
28
+ upload_files: (root: string, files: File[], token?: `hf_${string}`) => Promise<UploadResponse>;
29
+ client: (app_reference: string, options: {
33
30
  hf_token?: `hf_${string}`;
34
31
  status_callback?: SpaceStatusCallback;
35
32
  normalise_files?: boolean;
36
33
  }) => Promise<client_return>;
37
- handle_blob: (endpoint: string, data: unknown[], api_info: any, token?: `hf_${string}`) => Promise<unknown[]>;
38
- };
39
- export declare const post_data: (url: string, body: unknown, token?: `hf_${string}`) => Promise<[PostResponse, number]>, upload_files: (root: string, files: Array<File>, token?: `hf_${string}`) => Promise<UploadResponse>, client: (app_reference: string, options?: {
34
+ handle_blob: (endpoint: string, data: unknown[], api_info: ApiInfo<JsApiData>, token?: `hf_${string}`) => Promise<unknown[]>;
35
+ }
36
+ export declare function api_factory(fetch_implementation: typeof fetch): Client;
37
+ export declare const post_data: (url: string, body: unknown, token?: `hf_${string}`) => Promise<[PostResponse, number]>, upload_files: (root: string, files: File[], token?: `hf_${string}`) => Promise<UploadResponse>, client: (app_reference: string, options: {
40
38
  hf_token?: `hf_${string}`;
41
39
  status_callback?: SpaceStatusCallback;
42
40
  normalise_files?: boolean;
43
- }) => Promise<client_return>, handle_blob: (endpoint: string, data: unknown[], api_info: any, token?: `hf_${string}`) => Promise<unknown[]>;
41
+ }) => Promise<client_return>, handle_blob: (endpoint: string, data: unknown[], api_info: ApiInfo<JsApiData>, token?: `hf_${string}`) => Promise<unknown[]>;
44
42
  interface ApiData {
45
43
  label: string;
46
44
  type: {
@@ -68,6 +66,11 @@ interface ApiInfo<T extends ApiData | JsApiData> {
68
66
  [key: string]: EndpointInfo<T>;
69
67
  };
70
68
  }
71
- export declare function walk_and_store_blobs(param: any, type?: any, path?: any[], root?: boolean, api_info?: any): Promise<any[]>;
69
+ export declare function walk_and_store_blobs(param: any, type?: any, path?: any[], root?: boolean, api_info?: any): Promise<{
70
+ path: string[];
71
+ data: string | false;
72
+ type: string;
73
+ blob: Blob | false;
74
+ }[]>;
72
75
  export {};
73
76
  //# sourceMappingURL=client.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,EAQN,cAAc,EACd,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EACX,SAAS,EACT,aAAa,EAIb,YAAY,EACZ,cAAc,EAGd,mBAAmB,EAEnB,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,KAAK,KAAK,GAAG,CAAC,CAAC,SAAS,SAAS,EAChC,SAAS,EAAE,CAAC,EACZ,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,KACtB,YAAY,CAAC;AAClB,KAAK,OAAO,GAAG,CACd,QAAQ,EAAE,MAAM,GAAG,MAAM,EACzB,IAAI,CAAC,EAAE,OAAO,EAAE,EAChB,UAAU,CAAC,EAAE,OAAO,KAChB,OAAO,CAAC,OAAO,CAAC,CAAC;AAEtB,KAAK,aAAa,GAAG;IACpB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,CACP,QAAQ,EAAE,MAAM,GAAG,MAAM,EACzB,IAAI,CAAC,EAAE,OAAO,EAAE,EAChB,UAAU,CAAC,EAAE,OAAO,KAChB,YAAY,CAAC;IAClB,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC;CACtD,CAAC;AAEF,KAAK,YAAY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AG,KAAK,CAAC;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,IAAI,GAAG,KAAK,CAAC;CACnB,EAAE,CACH,CAsEA"}
package/dist/index.js CHANGED
@@ -13,13 +13,12 @@ function determine_protocol(endpoint) {
13
13
  host,
14
14
  http_protocol: protocol
15
15
  };
16
- } else {
17
- return {
18
- ws_protocol: protocol === "https:" ? "wss" : "ws",
19
- http_protocol: protocol,
20
- host
21
- };
22
16
  }
17
+ return {
18
+ ws_protocol: protocol === "https:" ? "wss" : "ws",
19
+ http_protocol: protocol,
20
+ host
21
+ };
23
22
  }
24
23
  return {
25
24
  ws_protocol: "wss",
@@ -86,8 +85,7 @@ async function discussions_enabled(space_id) {
86
85
  const error = r.headers.get("x-error-message");
87
86
  if (error && RE_DISABLED_DISCUSSION.test(error))
88
87
  return false;
89
- else
90
- return true;
88
+ return true;
91
89
  } catch (e) {
92
90
  return false;
93
91
  }
@@ -193,25 +191,20 @@ async function duplicate(app_reference, options) {
193
191
  );
194
192
  if (response.status === 409) {
195
193
  return client(`${user}/${space_name}`, options);
196
- } else {
197
- const duplicated_space = await response.json();
198
- let original_hardware;
199
- if (!hardware) {
200
- original_hardware = await get_space_hardware(app_reference, hf_token);
201
- }
202
- const requested_hardware = hardware || original_hardware || "cpu-basic";
203
- await set_space_hardware(
204
- `${user}/${space_name}`,
205
- requested_hardware,
206
- hf_token
207
- );
208
- await set_space_timeout(
209
- `${user}/${space_name}`,
210
- timeout || 300,
211
- hf_token
212
- );
213
- return client(duplicated_space.url, options);
214
194
  }
195
+ const duplicated_space = await response.json();
196
+ let original_hardware;
197
+ if (!hardware) {
198
+ original_hardware = await get_space_hardware(app_reference, hf_token);
199
+ }
200
+ const requested_hardware = hardware || original_hardware || "cpu-basic";
201
+ await set_space_hardware(
202
+ `${user}/${space_name}`,
203
+ requested_hardware,
204
+ hf_token
205
+ );
206
+ await set_space_timeout(`${user}/${space_name}`, timeout || 300, hf_token);
207
+ return client(duplicated_space.url, options);
215
208
  } catch (e) {
216
209
  throw new Error(e);
217
210
  }
@@ -240,21 +233,27 @@ function api_factory(fetch_implementation) {
240
233
  if (token) {
241
234
  headers.Authorization = `Bearer ${token}`;
242
235
  }
243
- const formData = new FormData();
244
- files.forEach((file) => {
245
- formData.append("files", file);
246
- });
247
- try {
248
- var response = await fetch_implementation(`${root}/upload`, {
249
- method: "POST",
250
- body: formData,
251
- headers
236
+ const chunkSize = 1e3;
237
+ const uploadResponses = [];
238
+ for (let i = 0; i < files.length; i += chunkSize) {
239
+ const chunk = files.slice(i, i + chunkSize);
240
+ const formData = new FormData();
241
+ chunk.forEach((file) => {
242
+ formData.append("files", file);
252
243
  });
253
- } catch (e) {
254
- return { error: BROKEN_CONNECTION_MSG };
244
+ try {
245
+ var response = await fetch_implementation(`${root}/upload`, {
246
+ method: "POST",
247
+ body: formData,
248
+ headers
249
+ });
250
+ } catch (e) {
251
+ return { error: BROKEN_CONNECTION_MSG };
252
+ }
253
+ const output = await response.json();
254
+ uploadResponses.push(...output);
255
255
  }
256
- const output = await response.json();
257
- return { files: output };
256
+ return { files: uploadResponses };
258
257
  }
259
258
  async function client2(app_reference, options = { normalise_files: true }) {
260
259
  return new Promise(async (res) => {
@@ -347,22 +346,37 @@ function api_factory(fetch_implementation) {
347
346
  function predict(endpoint, data, event_data) {
348
347
  let data_returned = false;
349
348
  let status_complete = false;
349
+ let dependency;
350
+ if (typeof endpoint === "number") {
351
+ dependency = config.dependencies[endpoint];
352
+ } else {
353
+ const trimmed_endpoint = endpoint.replace(/^\//, "");
354
+ dependency = config.dependencies[api_map[trimmed_endpoint]];
355
+ }
356
+ if (dependency.types.continuous) {
357
+ throw new Error(
358
+ "Cannot call predict on this function as it may run forever. Use submit instead"
359
+ );
360
+ }
350
361
  return new Promise((res2, rej) => {
351
362
  const app = submit(endpoint, data, event_data);
363
+ let result;
352
364
  app.on("data", (d) => {
353
- data_returned = true;
354
365
  if (status_complete) {
355
366
  app.destroy();
367
+ res2(d);
356
368
  }
357
- res2(d);
369
+ data_returned = true;
370
+ result = d;
358
371
  }).on("status", (status) => {
359
372
  if (status.stage === "error")
360
373
  rej(status);
361
- if (status.stage === "complete" && data_returned) {
362
- app.destroy();
363
- }
364
374
  if (status.stage === "complete") {
365
375
  status_complete = true;
376
+ app.destroy();
377
+ if (data_returned) {
378
+ res2(result);
379
+ }
366
380
  }
367
381
  });
368
382
  });
@@ -477,6 +491,7 @@ function api_factory(fetch_implementation) {
477
491
  fire_event({
478
492
  type: "status",
479
493
  stage: "error",
494
+ broken: true,
480
495
  message: BROKEN_CONNECTION_MSG,
481
496
  queue: true,
482
497
  endpoint: _endpoint,
@@ -509,6 +524,14 @@ function api_factory(fetch_implementation) {
509
524
  websocket.send(JSON.stringify({ ...payload, session_hash }));
510
525
  } else if (type === "complete") {
511
526
  complete = status;
527
+ } else if (type === "log") {
528
+ fire_event({
529
+ type: "log",
530
+ log: data2.log,
531
+ level: data2.level,
532
+ endpoint: _endpoint,
533
+ fn_index
534
+ });
512
535
  } else if (type === "generating") {
513
536
  fire_event({
514
537
  type: "status",
@@ -676,9 +699,8 @@ function api_factory(fetch_implementation) {
676
699
  if (blob) {
677
700
  const file_url = (await upload_files2(endpoint, [blob], token)).files[0];
678
701
  return { path, file_url, type };
679
- } else {
680
- return { path, base64: data2, type };
681
702
  }
703
+ return { path, base64: data2, type };
682
704
  })
683
705
  ).then((r) => {
684
706
  r.forEach(({ path, file_url, base64, type }) => {
@@ -704,17 +726,16 @@ const { post_data, upload_files, client, handle_blob } = api_factory(fetch);
704
726
  function transform_output(data, api_info, root_url, remote_url) {
705
727
  return data.map((d, i) => {
706
728
  var _a, _b, _c, _d;
707
- if (((_b = (_a = api_info.returns) == null ? void 0 : _a[i]) == null ? void 0 : _b.component) === "File") {
729
+ if (((_b = (_a = api_info == null ? void 0 : api_info.returns) == null ? void 0 : _a[i]) == null ? void 0 : _b.component) === "File") {
708
730
  return normalise_file(d, root_url, remote_url);
709
- } else if (((_d = (_c = api_info.returns) == null ? void 0 : _c[i]) == null ? void 0 : _d.component) === "Gallery") {
731
+ } else if (((_d = (_c = api_info == null ? void 0 : api_info.returns) == null ? void 0 : _c[i]) == null ? void 0 : _d.component) === "Gallery") {
710
732
  return d.map((img) => {
711
733
  return Array.isArray(img) ? [normalise_file(img[0], root_url, remote_url), img[1]] : [normalise_file(img, root_url, remote_url), null];
712
734
  });
713
- } else if (typeof d === "object" && d.is_file) {
735
+ } else if (typeof d === "object" && (d == null ? void 0 : d.is_file)) {
714
736
  return normalise_file(d, root_url, remote_url);
715
- } else {
716
- return d;
717
737
  }
738
+ return d;
718
739
  });
719
740
  }
720
741
  function normalise_file(file, root, root_url) {
@@ -762,9 +783,8 @@ function get_type(type, component, serializer, signature_type) {
762
783
  } else if (serializer === "FileSerializable") {
763
784
  if ((type == null ? void 0 : type.type) === "array") {
764
785
  return signature_type === "parameter" ? "(Blob | File | Buffer)[]" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}[]`;
765
- } else {
766
- return signature_type === "parameter" ? "Blob | File | Buffer" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}`;
767
786
  }
787
+ return signature_type === "parameter" ? "Blob | File | Buffer" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}`;
768
788
  } else if (serializer === "GallerySerializable") {
769
789
  return signature_type === "parameter" ? "[(Blob | File | Buffer), (string | null)][]" : `[{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}, (string | null))][]`;
770
790
  }
@@ -776,9 +796,8 @@ function get_description(type, serializer) {
776
796
  return "array of strings";
777
797
  } else if (serializer === "FileSerializable") {
778
798
  return "array of files or single file";
779
- } else {
780
- return type.description;
781
799
  }
800
+ return type.description;
782
801
  }
783
802
  function transform_api_info(api_info, config, api_map) {
784
803
  const new_data = {
@@ -872,10 +891,9 @@ async function walk_and_store_blobs(param, type = void 0, path = [], root = fals
872
891
  const buffer = await param.arrayBuffer();
873
892
  data = Buffer.from(buffer).toString("base64");
874
893
  }
875
- return [{ path, data, type }];
876
- } else {
877
- return [{ path, blob: param, type }];
894
+ return [{ path, data, type, blob: false }];
878
895
  }
896
+ return [{ path, blob: param, type, data: false }];
879
897
  } else if (typeof param === "object") {
880
898
  let blob_refs = [];
881
899
  for (let key in param) {
@@ -894,9 +912,8 @@ async function walk_and_store_blobs(param, type = void 0, path = [], root = fals
894
912
  }
895
913
  }
896
914
  return blob_refs;
897
- } else {
898
- return [];
899
915
  }
916
+ return [];
900
917
  }
901
918
  function image_to_data_uri(blob) {
902
919
  return new Promise((resolve, _) => {
@@ -928,9 +945,8 @@ async function resolve_config(fetch_implementation, endpoint, token) {
928
945
  config.path = config.path ?? "";
929
946
  config.root = endpoint;
930
947
  return config;
931
- } else {
932
- throw new Error("Could not get config.");
933
948
  }
949
+ throw new Error("Could not get config.");
934
950
  }
935
951
  throw new Error("No config or app endpoint found");
936
952
  }
@@ -1055,6 +1071,8 @@ function handle_message(data, last_status) {
1055
1071
  success: data.success
1056
1072
  }
1057
1073
  };
1074
+ case "log":
1075
+ return { type: "log", data };
1058
1076
  case "process_generating":
1059
1077
  return {
1060
1078
  type: "generating",
@@ -1080,20 +1098,19 @@ function handle_message(data, last_status) {
1080
1098
  success: data.success
1081
1099
  }
1082
1100
  };
1083
- } else {
1084
- return {
1085
- type: "complete",
1086
- status: {
1087
- queue,
1088
- message: !data.success ? data.output.error : void 0,
1089
- stage: data.success ? "complete" : "error",
1090
- code: data.code,
1091
- progress_data: data.progress_data,
1092
- eta: data.output.average_duration
1093
- },
1094
- data: data.success ? data.output : null
1095
- };
1096
1101
  }
1102
+ return {
1103
+ type: "complete",
1104
+ status: {
1105
+ queue,
1106
+ message: !data.success ? data.output.error : void 0,
1107
+ stage: data.success ? "complete" : "error",
1108
+ code: data.code,
1109
+ progress_data: data.progress_data,
1110
+ eta: data.output.average_duration
1111
+ },
1112
+ data: data.success ? data.output : null
1113
+ };
1097
1114
  case "process_starts":
1098
1115
  return {
1099
1116
  type: "update",
package/dist/types.d.ts CHANGED
@@ -20,7 +20,7 @@ export interface Config {
20
20
  path: string;
21
21
  }
22
22
  export interface Payload {
23
- data: Array<unknown>;
23
+ data: unknown[];
24
24
  fn_index?: number;
25
25
  event_data?: unknown;
26
26
  time?: Date;
@@ -31,26 +31,31 @@ export interface PostResponse {
31
31
  }
32
32
  export interface UploadResponse {
33
33
  error?: string;
34
- files?: Array<string>;
34
+ files?: string[];
35
35
  }
36
36
  export interface Status {
37
37
  queue: boolean;
38
38
  code?: string;
39
39
  success?: boolean;
40
40
  stage: "pending" | "error" | "complete" | "generating";
41
+ broken?: boolean;
41
42
  size?: number;
42
43
  position?: number;
43
44
  eta?: number;
44
45
  message?: string;
45
- progress_data?: Array<{
46
+ progress_data?: {
46
47
  progress: number | null;
47
48
  index: number | null;
48
49
  length: number | null;
49
50
  unit: string | null;
50
51
  desc: string | null;
51
- }>;
52
+ }[];
52
53
  time?: Date;
53
54
  }
55
+ export interface LogMessage {
56
+ log: string;
57
+ level: "warning" | "info";
58
+ }
54
59
  export interface SpaceStatusNormal {
55
60
  status: "sleeping" | "running" | "building" | "error" | "stopped";
56
61
  detail: "SLEEPING" | "RUNNING" | "RUNNING_BUILDING" | "BUILDING" | "NOT_FOUND";
@@ -67,10 +72,11 @@ export interface SpaceStatusError {
67
72
  export type SpaceStatus = SpaceStatusNormal | SpaceStatusError;
68
73
  export type status_callback_function = (a: Status) => void;
69
74
  export type SpaceStatusCallback = (a: SpaceStatus) => void;
70
- export type EventType = "data" | "status";
75
+ export type EventType = "data" | "status" | "log";
71
76
  export interface EventMap {
72
77
  data: Payload;
73
78
  status: Status;
79
+ log: LogMessage;
74
80
  }
75
81
  export type Event<K extends EventType> = {
76
82
  [P in K]: EventMap[P] & {
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,MAAM;IACtB,aAAa,EAAE,OAAO,GAAG,SAAS,CAAC;IACnC,YAA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;CAChB;AAED,MAAM,MAAM,KAAK,CAAC,CAAC,SAAS,SAAS,IAAI;KACvC,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG;QAAE,IAAI,EAAE,CAAC,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE;CACvE,CAAC,CAAC,CAAC,CAAC;AACL,MAAM,MAAM,aAAa,CAAC,CAAC,SAAS,SAAS,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;AAC3E,MAAM,MAAM,WAAW,CAAC,CAAC,SAAS,SAAS,IAAI;KAC7C,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,aAAa,CAAC,CAAC,CAAC,EAAE;CAC7B,CAAC;AACF,MAAM,WAAW,QAAQ;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,IAAI,CAAC;IACZ,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;CAClB"}
package/dist/utils.d.ts CHANGED
@@ -14,8 +14,8 @@ export declare function process_endpoint(app_reference: string, token?: `hf_${st
14
14
  }>;
15
15
  export declare function map_names_to_ids(fns: Config["dependencies"]): Record<string, number>;
16
16
  export declare function discussions_enabled(space_id: string): Promise<boolean>;
17
- export declare function get_space_hardware(space_id: string, token: `hf_${string}`): Promise<any>;
18
- export declare function set_space_hardware(space_id: string, new_hardware: typeof hardware_types[number], token: `hf_${string}`): Promise<any>;
19
- export declare function set_space_timeout(space_id: string, timeout: number, token: `hf_${string}`): Promise<any>;
17
+ export declare function get_space_hardware(space_id: string, token: `hf_${string}`): Promise<(typeof hardware_types)[number]>;
18
+ export declare function set_space_hardware(space_id: string, new_hardware: (typeof hardware_types)[number], token: `hf_${string}`): Promise<(typeof hardware_types)[number]>;
19
+ export declare function set_space_timeout(space_id: string, timeout: number, token: `hf_${string}`): Promise<number>;
20
20
  export declare const hardware_types: readonly ["cpu-basic", "cpu-upgrade", "t4-small", "t4-medium", "a10g-small", "a10g-large", "a100-large"];
21
21
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG;IACrD,WAAW,EAAE,IAAI,GAAG,KAAK,CAAC;IAC1B,aAAa,EAAE,OAAO,GAAG,QAAQ,CAAC;IAClC,IAAI,EAAE,MAAM,CAAC;CACb,CAyBA;AAED,eAAO,MAAM,aAAa,QAAqB,CAAC;AAChD,eAAO,MAAM,eAAe,QAAwB,CAAC;AACrD,wBAAsB,gBAAgB,CACrC,aAAa,EAAE,MAAM,EACrB,KAAK,CAAC,EAAE,MAAM,MAAM,EAAE,GACpB,OAAO,CAAC;IACV,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,IAAI,GAAG,KAAK,CAAC;IAC1B,aAAa,EAAE,OAAO,GAAG,QAAQ,CAAC;CAClC,CAAC,CA4CD;AAED,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,CAAC,cAAc,CAAC,0BAQ3D;AAID,wBAAsB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,oBAezD;AAED,wBAAsB,kBAAkB,CACvC,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,MAAM,EAAE,gBAsBrB;AAED,wBAAsB,kBAAkB,CACvC,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,OAAO,cAAc,CAAC,MA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AED,wBAAsB,kBAAkB,CACvC,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,EAC7C,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,CAuB1C;AAED,wBAAsB,iBAAiB,CACtC,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,MAAM,EAAE,GACnB,OAAO,CAAC,MAAM,CAAC,CAuBjB;AAED,eAAO,MAAM,cAAc,0GAQjB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradio/client",
3
- "version": "0.1.4",
3
+ "version": "0.2.1",
4
4
  "description": "Gradio API client",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -19,11 +19,12 @@
19
19
  },
20
20
  "devDependencies": {
21
21
  "@types/ws": "^8.5.4",
22
- "esbuild": "^0.18.0"
22
+ "esbuild": "^0.19.0"
23
23
  },
24
24
  "engines": {
25
25
  "node": ">=18.0.0"
26
26
  },
27
+ "main_changeset": true,
27
28
  "scripts": {
28
29
  "bundle": "vite build --ssr",
29
30
  "generate_types": "tsc",
package/src/client.ts CHANGED
@@ -66,10 +66,10 @@ export async function duplicate(
66
66
  hf_token: `hf_${string}`;
67
67
  private?: boolean;
68
68
  status_callback: SpaceStatusCallback;
69
- hardware?: typeof hardware_types[number];
69
+ hardware?: (typeof hardware_types)[number];
70
70
  timeout?: number;
71
71
  }
72
- ) {
72
+ ): Promise<client_return> {
73
73
  const { hf_token, private: _private, hardware, timeout } = options;
74
74
 
75
75
  if (hardware && !hardware_types.includes(hardware)) {
@@ -115,38 +115,57 @@ export async function duplicate(
115
115
 
116
116
  if (response.status === 409) {
117
117
  return client(`${user}/${space_name}`, options);
118
- } else {
119
- const duplicated_space = await response.json();
118
+ }
119
+ const duplicated_space = await response.json();
120
120
 
121
- let original_hardware;
121
+ let original_hardware;
122
122
 
123
- if (!hardware) {
124
- original_hardware = await get_space_hardware(app_reference, hf_token);
125
- }
123
+ if (!hardware) {
124
+ original_hardware = await get_space_hardware(app_reference, hf_token);
125
+ }
126
126
 
127
- const requested_hardware = hardware || original_hardware || "cpu-basic";
128
- await set_space_hardware(
129
- `${user}/${space_name}`,
130
- requested_hardware,
131
- hf_token
132
- );
127
+ const requested_hardware = hardware || original_hardware || "cpu-basic";
128
+ await set_space_hardware(
129
+ `${user}/${space_name}`,
130
+ requested_hardware,
131
+ hf_token
132
+ );
133
133
 
134
- await set_space_timeout(
135
- `${user}/${space_name}`,
136
- timeout || 300,
137
- hf_token
138
- );
139
- return client(duplicated_space.url, options);
140
- }
134
+ await set_space_timeout(`${user}/${space_name}`, timeout || 300, hf_token);
135
+ return client(duplicated_space.url, options);
141
136
  } catch (e: any) {
142
137
  throw new Error(e);
143
138
  }
144
139
  }
145
140
 
146
- /**
147
- * We need to inject a customized fetch implementation for the Wasm version.
148
- */
149
- export function api_factory(fetch_implementation: typeof fetch) {
141
+ interface Client {
142
+ post_data: (
143
+ url: string,
144
+ body: unknown,
145
+ token?: `hf_${string}`
146
+ ) => Promise<[PostResponse, number]>;
147
+ upload_files: (
148
+ root: string,
149
+ files: File[],
150
+ token?: `hf_${string}`
151
+ ) => Promise<UploadResponse>;
152
+ client: (
153
+ app_reference: string,
154
+ options: {
155
+ hf_token?: `hf_${string}`;
156
+ status_callback?: SpaceStatusCallback;
157
+ normalise_files?: boolean;
158
+ }
159
+ ) => Promise<client_return>;
160
+ handle_blob: (
161
+ endpoint: string,
162
+ data: unknown[],
163
+ api_info: ApiInfo<JsApiData>,
164
+ token?: `hf_${string}`
165
+ ) => Promise<unknown[]>;
166
+ }
167
+
168
+ export function api_factory(fetch_implementation: typeof fetch): Client {
150
169
  return { post_data, upload_files, client, handle_blob };
151
170
 
152
171
  async function post_data(
@@ -176,7 +195,7 @@ export function api_factory(fetch_implementation: typeof fetch) {
176
195
 
177
196
  async function upload_files(
178
197
  root: string,
179
- files: Array<File>,
198
+ files: (Blob | File)[],
180
199
  token?: `hf_${string}`
181
200
  ): Promise<UploadResponse> {
182
201
  const headers: {
@@ -185,22 +204,27 @@ export function api_factory(fetch_implementation: typeof fetch) {
185
204
  if (token) {
186
205
  headers.Authorization = `Bearer ${token}`;
187
206
  }
188
-
189
- const formData = new FormData();
190
- files.forEach((file) => {
191
- formData.append("files", file);
192
- });
193
- try {
194
- var response = await fetch_implementation(`${root}/upload`, {
195
- method: "POST",
196
- body: formData,
197
- headers
207
+ const chunkSize = 1000;
208
+ const uploadResponses = [];
209
+ for (let i = 0; i < files.length; i += chunkSize) {
210
+ const chunk = files.slice(i, i + chunkSize);
211
+ const formData = new FormData();
212
+ chunk.forEach((file) => {
213
+ formData.append("files", file);
198
214
  });
199
- } catch (e) {
200
- return { error: BROKEN_CONNECTION_MSG };
215
+ try {
216
+ var response = await fetch_implementation(`${root}/upload`, {
217
+ method: "POST",
218
+ body: formData,
219
+ headers
220
+ });
221
+ } catch (e) {
222
+ return { error: BROKEN_CONNECTION_MSG };
223
+ }
224
+ const output: UploadResponse["files"] = await response.json();
225
+ uploadResponses.push(...output);
201
226
  }
202
- const output: UploadResponse["files"] = await response.json();
203
- return { files: output };
227
+ return { files: uploadResponses };
204
228
  }
205
229
 
206
230
  async function client(
@@ -242,7 +266,7 @@ export function api_factory(fetch_implementation: typeof fetch) {
242
266
  jwt = await get_jwt(space_id, hf_token);
243
267
  }
244
268
 
245
- async function config_success(_config: Config) {
269
+ async function config_success(_config: Config): Promise<client_return> {
246
270
  config = _config;
247
271
  api_map = map_names_to_ids(_config?.dependencies || []);
248
272
  try {
@@ -257,7 +281,7 @@ export function api_factory(fetch_implementation: typeof fetch) {
257
281
  };
258
282
  }
259
283
  let api: ApiInfo<JsApiData>;
260
- async function handle_space_sucess(status: SpaceStatus) {
284
+ async function handle_space_sucess(status: SpaceStatus): Promise<void> {
261
285
  if (status_callback) status_callback(status);
262
286
  if (status.status === "running")
263
287
  try {
@@ -310,37 +334,50 @@ export function api_factory(fetch_implementation: typeof fetch) {
310
334
  }
311
335
  }
312
336
 
313
- /**
314
- * Run a prediction.
315
- * @param endpoint - The prediction endpoint to use.
316
- * @param status_callback - A function that is called with the current status of the prediction immediately and every time it updates.
317
- * @return Returns the data for the prediction or an error message.
318
- */
319
337
  function predict(
320
338
  endpoint: string,
321
339
  data: unknown[],
322
340
  event_data?: unknown
323
- ) {
341
+ ): Promise<unknown> {
324
342
  let data_returned = false;
325
343
  let status_complete = false;
344
+ let dependency;
345
+ if (typeof endpoint === "number") {
346
+ dependency = config.dependencies[endpoint];
347
+ } else {
348
+ const trimmed_endpoint = endpoint.replace(/^\//, "");
349
+ dependency = config.dependencies[api_map[trimmed_endpoint]];
350
+ }
351
+
352
+ if (dependency.types.continuous) {
353
+ throw new Error(
354
+ "Cannot call predict on this function as it may run forever. Use submit instead"
355
+ );
356
+ }
357
+
326
358
  return new Promise((res, rej) => {
327
359
  const app = submit(endpoint, data, event_data);
360
+ let result;
328
361
 
329
362
  app
330
363
  .on("data", (d) => {
331
- data_returned = true;
364
+ // if complete message comes before data, resolve here
332
365
  if (status_complete) {
333
366
  app.destroy();
367
+ res(d);
334
368
  }
335
- res(d);
369
+ data_returned = true;
370
+ result = d;
336
371
  })
337
372
  .on("status", (status) => {
338
373
  if (status.stage === "error") rej(status);
339
- if (status.stage === "complete" && data_returned) {
340
- app.destroy();
341
- }
342
374
  if (status.stage === "complete") {
343
375
  status_complete = true;
376
+ app.destroy();
377
+ // if complete message comes after data, resolve here
378
+ if (data_returned) {
379
+ res(result);
380
+ }
344
381
  }
345
382
  });
346
383
  });
@@ -478,6 +515,7 @@ export function api_factory(fetch_implementation: typeof fetch) {
478
515
  fire_event({
479
516
  type: "status",
480
517
  stage: "error",
518
+ broken: true,
481
519
  message: BROKEN_CONNECTION_MSG,
482
520
  queue: true,
483
521
  endpoint: _endpoint,
@@ -513,6 +551,14 @@ export function api_factory(fetch_implementation: typeof fetch) {
513
551
  websocket.send(JSON.stringify({ ...payload, session_hash }));
514
552
  } else if (type === "complete") {
515
553
  complete = status;
554
+ } else if (type === "log") {
555
+ fire_event({
556
+ type: "log",
557
+ log: data.log,
558
+ level: data.level,
559
+ endpoint: _endpoint,
560
+ fn_index
561
+ });
516
562
  } else if (type === "generating") {
517
563
  fire_event({
518
564
  type: "status",
@@ -565,7 +611,7 @@ export function api_factory(fetch_implementation: typeof fetch) {
565
611
  }
566
612
  });
567
613
 
568
- function fire_event<K extends EventType>(event: Event<K>) {
614
+ function fire_event<K extends EventType>(event: Event<K>): void {
569
615
  const narrowed_listener_map: ListenerMap<K> = listener_map;
570
616
  const listeners = narrowed_listener_map[event.type] || [];
571
617
  listeners?.forEach((l) => l(event));
@@ -574,7 +620,7 @@ export function api_factory(fetch_implementation: typeof fetch) {
574
620
  function on<K extends EventType>(
575
621
  eventType: K,
576
622
  listener: EventListener<K>
577
- ) {
623
+ ): SubmitReturn {
578
624
  const narrowed_listener_map: ListenerMap<K> = listener_map;
579
625
  const listeners = narrowed_listener_map[eventType] || [];
580
626
  narrowed_listener_map[eventType] = listeners;
@@ -586,7 +632,7 @@ export function api_factory(fetch_implementation: typeof fetch) {
586
632
  function off<K extends EventType>(
587
633
  eventType: K,
588
634
  listener: EventListener<K>
589
- ) {
635
+ ): SubmitReturn {
590
636
  const narrowed_listener_map: ListenerMap<K> = listener_map;
591
637
  let listeners = narrowed_listener_map[eventType] || [];
592
638
  listeners = listeners?.filter((l) => l !== listener);
@@ -595,7 +641,7 @@ export function api_factory(fetch_implementation: typeof fetch) {
595
641
  return { on, off, cancel, destroy };
596
642
  }
597
643
 
598
- async function cancel() {
644
+ async function cancel(): Promise<void> {
599
645
  const _status: Status = {
600
646
  stage: "complete",
601
647
  queue: false,
@@ -633,7 +679,7 @@ export function api_factory(fetch_implementation: typeof fetch) {
633
679
  }
634
680
  }
635
681
 
636
- function destroy() {
682
+ function destroy(): void {
637
683
  for (const event_type in listener_map) {
638
684
  listener_map[event_type as "data" | "status"].forEach((fn) => {
639
685
  off(event_type as "data" | "status", fn);
@@ -706,7 +752,7 @@ export function api_factory(fetch_implementation: typeof fetch) {
706
752
  async function handle_blob(
707
753
  endpoint: string,
708
754
  data: unknown[],
709
- api_info,
755
+ api_info: ApiInfo<JsApiData>,
710
756
  token?: `hf_${string}`
711
757
  ): Promise<unknown[]> {
712
758
  const blob_refs = await walk_and_store_blobs(
@@ -723,9 +769,8 @@ export function api_factory(fetch_implementation: typeof fetch) {
723
769
  const file_url = (await upload_files(endpoint, [blob], token))
724
770
  .files[0];
725
771
  return { path, file_url, type };
726
- } else {
727
- return { path, base64: data, type };
728
772
  }
773
+ return { path, base64: data, type };
729
774
  })
730
775
  ).then((r) => {
731
776
  r.forEach(({ path, file_url, base64, type }) => {
@@ -759,27 +804,26 @@ function transform_output(
759
804
  remote_url?: string
760
805
  ): unknown[] {
761
806
  return data.map((d, i) => {
762
- if (api_info.returns?.[i]?.component === "File") {
807
+ if (api_info?.returns?.[i]?.component === "File") {
763
808
  return normalise_file(d, root_url, remote_url);
764
- } else if (api_info.returns?.[i]?.component === "Gallery") {
809
+ } else if (api_info?.returns?.[i]?.component === "Gallery") {
765
810
  return d.map((img) => {
766
811
  return Array.isArray(img)
767
812
  ? [normalise_file(img[0], root_url, remote_url), img[1]]
768
813
  : [normalise_file(img, root_url, remote_url), null];
769
814
  });
770
- } else if (typeof d === "object" && d.is_file) {
815
+ } else if (typeof d === "object" && d?.is_file) {
771
816
  return normalise_file(d, root_url, remote_url);
772
- } else {
773
- return d;
774
817
  }
818
+ return d;
775
819
  });
776
820
  }
777
821
 
778
822
  function normalise_file(
779
- file: Array<FileData>,
823
+ file: FileData[],
780
824
  root: string,
781
825
  root_url: string | null
782
- ): Array<FileData>;
826
+ ): FileData[];
783
827
  function normalise_file(
784
828
  file: FileData | string,
785
829
  root: string,
@@ -790,11 +834,7 @@ function normalise_file(
790
834
  root: string,
791
835
  root_url: string | null
792
836
  ): null;
793
- function normalise_file(
794
- file,
795
- root,
796
- root_url
797
- ): Array<FileData> | FileData | null {
837
+ function normalise_file(file, root, root_url): FileData[] | FileData | null {
798
838
  if (file == null) return null;
799
839
  if (typeof file === "string") {
800
840
  return {
@@ -802,7 +842,7 @@ function normalise_file(
802
842
  data: file
803
843
  };
804
844
  } else if (Array.isArray(file)) {
805
- const normalized_file: Array<FileData | null> = [];
845
+ const normalized_file: (FileData | null)[] = [];
806
846
 
807
847
  for (const x of file) {
808
848
  if (x === null) {
@@ -812,7 +852,7 @@ function normalise_file(
812
852
  }
813
853
  }
814
854
 
815
- return normalized_file as Array<FileData>;
855
+ return normalized_file as FileData[];
816
856
  } else if (file.is_file) {
817
857
  if (!root_url) {
818
858
  file.data = root + "/file=" + file.name;
@@ -858,7 +898,7 @@ function get_type(
858
898
  component: string,
859
899
  serializer: string,
860
900
  signature_type: "return" | "parameter"
861
- ) {
901
+ ): string {
862
902
  switch (type.type) {
863
903
  case "string":
864
904
  return "string";
@@ -882,11 +922,10 @@ function get_type(
882
922
  return signature_type === "parameter"
883
923
  ? "(Blob | File | Buffer)[]"
884
924
  : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}[]`;
885
- } else {
886
- return signature_type === "parameter"
887
- ? "Blob | File | Buffer"
888
- : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}`;
889
925
  }
926
+ return signature_type === "parameter"
927
+ ? "Blob | File | Buffer"
928
+ : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}`;
890
929
  } else if (serializer === "GallerySerializable") {
891
930
  return signature_type === "parameter"
892
931
  ? "[(Blob | File | Buffer), (string | null)][]"
@@ -897,16 +936,15 @@ function get_type(
897
936
  function get_description(
898
937
  type: { type: any; description: string },
899
938
  serializer: string
900
- ) {
939
+ ): string {
901
940
  if (serializer === "GallerySerializable") {
902
941
  return "array of [file, label] tuples";
903
942
  } else if (serializer === "ListStringSerializable") {
904
943
  return "array of strings";
905
944
  } else if (serializer === "FileSerializable") {
906
945
  return "array of files or single file";
907
- } else {
908
- return type.description;
909
946
  }
947
+ return type.description;
910
948
  }
911
949
 
912
950
  function transform_api_info(
@@ -974,7 +1012,7 @@ async function get_jwt(
974
1012
  }
975
1013
  }
976
1014
 
977
- function update_object(object, newValue, stack) {
1015
+ function update_object(object, newValue, stack): void {
978
1016
  while (stack.length > 1) {
979
1017
  object = object[stack.shift()];
980
1018
  }
@@ -988,7 +1026,14 @@ export async function walk_and_store_blobs(
988
1026
  path = [],
989
1027
  root = false,
990
1028
  api_info = undefined
991
- ) {
1029
+ ): Promise<
1030
+ {
1031
+ path: string[];
1032
+ data: string | false;
1033
+ type: string;
1034
+ blob: Blob | false;
1035
+ }[]
1036
+ > {
992
1037
  if (Array.isArray(param)) {
993
1038
  let blob_refs = [];
994
1039
 
@@ -1035,10 +1080,9 @@ export async function walk_and_store_blobs(
1035
1080
  data = Buffer.from(buffer).toString("base64");
1036
1081
  }
1037
1082
 
1038
- return [{ path, data, type }];
1039
- } else {
1040
- return [{ path: path, blob: param, type }];
1083
+ return [{ path, data, type, blob: false }];
1041
1084
  }
1085
+ return [{ path: path, blob: param, type, data: false }];
1042
1086
  } else if (typeof param === "object") {
1043
1087
  let blob_refs = [];
1044
1088
  for (let key in param) {
@@ -1057,12 +1101,11 @@ export async function walk_and_store_blobs(
1057
1101
  }
1058
1102
  }
1059
1103
  return blob_refs;
1060
- } else {
1061
- return [];
1062
1104
  }
1105
+ return [];
1063
1106
  }
1064
1107
 
1065
- function image_to_data_uri(blob: Blob) {
1108
+ function image_to_data_uri(blob: Blob): Promise<string | ArrayBuffer> {
1066
1109
  return new Promise((resolve, _) => {
1067
1110
  const reader = new FileReader();
1068
1111
  reader.onloadend = () => resolve(reader.result);
@@ -1070,7 +1113,7 @@ function image_to_data_uri(blob: Blob) {
1070
1113
  });
1071
1114
  }
1072
1115
 
1073
- function skip_queue(id: number, config: Config) {
1116
+ function skip_queue(id: number, config: Config): boolean {
1074
1117
  return (
1075
1118
  !(config?.dependencies?.[id]?.queue === null
1076
1119
  ? config.enable_queue
@@ -1106,9 +1149,8 @@ async function resolve_config(
1106
1149
  config.path = config.path ?? "";
1107
1150
  config.root = endpoint;
1108
1151
  return config;
1109
- } else {
1110
- throw new Error("Could not get config.");
1111
1152
  }
1153
+ throw new Error("Could not get config.");
1112
1154
  }
1113
1155
 
1114
1156
  throw new Error("No config or app endpoint found");
@@ -1118,7 +1160,7 @@ async function check_space_status(
1118
1160
  id: string,
1119
1161
  type: "subdomain" | "space_name",
1120
1162
  status_callback: SpaceStatusCallback
1121
- ) {
1163
+ ): Promise<void> {
1122
1164
  let endpoint =
1123
1165
  type === "subdomain"
1124
1166
  ? `https://huggingface.co/api/spaces/by-subdomain/${id}`
@@ -1211,7 +1253,7 @@ function handle_message(
1211
1253
  data: any,
1212
1254
  last_status: Status["stage"]
1213
1255
  ): {
1214
- type: "hash" | "data" | "update" | "complete" | "generating" | "none";
1256
+ type: "hash" | "data" | "update" | "complete" | "generating" | "log" | "none";
1215
1257
  data?: any;
1216
1258
  status?: Status;
1217
1259
  } {
@@ -1256,6 +1298,8 @@ function handle_message(
1256
1298
  success: data.success
1257
1299
  }
1258
1300
  };
1301
+ case "log":
1302
+ return { type: "log", data: data };
1259
1303
  case "process_generating":
1260
1304
  return {
1261
1305
  type: "generating",
@@ -1281,20 +1325,19 @@ function handle_message(
1281
1325
  success: data.success
1282
1326
  }
1283
1327
  };
1284
- } else {
1285
- return {
1286
- type: "complete",
1287
- status: {
1288
- queue,
1289
- message: !data.success ? data.output.error : undefined,
1290
- stage: data.success ? "complete" : "error",
1291
- code: data.code,
1292
- progress_data: data.progress_data,
1293
- eta: data.output.average_duration
1294
- },
1295
- data: data.success ? data.output : null
1296
- };
1297
1328
  }
1329
+ return {
1330
+ type: "complete",
1331
+ status: {
1332
+ queue,
1333
+ message: !data.success ? data.output.error : undefined,
1334
+ stage: data.success ? "complete" : "error",
1335
+ code: data.code,
1336
+ progress_data: data.progress_data,
1337
+ eta: data.output.average_duration
1338
+ },
1339
+ data: data.success ? data.output : null
1340
+ };
1298
1341
 
1299
1342
  case "process_starts":
1300
1343
  return {
package/src/globals.d.ts CHANGED
@@ -1,11 +1,9 @@
1
1
  declare global {
2
2
  interface Window {
3
3
  __gradio_mode__: "app" | "website";
4
- launchGradio: Function;
5
- launchGradioFromSpaces: Function;
6
4
  gradio_config: Config;
7
- scoped_css_attach: (link: HTMLLinkElement) => void;
8
5
  __is_colab__: boolean;
6
+ __gradio_space__: string | null;
9
7
  }
10
8
  }
11
9
 
package/src/types.ts CHANGED
@@ -21,7 +21,7 @@ export interface Config {
21
21
  }
22
22
 
23
23
  export interface Payload {
24
- data: Array<unknown>;
24
+ data: unknown[];
25
25
  fn_index?: number;
26
26
  event_data?: unknown;
27
27
  time?: Date;
@@ -33,7 +33,7 @@ export interface PostResponse {
33
33
  }
34
34
  export interface UploadResponse {
35
35
  error?: string;
36
- files?: Array<string>;
36
+ files?: string[];
37
37
  }
38
38
 
39
39
  export interface Status {
@@ -41,20 +41,26 @@ export interface Status {
41
41
  code?: string;
42
42
  success?: boolean;
43
43
  stage: "pending" | "error" | "complete" | "generating";
44
+ broken?: boolean;
44
45
  size?: number;
45
46
  position?: number;
46
47
  eta?: number;
47
48
  message?: string;
48
- progress_data?: Array<{
49
+ progress_data?: {
49
50
  progress: number | null;
50
51
  index: number | null;
51
52
  length: number | null;
52
53
  unit: string | null;
53
54
  desc: string | null;
54
- }>;
55
+ }[];
55
56
  time?: Date;
56
57
  }
57
58
 
59
+ export interface LogMessage {
60
+ log: string;
61
+ level: "warning" | "info";
62
+ }
63
+
58
64
  export interface SpaceStatusNormal {
59
65
  status: "sleeping" | "running" | "building" | "error" | "stopped";
60
66
  detail:
@@ -83,11 +89,12 @@ export type SpaceStatus = SpaceStatusNormal | SpaceStatusError;
83
89
  export type status_callback_function = (a: Status) => void;
84
90
  export type SpaceStatusCallback = (a: SpaceStatus) => void;
85
91
 
86
- export type EventType = "data" | "status";
92
+ export type EventType = "data" | "status" | "log";
87
93
 
88
94
  export interface EventMap {
89
95
  data: Payload;
90
96
  status: Status;
97
+ log: LogMessage;
91
98
  }
92
99
 
93
100
  export type Event<K extends EventType> = {
package/src/utils.ts CHANGED
@@ -14,13 +14,12 @@ export function determine_protocol(endpoint: string): {
14
14
  host: host,
15
15
  http_protocol: protocol as "http:" | "https:"
16
16
  };
17
- } else {
18
- return {
19
- ws_protocol: protocol === "https:" ? "wss" : "ws",
20
- http_protocol: protocol as "http:" | "https:",
21
- host
22
- };
23
17
  }
18
+ return {
19
+ ws_protocol: protocol === "https:" ? "wss" : "ws",
20
+ http_protocol: protocol as "http:" | "https:",
21
+ host
22
+ };
24
23
  }
25
24
 
26
25
  // default to secure if no protocol is provided
@@ -87,7 +86,9 @@ export async function process_endpoint(
87
86
  };
88
87
  }
89
88
 
90
- export function map_names_to_ids(fns: Config["dependencies"]) {
89
+ export function map_names_to_ids(
90
+ fns: Config["dependencies"]
91
+ ): Record<string, number> {
91
92
  let apis: Record<string, number> = {};
92
93
 
93
94
  fns.forEach(({ api_name }, i) => {
@@ -99,7 +100,7 @@ export function map_names_to_ids(fns: Config["dependencies"]) {
99
100
 
100
101
  const RE_DISABLED_DISCUSSION =
101
102
  /^(?=[^]*\b[dD]iscussions{0,1}\b)(?=[^]*\b[dD]isabled\b)[^]*$/;
102
- export async function discussions_enabled(space_id: string) {
103
+ export async function discussions_enabled(space_id: string): Promise<boolean> {
103
104
  try {
104
105
  const r = await fetch(
105
106
  `https://huggingface.co/api/spaces/${space_id}/discussions`,
@@ -110,7 +111,7 @@ export async function discussions_enabled(space_id: string) {
110
111
  const error = r.headers.get("x-error-message");
111
112
 
112
113
  if (error && RE_DISABLED_DISCUSSION.test(error)) return false;
113
- else return true;
114
+ return true;
114
115
  } catch (e) {
115
116
  return false;
116
117
  }
@@ -119,7 +120,7 @@ export async function discussions_enabled(space_id: string) {
119
120
  export async function get_space_hardware(
120
121
  space_id: string,
121
122
  token: `hf_${string}`
122
- ) {
123
+ ): Promise<(typeof hardware_types)[number]> {
123
124
  const headers: { Authorization?: string } = {};
124
125
  if (token) {
125
126
  headers.Authorization = `Bearer ${token}`;
@@ -144,9 +145,9 @@ export async function get_space_hardware(
144
145
 
145
146
  export async function set_space_hardware(
146
147
  space_id: string,
147
- new_hardware: typeof hardware_types[number],
148
+ new_hardware: (typeof hardware_types)[number],
148
149
  token: `hf_${string}`
149
- ) {
150
+ ): Promise<(typeof hardware_types)[number]> {
150
151
  const headers: { Authorization?: string } = {};
151
152
  if (token) {
152
153
  headers.Authorization = `Bearer ${token}`;
@@ -175,7 +176,7 @@ export async function set_space_timeout(
175
176
  space_id: string,
176
177
  timeout: number,
177
178
  token: `hf_${string}`
178
- ) {
179
+ ): Promise<number> {
179
180
  const headers: { Authorization?: string } = {};
180
181
  if (token) {
181
182
  headers.Authorization = `Bearer ${token}`;