GopiBNA commited on
Commit
5d4d6fa
·
verified ·
1 Parent(s): d024be9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -2
app.py CHANGED
@@ -550,9 +550,31 @@ def _kpi(label, value, kind=""):
550
  )
551
 
552
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
553
  def _run_batch_in_subprocess(ai_dir, out_dir, tie_path, email, password,
554
  lang, log_box, progress_callback,
555
- bna_cookies=""):
556
  """Spawn a fresh Python subprocess to run the batch. Each subprocess
557
  gets a clean Playwright instance — sidesteps the "Event loop is
558
  closed" issue we'd hit if we called sync_playwright twice in this
@@ -594,6 +616,7 @@ def _run_batch_in_subprocess(ai_dir, out_dir, tie_path, email, password,
594
 
595
  # Tail the log file while the subprocess runs
596
  last_pos = 0
 
597
  while proc.poll() is None:
598
  try:
599
  with log_jsonl.open("r", encoding="utf-8") as fh:
@@ -608,15 +631,28 @@ def _run_batch_in_subprocess(ai_dir, out_dir, tie_path, email, password,
608
  except Exception:
609
  continue
610
  if rec.get("type") == "log":
611
- ss.log_lines.append(f"[{rec['level']}] {rec['message']}")
 
612
  if len(ss.log_lines) > 500:
613
  ss.log_lines[:] = ss.log_lines[-500:]
 
 
614
  elif rec.get("type") == "progress":
615
  progress_callback(rec["info"])
616
  try:
617
  log_box.code("\n".join(ss.log_lines[-60:]), language="text")
618
  except Exception:
619
  pass
 
 
 
 
 
 
 
 
 
 
620
  except Exception:
621
  pass
622
  time.sleep(0.6)
@@ -756,6 +792,11 @@ with tab_inputs:
756
  )
757
  else:
758
  st.progress(0.05, text="Starting up the browser session…")
 
 
 
 
 
759
 
760
  elif ss.run_status == "success" and ss.last_run is not None:
761
  s = ss.last_run
@@ -1006,6 +1047,10 @@ if ss.trigger_run:
1006
  ss.queue_id = None
1007
  raise
1008
 
 
 
 
 
1009
  try:
1010
  with tab_log:
1011
  log_box = st.empty()
@@ -1035,6 +1080,7 @@ if ss.trigger_run:
1035
  ai_dir=ai_dir, out_dir=engine_out, tie_path=tie_path,
1036
  email=bna_email, password=bna_password,
1037
  lang=lang, log_box=log_box, progress_callback=_progress_cb,
 
1038
  )
1039
  if error:
1040
  ss.log_lines.append(f"[ERROR] {error}")
 
550
  )
551
 
552
 
553
+ def _live_error_banner(lines: list[str]) -> str:
554
+ """Build the live error/warning banner HTML for the Inputs tab.
555
+ Only the last few ERROR/WARNING lines are shown so the banner
556
+ stays compact during a long-running batch."""
557
+ error_lines = [l for l in lines if "[ERROR]" in l or "[WARNING]" in l]
558
+ if not error_lines:
559
+ return ""
560
+ recent = error_lines[-5:]
561
+ body = "\n".join(recent).replace("<", "&lt;").replace(">", "&gt;")
562
+ return (
563
+ '<div class="status-banner error" style="margin-top: 14px;">'
564
+ '<div class="icon">⚠️</div>'
565
+ '<div style="flex:1;"><b>Something is going wrong in the run.</b>'
566
+ '<br>The Log tab has the full trace. Latest lines:'
567
+ f'<pre style="white-space: pre-wrap; margin: 8px 0 0; '
568
+ f'font-size: 12px; background: rgba(255,255,255,0.5); '
569
+ f'padding: 8px; border-radius: 4px; max-height: 160px; '
570
+ f'overflow: auto;">{body}</pre>'
571
+ '</div></div>'
572
+ )
573
+
574
+
575
  def _run_batch_in_subprocess(ai_dir, out_dir, tie_path, email, password,
576
  lang, log_box, progress_callback,
577
+ bna_cookies="", inputs_error_box=None):
578
  """Spawn a fresh Python subprocess to run the batch. Each subprocess
579
  gets a clean Playwright instance — sidesteps the "Event loop is
580
  closed" issue we'd hit if we called sync_playwright twice in this
 
616
 
617
  # Tail the log file while the subprocess runs
618
  last_pos = 0
619
+ saw_error = False
620
  while proc.poll() is None:
621
  try:
622
  with log_jsonl.open("r", encoding="utf-8") as fh:
 
631
  except Exception:
632
  continue
633
  if rec.get("type") == "log":
634
+ level = str(rec.get("level", "INFO")).upper()
635
+ ss.log_lines.append(f"[{level}] {rec['message']}")
636
  if len(ss.log_lines) > 500:
637
  ss.log_lines[:] = ss.log_lines[-500:]
638
+ if level in ("ERROR", "WARNING"):
639
+ saw_error = True
640
  elif rec.get("type") == "progress":
641
  progress_callback(rec["info"])
642
  try:
643
  log_box.code("\n".join(ss.log_lines[-60:]), language="text")
644
  except Exception:
645
  pass
646
+ # Push a live error excerpt into the Inputs tab so the
647
+ # researcher sees problems without having to switch tabs.
648
+ if saw_error and inputs_error_box is not None:
649
+ try:
650
+ inputs_error_box.markdown(
651
+ _live_error_banner(ss.log_lines),
652
+ unsafe_allow_html=True,
653
+ )
654
+ except Exception:
655
+ pass
656
  except Exception:
657
  pass
658
  time.sleep(0.6)
 
792
  )
793
  else:
794
  st.progress(0.05, text="Starting up the browser session…")
795
+ # Surface any ERROR / WARNING lines already in the log without
796
+ # waiting for the subprocess to finish.
797
+ error_banner = _live_error_banner(ss.log_lines)
798
+ if error_banner:
799
+ st.markdown(error_banner, unsafe_allow_html=True)
800
 
801
  elif ss.run_status == "success" and ss.last_run is not None:
802
  s = ss.last_run
 
1047
  ss.queue_id = None
1048
  raise
1049
 
1050
+ # Placeholder in the Inputs tab so the polling loop can push live
1051
+ # error excerpts there without the researcher having to switch tabs.
1052
+ inputs_error_box = tab_inputs.empty()
1053
+
1054
  try:
1055
  with tab_log:
1056
  log_box = st.empty()
 
1080
  ai_dir=ai_dir, out_dir=engine_out, tie_path=tie_path,
1081
  email=bna_email, password=bna_password,
1082
  lang=lang, log_box=log_box, progress_callback=_progress_cb,
1083
+ inputs_error_box=inputs_error_box,
1084
  )
1085
  if error:
1086
  ss.log_lines.append(f"[ERROR] {error}")