TilanB commited on
Commit
1c60a4f
·
verified ·
1 Parent(s): a30d34a
Files changed (1) hide show
  1. main.py +91 -10
main.py CHANGED
@@ -516,10 +516,93 @@ def main():
516
  border: 2px solid #d1d5db !important;
517
  margin-bottom: 16px !important;
518
  }
519
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
520
 
 
 
 
521
 
522
- with gr.Blocks(theme=gr.themes.Soft(), title="SmartDoc AI", css=css) as demo:
523
  gr.Markdown("### SmartDoc AI - Document Q&A", elem_classes="app-title")
524
  gr.Markdown("Upload your documents and ask questions. Answers will appear below, just like a chat.", elem_classes="app-description")
525
  gr.Markdown("---")
@@ -569,8 +652,7 @@ def main():
569
  )
570
  try:
571
  if not question_text.strip():
572
- chat_history.append({"role": "user", "content": question_text})
573
- chat_history.append({"role": "assistant", "content": "Please enter a question."})
574
  yield (
575
  chat_history,
576
  gr.update(visible=False),
@@ -583,8 +665,8 @@ def main():
583
  )
584
  return
585
  if not uploaded_files:
586
- chat_history.append({"role": "user", "content": question_text})
587
- chat_history.append({"role": "assistant", "content": "Please upload at least one document."})
588
  yield (
589
  chat_history,
590
  gr.update(visible=False),
@@ -708,8 +790,7 @@ def main():
708
  verification = result.get("verification_report", "No verification details available.")
709
  logger.info(f"Verification (internal):\n{verification}")
710
  # Do not display verification to user, only use internally
711
- chat_history.append({"role": "user", "content": question_text})
712
- chat_history.append({"role": "assistant", "content": f"**Answer:**\n{answer}"})
713
  session_state.value["last_documents"] = retriever.invoke(question_text)
714
  yield (
715
  chat_history,
@@ -736,8 +817,8 @@ def main():
736
  )
737
  except Exception as e:
738
  logger.error(f"Processing error: {e}", exc_info=True)
739
- chat_history.append({"role": "user", "content": question_text})
740
- chat_history.append({"role": "assistant", "content": f"Error: {str(e)}"})
741
  yield (
742
  chat_history,
743
  gr.update(visible=False),
 
516
  border: 2px solid #d1d5db !important;
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")
607
  gr.Markdown("Upload your documents and ask questions. Answers will appear below, just like a chat.", elem_classes="app-description")
608
  gr.Markdown("---")
 
652
  )
653
  try:
654
  if not question_text.strip():
655
+ chat_history.append([question_text, "Please enter a question."])
 
656
  yield (
657
  chat_history,
658
  gr.update(visible=False),
 
665
  )
666
  return
667
  if not uploaded_files:
668
+
669
+ chat_history.append([question_text, "Please upload at least one document."])
670
  yield (
671
  chat_history,
672
  gr.update(visible=False),
 
790
  verification = result.get("verification_report", "No verification details available.")
791
  logger.info(f"Verification (internal):\n{verification}")
792
  # Do not display verification to user, only use internally
793
+ chat_history.append([question_text, f"**Answer:**\n{answer}"])
 
794
  session_state.value["last_documents"] = retriever.invoke(question_text)
795
  yield (
796
  chat_history,
 
817
  )
818
  except Exception as e:
819
  logger.error(f"Processing error: {e}", exc_info=True)
820
+
821
+ chat_history.append([question_text, f"Error: {str(e)}"])
822
  yield (
823
  chat_history,
824
  gr.update(visible=False),