ragunath-ravi commited on
Commit
3398b2c
·
verified ·
1 Parent(s): 62e936c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -283
app.py CHANGED
@@ -411,339 +411,161 @@ retrieval_agent = RetrievalAgent(message_bus)
411
  llm_response_agent = LLMResponseAgent(message_bus)
412
  coordinator_agent = CoordinatorAgent(message_bus)
413
 
414
- # Gradio Interface
415
  def create_interface():
416
  """Create Gradio interface"""
417
 
418
  with gr.Blocks(
419
- theme=gr.themes.Base().set(
420
- background_fill_primary="#0b0f19",
421
- text_color_primary="#ffffff",
422
- button_primary_background="#6366f1",
423
- button_primary_background_hover="#4f46e5",
424
- button_primary_text_color="#ffffff",
425
- block_background="#1f2937",
426
- block_border_color="#374151",
427
- input_background="#374151",
428
- input_border_color="#4b5563",
429
- input_placeholder_color="#9ca3af",
430
- checkbox_background_color_selected="#374151",
431
- checkbox_border_color="#4b5563",
432
- panel_background_fill="#111827",
433
- panel_border_color="#374151",
434
- ),
435
  css="""
436
  .gradio-container {
437
- max-width: 100vw !important;
438
- margin: 0 !important;
439
  padding: 20px !important;
440
- background: linear-gradient(135deg, #0b0f19 0%, #1a1b3a 100%);
441
- min-height: 100vh;
442
  }
443
- .header-container {
444
  text-align: center;
445
- padding: 2rem 0;
446
- margin-bottom: 2rem;
447
- background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
448
- -webkit-background-clip: text;
449
- -webkit-text-fill-color: transparent;
450
- background-clip: text;
451
  }
452
- .header-title {
453
- font-size: 3.5rem;
454
- font-weight: 800;
455
- margin: 0;
456
- text-shadow: 0 0 30px rgba(99, 102, 241, 0.5);
457
- font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
458
- }
459
- .header-subtitle {
460
- font-size: 1.25rem;
461
- margin-top: 0.5rem;
462
- color: #9ca3af;
463
- font-weight: 400;
464
  }
465
- .sidebar-container {
466
- background: rgba(31, 41, 55, 0.8) !important;
467
- border-radius: 20px !important;
468
- padding: 24px !important;
469
- backdrop-filter: blur(10px);
470
- border: 1px solid #374151;
471
- height: fit-content;
472
- position: sticky;
473
- top: 20px;
474
  }
475
  .chat-container {
476
- background: rgba(31, 41, 55, 0.6) !important;
477
- border-radius: 20px !important;
478
- padding: 24px !important;
479
- backdrop-filter: blur(10px);
480
- border: 1px solid #374151;
481
- height: calc(100vh - 200px);
482
- display: flex;
483
- flex-direction: column;
484
- }
485
- .upload-area {
486
- border: 2px dashed #6366f1 !important;
487
- border-radius: 15px !important;
488
- padding: 24px !important;
489
- margin: 16px 0 !important;
490
- background: rgba(99, 102, 241, 0.05) !important;
491
- transition: all 0.3s ease;
492
- }
493
- .upload-area:hover {
494
- border-color: #8b5cf6 !important;
495
- background: rgba(139, 92, 246, 0.1) !important;
496
- transform: translateY(-2px);
497
- box-shadow: 0 8px 25px rgba(99, 102, 241, 0.2);
498
- }
499
- .status-display {
500
- background: linear-gradient(135deg, #065f46, #047857) !important;
501
- border: none !important;
502
- border-radius: 12px !important;
503
- color: #ecfdf5 !important;
504
- padding: 12px 16px !important;
505
- font-family: 'JetBrains Mono', monospace !important;
506
- font-size: 0.9rem !important;
507
- }
508
- .process-btn {
509
- background: linear-gradient(135deg, #6366f1, #8b5cf6) !important;
510
- border: none !important;
511
- border-radius: 12px !important;
512
- padding: 12px 24px !important;
513
- font-weight: 600 !important;
514
- text-transform: uppercase !important;
515
- letter-spacing: 0.5px !important;
516
- transition: all 0.3s ease !important;
517
- box-shadow: 0 4px 15px rgba(99, 102, 241, 0.3) !important;
518
- }
519
- .process-btn:hover {
520
- transform: translateY(-2px) !important;
521
- box-shadow: 0 8px 25px rgba(99, 102, 241, 0.4) !important;
522
- }
523
- .chatbot-container {
524
- flex: 1 !important;
525
- background: rgba(17, 24, 39, 0.8) !important;
526
- border-radius: 15px !important;
527
- border: 1px solid #374151 !important;
528
- margin-bottom: 20px !important;
529
- overflow: hidden !important;
530
  }
531
- .input-row {
532
- background: rgba(55, 65, 81, 0.5) !important;
533
- border-radius: 15px !important;
534
- padding: 16px !important;
535
- margin-top: 16px !important;
536
- border: 1px solid #4b5563 !important;
537
- }
538
- .send-btn {
539
- background: linear-gradient(135deg, #10b981, #059669) !important;
540
- border: none !important;
541
- border-radius: 10px !important;
542
- padding: 12px 20px !important;
543
- font-weight: 600 !important;
544
- transition: all 0.3s ease !important;
545
- box-shadow: 0 4px 15px rgba(16, 185, 129, 0.3) !important;
546
- }
547
- .send-btn:hover {
548
- transform: translateY(-2px) !important;
549
- box-shadow: 0 8px 25px rgba(16, 185, 129, 0.4) !important;
550
- }
551
- .examples-container {
552
- margin-top: 16px !important;
553
- }
554
- .examples-container .gr-button {
555
- background: rgba(75, 85, 99, 0.5) !important;
556
- border: 1px solid #6b7280 !important;
557
- border-radius: 8px !important;
558
- margin: 4px !important;
559
- transition: all 0.3s ease !important;
560
- }
561
- .examples-container .gr-button:hover {
562
- background: rgba(99, 102, 241, 0.2) !important;
563
- border-color: #6366f1 !important;
564
- transform: translateY(-1px) !important;
565
- }
566
- .architecture-info {
567
- background: rgba(17, 24, 39, 0.8) !important;
568
- border-radius: 15px !important;
569
- padding: 20px !important;
570
- margin-top: 24px !important;
571
- border: 1px solid #374151 !important;
572
  }
573
- .architecture-info h3 {
574
- color: #6366f1 !important;
575
- margin-bottom: 16px !important;
576
- font-size: 1.1rem !important;
577
- font-weight: 600 !important;
578
- }
579
- .architecture-info p, .architecture-info ul {
580
- color: #d1d5db !important;
581
- line-height: 1.6 !important;
582
- font-size: 0.9rem !important;
583
- }
584
- .architecture-info ul {
585
- margin-left: 16px !important;
586
- }
587
- .architecture-info li {
588
- margin-bottom: 8px !important;
589
- }
590
- .gr-textbox {
591
- background: rgba(55, 65, 81, 0.8) !important;
592
- border: 1px solid #4b5563 !important;
593
- border-radius: 12px !important;
594
- color: #f9fafb !important;
595
- }
596
- .gr-textbox:focus {
597
- border-color: #6366f1 !important;
598
- box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1) !important;
599
  }
600
  """,
601
  title="Agentic RAG Chatbot"
602
  ) as iface:
