Jiawei Dong commited on
Commit
e2eb98f
·
1 Parent(s): c86fbed

eng updated

Browse files
Files changed (1) hide show
  1. app.py +15 -33
app.py CHANGED
@@ -430,10 +430,23 @@ UPDATE_RESULTS_JS = """
430
  if (window.__hiroResults == null) {
431
  window.__hiroResults = { count: 0, layout: null, lastJson: "" };
432
  }
433
- if (raw === window.__hiroResults.lastJson) return;
434
- window.__hiroResults.lastJson = raw;
 
 
 
 
 
435
 
436
  const store = window.__hiroResults;
 
 
 
 
 
 
 
 
437
  const esc = (value) => {
438
  const el = document.createElement("div");
439
  el.textContent = value == null ? "" : String(value);
@@ -454,13 +467,6 @@ UPDATE_RESULTS_JS = """
454
  return { src: `Source (${src})`, tgt: `Target (${tgt})` };
455
  };
456
 
457
- let state;
458
- try {
459
- state = JSON.parse(raw || "{}");
460
- } catch (_err) {
461
- return;
462
- }
463
-
464
  const meta = document.getElementById("hiro-progress-meta");
465
  const fill = document.getElementById("hiro-progress-fill");
466
  const pct = document.getElementById("hiro-progress-pct");
@@ -568,28 +574,6 @@ INIT_STREAM_WATCH_JS = """
568
  }
569
  """
570
 
571
- RESET_STREAM_JS = """
572
- (text, lang, mode, api_key) => {
573
- if (window.__hiroResults) {
574
- window.__hiroResults.count = 0;
575
- window.__hiroResults.layout = null;
576
- window.__hiroResults.lastJson = "";
577
- }
578
- return [text, lang, mode, api_key];
579
- }
580
- """
581
-
582
- CLEAR_STREAM_JS = """
583
- () => {
584
- if (window.__hiroResults) {
585
- window.__hiroResults.count = 0;
586
- window.__hiroResults.layout = null;
587
- window.__hiroResults.lastJson = "";
588
- }
589
- return [];
590
- }
591
- """
592
-
593
 
594
  def run_translation(text: str, lang: str, mode: str, api_key: str = ""):
595
  text = (text or "").strip()
@@ -809,7 +793,6 @@ def build_ui() -> gr.Blocks:
809
  fn=run_translation,
810
  inputs=[input_text, lang, mode, api_key],
811
  outputs=[stream_state, results_panel, copy_btn],
812
- js=RESET_STREAM_JS,
813
  )
814
 
815
  stream_state.change(
@@ -844,7 +827,6 @@ def build_ui() -> gr.Blocks:
844
  clear_btn.click(
845
  on_clear,
846
  outputs=[input_text, stream_state, results_panel, copy_btn],
847
- js=CLEAR_STREAM_JS,
848
  )
849
 
850
  demo.load(
 
430
  if (window.__hiroResults == null) {
431
  window.__hiroResults = { count: 0, layout: null, lastJson: "" };
432
  }
433
+
434
+ let state;
435
+ try {
436
+ state = JSON.parse(raw || "{}");
437
+ } catch (_err) {
438
+ return;
439
+ }
440
 
441
  const store = window.__hiroResults;
442
+ if (state.reset) {
443
+ store.count = 0;
444
+ store.layout = null;
445
+ store.lastJson = "";
446
+ }
447
+ if (raw === store.lastJson) return;
448
+ store.lastJson = raw;
449
+
450
  const esc = (value) => {
451
  const el = document.createElement("div");
452
  el.textContent = value == null ? "" : String(value);
 
467
  return { src: `Source (${src})`, tgt: `Target (${tgt})` };
468
  };
469
 
 
 
 
 
 
 
 
470
  const meta = document.getElementById("hiro-progress-meta");
471
  const fill = document.getElementById("hiro-progress-fill");
472
  const pct = document.getElementById("hiro-progress-pct");
 
574
  }
575
  """
576
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
577
 
578
  def run_translation(text: str, lang: str, mode: str, api_key: str = ""):
579
  text = (text or "").strip()
 
793
  fn=run_translation,
794
  inputs=[input_text, lang, mode, api_key],
795
  outputs=[stream_state, results_panel, copy_btn],
 
796
  )
797
 
798
  stream_state.change(
 
827
  clear_btn.click(
828
  on_clear,
829
  outputs=[input_text, stream_state, results_panel, copy_btn],
 
830
  )
831
 
832
  demo.load(