ragunath-ravi commited on
Commit
14f7fae
Β·
verified Β·
1 Parent(s): d8178c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +288 -63
app.py CHANGED
@@ -413,147 +413,371 @@ 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.Soft(primary_hue="blue", secondary_hue="purple"),
420
  css="""
 
421
  .gradio-container {
422
- max-width: 1200px !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: 10px 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442
  }
 
 
443
  .chat-container {
444
- height: 500px;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
445
  }
446
  """,
447
- title="Agentic RAG Chatbot"
448
  ) as iface:
449
 
450
  # Header
451
  gr.HTML("""
452
- <div class="header-text">Agentic RAG Chatbot</div>
453
- <div class="subheader-text">Multi-Format Document QA with Model Context Protocol (MCP)</div>
 
 
454
  """)
455
 
456
  with gr.Row():
457
- with gr.Column(scale=1):
458
- gr.Markdown("## Document Upload")
 
459
 
460
  file_upload = gr.File(
461
  file_count="multiple",
462
  file_types=[".pdf", ".pptx", ".csv", ".docx", ".txt", ".md"],
463
- label="Upload Documents (PDF, PPTX, CSV, DOCX, TXT, MD)",
464
- elem_classes=["upload-section"]
465
- )
466
-
467
- upload_status = gr.Textbox(
468
- label="Upload Status",
469
- interactive=False,
470
- max_lines=3
471
  )
472
 
473
  process_btn = gr.Button(
474
- "Process Documents",
475
  variant="primary",
 
476
  size="lg"
477
  )
478
 
479
- gr.Markdown("## Architecture Info")
480
- gr.Markdown("""
481
- **Agents:**
482
- - IngestionAgent: Document parsing
483
- - RetrievalAgent: Semantic search
484
- - LLMResponseAgent: Response generation
485
- - CoordinatorAgent: Workflow orchestration
486
 
487
- **MCP Communication:** Structured message passing between agents
 
 
 
 
 
 
 
 
 
 
 
488
  """)
489
 
490
- with gr.Column(scale=3):
491
- gr.Markdown("## Chat Interface")
492
-
493
  chatbot = gr.Chatbot(
494
- height=500,
495
- elem_classes=["chat-container"],
496
  show_copy_button=True,
497
- type="messages" # This requires the new data format
 
 
 
498
  )
499
 
 
500
  with gr.Row():
501
  msg = gr.Textbox(
502
- label="Ask a question about your documents...",
503
- placeholder="What are the key findings in the uploaded documents?",
504
- scale=4,
 
 
505
  autofocus=True
506
  )
507
- submit_btn = gr.Button("Send ", scale=1, variant="primary")
 
 
 
 
 
 
508
 
 
 
509
  gr.Examples(
510
  examples=[
511
  "What are the main topics discussed in the documents?",
512
- "Can you summarize the key findings?",
513
- "What metrics or KPIs are mentioned?",
514
- "What recommendations are provided?",
515
- "Are there any trends or patterns identified?"
 
516
  ],
517
- inputs=msg
 
518
  )
 
519
 
520
  # Event handlers
521
  def process_files_handler(files):
522
- return coordinator_agent.process_files(files)
 
 
523
 
524
  def respond(message, history):
525
  if message.strip():
526
- # Add user message to history in the new format
527
  history.append({"role": "user", "content": message})
528
- # Add a placeholder for the assistant's response
529
  history.append({"role": "assistant", "content": ""})
530
 
531
  # Get streaming response
532
  for token in coordinator_agent.handle_query(message, history):
533
- # Append each token to the assistant's message content
534
  history[-1]["content"] += token
535
- yield history, "" # Yield updated history and clear the textbox
536
  else:
537
  yield history, message
538
 
 
 
 
 
539
  process_btn.click(
540
  process_files_handler,
541
  inputs=[file_upload],
542
- outputs=[upload_status]
 
543
  )
544
 
545
  submit_btn.click(
546
  respond,
547
  inputs=[msg, chatbot],
548
  outputs=[chatbot, msg],
549
- show_progress=True
550
  )
551
 
552
  msg.submit(
553
  respond,
554
  inputs=[msg, chatbot],
555
  outputs=[chatbot, msg],
556
- show_progress=True
557
  )
558
 
559
  return iface
