@gradio/client 0.7.0-beta.0 → 0.7.0-beta.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,13 @@
1
1
  # @gradio/client
2
2
 
3
+ ## 0.7.0-beta.1
4
+
5
+ ### Features
6
+
7
+ - [#6143](https://github.com/gradio-app/gradio/pull/6143) [`e4f7b4b40`](https://github.com/gradio-app/gradio/commit/e4f7b4b409323b01aa01b39e15ce6139e29aa073) - fix circular dependency with client + upload. Thanks [@pngwn](https://github.com/pngwn)!
8
+ - [#6094](https://github.com/gradio-app/gradio/pull/6094) [`c476bd5a5`](https://github.com/gradio-app/gradio/commit/c476bd5a5b70836163b9c69bf4bfe068b17fbe13) - Image v4. Thanks [@pngwn](https://github.com/pngwn)!
9
+ - [#6069](https://github.com/gradio-app/gradio/pull/6069) [`bf127e124`](https://github.com/gradio-app/gradio/commit/bf127e1241a41401e144874ea468dff8474eb505) - Swap websockets for SSE. Thanks [@aliabid94](https://github.com/aliabid94)!
10
+
3
11
  ## 0.7.0-beta.0
4
12
 
5
13
  ### Features
package/dist/client.d.ts CHANGED
@@ -69,7 +69,6 @@ interface ApiInfo<T extends ApiData | JsApiData> {
69
69
  }
70
70
  export declare function walk_and_store_blobs(param: any, type?: any, path?: any[], root?: boolean, api_info?: any): Promise<{
71
71
  path: string[];
72
- data: string | false;
73
72
  type: string;
74
73
  blob: Blob | false;
75
74
  }[]>;
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAEA,OAAO,EAQN,cAAc,EAEd,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,K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;IACF,iBAAiB,EAAE;QAClB,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC;KAC/B,CAAC;CACF;AAiID,wBAAsB,oBAAoB,CACzC,KAAK,KAAA,EACL,IAAI,MAAY,EAChB,IAAI,QAAK,EACT,IAAI,UAAQ,EACZ,QAAQ,MAAY,GAClB,OAAO,CACT;IACC,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,IAAI,GAAG,KAAK,CAAC;CACnB,EAAE,CACH,CAmDA"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { client, post_data, upload_files, duplicate, api_factory } from "./client.js";
2
2
  export type { SpaceStatus } from "./types.js";
3
+ export { normalise_file, FileData, upload, get_fetchable_url_or_file, prepare_files } from "./upload.js";
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,MAAM,EACN,SAAS,EACT,YAAY,EACZ,SAAS,EACT,WAAW,EACX,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,MAAM,EACN,SAAS,EACT,YAAY,EACZ,SAAS,EACT,WAAW,EACX,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,EACN,cAAc,EACd,QAAQ,EACR,MAAM,EACN,yBAAyB,EACzB,aAAa,EACb,MAAM,aAAa,CAAC"}
package/dist/index.js CHANGED
@@ -170,6 +170,110 @@ const hardware_types = [
170
170
  "a10g-large",
171
171
  "a100-large"
172
172
  ];
173
+ function normalise_file(file, server_url, proxy_url) {
174
+ if (file == null) {
175
+ return null;
176
+ }
177
+ if (Array.isArray(file)) {
178
+ const normalized_file = [];
179
+ for (const x of file) {
180
+ if (x == null) {
181
+ normalized_file.push(null);
182
+ } else {
183
+ normalized_file.push(normalise_file(x, server_url, proxy_url));
184
+ }
185
+ }
186
+ return normalized_file;
187
+ }
188
+ if (file.is_stream) {
189
+ if (proxy_url == null) {
190
+ return new FileData({
191
+ ...file,
192
+ url: server_url + "/stream/" + file.path
193
+ });
194
+ }
195
+ return new FileData({
196
+ ...file,
197
+ url: "/proxy=" + proxy_url + "stream/" + file.path
198
+ });
199
+ }
200
+ return new FileData({
201
+ ...file,
202
+ url: get_fetchable_url_or_file(file.path, server_url, proxy_url)
203
+ });
204
+ }
205
+ function is_url(str) {
206
+ try {
207
+ const url = new URL(str);
208
+ return url.protocol === "http:" || url.protocol === "https:";
209
+ } catch {
210
+ return false;
211
+ }
212
+ }
213
+ function get_fetchable_url_or_file(path, server_url, proxy_url) {
214
+ if (path == null) {
215
+ return proxy_url ? `/proxy=${proxy_url}file=` : `${server_url}/file=`;
216
+ }
217
+ if (is_url(path)) {
218
+ return path;
219
+ }
220
+ return proxy_url ? `/proxy=${proxy_url}file=${path}` : `${server_url}/file=${path}`;
221
+ }
222
+ async function upload(file_data, root, upload_fn = upload_files) {
223
+ let files = (Array.isArray(file_data) ? file_data : [file_data]).map(
224
+ (file_data2) => file_data2.blob
225
+ );
226
+ return await Promise.all(
227
+ await upload_fn(root, files).then(
228
+ async (response) => {
229
+ if (response.error) {
230
+ throw new Error(response.error);
231
+ } else {
232
+ if (response.files) {
233
+ return response.files.map((f, i) => {
234
+ const file = new FileData({ ...file_data[i], path: f });
235
+ return normalise_file(file, root, null);
236
+ });
237
+ }
238
+ return [];
239
+ }
240
+ }
241
+ )
242
+ );
243
+ }
244
+ async function prepare_files(files, is_stream) {
245
+ return files.map(
246
+ (f, i) => new FileData({
247
+ path: f.name,
248
+ orig_name: f.name,
249
+ blob: f,
250
+ size: f.size,
251
+ mime_type: f.type,
252
+ is_stream
253
+ })
254
+ );
255
+ }
256
+ class FileData {
257
+ constructor({
258
+ path,
259
+ url,
260
+ orig_name,
261
+ size,
262
+ blob,
263
+ is_stream,
264
+ mime_type,
265
+ alt_text
266
+ }) {
267
+ this.path = path;
268
+ this.url = url;
269
+ this.orig_name = orig_name;
270
+ this.size = size;
271
+ this.blob = url ? void 0 : blob;
272
+ this.is_stream = is_stream;
273
+ this.mime_type = mime_type;
274
+ this.alt_text = alt_text;
275
+ }
276
+ }
173
277
  const QUEUE_FULL_MSG = "This application is too busy. Keep trying!";
174
278
  const BROKEN_CONNECTION_MSG = "Connection errored out.";
175
279
  let NodeBlob;
@@ -276,7 +380,6 @@ function api_factory(fetch_implementation, WebSocket_factory) {
276
380
  submit,
277
381
  view_api,
278
382
  component_server
279
- // duplicate
280
383
  };
281
384
  const transform_files = normalise_files ?? true;
282
385
  if ((typeof window === "undefined" || !("WebSocket" in window)) && !global.Websocket) {
@@ -418,8 +521,11 @@ function api_factory(fetch_implementation, WebSocket_factory) {
418
521
  );
419
522
  }
420
523
  let websocket;
524
+ let eventSource;
525
+ let protocol = config.protocol ?? "sse";
421
526
  const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint;
422
527
  let payload;
528
+ let event_id = null;
423
529
  let complete = false;
424
530
  const listener_map = {};
425
531
  let url_params = "";
@@ -495,7 +601,7 @@ function api_factory(fetch_implementation, WebSocket_factory) {
495
601
  time: /* @__PURE__ */ new Date()
496
602
  });
497
603
  });
