TilanB commited on
Commit
b8ecadf
·
verified ·
1 Parent(s): e3182ed
Files changed (1) hide show
  1. main.py +32 -33
main.py CHANGED
@@ -517,9 +517,9 @@ def main():
517
  margin-bottom: 16px !important;
518
  }
519
  """
520
- js = r"""
521
- (function () {
522
- var uploadMessages = [
523
  "Crunching your documents...",
524
  "Warming up the AI...",
525
  "Extracting knowledge...",
@@ -532,65 +532,64 @@ def main():
532
  "Almost ready..."
533
  ];
534
 
535
- var msgInterval = null;
536
- var timerInterval = null;
537
- var startMs = 0;
538
- var lastMsg = null;
539
 
540
- function root() { return document.getElementById("processing-message"); }
541
- function isVisible(el) { return !!(el && (el.offsetWidth || el.offsetHeight || el.getClientRects().length)); }
542
 
543
- function pickMsg() {
544
  if (uploadMessages.length === 0) return "";
545
  if (uploadMessages.length === 1) return uploadMessages[0];
546
- var m;
547
  do { m = uploadMessages[Math.floor(Math.random() * uploadMessages.length)]; }
548
  while (m === lastMsg);
549
  lastMsg = m;
550
  return m;
551
- }
552
 
553
- function getMsgSpan() { var r = root(); return r ? r.querySelector("#processing-msg") : null; }
554
- function getTimerSpan() { var r = root(); return r ? r.querySelector("#processing-timer") : null; }
555
 
556
- function setMsg(t) { var s = getMsgSpan(); if (s) s.textContent = t; }
557
- function fmtElapsed() { return ((Date.now() - startMs) / 1000).toFixed(1) + "s elapsed"; }
558
 
559
- function start() {
560
  if (msgInterval || timerInterval) return;
561
  startMs = Date.now();
562
  setMsg(pickMsg());
563
 
564
- msgInterval = setInterval(function () { setMsg(pickMsg()); }, 2000);
565
 
566
- var t = getTimerSpan();
567
  if (t) {
568
  t.textContent = fmtElapsed();
569
- timerInterval = setInterval(function () { t.textContent = fmtElapsed(); }, 200);
570
  }
571
- }
572
 
573
- function stop() {
574
  if (msgInterval) { clearInterval(msgInterval); msgInterval = null; }
575
  if (timerInterval) { clearInterval(timerInterval); timerInterval = null; }
576
- var t = getTimerSpan();
577
  if (t) t.textContent = "";
578
- }
579
 
580
- function tick() {
581
- var r = root();
582
  if (isVisible(r)) start();
583
  else stop();
584
- }
585
 
586
- try {
587
- var obs = new MutationObserver(tick);
588
- obs.observe(document.body, { subtree: true, childList: true, attributes: true });
589
- } catch (e) {}
590
 
591
  window.addEventListener("load", tick);
592
- setInterval(tick, 500);
593
- })();
 
594
  """
595
 
596
  with gr.Blocks(theme=gr.themes.Soft(), title="SmartDoc AI", css=css, js=js) as demo:
 
517
  margin-bottom: 16px !important;
518
  }
519
  """
520
+ js = r"""
521
+ (() => {
522
+ const uploadMessages = [
523
  "Crunching your documents...",
524
  "Warming up the AI...",
525
  "Extracting knowledge...",
 
532
  "Almost ready..."
533
  ];
534
 
535
+ let msgInterval = null;
536
+ let timerInterval = null;
537
+ let startMs = 0;
538
+ let lastMsg = null;
539
 
540
+ const root = () => document.getElementById("processing-message");
541
+ const isVisible = (el) => !!(el && (el.offsetWidth || el.offsetHeight || el.getClientRects().length));
542
 
543
+ const pickMsg = () => {
544
  if (uploadMessages.length === 0) return "";
545
  if (uploadMessages.length === 1) return uploadMessages[0];
546
+ let m;
547
  do { m = uploadMessages[Math.floor(Math.random() * uploadMessages.length)]; }
548
  while (m === lastMsg);
549
  lastMsg = m;
550
  return m;
551
+ };
552
 
553
+ const getMsgSpan = () => root()?.querySelector("#processing-msg");
554
+ const getTimerSpan = () => root()?.querySelector("#processing-timer");
555
 
556
+ const setMsg = (t) => { const s = getMsgSpan(); if (s) s.textContent = t; };
557
+ const fmtElapsed = () => `${((Date.now() - startMs) / 1000).toFixed(1)}s elapsed`;
558
 
559
+ const start = () => {
560
  if (msgInterval || timerInterval) return;
561
  startMs = Date.now();
562
  setMsg(pickMsg());
563
 
564
+ msgInterval = setInterval(() => setMsg(pickMsg()), 2000);
565
 
566
+ const t = getTimerSpan();
567
  if (t) {
568
  t.textContent = fmtElapsed();
569
+ timerInterval = setInterval(() => { t.textContent = fmtElapsed(); }, 200);
570
  }
571
+ };
572
 
573
+ const stop = () => {
574
  if (msgInterval) { clearInterval(msgInterval); msgInterval = null; }
575
  if (timerInterval) { clearInterval(timerInterval); timerInterval = null; }
576
+ const t = getTimerSpan();
577
  if (t) t.textContent = "";
578
+ };
579
 
580
+ const tick = () => {
581
+ const r = root();
582
  if (isVisible(r)) start();
583
  else stop();
584
+ };
585
 
586
+ const obs = new MutationObserver(tick);
587
+ obs.observe(document.body, { subtree: true, childList: true, attributes: true });
 
 
588
 
589
  window.addEventListener("load", tick);
590
+ setInterval(tick, 500); // fallback
591
+ })();
592
+
593
  """
594
 
595
  with gr.Blocks(theme=gr.themes.Soft(), title="SmartDoc AI", css=css, js=js) as demo: