TilanB commited on
Commit
d7ecf6b
·
verified ·
1 Parent(s): 054b6c1
Files changed (1) hide show
  1. main.py +84 -78
main.py CHANGED
@@ -517,84 +517,90 @@ def main():
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...",
526
- "Scanning for insights...",
527
- "Preparing your data...",
528
- "Looking for answers...",
529
- "Analyzing file structure...",
530
- "Reading your files...",
531
- "Indexing content...",
532
- "Almost ready..."
533
- ];
534
-
535
- let msgInterval = null;
536
- let timerInterval = null;
537
- let startMs = 0;
538
- let lastMsg = null;
539
-
540
- // In Gradio re-renders, the element may get replaced; pick the visible one if duplicates ever appear
541
- const root = () => {
542
- const all = Array.from(document.querySelectorAll("#processing-message"));
543
- return all.find(el => el && (el.offsetWidth || el.offsetHeight || el.getClientRects().length)) || all[0] || null;
544
- };
545
-
546
- const isVisible = (el) => !!(el && (el.offsetWidth || el.offsetHeight || el.getClientRects().length));
547
-
548
- const pickMsg = () => {
549
- if (uploadMessages.length === 0) return "";
550
- if (uploadMessages.length === 1) return uploadMessages[0];
551
- let m;
552
- do { m = uploadMessages[Math.floor(Math.random() * uploadMessages.length)]; }
553
- while (m === lastMsg);
554
- lastMsg = m;
555
- return m;
556
- };
557
-
558
- const getMsgSpan = () => root()?.querySelector("#processing-msg");
559
- const getTimerSpan = () => root()?.querySelector("#processing-timer");
560
-
561
- const setMsg = (t) => { const s = getMsgSpan(); if (s) s.textContent = t; };
562
- const fmtElapsed = () => `${((Date.now() - startMs) / 1000).toFixed(1)}s elapsed`;
563
-
564
- const start = () => {
565
- if (msgInterval || timerInterval) return;
566
- startMs = Date.now();
567
- setMsg(pickMsg());
568
-
569
- msgInterval = setInterval(() => setMsg(pickMsg()), 2000);
570
-
571
- const t = getTimerSpan();
572
- if (t) {
573
- t.textContent = fmtElapsed();
574
- timerInterval = setInterval(() => { t.textContent = fmtElapsed(); }, 200);
575
- }
576
- };
577
-
578
- const stop = () => {
579
- if (msgInterval) { clearInterval(msgInterval); msgInterval = null; }
580
- if (timerInterval) { clearInterval(timerInterval); timerInterval = null; }
581
- const t = getTimerSpan();
582
- if (t) t.textContent = "";
583
- };
584
-
585
- const tick = () => {
586
- const r = root();
587
- if (isVisible(r)) start();
588
- else stop();
589
- };
590
-
591
- const obs = new MutationObserver(tick);
592
- obs.observe(document.body, { subtree: true, childList: true, attributes: true });
593
-
594
- window.addEventListener("load", tick);
595
- setInterval(tick, 500);
596
- })();
597
- """
 
 
 
 
 
 
598
 
599
  with gr.Blocks(theme=gr.themes.Soft(), title="SmartDoc AI", css=css, js=js) as demo:
600
  gr.Markdown("### SmartDoc AI - Document Q&A", elem_classes="app-title")
 
517
  margin-bottom: 16px !important;
518
  }
519
  """
520
+ js = r'''
521
+ const uploadMessages = [
522
+ "Crunching your documents...",
523
+ "Warming up the AI...",
524
+ "Extracting knowledge...",
525
+ "Scanning for insights...",
526
+ "Preparing your data...",
527
+ "Looking for answers...",
528
+ "Analyzing file structure...",
529
+ "Reading your files...",
530
+ "Indexing content...",
531
+ "Almost ready..."
532
+ ];
533
+
534
+ let msgInterval = null;
535
+ let timerInterval = null;
536
+ let startMs = 0;
537
+ let lastMsg = null;
538
+
539
+ function root() {
540
+ return document.getElementById("processing-message");
541
+ }
542
+ function isVisible(el) {
543
+ return !!(el && (el.offsetWidth || el.offsetHeight || el.getClientRects().length));
544
+ }
545
+ function pickMsg() {
546
+ if (uploadMessages.length === 0) return "";
547
+ if (uploadMessages.length === 1) return uploadMessages[0];
548
+ let m;
549
+ do { m = uploadMessages[Math.floor(Math.random() * uploadMessages.length)]; }
550
+ while (m === lastMsg);
551
+ lastMsg = m;
552
+ return m;
553
+ }
554
+ function getMsgSpan() {
555
+ const r = root();
556
+ return r ? r.querySelector("#processing-msg") : null;
557
+ }
558
+ function getTimerSpan() {
559
+ const r = root();
560
+ return r ? r.querySelector("#processing-timer") : null;
561
+ }
562
+ function setMsg(t) {
563
+ const s = getMsgSpan();
564
+ if (s) s.textContent = t;
565
+ }
566
+ function fmtElapsed() {
567
+ return ((Date.now() - startMs) / 1000).toFixed(1) + "s elapsed";
568
+ }
569
+
570
+ function start() {
571
+ if (msgInterval || timerInterval) return;
572
+ startMs = Date.now();
573
+ setMsg(pickMsg());
574
+
575
+ msgInterval = setInterval(() => setMsg(pickMsg()), 2000);
576
+
577
+ const t = getTimerSpan();
578
+ if (t) {
579
+ t.textContent = fmtElapsed();
580
+ timerInterval = setInterval(() => { t.textContent = fmtElapsed(); }, 200);
581
+ }
582
+ }
583
+
584
+ function stop() {
585
+ if (msgInterval) { clearInterval(msgInterval); msgInterval = null; }
586
+ if (timerInterval) { clearInterval(timerInterval); timerInterval = null; }
587
+ const t = getTimerSpan();
588
+ if (t) t.textContent = "";
589
+ }
590
+
591
+ function tick() {
592
+ const r = root();
593
+ if (isVisible(r)) start();
594
+ else stop();
595
+ }
596
+
597
+ // Observe rerenders / visibility changes
598
+ const obs = new MutationObserver(tick);
599
+ obs.observe(document.body, { subtree: true, childList: true, attributes: true });
600
+
601
+ window.addEventListener("load", tick);
602
+ setInterval(tick, 500);
603
+ '''
604
 
605
  with gr.Blocks(theme=gr.themes.Soft(), title="SmartDoc AI", css=css, js=js) as demo:
606
  gr.Markdown("### SmartDoc AI - Document Q&A", elem_classes="app-title")