@gradio/client 0.9.0 → 0.9.2

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.2
4
+
5
+ ### Features
6
+
7
+ - [#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)!
8
+
9
+ ## 0.9.1
10
+
11
+ ### Fixes
12
+
13
+ - [#6693](https://github.com/gradio-app/gradio/pull/6693) [`34f9431`](https://github.com/gradio-app/gradio/commit/34f943101bf7dd6b8a8974a6131c1ed7c4a0dac0) - Python client properly handles hearbeat and log messages. Also handles responses longer than 65k. Thanks [@freddyaboulton](https://github.com/freddyaboulton)!
14
+
3
15
  ## 0.9.0
4
16
 
5
17
  ### Features
@@ -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,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AI,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.js CHANGED
@@ -342,8 +342,16 @@ function api_factory(fetch_implementation, EventSource_factory) {
342
342
  } catch (e) {
343
343
  return [{ error: BROKEN_CONNECTION_MSG }, 500];
344
344
  }
345
- const output = await response.json();
346
- return [output, response.status];
345
+ let output;
346
+ let status;
347
+ try {
348
+ output = await response.json();
349
+ status = response.status;
350
+ } catch (e) {
351
+ output = { error: `Could not parse server response: ${e}` };
352
+ status = 500;
353
+ }
354
+ return [output, status];
347
355
  }
348
356
  async function upload_files2(root, files, token, upload_id) {
349
357
  const headers = {};
@@ -848,7 +856,17 @@ function api_factory(fetch_implementation, EventSource_factory) {
848
856
  },
849
857
  hf_token
850
858
  ).then(([response, status]) => {
851
- if (status !== 200) {
859
+ if (status === 503) {
860
+ fire_event({
861
+ type: "status",
862
+ stage: "error",
863
+ message: QUEUE_FULL_MSG,
864
+ queue: true,
865
+ endpoint: _endpoint,
866
+ fn_index,
867
+ time: /* @__PURE__ */ new Date()
868
+ });
869
+ } else if (status !== 200) {
852
870
  fire_event({
853
871
  type: "status",
854
872
  stage: "error",
@@ -860,78 +878,107 @@ function api_factory(fetch_implementation, EventSource_factory) {
860
878
  });
861
879
  } else {
862
880
  event_id = response.event_id;
863
- if (!stream_open) {
864
- open_stream();
865
- }
866
881
  let callback = async function(_data) {
867
- const { type, status: status2, data: data2 } = handle_message(
868
- _data,
869
- last_status[fn_index]
870
- );
871
- if (type === "update" && status2 && !complete) {
872
- fire_event({
873
- type: "status",
874
- endpoint: _endpoint,
875
- fn_index,
876
- time: /* @__PURE__ */ new Date(),
877
- ...status2
878
- });
879
- } else if (type === "complete") {
880
- complete = status2;
881
- } else if (type === "log") {
882
- fire_event({
883
- type: "log",
884
- log: data2.log,
885
- level: data2.level,
886
- endpoint: _endpoint,
887
- fn_index
888
- });
889
- } else if (type === "generating") {
890
- fire_event({
891
- type: "status",
892
- time: /* @__PURE__ */ new Date(),
893
- ...status2,
894
- stage: status2 == null ? void 0 : status2.stage,
895
- queue: true,
896
- endpoint: _endpoint,
897
- fn_index
898
- });
899
- }
900
- if (data2) {
901
- fire_event({
902
- type: "data",
903
- time: /* @__PURE__ */ new Date(),
904
- data: transform_files ? transform_output(
905
- data2.data,
906
- api_info,
907
- config.root,
908
- config.root_url
909
- ) : data2.data,
910
- endpoint: _endpoint,
911
- fn_index
912
- });
913
- if (complete) {
882
+ try {
883
+ const { type, status: status2, data: data2 } = handle_message(
884
+ _data,
885
+ last_status[fn_index]
886
+ );
887
+ if (type == "heartbeat") {
888
+ return;
889
+ }
890
+ if (type === "update" && status2 && !complete) {
914
891
  fire_event({
915
892
  type: "status",
893
+ endpoint: _endpoint,
894
+ fn_index,
916
895
  time: /* @__PURE__ */ new Date(),
917
- ...complete,
896
+ ...status2
897
+ });
898
+ } else if (type === "complete") {
899
+ complete = status2;
900
+ } else if (type == "unexpected_error") {
901
+ console.error("Unexpected error", status2 == null ? void 0 : status2.message);
902
+ fire_event({
903
+ type: "status",
904
+ stage: "error",
905
+ message: "An Unexpected Error Occurred!",
906
+ queue: true,
907
+ endpoint: _endpoint,
908
+ fn_index,
909
+ time: /* @__PURE__ */ new Date()
910
+ });
911
+ } else if (type === "log") {
912
+ fire_event({
913
+ type: "log",
914
+ log: data2.log,
915
+ level: data2.level,
916
+ endpoint: _endpoint,
917
+ fn_index
918
+ });
919
+ return;
920
+ } else if (type === "generating") {
921
+ fire_event({
922
+ type: "status",
923
+ time: /* @__PURE__ */ new Date(),
924
+ ...status2,
918
925
  stage: status2 == null ? void 0 : status2.stage,
919
926
  queue: true,
920
927
  endpoint: _endpoint,
921
928
  fn_index
922
929
  });
923
930
  }
924
- }
925
- if (status2.stage === "complete" || status2.stage === "error") {
926
- if (event_callbacks[event_id]) {
927
- delete event_callbacks[event_id];
928
- if (Object.keys(event_callbacks).length === 0) {
929
- close_stream();
931
+ if (data2) {
932
+ fire_event({
933
+ type: "data",
934
+ time: /* @__PURE__ */ new Date(),
935
+ data: transform_files ? transform_output(
936
+ data2.data,
937
+ api_info,
938
+ config.root,
939
+ config.root_url
940
+ ) : data2.data,
941
+ endpoint: _endpoint,
942
+ fn_index
943
+ });
944
+ if (complete) {
945
+ fire_event({
946
+ type: "status",
947
+ time: /* @__PURE__ */ new Date(),
948
+ ...complete,
949
+ stage: status2 == null ? void 0 : status2.stage,
950
+ queue: true,
951
+ endpoint: _endpoint,
952
+ fn_index
953
+ });
954
+ }
955
+ }
956
+ if (status2.stage === "complete" || status2.stage === "error") {
957
+ if (event_callbacks[event_id]) {
958
+ delete event_callbacks[event_id];
959
+ if (Object.keys(event_callbacks).length === 0) {
960
+ close_stream();
961
+ }
930
962
  }
931
963
  }
964
+ } catch (e) {
965
+ console.error("Unexpected client exception", e);
966
+ fire_event({
967
+ type: "status",
968
+ stage: "error",
969
+ message: "An Unexpected Error Occurred!",
970
+ queue: true,
971
+ endpoint: _endpoint,
972
+ fn_index,
973
+ time: /* @__PURE__ */ new Date()
974
+ });
975
+ close_stream();
932
976
  }
933
977
  };
934
978
  event_callbacks[event_id] = callback;
979
+ if (!stream_open) {
980
+ open_stream();
981
+ }
935
982
  }
936
983
  });
937
984
  }
@@ -1030,6 +1077,14 @@ function api_factory(fetch_implementation, EventSource_factory) {
1030
1077
  event_stream = new EventSource(url);
1031
1078
  event_stream.onmessage = async function(event) {
1032
1079
  let _data = JSON.parse(event.data);
1080
+ if (!("event_id" in _data)) {
1081
+ await Promise.all(
1082
+ Object.keys(event_callbacks).map(
1083
+ (event_id) => event_callbacks[event_id](_data)
1084
+ )
1085
+ );
1086
+ return;
1087
+ }
1033
1088
  await event_callbacks[_data.event_id](_data);
1034
1089
  };
1035
1090
  }
@@ -1427,6 +1482,20 @@ function handle_message(data, last_status) {
1427
1482
  success: data.success
1428
1483
  }
1429
1484
  };
1485
+ case "heartbeat":
1486
+ return {
1487
+ type: "heartbeat"
1488
+ };
1489
+ case "unexpected_error":
1490
+ return {
1491
+ type: "unexpected_error",
1492
+ status: {
1493
+ queue,
1494
+ message: data.message,
1495
+ stage: "error",
1496
+ success: false
1497
+ }
1498
+ };
1430
1499
  case "estimation":
1431
1500
  return {
1432
1501
  type: "update",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradio/client",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "Gradio API client",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/client.ts CHANGED
@@ -203,8 +203,16 @@ export function api_factory(
203
203
  } catch (e) {
204
204
  return [{ error: BROKEN_CONNECTION_MSG }, 500];
205
205
  }
206
- const output: PostResponse = await response.json();
207
- return [output, response.status];
206
+ let output: PostResponse;
207
+ let status: int;
208
+ try {
209
+ output = await response.json();
210
+ status = response.status;
211
+ } catch (e) {
212
+ output = { error: `Could not parse server response: ${e}` };
213
+ status = 500;
214
+ }
215
+ return [output, status];
208
216
  }
209
217
 
210
218
  async function upload_files(
@@ -791,7 +799,17 @@ export function api_factory(
791
799
  },
792
800
  hf_token
793
801
  ).then(([response, status]) => {
794
- if (status !== 200) {
802
+ if (status === 503) {
803
+ fire_event({
804
+ type: "status",
805
+ stage: "error",
806
+ message: QUEUE_FULL_MSG,
807
+ queue: true,
808
+ endpoint: _endpoint,
809
+ fn_index,
810
+ time: new Date()
811
+ });
812
+ } else if (status !== 200) {
795
813
  fire_event({
796
814
  type: "status",
797
815
  stage: "error",
@@ -803,85 +821,121 @@ export function api_factory(
803
821
  });
804
822
  } else {
805
823
  event_id = response.event_id as string;
806
- if (!stream_open) {
807
- open_stream();
808
- }
809
-
810
824
  let callback = async function (_data: object): void {
811
- const { type, status, data } = handle_message(
812
- _data,
813
- last_status[fn_index]
814
- );
815
-
816
- if (type === "update" && status && !complete) {
817
- // call 'status' listeners
818
- fire_event({
819
- type: "status",
820
- endpoint: _endpoint,
821
- fn_index,
822
- time: new Date(),
823
- ...status
824
- });
825
- } else if (type === "complete") {
826
- complete = status;
827
- } else if (type === "log") {
828
- fire_event({
829
- type: "log",
830
- log: data.log,
831
- level: data.level,
832
- endpoint: _endpoint,
833
- fn_index
834
- });
835
- } else if (type === "generating") {
836
- fire_event({
837
- type: "status",
838
- time: new Date(),
839
- ...status,
840
- stage: status?.stage!,
841
- queue: true,
842
- endpoint: _endpoint,
843
- fn_index
844
- });
845
- }
846
- if (data) {
847
- fire_event({
848
- type: "data",
849
- time: new Date(),
850
- data: transform_files
851
- ? transform_output(
852
- data.data,
853
- api_info,
854
- config.root,
855
- config.root_url
856
- )
857
- : data.data,
858
- endpoint: _endpoint,
859
- fn_index
860
- });
825
+ try {
826
+ const { type, status, data } = handle_message(
827
+ _data,
828
+ last_status[fn_index]
829
+ );
830
+
831
+ // TODO: Find out how to print this information
832
+ // only during testing
833
+ // console.info("data", type, status, data);
834
+
835
+ if (type == "heartbeat") {
836
+ return;
837
+ }
861
838
 
862
- if (complete) {
839
+ if (type === "update" && status && !complete) {
840
+ // call 'status' listeners
841
+ fire_event({
842
+ type: "status",
843
+ endpoint: _endpoint,
844
+ fn_index,
845
+ time: new Date(),
846
+ ...status
847
+ });
848
+ } else if (type === "complete") {
849
+ complete = status;
850
+ } else if (type == "unexpected_error") {
851
+ console.error("Unexpected error", status?.message);
852
+ fire_event({
853
+ type: "status",
854
+ stage: "error",
855
+ message: "An Unexpected Error Occurred!",
856
+ queue: true,
857
+ endpoint: _endpoint,
858
+ fn_index,
859
+ time: new Date()
860
+ });
861
+ } else if (type === "log") {
862
+ fire_event({
863
+ type: "log",
864
+ log: data.log,
865
+ level: data.level,
866
+ endpoint: _endpoint,
867
+ fn_index
868
+ });
869
+ return;
870
+ } else if (type === "generating") {
863
871
  fire_event({
864
872
  type: "status",
865
873
  time: new Date(),
866
- ...complete,
874
+ ...status,
867
875
  stage: status?.stage!,
868
876
  queue: true,
869
877
  endpoint: _endpoint,
870
878
  fn_index
871
879
  });
872
880
  }
873
- }
881
+ if (data) {
882
+ fire_event({
883
+ type: "data",
884
+ time: new Date(),
885
+ data: transform_files
886
+ ? transform_output(
887
+ data.data,
888
+ api_info,
889
+ config.root,
890
+ config.root_url
891
+ )
892
+ : data.data,
893
+ endpoint: _endpoint,
894
+ fn_index
895
+ });
896
+
897
+ if (complete) {
898
+ fire_event({
899
+ type: "status",
900
+ time: new Date(),
901
+ ...complete,
902
+ stage: status?.stage!,
903
+ queue: true,
904
+ endpoint: _endpoint,
905
+ fn_index
906
+ });
907
+ }
908
+ }
874
909
 
875
- if (status.stage === "complete" || status.stage === "error") {
876
- if (event_callbacks[event_id]) {
877
- delete event_callbacks[event_id];
878
- if (Object.keys(event_callbacks).length === 0) {
879
- close_stream();
910
+ if (
911
+ status.stage === "complete" ||
912
+ status.stage === "error"
913
+ ) {
914
+ if (event_callbacks[event_id]) {
915
+ delete event_callbacks[event_id];
916
+ if (Object.keys(event_callbacks).length === 0) {
917
+ close_stream();
918
+ }
880
919
  }
881
920
  }
921
+ } catch (e) {
922
+ console.error("Unexpected client exception", e);
923
+ fire_event({
924
+ type: "status",
925
+ stage: "error",
926
+ message: "An Unexpected Error Occurred!",
927
+ queue: true,
928
+ endpoint: _endpoint,
929
+ fn_index,
930
+ time: new Date()
931
+ });
932
+ close_stream();
882
933
  }
883
934
  };
884
935
  event_callbacks[event_id] = callback;
936
+ if (!stream_open) {
937
+ open_stream();
938
+ }
885
939
  }
886
940
  });
887
941
  }
@@ -997,6 +1051,14 @@ export function api_factory(
997
1051
  event_stream = new EventSource(url);
998
1052
  event_stream.onmessage = async function (event) {
999
1053
  let _data = JSON.parse(event.data);
1054
+ if (!("event_id" in _data)) {
1055
+ await Promise.all(
1056
+ Object.keys(event_callbacks).map((event_id) =>
1057
+ event_callbacks[event_id](_data)
1058
+ )
1059
+ );
1060
+ return;
1061
+ }
1000
1062
  await event_callbacks[_data.event_id](_data);
1001
1063
  };
1002
1064
  }
@@ -1566,6 +1628,20 @@ function handle_message(
1566
1628
  success: data.success
1567
1629
  }
1568
1630
  };
1631
+ case "heartbeat":
1632
+ return {
1633
+ type: "heartbeat"
1634
+ };
1635
+ case "unexpected_error":
1636
+ return {
1637
+ type: "unexpected_error",
1638
+ status: {
1639
+ queue,
1640
+ message: data.message,
1641
+ stage: "error",
1642
+ success: false
1643
+ }
1644
+ };
1569
1645
  case "estimation":
1570
1646
  return {
1571
1647
  type: "update",