498
- } else {
604
+ } else if (protocol == "ws") {
499
605
  fire_event({
500
606
  type: "status",
501
607
  stage: "pending",
@@ -604,6 +710,119 @@ function api_factory(fetch_implementation, WebSocket_factory) {
604
710
  () => websocket.send(JSON.stringify({ hash: session_hash }))
605
711
  );
606
712
  }
713
+ } else {
714
+ fire_event({
715
+ type: "status",
716
+ stage: "pending",
717
+ queue: true,
718
+ endpoint: _endpoint,
719
+ fn_index,
720
+ time: /* @__PURE__ */ new Date()
721
+ });
722
+ var params = new URLSearchParams({
723
+ fn_index: fn_index.toString(),
724
+ session_hash
725
+ }).toString();
726
+ let url = new URL(
727
+ `${http_protocol}//${resolve_root(
728
+ host,
729
+ config.path,
730
+ true
731
+ )}/queue/join?${params}`
732
+ );
733
+ eventSource = new EventSource(url);
734
+ eventSource.onmessage = async function(event) {
735
+ const _data = JSON.parse(event.data);
736
+ const { type, status, data: data2 } = handle_message(
737
+ _data,
738
+ last_status[fn_index]
739
+ );
740
+ if (type === "update" && status && !complete) {
741
+ fire_event({
742
+ type: "status",
743
+ endpoint: _endpoint,
744
+ fn_index,
745
+ time: /* @__PURE__ */ new Date(),
746
+ ...status
747
+ });
748
+ if (status.stage === "error") {
749
+ eventSource.close();
750
+ }
751
+ } else if (type === "data") {
752
+ event_id = _data.event_id;
753
+ let [_, status2] = await post_data2(
754
+ `${http_protocol}//${resolve_root(
755
+ host,
756
+ config.path,
757
+ true
758
+ )}/queue/data`,
759
+ {
760
+ ...payload,
761
+ session_hash,
762
+ event_id
763
+ },
764
+ hf_token
765
+ );
766
+ if (status2 !== 200) {
767
+ fire_event({
768
+ type: "status",
769
+ stage: "error",
770
+ message: BROKEN_CONNECTION_MSG,
771
+ queue: true,
772
+ endpoint: _endpoint,
773
+ fn_index,
774
+ time: /* @__PURE__ */ new Date()
775
+ });
776
+ eventSource.close();
777
+ }
778
+ } else if (type === "complete") {
779
+ complete = status;
780
+ } else if (type === "log") {
781
+ fire_event({
782
+ type: "log",
783
+ log: data2.log,
784
+ level: data2.level,
785
+ endpoint: _endpoint,
786
+ fn_index
787
+ });
788
+ } else if (type === "generating") {
789
+ fire_event({
790
+ type: "status",
791
+ time: /* @__PURE__ */ new Date(),
792
+ ...status,
793
+ stage: status == null ? void 0 : status.stage,
794
+ queue: true,
795
+ endpoint: _endpoint,
796
+ fn_index
797
+ });
798
+ }
799
+ if (data2) {
800
+ fire_event({
801
+ type: "data",
802
+ time: /* @__PURE__ */ new Date(),
803
+ data: transform_files ? transform_output(
804
+ data2.data,
805
+ api_info,
806
+ config.root,
807
+ config.root_url
808
+ ) : data2.data,
809
+ endpoint: _endpoint,
810
+ fn_index
811
+ });
812
+ if (complete) {
813
+ fire_event({
814
+ type: "status",
815
+ time: /* @__PURE__ */ new Date(),
816
+ ...complete,
817
+ stage: status == null ? void 0 : status.stage,
818
+ queue: true,
819
+ endpoint: _endpoint,
820
+ fn_index
821
+ });
822
+ eventSource.close();
823
+ }
824
+ }
825
+ };
607
826
  }
608
827
  });