@@ -564,5 +788,6 @@ if __name__ == "__main__":
564
  demo.launch(
565
  share=True,
566
  server_name="0.0.0.0",
567
- server_port=7860
 
568
  )
 
413
 
414
  # Gradio Interface
415
  def create_interface():
416
+ """Create modern ChatGPT-like Gradio interface"""
417
 
418
  with gr.Blocks(
419
+ theme=gr.themes.Soft(),
420
  css="""
421
+ /* Main container styling */
422
  .gradio-container {
423
+ max-width: 100vw !important;
424
+ margin: 0 !important;
425
+ padding: 0 !important;
426
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
427
+ min-height: 100vh;
428
  }
429
+
430
+ /* Header styling */
431
+ .header-container {
432
+ background: rgba(255, 255, 255, 0.95);
433
+ backdrop-filter: blur(10px);
434
+ border-bottom: 1px solid rgba(255, 255, 255, 0.2);
435
+ padding: 1rem 2rem;
436
+ box-shadow: 0 2px 20px rgba(0, 0, 0, 0.1);
437
+ }
438
+
439
+ .header-title {
440
+ font-size: 2.5rem;
441
+ font-weight: 700;
442
+ background: linear-gradient(135deg, #667eea, #764ba2);
443
+ -webkit-background-clip: text;
444
+ -webkit-text-fill-color: transparent;
445
  text-align: center;
446
+ margin: 0;
 
 
 
447
  }
448
+
449
+ .header-subtitle {
450
+ font-size: 1.1rem;
451
+ color: #6b7280;
452
  text-align: center;
453
+ margin-top: 0.5rem;
454
+ font-weight: 400;
455
+ }
456
+
457
+ /* Sidebar styling */
458
+ .sidebar {
459
+ background: rgba(255, 255, 255, 0.95) !important;
460
+ backdrop-filter: blur(10px);
461
+ border-right: 1px solid rgba(255, 255, 255, 0.2);
462
+ padding: 2rem 1.5rem !important;
463
+ min-height: calc(100vh - 100px);
464
+ box-shadow: 2px 0 20px rgba(0, 0, 0, 0.05);
465
+ }
466
+
467
+ .sidebar h3 {
468
+ color: #374151;
469
+ font-weight: 600;
470
+ margin-bottom: 1rem;
471
+ font-size: 1.2rem;
472
  }
473
+
474
+ /* Upload area styling */
475
+ .upload-area {
476
+ background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
477
  border: 2px dashed #667eea;
478
+ border-radius: 12px;
479
+ padding: 2rem 1rem;
480
+ margin: 1rem 0;
481
+ transition: all 0.3s ease;
482
+ text-align: center;
483
+ }
484
+
485
+ .upload-area:hover {
486
+ border-color: #764ba2;
487
+ background: linear-gradient(135deg, #f1f5f9 0%, #ddd6fe 100%);
488
+ transform: translateY(-2px);
489
+ box-shadow: 0 4px 15px rgba(102, 126, 234, 0.2);
490
+ }
491
+
492
+ /* Process button styling */
493
+ .process-btn {
494
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
495
+ border: none !important;
496
+ border-radius: 8px !important;
497
+ padding: 0.75rem 2rem !important;
498
+ color: white !important;
499
+ font-weight: 600 !important;
500
+ transition: all 0.3s ease !important;
501
+ box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4) !important;
502
+ }
503
+
504
+ .process-btn:hover {
505
+ transform: translateY(-2px) !important;
506
+ box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6) !important;
507
  }
508
+
509
+ /* Chat container styling */
510
  .chat-container {
511
+ background: rgba(255, 255, 255, 0.95) !important;
512
+ backdrop-filter: blur(10px);
513
+ border-radius: 16px !important;
514
+ margin: 2rem;
515
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1) !important;
516
+ overflow: hidden;
517
+ min-height: calc(100vh - 200px);
518
+ }
519
+
520
+ /* Chatbot styling */
521
+ .chatbot {
522
+ background: transparent !important;
523
+ border: none !important;
524
+ }
525
+
526
+ /* Message input styling */
527
+ .message-input {
528
+ background: rgba(255, 255, 255, 0.9) !important;
529
+ border: 2px solid rgba(102, 126, 234, 0.2) !important;
530
+ border-radius: 25px !important;
531
+ padding: 0.75rem 1.5rem !important;
532
+ font-size: 1rem !important;
533
+ transition: all 0.3s ease !important;
534
+ }
535
+
536
+ .message-input:focus {
537
+ border-color: #667eea !important;
538
+ box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1) !important;
539
+ outline: none !important;
540
+ }
541
+
542
+ /* Send button styling */
543
+ .send-btn {
544
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
545
+ border: none !important;
546
+ border-radius: 50% !important;
547
+ width: 48px !important;
548
+ height: 48px !important;
549
+ display: flex !important;
550
+ align-items: center !important;
551
+ justify-content: center !important;
552
+ color: white !important;
553
+ font-weight: 600 !important;
554
+ transition: all 0.3s ease !important;
555
+ box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4) !important;
556
+ margin-left: 0.5rem !important;
557
+ }
558
+
559
+ .send-btn:hover {
560
+ transform: scale(1.05) !important;
561
+ box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6) !important;
562
+ }
563
+
564
+ /* Status display styling */
565
+ .status-display {
566
+ background: linear-gradient(135deg, #f0f9ff 0%, #e0e7ff 100%) !important;
567
+ border: 1px solid rgba(102, 126, 234, 0.2) !important;
568
+ border-radius: 8px !important;
569
+ padding: 1rem !important;
570
+ margin: 1rem 0 !important;
571
+ font-family: 'SF Mono', 'Monaco', monospace !important;
572
+ font-size: 0.9rem !important;
573
+ }
574
+
575
+ /* Info section styling */
576
+ .info-section {
577
+ background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
578
+ border-radius: 12px;
579
+ padding: 1.5rem;
580
+ margin-top: 2rem;
581
+ border: 1px solid rgba(102, 126, 234, 0.1);
582
+ }
583
+
584
+ .info-section h4 {
585
+ color: #374151;
586
+ font-weight: 600;
587
+ margin-bottom: 1rem;
588
+ font-size: 1.1rem;
589
+ }
590
+
591
+ .info-section p {
592
+ color: #6b7280;
593
+ font-size: 0.9rem;
594
+ line-height: 1.5;
595
+ margin: 0.5rem 0;
596
+ }
597
+
598
+ /* Examples styling */
599
+ .examples-container {
600
+ padding: 1rem 2rem;
601
+ }
602
+
603
+ .example-btn {
604
+ background: rgba(255, 255, 255, 0.8) !important;
605
+ border: 1px solid rgba(102, 126, 234, 0.3) !important;
606
+ border-radius: 20px !important;
607
+ padding: 0.5rem 1rem !important;
608
+ margin: 0.25rem !important;
609
+ font-size: 0.9rem !important;
610
+ color: #374151 !important;
611
+ transition: all 0.3s ease !important;
612
+ }
613
+
614
+ .example-btn:hover {
615
+ background: rgba(102, 126, 234, 0.1) !important;
616
+ border-color: #667eea !important;
617
+ transform: translateY(-1px) !important;
618
+ }
619
+
620
+ /* Responsive design */
621
+ @media (max-width: 768px) {
622
+ .gradio-container {
623
+ padding: 0 !important;
624
+ }
625
+
626
+ .header-title {
627
+ font-size: 2rem;
628
+ }
629
+
630
+ .sidebar {
631
+ padding: 1rem !important;
632
+ }
633
+
634
+ .chat-container {
635
+ margin: 1rem;
636
+ }
637
  }
638
  """,
639
+ title="AI Document Assistant"
640
  ) as iface:
641
 
642
  # Header
643
  gr.HTML("""
644
+ <div class="header-container">
645
+ <h1 class="header-title">AI Document Assistant</h1>
646
+ <p class="header-subtitle">Intelligent multi-format document analysis with advanced RAG architecture</p>
647
+ </div>
648
  """)
649
 
650
  with gr.Row():
651
+ # Sidebar
652
+ with gr.Column(scale=1, elem_classes=["sidebar"]):
653
+ gr.HTML("<h3>πŸ“ Document Upload</h3>")
654
 
655
  file_upload = gr.File(
656
  file_count="multiple",
657
  file_types=[".pdf", ".pptx", ".csv", ".docx", ".txt", ".md"],
658
+ label="",
659
+ elem_classes=["upload-area"],
660
+ interactive=True
 
 
 
 
 
661
  )
662
 
663
  process_btn = gr.Button(
664
+ "πŸš€ Process Documents",
665
  variant="primary",
666
+ elem_classes=["process-btn"],
667
  size="lg"
668
  )
669
 
670
+ upload_status = gr.Textbox(
671
+ label="πŸ“Š Status",
672
+ interactive=False,
673
+ elem_classes=["status-display"],
674
+ max_lines=4,
675
+ show_label=True
676
+ )
677
 
678
+ # Info section
679
+ gr.HTML("""
680
+ <div class="info-section">
681
+ <h4>πŸ€– AI Agents</h4>
682
+ <p><strong>Ingestion:</strong> Document parsing & preprocessing</p>
683
+ <p><strong>Retrieval:</strong> Semantic search & context extraction</p>
684
+ <p><strong>Response:</strong> Intelligent answer generation</p>
685
+ <p><strong>Coordinator:</strong> Workflow orchestration</p>
686
+
687
+ <h4 style="margin-top: 1.5rem;">πŸ”— Architecture</h4>
688
+ <p>Built with Model Context Protocol (MCP) for seamless agent communication and advanced RAG capabilities.</p>
689
+ </div>
690
  """)