603
-
604
  # Header
605
  gr.HTML("""
606
- <div class="header-container">
607
- <h1 class="header-title">Agentic RAG Chatbot</h1>
608
- <p class="header-subtitle">Multi-Format Document QA with Model Context Protocol (MCP)</p>
609
- </div>
610
  """)
611
-
612
- with gr.Row(equal_height=False):
613
- # Sidebar
614
- with gr.Column(scale=1, min_width=350):
615
- with gr.Group(elem_classes=["sidebar-container"]):
616
- gr.Markdown("### Document Upload", elem_id="upload-title")
617
-
618
- file_upload = gr.File(
619
- file_count="multiple",
620
- file_types=[".pdf", ".pptx", ".csv", ".docx", ".txt", ".md"],
621
- label="Drop files here or click to browse",
622
- elem_classes=["upload-area"],
623
- scale=4
624
- )
625
-
626
- upload_status = gr.Textbox(
627
- label="Processing Status",
628
- interactive=False,
629
- max_lines=4,
630
- elem_classes=["status-display"],
631
- value="Ready to process documents..."
632
- )
633
-
634
- process_btn = gr.Button(
635
- "Process Documents",
636
- variant="primary",
637
- size="lg",
638
- elem_classes=["process-btn"],
639
- scale=1
640
- )
641
-
642
- with gr.Group(elem_classes=["architecture-info"]):
643
- gr.Markdown("""
644
- ### System Architecture
645
-
646
- ** Agents:**
647
- - **IngestionAgent**: Document parsing & preprocessing
648
- - **RetrievalAgent**: Semantic search & embeddings
649
- - **LLMResponseAgent**: AI response generation
650
- - **CoordinatorAgent**: Workflow orchestration
651
-
652
- **📡 MCP Communication:**
653
- Structured message passing between agents for scalable, maintainable AI workflows.
654
-
655
- **🔧 Supported Formats:**
656
- PDF, PPTX, CSV, DOCX, TXT, MD
657
- """)
658
-
659
- # Main Chat Area
660
- with gr.Column(scale=2, min_width=600):
661
- with gr.Group(elem_classes=["chat-container"]):
662
- gr.Markdown("### 💬 Chat Interface", elem_id="chat-title")
663
-
664
- chatbot = gr.Chatbot(
665
- height=550,
666
- elem_classes=["chatbot-container"],
667
- show_copy_button=True,
668
- type="messages",
669
- avatar_images=("[( ͡° ͜ʖ ͡°)]", " [¯\_(ツ)_/¯]"),
670
- bubble_full_width=False,
671
- show_share_button=False
672
  )