609
828
  function fire_event(event) {
@@ -638,12 +857,19 @@ function api_factory(fetch_implementation, WebSocket_factory) {
638
857
  endpoint: _endpoint,
639
858
  fn_index
640
859
  });
641
- if (websocket && websocket.readyState === 0) {
642
- websocket.addEventListener("open", () => {
860
+ let cancel_request = {};
861
+ if (protocol === "ws") {
862
+ if (websocket && websocket.readyState === 0) {
863
+ websocket.addEventListener("open", () => {
864
+ websocket.close();
865
+ });
866
+ } else {
643
867
  websocket.close();
644
- });
868
+ }
869
+ cancel_request = { fn_index, session_hash };
645
870
  } else {
646
- websocket.close();
871
+ eventSource.close();
872
+ cancel_request = { event_id };
647
873
  }
648
874
  try {
649
875
  await fetch_implementation(
@@ -655,7 +881,7 @@ function api_factory(fetch_implementation, WebSocket_factory) {
655
881
  {
656
882
  headers: { "Content-Type": "application/json" },
657
883
  method: "POST",
658
- body: JSON.stringify({ fn_index, session_hash })
884
+ body: JSON.stringify(cancel_request)
659
885
  }
660
886
  );
661
887
  } catch (e) {
@@ -767,27 +993,20 @@ function api_factory(fetch_implementation, WebSocket_factory) {
767
993
  api_info
768
994
  );
769
995
  return Promise.all(
770
- blob_refs.map(async ({ path, blob, data: data2, type }) => {
996
+ blob_refs.map(async ({ path, blob, type }) => {
771
997
  if (blob) {
772
998
  const file_url = (await upload_files2(endpoint, [blob], token)).files[0];
773
- return { path, file_url, type };
999
+ return { path, file_url, type, name: blob == null ? void 0 : blob.name };
774
1000
  }
775
- return { path, base64: data2, type };
1001
+ return { path, type };
776
1002
  })
777
1003
  ).then((r) => {
778
- r.forEach(({ path, file_url, base64, type }) => {
779
- if (base64) {
780
- update_object(data, base64, path);
781
- } else if (type === "Gallery") {
1004
+ r.forEach(({ path, file_url, type, name }) => {
1005
+ if (type === "Gallery") {
782
1006
  update_object(data, file_url, path);
783
1007
  } else if (file_url) {
784
- const o = {
785
- is_file: true,
786
- name: `${file_url}`,
787
- data: null
788
- // orig_name: "file.csv"
789
- };
790
- update_object(data, o, path);
1008
+ const file = new FileData({ path: file_url, orig_name: name });
1009
+ update_object(data, file, path);
791
1010
  }
792
1011
  });
793
1012
  return data;
@@ -807,39 +1026,12 @@ function transform_output(data, api_info, root_url, remote_url) {
807
1026
  return d.map((img) => {
808
1027
  return Array.isArray(img) ? [normalise_file(img[0], root_url, remote_url), img[1]] : [normalise_file(img, root_url, remote_url), null];
809
1028
  });
810
- } else if (typeof d === "object" && (d == null ? void 0 : d.is_file)) {
1029
+ } else if (typeof d === "object" && d.path) {
811
1030
  return normalise_file(d, root_url, remote_url);
812
1031
  }
813
1032
  return d;
814
1033
  });
815
1034
  }
816
- function normalise_file(file, root, root_url) {
817
- if (file == null)
818
- return null;
819
- if (typeof file === "string") {
820
- return {
821
- name: "file_data",
822
- data: file
823
- };
824
- } else if (Array.isArray(file)) {
825
- const normalized_file = [];
826
- for (const x of file) {
827
- if (x === null) {
828
- normalized_file.push(null);
829
- } else {
830
- normalized_file.push(normalise_file(x, root, root_url));
831
- }
832
- }
833
- return normalized_file;
834
- } else if (file.is_file) {
835
- if (!root_url) {
836
- file.data = root + "/file=" + file.name;
837
- } else {
838
- file.data = "/proxy=" + root_url + "file=" + file.name;
839
- }
840
- }
841
- return file;
842
- }
843
1035
  function get_type(type, component, serializer, signature_type) {
844
1036
  switch (type.type) {
845
1037
  case "string":
@@ -953,22 +1145,9 @@ async function walk_and_store_blobs(param, type = void 0, path = [], root = fals
953
1145
  {
954
1146
  path,
955
1147
  blob: is_image ? false : new NodeBlob([param]),
956
- data: is_image ? `${param.toString("base64")}` : false,
957
1148
  type
958
1149
  }
959
1150
  ];
960
- } else if (param instanceof Blob || typeof window !== "undefined" && param instanceof File) {
961
- if (type === "Image") {
962
- let data;
963
- if (typeof window !== "undefined") {
964
- data = await image_to_data_uri(param);
965
- } else {
966
- const buffer = await param.arrayBuffer();
967
- data = Buffer.from(buffer).toString("base64");
968
- }
969
- return [{ path, data, type, blob: false }];
970
- }
971
- return [{ path, blob: param, type, data: false }];
972
1151
  } else if (typeof param === "object") {
973
1152
  let blob_refs = [];
974
1153
  for (let key in param) {
@@ -990,13 +1169,6 @@ async function walk_and_store_blobs(param, type = void 0, path = [], root = fals
990
1169
  }
991
1170
  return [];
992
1171
  }
993
- function image_to_data_uri(blob) {
994
- return new Promise((resolve, _) => {
995
- const reader = new FileReader();
996
- reader.onloadend = () => resolve(reader.result);
997
- reader.readAsDataURL(blob);
998
- });
999
- }
1000
1172
  function skip_queue(id, config) {
1001
1173
  var _a, _b, _c, _d;
1002
1174
  return !(((_b = (_a = config == null ? void 0 : config.dependencies) == null ? void 0 : _a[id]) == null ? void 0 : _b.queue) === null ? config.enable_queue : (_d = (_c = config == null ? void 0 : config.dependencies) == null ? void 0 : _c[id]) == null ? void 0 : _d.queue) || false;
@@ -1202,9 +1374,14 @@ function handle_message(data, last_status) {
1202
1374
  return { type: "none", status: { stage: "error", queue } };
1203
1375
  }
1204
1376
  export {
1377
+ FileData,
1205
1378
  api_factory,
1206
1379
  client,
1207
1380
  duplicate,
1381
+ get_fetchable_url_or_file,
1382
+ normalise_file,
1208
1383
  post_data,
1384
+ prepare_files,
1385
+ upload,
1209
1386
  upload_files
1210
1387
  };
package/dist/types.d.ts CHANGED
@@ -18,6 +18,7 @@ export interface Config {
18
18
  show_api: boolean;
19
19
  stylesheets: string[];
20
20
  path: string;
21
+ protocol?: "sse" | "ws";
21
22
  }
22
23
  export interface Payload {
23
24
  data: unknown[];
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,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;AAC3D,MAAM,MAAM,mBAAmB,GAAG,CAAC,CAAC,EAAE,WAAW,KAAK,IAAI,CAAC;AAE3D,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,QAAQ,GAAG,KAAK,CAAC;AAElD,MAAM,WAAW,QAAQ;IACxB,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,UAAU,CAAC;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"}
@@ -0,0 +1,29 @@
1
+ import { upload_files } from "./client";
2
+ export declare function normalise_file(file: FileData | null, server_url: string, proxy_url: string | null): FileData | null;
3
+ export declare function normalise_file(file: FileData[] | null, server_url: string, proxy_url: string | null): FileData[] | null;
4
+ export declare function normalise_file(file: FileData[] | FileData | null, server_url: string, // root: string,
5
+ proxy_url: string | null): FileData[] | FileData | null;
6
+ export declare function get_fetchable_url_or_file(path: string | null, server_url: string, proxy_url: string | null): string;
7
+ export declare function upload(file_data: FileData[], root: string, upload_fn?: typeof upload_files): Promise<(FileData | null)[] | null>;
8
+ export declare function prepare_files(files: File[], is_stream?: boolean): Promise<FileData[]>;
9
+ export declare class FileData {
10
+ path: string;
11
+ url?: string;
12
+ orig_name: string;
13
+ size?: number;
14
+ blob?: File;
15
+ is_stream?: boolean;
16
+ mime_type?: string;
17
+ alt_text?: string;
18
+ constructor({ path, url, orig_name, size, blob, is_stream, mime_type, alt_text }: {
19
+ path: string;
20
+ url?: string;
21
+ orig_name: string;
22
+ size?: number;
23
+ blob?: File;
24
+ is_stream?: boolean;
25
+ mime_type?: string;
26
+ alt_text?: string;
27
+ });
28
+ }
29
+ //# sourceMappingURL=upload.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"upload.d.ts","sourceRoot":"","sources":["../src/upload.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAExC,wBAAgB,cAAc,CAC7B,IAAI,EAAE,QAAQ,GAAG,IAAI,EACrB,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,GAAG,IAAI,GACtB,QAAQ,GAAG,IAAI,CAAC;AAEnB,wBAAgB,cAAc,CAC7B,IAAI,EAAE,QAAQ,EAAE,GAAG,IAAI,EACvB,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,GAAG,IAAI,GACtB,QAAQ,EAAE,GAAG,IAAI,CAAC;AAErB,wBAAgB,cAAc,CAC7B,IAAI,EAAE,QAAQ,EAAE,GAAG,QAAQ,GAAG,IAAI,EAClC,UAAU,EAAE,MAAM,EAAE,gBAAgB;AACpC,SAAS,EAAE,MAAM,GAAG,IAAI,GACtB,QAAQ,EAAE,GAAG,QAAQ,GAAG,IAAI,CAAC;AAqD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0.7.0-beta.1",
4
4
  "description": "Gradio API client",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/client.ts CHANGED
@@ -1,3 +1,5 @@
1
+ //@ts-nocheck
2
+
1
3
  import semiver from "semiver";
2
4
 
3
5
  import {
@@ -22,10 +24,11 @@ import type {
22
24
  UploadResponse,
23
25
  Status,
24
26
  SpaceStatus,
25
- SpaceStatusCallback,
26
- FileData
27
+ SpaceStatusCallback
27
28
  } from "./types.js";
28
29
 
30
+ import { FileData, normalise_file } from "./upload";
31
+
29
32
  import type { Config } from "./types.js";
30
33
 
31
34
  type event = <K extends EventType>(
@@ -251,7 +254,6 @@ export function api_factory(
251
254
  submit,
252
255
  view_api,
253
256
  component_server
254
- // duplicate
255
257
  };
256
258
 
257
259
  const transform_files = normalise_files ?? true;
@@ -427,9 +429,12 @@ export function api_factory(
427
429
  }
428
430
 
429
431
  let websocket: WebSocket;
432
+ let eventSource: EventSource;
433
+ let protocol = config.protocol ?? "sse";
430
434
 
431
435
  const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint;
432
436
  let payload: Payload;
437
+ let event_id: string | null = null;
433
438
  let complete: false | Record<string, any> = false;
434
439
  const listener_map: ListenerMap<EventType> = {};
435
440
  let url_params = "";
@@ -514,7 +519,7 @@ export function api_factory(
514
519
  time: new Date()
515
520
  });
516
521
  });
517
- } else {
522
+ } else if (protocol == "ws") {
518
523
  fire_event({
519
524
  type: "status",
520
525
  stage: "pending",
@@ -634,6 +639,126 @@ export function api_factory(
634
639
  websocket.send(JSON.stringify({ hash: session_hash }))
635
640
  );
636
641
  }
642
+ } else {
643
+ fire_event({
644
+ type: "status",
645
+ stage: "pending",
646
+ queue: true,
647
+ endpoint: _endpoint,
648
+ fn_index,
649
+ time: new Date()
650
+ });
651
+ var params = new URLSearchParams({
652
+ fn_index: fn_index.toString(),
653
+ session_hash: session_hash
654
+ }).toString();
655
+ let url = new URL(
656
+ `${http_protocol}//${resolve_root(
657
+ host,
658
+ config.path,
659
+ true
660
+ )}/queue/join?${params}`
661
+ );
662
+
663
+ eventSource = new EventSource(url);
664
+
665
+ eventSource.onmessage = async function (event) {
666
+ const _data = JSON.parse(event.data);
667
+ const { type, status, data } = handle_message(
668
+ _data,
669
+ last_status[fn_index]
670
+ );
671
+
672
+ if (type === "update" && status && !complete) {
673
+ // call 'status' listeners
674
+ fire_event({
675
+ type: "status",
676
+ endpoint: _endpoint,
677
+ fn_index,
678
+ time: new Date(),
679
+ ...status
680
+ });
681
+ if (status.stage === "error") {
682
+ eventSource.close();
683
+ }
684
+ } else if (type === "data") {
685
+ event_id = _data.event_id as string;
686
+ let [_, status] = await post_data(
687
+ `${http_protocol}//${resolve_root(
688
+ host,
689
+ config.path,
690
+ true
691
+ )}/queue/data`,
692
+ {
693
+ ...payload,
694
+ session_hash,
695
+ event_id
696
+ },
697
+ hf_token
698
+ );
699
+ if (status !== 200) {
700
+ fire_event({
701
+ type: "status",
702
+ stage: "error",
703
+ message: BROKEN_CONNECTION_MSG,
704
+ queue: true,
705
+ endpoint: _endpoint,
706
+ fn_index,
707
+ time: new Date()
708
+ });
709
+ eventSource.close();
710
+ }
711
+ } else if (type === "complete") {
712
+ complete = status;
713
+ } else if (type === "log") {
714
+ fire_event({
715
+ type: "log",
716
+ log: data.log,
717
+ level: data.level,
718
+ endpoint: _endpoint,
719
+ fn_index
720
+ });
721
+ } else if (type === "generating") {
722
+ fire_event({
723
+ type: "status",
724
+ time: new Date(),
725
+ ...status,
726
+ stage: status?.stage!,
727
+ queue: true,
728
+ endpoint: _endpoint,
729
+ fn_index
730
+ });
731
+ }
732
+ if (data) {
733
+ fire_event({
734
+ type: "data",
735
+ time: new Date(),
736
+ data: transform_files
737
+ ? transform_output(
738
+ data.data,
739
+ api_info,
740
+ config.root,
741
+ config.root_url
742
+ )
743
+ : data.data,
744
+ endpoint: _endpoint,
745
+ fn_index
746
+ });
747
+
748
+ if (complete) {
749
+ fire_event({
750
+ type: "status",
751
+ time: new Date(),
752
+ ...complete,
753
+ stage: status?.stage!,
754
+ queue: true,
755
+ endpoint: _endpoint,
756
+ fn_index
757
+ });
758
+ eventSource.close();
759
+ }
760
+ }
761
+ };
637
762
  }
638
763
  });
639
764
 
@@ -681,12 +806,19 @@ export function api_factory(
681
806
  fn_index: fn_index
682
807
  });
683
808
 
684
- if (websocket && websocket.readyState === 0) {
685
- websocket.addEventListener("open", () => {
809
+ let cancel_request = {};
810
+ if (protocol === "ws") {
811
+ if (websocket && websocket.readyState === 0) {
812
+ websocket.addEventListener("open", () => {
813
+ websocket.close();
814
+ });
815
+ } else {
686
816
  websocket.close();
687
- });
817
+ }
818
+ cancel_request = { fn_index, session_hash };
688
819
  } else {
689
- websocket.close();
820
+ eventSource.close();
821
+ cancel_request = { event_id };
690
822
  }
691
823
 
692
824
  try {
@@ -699,7 +831,7 @@ export function api_factory(
699
831
  {
700
832
  headers: { "Content-Type": "application/json" },
701
833
  method: "POST",
702
- body: JSON.stringify({ fn_index, session_hash })
834
+ body: JSON.stringify(cancel_request)
703
835
  }
704
836
  );
705
837
  } catch (e) {
@@ -843,28 +975,21 @@ export function api_factory(
843
975
  );
844
976
 
845
977
  return Promise.all(
846
- blob_refs.map(async ({ path, blob, data, type }) => {
978
+ blob_refs.map(async ({ path, blob, type }) => {
847
979
  if (blob) {
848
980
  const file_url = (await upload_files(endpoint, [blob], token))
849
981
  .files[0];
850
- return { path, file_url, type };
982
+ return { path, file_url, type, name: blob?.name };
851
983
  }
852
- return { path, base64: data, type };
984
+ return { path, type };
853
985
  })
854
986
  ).then((r) => {
855
- r.forEach(({ path, file_url, base64, type }) => {
856
- if (base64) {
857
- update_object(data, base64, path);
858
- } else if (type === "Gallery") {
987
+ r.forEach(({ path, file_url, type, name }) => {
988
+ if (type === "Gallery") {
859
989
  update_object(data, file_url, path);
860
990
  } else if (file_url) {
861
- const o = {
862
- is_file: true,
863
- name: `${file_url}`,
864
- data: null
865
- // orig_name: "file.csv"
866
- };
867
- update_object(data, o, path);
991
+ const file = new FileData({ path: file_url, orig_name: name });
992
+ update_object(data, file, path);
868
993
  }
869
994
  });
870
995
 
@@ -893,57 +1018,13 @@ function transform_output(
893
1018
  ? [normalise_file(img[0], root_url, remote_url), img[1]]
894
1019
  : [normalise_file(img, root_url, remote_url), null];
895
1020
  });
896
- } else if (typeof d === "object" && d?.is_file) {
1021
+ } else if (typeof d === "object" && d.path) {
897
1022
  return normalise_file(d, root_url, remote_url);
898
1023
  }
899
1024
  return d;
900
1025
  });
901
1026
  }
902
1027
 
903
- function normalise_file(
904
- file: FileData[],
905
- root: string,
906
- root_url: string | null
907
- ): FileData[];
908
- function normalise_file(
909
- file: FileData | string,
910
- root: string,
911
- root_url: string | null
912
- ): FileData;
913
- function normalise_file(
914
- file: null,
915
- root: string,
916
- root_url: string | null
917
- ): null;
918
- function normalise_file(file, root, root_url): FileData[] | FileData | null {
919
- if (file == null) return null;
920
- if (typeof file === "string") {
921
- return {
922
- name: "file_data",
923
- data: file
924
- };
925
- } else if (Array.isArray(file)) {
926
- const normalized_file: (FileData | null)[] = [];
927
-
928
- for (const x of file) {
929
- if (x === null) {
930
- normalized_file.push(null);
931
- } else {
932
- normalized_file.push(normalise_file(x, root, root_url));
933
- }
934
- }
935
-
936
- return normalized_file as FileData[];
937
- } else if (file.is_file) {
938
- if (!root_url) {
939
- file.data = root + "/file=" + file.name;
940
- } else {
941
- file.data = "/proxy=" + root_url + "file=" + file.name;
942
- }
943
- }
944
- return file;
945
- }
946
-
947
1028
  interface ApiData {
948
1029
  label: string;
949
1030
  type: {
@@ -1110,7 +1191,6 @@ export async function walk_and_store_blobs(
1110
1191
  ): Promise<
1111
1192
  {
1112
1193
  path: string[];
1113
- data: string | false;
1114
1194
  type: string;
1115
1195
  blob: Blob | false;
1116
1196
  }[]
@@ -1142,28 +1222,9 @@ export async function walk_and_store_blobs(
1142
1222
  {
1143
1223
  path: path,
1144
1224
  blob: is_image ? false : new NodeBlob([param]),
1145
- data: is_image ? `${param.toString("base64")}` : false,
1146
1225
  type
1147
1226
  }
1148
1227
  ];
1149
- } else if (
1150
- param instanceof Blob ||
1151
- (typeof window !== "undefined" && param instanceof File)
1152
- ) {
1153
- if (type === "Image") {
1154
- let data;
1155
-
1156
- if (typeof window !== "undefined") {
1157
- // browser
1158
- data = await image_to_data_uri(param);
1159
- } else {
1160
- const buffer = await param.arrayBuffer();
1161
- data = Buffer.from(buffer).toString("base64");
1162
- }
1163
-
1164
- return [{ path, data, type, blob: false }];
1165
- }
1166
- return [{ path: path, blob: param, type, data: false }];
1167
1228
  } else if (typeof param === "object") {
1168
1229
  let blob_refs = [];
1169
1230
  for (let key in param) {
package/src/index.ts CHANGED
@@ -6,3 +6,10 @@ export {
6
6
  api_factory
7
7
  } from "./client.js";
8
8
  export type { SpaceStatus } from "./types.js";
9
+ export {
10
+ normalise_file,
11
+ FileData,
12
+ upload,
13
+ get_fetchable_url_or_file,
14
+ prepare_files
15
+ } from "./upload.js";
package/src/types.ts CHANGED
@@ -18,6 +18,7 @@ export interface Config {
18
18
  show_api: boolean;
19
19
  stylesheets: string[];
20
20
  path: string;
21
+ protocol?: "sse" | "ws";
21
22
  }
22
23
 
23
24
  export interface Payload {
package/src/upload.ts ADDED
@@ -0,0 +1,173 @@
1
+ import { upload_files } from "./client";
2
+
3
+ export function normalise_file(
4
+ file: FileData | null,
5
+ server_url: string,
6
+ proxy_url: string | null
7
+ ): FileData | null;
8
+
9
+ export function normalise_file(
10
+ file: FileData[] | null,
11
+ server_url: string,
12
+ proxy_url: string | null
13
+ ): FileData[] | null;
14
+
15
+ export function normalise_file(
16
+ file: FileData[] | FileData | null,
17
+ server_url: string, // root: string,
18
+ proxy_url: string | null // root_url: string | null
19
+ ): FileData[] | FileData | null;
20
+
21
+ export function normalise_file(
22
+ file: FileData[] | FileData | null,
23
+ server_url: string, // root: string,
24
+ proxy_url: string | null // root_url: string | null
25
+ ): FileData[] | FileData | null {
26
+ if (file == null) {
27
+ return null;
28
+ }
29
+
30
+ if (Array.isArray(file)) {
31
+ const normalized_file: (FileData | null)[] = [];
32
+
33
+ for (const x of file) {
34
+ if (x == null) {
35
+ normalized_file.push(null);
36
+ } else {
37
+ normalized_file.push(normalise_file(x, server_url, proxy_url));
38
+ }
39
+ }
40
+
41
+ return normalized_file as FileData[];
42
+ }
43
+
44
+ if (file.is_stream) {
45
+ if (proxy_url == null) {
46
+ return new FileData({
47
+ ...file,
48
+ url: server_url + "/stream/" + file.path
49
+ });
50
+ }
51
+ return new FileData({
52
+ ...file,
53
+ url: "/proxy=" + proxy_url + "stream/" + file.path
54
+ });
55
+ }
56
+
57
+ return new FileData({
58
+ ...file,
59
+ url: get_fetchable_url_or_file(file.path, server_url, proxy_url)
60
+ });
61
+ }
62
+
63
+ function is_url(str: string): boolean {
64
+ try {
65
+ const url = new URL(str);
66
+ return url.protocol === "http:" || url.protocol === "https:";
67
+ } catch {
68
+ return false;
69
+ }
70
+ }
71
+
72
+ export function get_fetchable_url_or_file(
73
+ path: string | null,
74
+ server_url: string,
75
+ proxy_url: string | null
76
+ ): string {
77
+ if (path == null) {
78
+ return proxy_url ? `/proxy=${proxy_url}file=` : `${server_url}/file=`;
79
+ }
80
+ if (is_url(path)) {
81
+ return path;
82
+ }
83
+ return proxy_url
84
+ ? `/proxy=${proxy_url}file=${path}`
85
+ : `${server_url}/file=${path}`;
86
+ }
87
+
88
+ export async function upload(
89
+ file_data: FileData[],
90
+ root: string,
91
+ upload_fn: typeof upload_files = upload_files
92
+ ): Promise<(FileData | null)[] | null> {
93
+ let files = (Array.isArray(file_data) ? file_data : [file_data]).map(
94
+ (file_data) => file_data.blob!
95
+ );
96
+
97
+ return await Promise.all(
98
+ await upload_fn(root, files).then(
99
+ async (response: { files?: string[]; error?: string }) => {
100
+ if (response.error) {
101
+ throw new Error(response.error);
102
+ } else {
103
+ if (response.files) {
104
+ return response.files.map((f, i) => {
105
+ const file = new FileData({ ...file_data[i], path: f });
106
+
107
+ return normalise_file(file, root, null);
108
+ });
109
+ }
110
+
111
+ return [];
112
+ }
113
+ }
114
+ )
115
+ );
116
+ }
117
+
118
+ export async function prepare_files(
119
+ files: File[],
120
+ is_stream?: boolean
121
+ ): Promise<FileData[]> {
122
+ return files.map(
123
+ (f, i) =>
124
+ new FileData({
125
+ path: f.name,
126
+ orig_name: f.name,
127
+ blob: f,
128
+ size: f.size,
129
+ mime_type: f.type,
130
+ is_stream
131
+ })
132
+ );
133
+ }
134
+
135
+ export class FileData {
136
+ path: string;
137
+ url?: string;
138
+ orig_name: string;
139
+ size?: number;
140
+ blob?: File;
141
+ is_stream?: boolean;
142
+ mime_type?: string;
143
+ alt_text?: string;
144
+
145
+ constructor({
146
+ path,
147
+ url,
148
+ orig_name,
149
+ size,
150
+ blob,
151
+ is_stream,
152
+ mime_type,
153
+ alt_text
154
+ }: {
155
+ path: string;
156
+ url?: string;
157
+ orig_name: string;
158
+ size?: number;
159
+ blob?: File;
160
+ is_stream?: boolean;
161
+ mime_type?: string;
162
+ alt_text?: string;
163
+ }) {
164
+ this.path = path;
165
+ this.url = url;
166
+ this.orig_name = orig_name;
167
+ this.size = size;
168
+ this.blob = url ? undefined : blob;
169
+ this.is_stream = is_stream;
170
+ this.mime_type = mime_type;
171
+ this.alt_text = alt_text;
172
+ }
173
+ }
package/src/utils.ts CHANGED
@@ -92,7 +92,7 @@ export async function process_endpoint(
92
92
  space_id: app_reference,
93
93
  ...determine_protocol(_host)
94
94
  };
95
- } catch (e) {
95
+ } catch (e: any) {
96
96
  throw new Error("Space metadata could not be loaded." + e.message);
97
97
  }
98
98
  }
@@ -167,7 +167,7 @@ export async function get_space_hardware(
167
167
  const { hardware } = await res.json();
168
168
 
169
169
  return hardware;
170
- } catch (e) {
170
+ } catch (e: any) {
171
171
  throw new Error(e.message);
172
172
  }
173
173
  }
@@ -196,7 +196,7 @@ export async function set_space_hardware(
196
196
  const { hardware } = await res.json();
197
197
 
198
198
  return hardware;
199
- } catch (e) {
199
+ } catch (e: any) {
200
200
  throw new Error(e.message);
201
201
  }
202
202
  }
@@ -225,7 +225,7 @@ export async function set_space_timeout(
225
225
  const { hardware } = await res.json();
226
226
 
227
227
  return hardware;
228
- } catch (e) {
228
+ } catch (e: any) {
229
229
  throw new Error(e.message);
230
230
  }
231
231
  }
package/vite.config.js CHANGED
@@ -1,4 +1,8 @@
1
1
  import { defineConfig } from "vite";
2
+ import { svelte } from "@sveltejs/vite-plugin-svelte";
3
+ import { fileURLToPath } from "url";
4
+ import path from "path";
5
+ const __dirname = fileURLToPath(new URL(".", import.meta.url));
2
6
 
3
7
  export default defineConfig({
4
8
  build: {
@@ -13,10 +17,22 @@ export default defineConfig({
13
17
  }
14
18
  }
15
19
  },
20
+ plugins: [
21
+ svelte()
22
+ // {
23
+ // name: "resolve-gradio-client",
24
+ // enforce: "pre",
25
+ // resolveId(id) {
26
+ // if (id === "@gradio/client") {
27
+ // return path.join(__dirname, "src", "index.ts");
28
+ // }
29
+ // }
30
+ // }
31
+ ],
16
32
 
17
33
  ssr: {
18
34
  target: "node",
19
35
  format: "esm",
20
- noExternal: ["ws", "semiver"]
36
+ noExternal: ["ws", "semiver", "@gradio/upload"]
21
37
  }
22
38
  });