691
 
692
+ # Main chat area
693
+ with gr.Column(scale=3, elem_classes=["chat-container"]):
 
694
  chatbot = gr.Chatbot(
695
+ height=600,
696
+ elem_classes=["chatbot"],
697
  show_copy_button=True,
698
+ type="messages",
699
+ avatar_images=("πŸ§‘β€πŸ’Ό", "πŸ€–"),
700
+ bubble_full_width=False,
701
+ show_share_button=True
702
  )
703
 
704
+ # Message input area
705
  with gr.Row():
706
  msg = gr.Textbox(
707
+ label="",
708
+ placeholder="Ask me anything about your documents...",
709
+ scale=10,
710
+ elem_classes=["message-input"],
711
+ container=False,
712
  autofocus=True
713
  )
714
+ submit_btn = gr.Button(
715
+ "➀",
716
+ scale=1,
717
+ variant="primary",
718
+ elem_classes=["send-btn"],
719
+ size="sm"
720
+ )
721
 
722
+ # Examples
723
+ gr.HTML("<div class='examples-container'>")
724
  gr.Examples(
725
  examples=[
726
  "What are the main topics discussed in the documents?",
727
+ "Can you summarize the key findings and insights?",
728
+ "What metrics, KPIs, or data points are mentioned?",
729
+ "What recommendations or action items are provided?",
730
+ "Are there any trends, patterns, or correlations identified?",
731
+ "What are the potential risks or challenges mentioned?"
732
  ],
733
+ inputs=msg,
734
+ elem_classes=["example-btn"]
735
  )
736
+ gr.HTML("</div>")
737
 
738
  # Event handlers
739
  def process_files_handler(files):
740
+ if files:
741
+ return coordinator_agent.process_files(files)
742
+ return "Please select files to upload."
743
 
744
  def respond(message, history):
745
  if message.strip():
746
+ # Add user message to history
747
  history.append({"role": "user", "content": message})
748
+ # Add placeholder for assistant response
749
  history.append({"role": "assistant", "content": ""})
750
 
751
  # Get streaming response
752
  for token in coordinator_agent.handle_query(message, history):
 
753
  history[-1]["content"] += token
754
+ yield history, ""
755
  else:
756
  yield history, message
757
 
758
+ def clear_chat():
759
+ return [], ""
760
+
761
+ # Event bindings
762
  process_btn.click(
763
  process_files_handler,
764
  inputs=[file_upload],
765
+ outputs=[upload_status],
766
+ show_progress=True
767
  )
768
 
769
  submit_btn.click(
770
  respond,
771
  inputs=[msg, chatbot],
772
  outputs=[chatbot, msg],
773
+ show_progress="minimal"
774
  )
775
 
776
  msg.submit(
777
  respond,
778
  inputs=[msg, chatbot],
779
  outputs=[chatbot, msg],
780
+ show_progress="minimal"
781
  )
782
 
783
  return iface
 
788
  demo.launch(
789
  share=True,
790
  server_name="0.0.0.0",
791
+ server_port=7860,
792
+ show_error=True
793
  )