673
-
674
- with gr.Group(elem_classes=["input-row"]):
675
- with gr.Row():
676
- msg = gr.Textbox(
677
- label="",
678
- placeholder="💭 Ask a question about your documents...",
679
- scale=4,
680
- autofocus=True,
681
- container=False,
682
- show_label=False
683
- )
684
- submit_btn = gr.Button(
685
- "Send",
686
- scale=1,
687
- variant="primary",
688
- elem_classes=["send-btn"]
689
- )
690
-
691
- with gr.Group(elem_classes=["examples-container"]):
692
- gr.Examples(
693
- examples=[
694
- "What are the main topics discussed in the documents?",
695
- "Can you summarize the key findings?",
696
- "What metrics or KPIs are mentioned?",
697
- "What recommendations are provided?",
698
- "Are there any trends or patterns identified?"
699
- ],
700
- inputs=msg,
701
- label="Example Questions"
702
- )
703
-
704
  # Event handlers
705
  def process_files_handler(files):
706
- if files:
707
- return f" Processing {len(files)} files: {', '.join([os.path.basename(f.name) for f in files])}\n⏳ Please wait..."
708
- return "No files uploaded."
709
-
710
  def respond(message, history):
711
  if message.strip():
712
- # Add user message to history in the new format
713
  history.append({"role": "user", "content": message})
714
- # Add a placeholder for the assistant's response
715
  history.append({"role": "assistant", "content": ""})
716
-
717
- # Get streaming response
718
  for token in coordinator_agent.handle_query(message, history):
719
- # Append each token to the assistant's message content
720
  history[-1]["content"] += token
721
- yield history, "" # Yield updated history and clear the textbox
722
  else:
723
  yield history, message
724
-
725
  process_btn.click(
726
  process_files_handler,
727
  inputs=[file_upload],
728
  outputs=[upload_status]
729
  )
730
-
731
  submit_btn.click(
732
  respond,
733
  inputs=[msg, chatbot],
734
  outputs=[chatbot, msg],
735
- show_progress="minimal"
736
  )
737
-
738
  msg.submit(
739
  respond,
740
  inputs=[msg, chatbot],
741
  outputs=[chatbot, msg],
742
- show_progress="minimal"
743
  )
744
-
745
  return iface
746
 
 
747
  # Launch the application
748
  if __name__ == "__main__":
749
  demo = create_interface()
 
411
  llm_response_agent = LLMResponseAgent(message_bus)
412
  coordinator_agent = CoordinatorAgent(message_bus)
413
 
 
414
  def create_interface():
415
  """Create Gradio interface"""
416
 
