@gradio/client 0.9.1 → 0.9.3

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,17 @@
1
1
  # @gradio/client
2
2
 
3
+ ## 0.9.3
4
+
5
+ ### Features
6
+
7
+ - [#6814](https://github.com/gradio-app/gradio/pull/6814) [`828fb9e`](https://github.com/gradio-app/gradio/commit/828fb9e6ce15b6ea08318675a2361117596a1b5d) - Refactor queue so that there are separate queues for each concurrency id. Thanks [@aliabid94](https://github.com/aliabid94)!
8
+
9
+ ## 0.9.2
10
+
11
+ ### Features
12
+
13
+ - [#6798](https://github.com/gradio-app/gradio/pull/6798) [`245d58e`](https://github.com/gradio-app/gradio/commit/245d58eff788e8d44a59d37a2d9b26d0f08a62b4) - Improve how server/js client handle unexpected errors. Thanks [@freddyaboulton](https://github.com/freddyaboulton)!
14
+
3
15
  ## 0.9.1
4
16
 
5
17
  ### Fixes
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAIA,OAAO,EAQN,cAAc,EAEd,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EACX,SAAS,EACT,aAAa,EAIb,YAAY,EACZ,cAAc,EAGd,mBAAmB,EACnB,MAAM,YAAY,CAAC;AAIpB,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,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AAI,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.js CHANGED
@@ -400,6 +400,7 @@ function api_factory(fetch_implementation, EventSource_factory) {
400
400
  const session_hash = Math.random().toString(36).substring(2);
401
401
  const last_status = {};
402
402
  let stream_open = false;
403
+ let pending_stream_messages = {};
403
404
  let event_stream = null;
404
405
  const event_callbacks = {};
405
406
  let config;
@@ -878,78 +879,113 @@ function api_factory(fetch_implementation, EventSource_factory) {
878
879
  });
879
880
  } else {
880
881
  event_id = response.event_id;
881
- if (!stream_open) {
882
- open_stream();
883
- }
884
882
  let callback = async function(_data) {
885
- const { type, status: status2, data: data2 } = handle_message(
886
- _data,
887
- last_status[fn_index]
888
- );
889
- if (type === "update" && status2 && !complete) {
890
- fire_event({
891
- type: "status",
892
- endpoint: _endpoint,
893
- fn_index,
894
- time: /* @__PURE__ */ new Date(),
895
- ...status2
896
- });
897
- } else if (type === "complete") {
898
- complete = status2;
899
- } else if (type === "log") {
900
- fire_event({
901
- type: "log",
902
- log: data2.log,
903
- level: data2.level,
904
- endpoint: _endpoint,
905
- fn_index
906
- });
907
- } else if (type === "generating") {
908
- fire_event({
909
- type: "status",
910
- time: /* @__PURE__ */ new Date(),
911
- ...status2,
912
- stage: status2 == null ? void 0 : status2.stage,
913
- queue: true,
914
- endpoint: _endpoint,
915
- fn_index
916
- });
917
- }
918
- if (data2) {
919
- fire_event({
920
- type: "data",
921
- time: /* @__PURE__ */ new Date(),
922
- data: transform_files ? transform_output(
923
- data2.data,
924
- api_info,
925
- config.root,
926
- config.root_url
927
- ) : data2.data,
928
- endpoint: _endpoint,
929
- fn_index
930
- });
931
- if (complete) {
883
+ try {
884
+ const { type, status: status2, data: data2 } = handle_message(
885
+ _data,
886
+ last_status[fn_index]
887
+ );
888
+ if (type == "heartbeat") {
889
+ return;
890
+ }
891
+ if (type === "update" && status2 && !complete) {
932
892
  fire_event({
933
893
  type: "status",
894
+ endpoint: _endpoint,
895
+ fn_index,
934
896
  time: /* @__PURE__ */ new Date(),
935
- ...complete,
897
+ ...status2
898
+ });
899
+ } else if (type === "complete") {
900
+ complete = status2;
901
+ } else if (type == "unexpected_error") {
902
+ console.error("Unexpected error", status2 == null ? void 0 : status2.message);
903
+ fire_event({
904
+ type: "status",
905
+ stage: "error",
906
+ message: "An Unexpected Error Occurred!",
907
+ queue: true,
908
+ endpoint: _endpoint,
909
+ fn_index,
910
+ time: /* @__PURE__ */ new Date()
911
+ });
912
+ } else if (type === "log") {
913
+ fire_event({
914
+ type: "log",
915
+ log: data2.log,
916
+ level: data2.level,
917
+ endpoint: _endpoint,
918
+ fn_index
919
+ });
920
+ return;
921
+ } else if (type === "generating") {
922
+ fire_event({
923
+ type: "status",
924
+ time: /* @__PURE__ */ new Date(),
925
+ ...status2,
936
926
  stage: status2 == null ? void 0 : status2.stage,
937
927
  queue: true,
938
928
  endpoint: _endpoint,
939
929
  fn_index
940
930
  });
941
931
  }
942
- }
943
- if (status2.stage === "complete" || status2.stage === "error") {
944
- if (event_callbacks[event_id]) {
945
- delete event_callbacks[event_id];
946
- if (Object.keys(event_callbacks).length === 0) {
947
- close_stream();
932
+ if (data2) {
933
+ fire_event({
934
+ type: "data",
935
+ time: /* @__PURE__ */ new Date(),
936
+ data: transform_files ? transform_output(
937
+ data2.data,
938
+ api_info,
939
+ config.root,
940
+ config.root_url
941
+ ) : data2.data,
942
+ endpoint: _endpoint,
943
+ fn_index
944
+ });
945
+ if (complete) {
946
+ fire_event({
947
+ type: "status",
948
+ time: /* @__PURE__ */ new Date(),
949
+ ...complete,
950
+ stage: status2 == null ? void 0 : status2.stage,
951
+ queue: true,
952
+ endpoint: _endpoint,
953
+ fn_index
954
+ });
955
+ }
956
+ }
957
+ if ((status2 == null ? void 0 : status2.stage) === "complete" || (status2 == null ? void 0 : status2.stage) === "error") {
958
+ if (event_callbacks[event_id]) {
959
+ delete event_callbacks[event_id];
960
+ if (Object.keys(event_callbacks).length === 0) {
961
+ close_stream();
962
+ }
948
963
  }
949
964
  }
965
+ } catch (e) {
966
+ console.error("Unexpected client exception", e);
967
+ fire_event({
968
+ type: "status",
969
+ stage: "error",
970
+ message: "An Unexpected Error Occurred!",
971
+ queue: true,
972
+ endpoint: _endpoint,
973
+ fn_index,
974
+ time: /* @__PURE__ */ new Date()
975
+ });
976
+ close_stream();
950
977
  }
951
978
  };
979
+ if (event_id in pending_stream_messages) {
980
+ pending_stream_messages[event_id].forEach(
981
+ (msg) => callback(msg)
982
+ );
983
+ delete pending_stream_messages[event_id];
984
+ }
952
985
  event_callbacks[event_id] = callback;
986
+ if (!stream_open) {
987
+ open_stream();
988
+ }
953
989
  }
954
990
  });
955
991
  }
@@ -1048,7 +1084,21 @@ function api_factory(fetch_implementation, EventSource_factory) {
1048
1084
  event_stream = new EventSource(url);
1049
1085
  event_stream.onmessage = async function(event) {
1050
1086
  let _data = JSON.parse(event.data);
1051
- await event_callbacks[_data.event_id](_data);
1087
+ const event_id = _data.event_id;
1088
+ if (!event_id) {
1089
+ await Promise.all(
1090
+ Object.keys(event_callbacks).map(
1091
+ (event_id2) => event_callbacks[event_id2](_data)
1092
+ )
1093
+ );
1094
+ } else if (event_callbacks[event_id]) {
1095
+ await event_callbacks[event_id](_data);
1096
+ } else {
1097
+ if (!pending_stream_messages[event_id]) {
1098
+ pending_stream_messages[event_id] = [];
1099
+ }
1100
+ pending_stream_messages[event_id].push(_data);
1101
+ }
1052
1102
  };
1053
1103
  }
1054
1104
  function close_stream() {
@@ -1445,6 +1495,20 @@ function handle_message(data, last_status) {
1445
1495
  success: data.success
1446
1496
  }
1447
1497
  };
1498
+ case "heartbeat":
1499
+ return {
1500
+ type: "heartbeat"
1501
+ };
1502
+ case "unexpected_error":
1503
+ return {
1504
+ type: "unexpected_error",
1505
+ status: {
1506
+ queue,
1507
+ message: data.message,
1508
+ stage: "error",
1509
+ success: false
1510
+ }
1511
+ };
1448
1512
  case "estimation":
1449
1513
  return {
1450
1514
  type: "update",
@@ -1504,8 +1568,7 @@ function handle_message(data, last_status) {
1504
1568
  message: !data.success ? data.output.error : void 0,
1505
1569
  stage: data.success ? "complete" : "error",
1506
1570
  code: data.code,
1507
- progress_data: data.progress_data,
1508
- eta: data.output.average_duration
1571
+ progress_data: data.progress_data
1509
1572
  },
1510
1573
  data: data.success ? data.output : null
1511
1574
  };
@@ -1518,7 +1581,8 @@ function handle_message(data, last_status) {
1518
1581
  code: data.code,
1519
1582
  size: data.rank,
1520
1583
  position: 0,
1521
- success: data.success
1584
+ success: data.success,
1585
+ eta: data.eta
1522
1586
  }
1523
1587
  };
1524
1588
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradio/client",
3
- "version": "0.9.1",
3
+ "version": "0.9.3",
4
4
  "description": "Gradio API client",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/client.ts CHANGED
@@ -287,6 +287,7 @@ export function api_factory(
287
287
  const session_hash = Math.random().toString(36).substring(2);
288
288
  const last_status: Record<string, Status["stage"]> = {};
289
289
  let stream_open = false;
290
+ let pending_stream_messages: Record<string, any[]> = {}; // Event messages may be received by the SSE stream before the initial data POST request is complete. To resolve this race condition, we store the messages in a dictionary and process them when the POST request is complete.
290
291
  let event_stream: EventSource | null = null;
291
292
  const event_callbacks: Record<string, () => Promise<void>> = {};
292
293
  let config: Config;
@@ -821,84 +822,127 @@ export function api_factory(
821
822
  });
822
823
  } else {
823
824
  event_id = response.event_id as string;
824
- if (!stream_open) {
825
- open_stream();
826
- }
827
825
  let callback = async function (_data: object): void {
828
- const { type, status, data } = handle_message(
829
- _data,
830
- last_status[fn_index]
831
- );
832
-
833
- if (type === "update" && status && !complete) {
834
- // call 'status' listeners
835
- fire_event({
836
- type: "status",
837
- endpoint: _endpoint,
838
- fn_index,
839
- time: new Date(),
840
- ...status
841
- });
842
- } else if (type === "complete") {
843
- complete = status;
844
- } else if (type === "log") {
845
- fire_event({
846
- type: "log",
847
- log: data.log,
848
- level: data.level,
849
- endpoint: _endpoint,
850
- fn_index
851
- });
852
- } else if (type === "generating") {
853
- fire_event({
854
- type: "status",
855
- time: new Date(),
856
- ...status,
857
- stage: status?.stage!,
858
- queue: true,
859
- endpoint: _endpoint,
860
- fn_index
861
- });
862
- }
863
- if (data) {
864
- fire_event({
865
- type: "data",
866
- time: new Date(),
867
- data: transform_files
868
- ? transform_output(
869
- data.data,
870
- api_info,
871
- config.root,
872
- config.root_url
873
- )
874
- : data.data,
875
- endpoint: _endpoint,
876
- fn_index
877
- });
826
+ try {
827
+ const { type, status, data } = handle_message(
828
+ _data,
829
+ last_status[fn_index]
830
+ );
831
+
832
+ // TODO: Find out how to print this information
833
+ // only during testing
834
+ // console.info("data", type, status, data);
835
+
836
+ if (type == "heartbeat") {
837
+ return;
838
+ }
878
839
 
879
- if (complete) {
840
+ if (type === "update" && status && !complete) {
841
+ // call 'status' listeners
880
842
  fire_event({
881
843
  type: "status",
844
+ endpoint: _endpoint,
845
+ fn_index,
882
846
  time: new Date(),
883
- ...complete,
847
+ ...status
848
+ });
849
+ } else if (type === "complete") {
850
+ complete = status;
851
+ } else if (type == "unexpected_error") {
852
+ console.error("Unexpected error", status?.message);
853
+ fire_event({
854
+ type: "status",
855
+ stage: "error",
856
+ message: "An Unexpected Error Occurred!",
857
+ queue: true,
858
+ endpoint: _endpoint,
859
+ fn_index,
860
+ time: new Date()
861
+ });
862
+ } else if (type === "log") {
863
+ fire_event({
864
+ type: "log",
865
+ log: data.log,
866
+ level: data.level,
867
+ endpoint: _endpoint,
868
+ fn_index
869
+ });
870
+ return;
871
+ } else if (type === "generating") {
872
+ fire_event({
873
+ type: "status",
874
+ time: new Date(),
875
+ ...status,
884
876
  stage: status?.stage!,
885
877
  queue: true,
886
878
  endpoint: _endpoint,
887
879
  fn_index
888
880
  });
889
881
  }
890
- }
882
+ if (data) {
883
+ fire_event({
884
+ type: "data",
885
+ time: new Date(),
886
+ data: transform_files
887
+ ? transform_output(
888
+ data.data,
889
+ api_info,
890
+ config.root,
891
+ config.root_url
892
+ )
893
+ : data.data,
894
+ endpoint: _endpoint,
895
+ fn_index
896
+ });
897
+
898
+ if (complete) {
899
+ fire_event({
900
+ type: "status",
901
+ time: new Date(),
902
+ ...complete,
903
+ stage: status?.stage!,
904
+ queue: true,
905
+ endpoint: _endpoint,
906
+ fn_index
907
+ });
908
+ }
909
+ }
891
910
 
892
- if (status.stage === "complete" || status.stage === "error") {
893
- if (event_callbacks[event_id]) {
894
- delete event_callbacks[event_id];
895
- if (Object.keys(event_callbacks).length === 0) {
896
- close_stream();
911
+ if (
912
+ status?.stage === "complete" ||
913
+ status?.stage === "error"
914
+ ) {
915
+ if (event_callbacks[event_id]) {
916
+ delete event_callbacks[event_id];
917
+ if (Object.keys(event_callbacks).length === 0) {
918
+ close_stream();
919
+ }
897
920
  }
898
921
  }
922
+ } catch (e) {
923
+ console.error("Unexpected client exception", e);
924
+ fire_event({
925
+ type: "status",
926
+ stage: "error",
927
+ message: "An Unexpected Error Occurred!",
928
+ queue: true,
929
+ endpoint: _endpoint,
930
+ fn_index,
931
+ time: new Date()
932
+ });
933
+ close_stream();
899
934
  }
900
935
  };
936
+ if (event_id in pending_stream_messages) {
937
+ pending_stream_messages[event_id].forEach((msg) =>
938
+ callback(msg)
939
+ );
940
+ delete pending_stream_messages[event_id];
941
+ }
901
942
  event_callbacks[event_id] = callback;
943
+ if (!stream_open) {
944
+ open_stream();
945
+ }
902
946
  }
903
947
  });
904
948
  }
@@ -1014,7 +1058,21 @@ export function api_factory(
1014
1058
  event_stream = new EventSource(url);
1015
1059
  event_stream.onmessage = async function (event) {
1016
1060
  let _data = JSON.parse(event.data);
1017
- await event_callbacks[_data.event_id](_data);
1061
+ const event_id = _data.event_id;
1062
+ if (!event_id) {
1063
+ await Promise.all(
1064
+ Object.keys(event_callbacks).map((event_id) =>
1065
+ event_callbacks[event_id](_data)
1066
+ )
1067
+ );
1068
+ } else if (event_callbacks[event_id]) {
1069
+ await event_callbacks[event_id](_data);
1070
+ } else {
1071
+ if (!pending_stream_messages[event_id]) {
1072
+ pending_stream_messages[event_id] = [];
1073
+ }
1074
+ pending_stream_messages[event_id].push(_data);
1075
+ }
1018
1076
  };
1019
1077
  }
1020
1078
 
@@ -1583,6 +1641,20 @@ function handle_message(
1583
1641
  success: data.success
1584
1642
  }
1585
1643
  };
1644
+ case "heartbeat":
1645
+ return {
1646
+ type: "heartbeat"
1647
+ };
1648
+ case "unexpected_error":
1649
+ return {
1650
+ type: "unexpected_error",
1651
+ status: {
1652
+ queue,
1653
+ message: data.message,
1654
+ stage: "error",
1655
+ success: false
1656
+ }
1657
+ };
1586
1658
  case "estimation":
1587
1659
  return {
1588
1660
  type: "update",
@@ -1642,8 +1714,7 @@ function handle_message(
1642
1714
  message: !data.success ? data.output.error : undefined,
1643
1715
  stage: data.success ? "complete" : "error",
1644
1716
  code: data.code,
1645
- progress_data: data.progress_data,
1646
- eta: data.output.average_duration
1717
+ progress_data: data.progress_data
1647
1718
  },
1648
1719
  data: data.success ? data.output : null
1649
1720
  };
@@ -1657,7 +1728,8 @@ function handle_message(
1657
1728
  code: data.code,
1658
1729
  size: data.rank,
1659
1730
  position: 0,
1660
- success: data.success
1731
+ success: data.success,
1732
+ eta: data.eta
1661
1733
  }
1662
1734
  };
1663
1735
  }