417
  with gr.Blocks(
418
+ theme=gr.themes.Soft(primary_hue="blue", secondary_hue="purple"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
419
  css="""
420
  .gradio-container {
421
+ max-width: 1200px !important;
 
422
  padding: 20px !important;
 
 
423
  }
424
+ .header-text {
425
  text-align: center;
426
+ color: #667eea;
427
+ font-size: 2.5em;
428
+ font-weight: bold;
429
+ margin-bottom: 10px;
 
 
430
  }
431
+ .subheader-text {
432
+ text-align: center;
433
+ color: #666;
434
+ font-size: 1.2em;
435
+ margin-bottom: 20px;
 
 
 
 
 
 
 
436
  }
437
+ .upload-section {
438
+ border: 2px dashed #667eea;
439
+ border-radius: 10px;
440
+ padding: 20px;
441
+ margin: 20px 0;
442
+ background-color: #f9f9f9;
 
 
 
443
  }
444
  .chat-container {
445
+ height: 500px;
446
+ margin-top: 10px;
447
+ border: 1px solid #ccc;
448
+ border-radius: 8px;
449
+ padding: 10px;
450
+ background-color: #fff;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
451
  }
452
+ .gr-row {
453
+ margin-bottom: 15px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
454
  }
455
+ .gr-column {
456
+ padding: 10px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457
  }
458
  """,
459
  title="Agentic RAG Chatbot"
460
  ) as iface:
461
+
462
  # Header
463
  gr.HTML("""
464
+ <div class="header-text">Agentic RAG Chatbot</div>
465
+ <div class="subheader-text">Multi-Format Document QA with Model Context Protocol (MCP)</div>
 
 
466
  """)
467
+
468
+ with gr.Row():
469
+ with gr.Column(scale=1):
470
+ gr.Markdown("## Document Upload")
471
+
472
+ file_upload = gr.File(
473
+ file_count="multiple",
474
+ file_types=[".pdf", ".pptx", ".csv", ".docx", ".txt", ".md"],
475
+ label="Upload Documents (PDF, PPTX, CSV, DOCX, TXT, MD)",
476
+ elem_classes=["upload-section"]
477
+ )
478
+
479
+ upload_status = gr.Textbox(
480
+ label="Upload Status",
481
+ interactive=False,
482
+ max_lines=3
483
+ )
484
+
485
+ process_btn = gr.Button(
486
+ "Process Documents",
487
+ variant="primary",
488
+ size="lg"
489
+ )
490
+
491
+ gr.Markdown("## Architecture Info")
492
+ gr.Markdown("""
493
+ **Agents:**
494
+ - IngestionAgent: Document parsing
495
+ - RetrievalAgent: Semantic search
496
+ - LLMResponseAgent: Response generation
497
+ - CoordinatorAgent: Workflow orchestration
498
+
499
+ **MCP Communication:** Structured message passing between agents
500
+ """)
501
+
502
+ with gr.Column(scale=3):
503
+ gr.Markdown("## Chat Interface")
504
+
505
+ chatbot = gr.Chatbot(
506
+ height=500,
507
+ elem_classes=["chat-container"],
508
+ show_copy_button=True,
509
+ type="messages"
510
+ )
511
+
512
+ with gr.Row():
513
+ msg = gr.Textbox(
514
+ label="Ask a question about your documents...",
515
+ placeholder="What are the key findings in the uploaded documents?",
516
+ scale=4,
517
+ autofocus=True
 
 
 
 
 
 
 
 
 
 
518
  )
519
+ submit_btn = gr.Button("Send", scale=1, variant="primary")
520
+
521
+ gr.Examples(
522
+ examples=[
523
+ "What are the main topics discussed in the documents?",
524
+ "Can you summarize the key findings?",
525
+ "What metrics or KPIs are mentioned?",
526
+ "What recommendations are provided?",
527
+ "Are there any trends or patterns identified?"
528
+ ],
529
+ inputs=msg
530
+ )
531
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
532
  # Event handlers
533
  def process_files_handler(files):
534
+ return coordinator_agent.process_files(files)
535
+
 
 
536
  def respond(message, history):
537
  if message.strip():
 
538
  history.append({"role": "user", "content": message})
 
539
  history.append({"role": "assistant", "content": ""})
 
 
540
  for token in coordinator_agent.handle_query(message, history):
 
541
  history[-1]["content"] += token
542
+ yield history, ""
543
  else:
544
  yield history, message
545
+
546
  process_btn.click(
547
  process_files_handler,
548
  inputs=[file_upload],
549
  outputs=[upload_status]
550
  )
551
+
552
  submit_btn.click(
553
  respond,
554
  inputs=[msg, chatbot],
555
  outputs=[chatbot, msg],
556
+ show_progress=True
557
  )
558
+
559
  msg.submit(
560
  respond,
561
  inputs=[msg, chatbot],
562
  outputs=[chatbot, msg],
563
+ show_progress=True
564
  )
565
+
566
  return iface
567
 
568
+
569
  # Launch the application
570
  if __name__ == "__main__":
571
  demo = create_interface()