Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -419,454 +419,407 @@ llm_response_agent = LLMResponseAgent(message_bus)
|
|
419 |
coordinator_agent = CoordinatorAgent(message_bus)
|
420 |
|
421 |
def create_interface():
|
422 |
-
"""Create Claude-style Gradio interface
|
423 |
|
424 |
with gr.Blocks(
|
425 |
theme=gr.themes.Base(),
|
426 |
css="""
|
427 |
-
/* Claude-inspired
|
428 |
.gradio-container {
|
429 |
-
background
|
430 |
-
color: #
|
431 |
-
|
432 |
-
max-width: none !important;
|
433 |
-
padding: 0 !important;
|
434 |
-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif !important;
|
435 |
}
|
436 |
|
437 |
-
/* Main
|
438 |
.main-container {
|
439 |
-
|
440 |
-
flex-direction: column;
|
441 |
-
height: 100vh;
|
442 |
-
background: #0f0f0f;
|
443 |
-
max-width: 800px;
|
444 |
margin: 0 auto;
|
|
|
445 |
}
|
446 |
|
447 |
-
/* Header
|
448 |
-
.header {
|
449 |
-
background: #
|
450 |
-
border-bottom: 1px solid #
|
451 |
-
padding:
|
452 |
text-align: center;
|
453 |
}
|
454 |
|
455 |
-
.header h1 {
|
456 |
-
color: #
|
457 |
margin: 0;
|
458 |
-
font-size: 1.
|
459 |
-
font-weight:
|
460 |
}
|
461 |
|
462 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
463 |
.chat-container {
|
464 |
-
|
465 |
-
|
466 |
-
flex-direction: column;
|
467 |
-
padding: 0;
|
468 |
-
background: #0f0f0f;
|
469 |
}
|
470 |
|
471 |
-
/* Chatbot
|
472 |
.gradio-chatbot {
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
border: none !important;
|
478 |
-
padding: 1rem !important;
|
479 |
-
overflow-y: auto !important;
|
480 |
}
|
481 |
|
482 |
-
|
483 |
-
|
484 |
-
|
485 |
-
border-radius: 16px !important;
|
486 |
-
padding: 12px 16px !important;
|
487 |
margin: 8px 0 !important;
|
488 |
-
color: #ffffff !important;
|
489 |
}
|
490 |
|
491 |
-
.message.
|
492 |
-
background:
|
493 |
-
border
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
|
|
|
|
499 |
}
|
500 |
|
501 |
-
/* Input area
|
502 |
.input-container {
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
border-top: 1px solid #2a2a2a;
|
507 |
-
padding: 1rem 2rem 2rem 2rem;
|
508 |
}
|
509 |
|
510 |
-
.
|
511 |
-
background: #
|
512 |
-
border
|
513 |
-
border:
|
514 |
-
padding:
|
515 |
-
|
516 |
-
align-items: end;
|
517 |
-
gap: 8px;
|
518 |
-
transition: border-color 0.2s;
|
519 |
}
|
520 |
|
521 |
-
.
|
522 |
-
|
|
|
|
|
523 |
}
|
524 |
|
525 |
-
/* File upload button
|
526 |
-
.
|
527 |
-
background:
|
528 |
border: none !important;
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
539 |
}
|
540 |
|
541 |
-
.
|
542 |
-
background: #
|
543 |
-
color: #ffffff !important;
|
544 |
}
|
545 |
|
546 |
-
/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
547 |
.gradio-textbox {
|
548 |
flex: 1 !important;
|
549 |
}
|
550 |
|
551 |
-
.gradio-textbox textarea,
|
|
|
552 |
background: transparent !important;
|
553 |
border: none !important;
|
554 |
-
color: #
|
|
|
555 |
resize: none !important;
|
556 |
-
|
557 |
-
font-size: 16px !important;
|
558 |
-
line-height: 1.5 !important;
|
559 |
-
max-height: 200px !important;
|
560 |
-
min-height: 24px !important;
|
561 |
}
|
562 |
|
563 |
-
.gradio-textbox textarea:focus,
|
|
|
564 |
outline: none !important;
|
565 |
box-shadow: none !important;
|
566 |
}
|
567 |
|
568 |
-
/* Send button
|
569 |
.send-btn {
|
570 |
-
background: #
|
571 |
border: none !important;
|
572 |
-
border-radius:
|
573 |
-
color:
|
574 |
-
|
575 |
-
|
576 |
-
height: 36px !important;
|
577 |
-
display: flex !important;
|
578 |
-
align-items: center !important;
|
579 |
-
justify-content: center !important;
|
580 |
-
cursor: pointer !important;
|
581 |
-
transition: all 0.2s !important;
|
582 |
}
|
583 |
|
584 |
.send-btn:hover {
|
585 |
-
background: #
|
586 |
-
}
|
587 |
-
|
588 |
-
.send-btn:disabled {
|
589 |
-
background: #404040 !important;
|
590 |
-
cursor: not-allowed !important;
|
591 |
-
}
|
592 |
-
|
593 |
-
/* File upload area - hidden by default */
|
594 |
-
.file-upload {
|
595 |
-
display: none !important;
|
596 |
}
|
597 |
|
598 |
/* Status messages */
|
599 |
.status-message {
|
600 |
-
|
601 |
-
border:
|
602 |
-
|
603 |
-
|
604 |
-
margin: 8px 0;
|
605 |
-
color: #cccccc;
|
606 |
-
font-size: 14px;
|
607 |
-
text-align: center;
|
608 |
}
|
609 |
|
610 |
.status-success {
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
}
|
615 |
|
616 |
.status-error {
|
617 |
-
|
618 |
-
|
619 |
-
|
620 |
}
|
621 |
|
622 |
.status-processing {
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
}
|
627 |
|
628 |
-
/* File list
|
629 |
.file-list {
|
630 |
-
background: #
|
631 |
-
border-radius:
|
632 |
-
padding:
|
633 |
-
margin:
|
634 |
-
font-size:
|
635 |
-
color: #
|
636 |
-
border-left: 3px solid #
|
637 |
}
|
638 |
|
639 |
-
/*
|
640 |
-
.
|
641 |
-
|
642 |
}
|
643 |
|
644 |
-
|
645 |
-
|
646 |
-
|
|
|
647 |
}
|
648 |
|
649 |
-
|
650 |
-
|
|
|
|
|
|
|
651 |
}
|
652 |
|
653 |
-
|
654 |
-
|
655 |
-
|
|
|
656 |
}
|
657 |
|
658 |
-
|
659 |
-
background: #
|
660 |
}
|
661 |
""",
|
662 |
title="Agentic RAG Assistant"
|
663 |
) as iface:
|
664 |
|
665 |
-
# Header
|
666 |
-
gr.
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
|
|
|
|
|
|
|
671 |
|
672 |
-
# Main
|
673 |
with gr.Row():
|
674 |
-
with gr.Column(
|
675 |
|
676 |
-
# Chat
|
677 |
chatbot = gr.Chatbot(
|
678 |
-
|
|
|
679 |
show_copy_button=True,
|
680 |
type="messages",
|
681 |
-
placeholder="
|
682 |
-
avatar_images=(
|
683 |
-
bubble_full_width=False
|
684 |
)
|
685 |
|
686 |
-
# Input
|
687 |
-
with gr.
|
688 |
-
|
689 |
-
|
690 |
-
|
691 |
-
|
|
|
|
|
692 |
file_upload = gr.File(
|
693 |
file_count="multiple",
|
694 |
file_types=[".pdf", ".pptx", ".csv", ".docx", ".txt", ".md"],
|
695 |
-
|
696 |
-
|
697 |
)
|
698 |
|
699 |
-
|
700 |
-
|
701 |
-
"
|
702 |
-
|
703 |
-
size="sm",
|
704 |
-
scale=0
|
705 |
-
)
|
706 |
-
|
707 |
-
# Message input
|
708 |
-
msg_input = gr.Textbox(
|
709 |
-
placeholder="Message Agentic RAG Assistant...",
|
710 |
-
show_label=False,
|
711 |
-
elem_classes=["message-input"],
|
712 |
-
scale=1,
|
713 |
-
container=False,
|
714 |
-
autofocus=True,
|
715 |
-
lines=1,
|
716 |
-
max_lines=10
|
717 |
-
)
|
718 |
-
|
719 |
-
# Send button
|
720 |
-
send_btn = gr.Button(
|
721 |
-
"β",
|
722 |
-
elem_classes=["send-btn"],
|
723 |
-
size="sm",
|
724 |
-
scale=0
|
725 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
726 |
|
727 |
# State management
|
728 |
doc_processed = gr.State(False)
|
729 |
-
uploaded_files = gr.State([])
|
730 |
-
|
731 |
-
# JavaScript for file upload integration
|
732 |
-
gr.HTML("""
|
733 |
-
<script>
|
734 |
-
document.addEventListener('DOMContentLoaded', function() {
|
735 |
-
// Make file button trigger file upload
|
736 |
-
const fileBtn = document.querySelector('.file-btn');
|
737 |
-
const fileInput = document.querySelector('.file-upload input[type="file"]');
|
738 |
-
|
739 |
-
if (fileBtn && fileInput) {
|
740 |
-
fileBtn.addEventListener('click', function(e) {
|
741 |
-
e.preventDefault();
|
742 |
-
e.stopPropagation();
|
743 |
-
fileInput.click();
|
744 |
-
});
|
745 |
-
}
|
746 |
-
|
747 |
-
// Auto-resize textarea
|
748 |
-
const textarea = document.querySelector('.message-input textarea');
|
749 |
-
if (textarea) {
|
750 |
-
textarea.addEventListener('input', function() {
|
751 |
-
this.style.height = 'auto';
|
752 |
-
this.style.height = Math.min(this.scrollHeight, 200) + 'px';
|
753 |
-
});
|
754 |
-
}
|
755 |
-
});
|
756 |
-
</script>
|
757 |
-
""")
|
758 |
|
759 |
-
# Event handlers
|
760 |
-
def
|
761 |
-
"""Handle file uploads and show in chat"""
|
762 |
if not files:
|
763 |
-
return
|
764 |
|
765 |
-
#
|
766 |
-
|
767 |
-
|
768 |
-
|
769 |
-
|
770 |
-
|
771 |
-
"content": f"π Uploaded {len(files)} file(s):\n{file_list}"
|
772 |
-
})
|
773 |
-
|
774 |
-
# Show processing message
|
775 |
-
history.append({
|
776 |
-
"role": "assistant",
|
777 |
-
"content": "π Processing documents... This may take a moment."
|
778 |
-
})
|
779 |
|
|
|
780 |
try:
|
781 |
-
# Process files
|
782 |
result = coordinator_agent.process_files(files)
|
783 |
|
784 |
-
# Wait for processing
|
785 |
import time
|
786 |
time.sleep(3)
|
787 |
|
788 |
-
|
789 |
-
|
790 |
-
|
791 |
-
|
|
|
|
|
792 |
|
793 |
except Exception as e:
|
794 |
-
|
795 |
-
|
|
|
|
|
|
|
|
|
796 |
|
797 |
-
def respond(message, history, doc_ready
|
798 |
-
|
799 |
-
if not
|
|
|
|
|
|
|
800 |
return history, ""
|
801 |
|
|
|
|
|
|
|
802 |
# Add user message
|
803 |
history.append({"role": "user", "content": message})
|
804 |
-
|
805 |
-
if not doc_ready and not any(cmd in message.lower() for cmd in ['hello', 'hi', 'help']):
|
806 |
-
history.append({
|
807 |
-
"role": "assistant",
|
808 |
-
"content": "π Hello! I'm your RAG assistant. Please upload some documents first using the π button, then I can help you analyze and answer questions about them."
|
809 |
-
})
|
810 |
-
return history, ""
|
811 |
-
|
812 |
-
# Handle general queries without documents
|
813 |
-
if not doc_ready:
|
814 |
-
if any(cmd in message.lower() for cmd in ['hello', 'hi', 'help']):
|
815 |
-
history.append({
|
816 |
-
"role": "assistant",
|
817 |
-
"content": "π Hello! I'm an AI assistant specialized in document analysis. Upload documents using the π button above, and I'll help you:\n\nβ’ Summarize content\nβ’ Answer questions\nβ’ Extract key insights\nβ’ Find specific information\n\nSupported formats: PDF, DOCX, PPTX, CSV, TXT, MD"
|
818 |
-
})
|
819 |
-
else:
|
820 |
-
history.append({
|
821 |
-
"role": "assistant",
|
822 |
-
"content": "Please upload documents first using the π button above, then I can help answer questions about them."
|
823 |
-
})
|
824 |
-
return history, ""
|
825 |
-
|
826 |
-
# Add assistant message placeholder
|
827 |
history.append({"role": "assistant", "content": ""})
|
828 |
|
829 |
-
# Stream response
|
830 |
try:
|
831 |
for token in coordinator_agent.handle_query(message, history):
|
832 |
-
|
833 |
-
|
834 |
-
yield history, ""
|
835 |
except Exception as e:
|
836 |
-
history[-1]["content"] = f"
|
837 |
yield history, ""
|
838 |
-
|
839 |
-
# Event bindings
|
840 |
-
|
841 |
-
|
842 |
-
|
843 |
-
|
844 |
-
|
845 |
-
outputs=[chatbot, uploaded_files, doc_processed, file_upload]
|
846 |
)
|
847 |
-
|
848 |
-
# File button click (handled by JavaScript)
|
849 |
-
file_btn.click(None, None, None, js="""
|
850 |
-
function() {
|
851 |
-
document.querySelector('.file-upload input[type="file"]').click();
|
852 |
-
}
|
853 |
-
""")
|
854 |
-
|
855 |
-
# Send button click
|
856 |
send_btn.click(
|
857 |
respond,
|
858 |
-
inputs=[msg_input, chatbot, doc_processed
|
859 |
-
outputs=[chatbot, msg_input]
|
|
|
860 |
)
|
861 |
-
|
862 |
-
# Enter key submit
|
863 |
msg_input.submit(
|
864 |
respond,
|
865 |
-
inputs=[msg_input, chatbot, doc_processed
|
866 |
-
outputs=[chatbot, msg_input]
|
|
|
867 |
)
|
868 |
|
869 |
return iface
|
|
|
870 |
# Launch the application
|
871 |
if __name__ == "__main__":
|
872 |
demo = create_interface()
|
|
|
419 |
coordinator_agent = CoordinatorAgent(message_bus)
|
420 |
|
421 |
def create_interface():
|
422 |
+
"""Create Claude-style Gradio interface"""
|
423 |
|
424 |
with gr.Blocks(
|
425 |
theme=gr.themes.Base(),
|
426 |
css="""
|
427 |
+
/* Claude-inspired styling */
|
428 |
.gradio-container {
|
429 |
+
background: #0d1117 !important;
|
430 |
+
color: #e6edf3 !important;
|
431 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif !important;
|
|
|
|
|
|
|
432 |
}
|
433 |
|
434 |
+
/* Main container */
|
435 |
.main-container {
|
436 |
+
max-width: 900px;
|
|
|
|
|
|
|
|
|
437 |
margin: 0 auto;
|
438 |
+
background: #0d1117;
|
439 |
}
|
440 |
|
441 |
+
/* Header styling */
|
442 |
+
.header-container {
|
443 |
+
background: #0d1117;
|
444 |
+
border-bottom: 1px solid #30363d;
|
445 |
+
padding: 1.5rem 0;
|
446 |
text-align: center;
|
447 |
}
|
448 |
|
449 |
+
.header-container h1 {
|
450 |
+
color: #f0f6fc;
|
451 |
margin: 0;
|
452 |
+
font-size: 1.5rem;
|
453 |
+
font-weight: 600;
|
454 |
}
|
455 |
|
456 |
+
.header-container p {
|
457 |
+
color: #8b949e;
|
458 |
+
margin: 0.5rem 0 0 0;
|
459 |
+
font-size: 0.9rem;
|
460 |
+
}
|
461 |
+
|
462 |
+
/* Chat interface */
|
463 |
.chat-container {
|
464 |
+
background: #0d1117;
|
465 |
+
min-height: 70vh;
|
|
|
|
|
|
|
466 |
}
|
467 |
|
468 |
+
/* Chatbot styling */
|
469 |
.gradio-chatbot {
|
470 |
+
background: #0d1117 !important;
|
471 |
+
border: 1px solid #30363d !important;
|
472 |
+
border-radius: 12px !important;
|
473 |
+
min-height: 500px !important;
|
|
|
|
|
|
|
474 |
}
|
475 |
|
476 |
+
.gradio-chatbot .message {
|
477 |
+
background: transparent !important;
|
478 |
+
border-radius: 8px !important;
|
|
|
|
|
479 |
margin: 8px 0 !important;
|
|
|
480 |
}
|
481 |
|
482 |
+
.gradio-chatbot .message.user {
|
483 |
+
background: #21262d !important;
|
484 |
+
border: 1px solid #30363d !important;
|
485 |
+
color: #f0f6fc !important;
|
486 |
+
}
|
487 |
+
|
488 |
+
.gradio-chatbot .message.bot {
|
489 |
+
background: #161b22 !important;
|
490 |
+
border-left: 3px solid #fd7e14 !important;
|
491 |
+
color: #e6edf3 !important;
|
492 |
}
|
493 |
|
494 |
+
/* Input area styling */
|
495 |
.input-container {
|
496 |
+
background: #0d1117;
|
497 |
+
padding: 1rem 0;
|
498 |
+
border-top: 1px solid #30363d;
|
|
|
|
|
499 |
}
|
500 |
|
501 |
+
.upload-section {
|
502 |
+
background: #161b22;
|
503 |
+
border: 1px solid #30363d;
|
504 |
+
border-radius: 8px;
|
505 |
+
padding: 1rem;
|
506 |
+
margin-bottom: 1rem;
|
|
|
|
|
|
|
507 |
}
|
508 |
|
509 |
+
.upload-section h4 {
|
510 |
+
color: #f0f6fc;
|
511 |
+
margin: 0 0 0.5rem 0;
|
512 |
+
font-size: 0.9rem;
|
513 |
}
|
514 |
|
515 |
+
/* File upload button */
|
516 |
+
.upload-btn {
|
517 |
+
background: linear-gradient(135deg, #fd7e14, #e8590c) !important;
|
518 |
border: none !important;
|
519 |
+
border-radius: 6px !important;
|
520 |
+
color: white !important;
|
521 |
+
font-weight: 500 !important;
|
522 |
+
padding: 0.5rem 1rem !important;
|
523 |
+
}
|
524 |
+
|
525 |
+
.upload-btn:hover {
|
526 |
+
background: linear-gradient(135deg, #e8590c, #d63384) !important;
|
527 |
+
}
|
528 |
+
|
529 |
+
/* Process button */
|
530 |
+
.process-btn {
|
531 |
+
background: #238636 !important;
|
532 |
+
border: none !important;
|
533 |
+
border-radius: 6px !important;
|
534 |
+
color: white !important;
|
535 |
+
font-weight: 500 !important;
|
536 |
+
margin-top: 0.5rem !important;
|
537 |
}
|
538 |
|
539 |
+
.process-btn:hover {
|
540 |
+
background: #2ea043 !important;
|
|
|
541 |
}
|
542 |
|
543 |
+
/* Input row */
|
544 |
+
.input-row {
|
545 |
+
display: flex !important;
|
546 |
+
gap: 12px !important;
|
547 |
+
align-items: end !important;
|
548 |
+
background: #161b22;
|
549 |
+
border: 1px solid #30363d;
|
550 |
+
border-radius: 8px;
|
551 |
+
padding: 0.75rem;
|
552 |
+
}
|
553 |
+
|
554 |
+
/* Message input */
|
555 |
.gradio-textbox {
|
556 |
flex: 1 !important;
|
557 |
}
|
558 |
|
559 |
+
.gradio-textbox textarea,
|
560 |
+
.gradio-textbox input {
|
561 |
background: transparent !important;
|
562 |
border: none !important;
|
563 |
+
color: #f0f6fc !important;
|
564 |
+
font-size: 1rem !important;
|
565 |
resize: none !important;
|
566 |
+
min-height: 40px !important;
|
|
|
|
|
|
|
|
|
567 |
}
|
568 |
|
569 |
+
.gradio-textbox textarea:focus,
|
570 |
+
.gradio-textbox input:focus {
|
571 |
outline: none !important;
|
572 |
box-shadow: none !important;
|
573 |
}
|
574 |
|
575 |
+
/* Send button */
|
576 |
.send-btn {
|
577 |
+
background: #fd7e14 !important;
|
578 |
border: none !important;
|
579 |
+
border-radius: 6px !important;
|
580 |
+
color: white !important;
|
581 |
+
min-height: 40px !important;
|
582 |
+
font-weight: 500 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
583 |
}
|
584 |
|
585 |
.send-btn:hover {
|
586 |
+
background: #e8590c !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
587 |
}
|
588 |
|
589 |
/* Status messages */
|
590 |
.status-message {
|
591 |
+
padding: 0.75rem;
|
592 |
+
border-radius: 6px;
|
593 |
+
margin: 0.5rem 0;
|
594 |
+
font-size: 0.9rem;
|
|
|
|
|
|
|
|
|
595 |
}
|
596 |
|
597 |
.status-success {
|
598 |
+
background: rgba(35, 134, 54, 0.15);
|
599 |
+
border: 1px solid #238636;
|
600 |
+
color: #2ea043;
|
601 |
}
|
602 |
|
603 |
.status-error {
|
604 |
+
background: rgba(248, 81, 73, 0.15);
|
605 |
+
border: 1px solid #da3633;
|
606 |
+
color: #f85149;
|
607 |
}
|
608 |
|
609 |
.status-processing {
|
610 |
+
background: rgba(253, 126, 20, 0.15);
|
611 |
+
border: 1px solid #fd7e14;
|
612 |
+
color: #fd7e14;
|
613 |
}
|
614 |
|
615 |
+
/* File list */
|
616 |
.file-list {
|
617 |
+
background: #21262d;
|
618 |
+
border-radius: 6px;
|
619 |
+
padding: 0.5rem;
|
620 |
+
margin: 0.25rem 0;
|
621 |
+
font-size: 0.85rem;
|
622 |
+
color: #8b949e;
|
623 |
+
border-left: 3px solid #fd7e14;
|
624 |
}
|
625 |
|
626 |
+
/* Examples */
|
627 |
+
.examples-container {
|
628 |
+
margin-top: 1rem;
|
629 |
}
|
630 |
|
631 |
+
.examples-container .gradio-examples {
|
632 |
+
border: 1px solid #30363d;
|
633 |
+
border-radius: 6px;
|
634 |
+
background: #161b22;
|
635 |
}
|
636 |
|
637 |
+
/* Hide default file upload styling */
|
638 |
+
.gradio-file {
|
639 |
+
background: transparent !important;
|
640 |
+
border: 1px dashed #30363d !important;
|
641 |
+
border-radius: 6px !important;
|
642 |
}
|
643 |
|
644 |
+
/* Progress bar */
|
645 |
+
.gradio-progress {
|
646 |
+
background: #21262d !important;
|
647 |
+
border-radius: 4px !important;
|
648 |
}
|
649 |
|
650 |
+
.gradio-progress .progress-bar {
|
651 |
+
background: #fd7e14 !important;
|
652 |
}
|
653 |
""",
|
654 |
title="Agentic RAG Assistant"
|
655 |
) as iface:
|
656 |
|
657 |
+
# Header
|
658 |
+
with gr.Row():
|
659 |
+
with gr.Column(elem_classes=["main-container"]):
|
660 |
+
gr.HTML("""
|
661 |
+
<div class="header-container">
|
662 |
+
<h1>π€ Agentic RAG Assistant</h1>
|
663 |
+
<p>Upload documents and ask intelligent questions powered by multi-agent architecture</p>
|
664 |
+
</div>
|
665 |
+
""")
|
666 |
|
667 |
+
# Main content area
|
668 |
with gr.Row():
|
669 |
+
with gr.Column(elem_classes=["main-container"]):
|
670 |
|
671 |
+
# Chat interface
|
672 |
chatbot = gr.Chatbot(
|
673 |
+
height=500,
|
674 |
+
elem_classes=["chat-container"],
|
675 |
show_copy_button=True,
|
676 |
type="messages",
|
677 |
+
placeholder="π Welcome! Upload your documents below, then start asking questions.",
|
678 |
+
avatar_images=("π€", "π€")
|
|
|
679 |
)
|
680 |
|
681 |
+
# Input section
|
682 |
+
with gr.Column(elem_classes=["input-container"]):
|
683 |
+
|
684 |
+
# File upload section
|
685 |
+
with gr.Group():
|
686 |
+
gr.HTML('<div class="upload-section"><h4>π Document Upload</h4></div>')
|
687 |
+
|
688 |
+
with gr.Row():
|
689 |
file_upload = gr.File(
|
690 |
file_count="multiple",
|
691 |
file_types=[".pdf", ".pptx", ".csv", ".docx", ".txt", ".md"],
|
692 |
+
label="Choose files",
|
693 |
+
show_label=False
|
694 |
)
|
695 |
|
696 |
+
process_btn = gr.Button(
|
697 |
+
"π Process Documents",
|
698 |
+
elem_classes=["process-btn"],
|
699 |
+
size="sm"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
700 |
)
|
701 |
+
|
702 |
+
# Status display
|
703 |
+
processing_status = gr.HTML(visible=False)
|
704 |
+
|
705 |
+
# Message input section
|
706 |
+
with gr.Row(elem_classes=["input-row"]):
|
707 |
+
msg_input = gr.Textbox(
|
708 |
+
placeholder="Ask me anything about your documents...",
|
709 |
+
label="Message",
|
710 |
+
show_label=False,
|
711 |
+
scale=4,
|
712 |
+
autofocus=True,
|
713 |
+
container=False
|
714 |
+
)
|
715 |
+
|
716 |
+
send_btn = gr.Button(
|
717 |
+
"Send β",
|
718 |
+
elem_classes=["send-btn"],
|
719 |
+
scale=1,
|
720 |
+
size="sm"
|
721 |
+
)
|
722 |
+
|
723 |
+
# Example questions
|
724 |
+
with gr.Group(elem_classes=["examples-container"]):
|
725 |
+
gr.Examples(
|
726 |
+
examples=[
|
727 |
+
"π What are the main topics discussed in the documents?",
|
728 |
+
"π Can you summarize the key findings and insights?",
|
729 |
+
"π What specific metrics or data points are mentioned?",
|
730 |
+
"π‘ What are the main recommendations or conclusions?"
|
731 |
+
],
|
732 |
+
inputs=msg_input,
|
733 |
+
label="π Example Questions",
|
734 |
+
elem_id="examples"
|
735 |
+
)
|
736 |
|
737 |
# State management
|
738 |
doc_processed = gr.State(False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
739 |
|
740 |
+
# Event handlers - keeping original functionality
|
741 |
+
def handle_file_upload_and_process(files):
|
|
|
742 |
if not files:
|
743 |
+
return gr.update(visible=False), False
|
744 |
|
745 |
+
# Show processing indicator
|
746 |
+
processing_html = f"""
|
747 |
+
<div class="status-message status-processing">
|
748 |
+
π Processing {len(files)} documents... Please wait.
|
749 |
+
</div>
|
750 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
751 |
|
752 |
+
# Process files using original logic
|
753 |
try:
|
|
|
754 |
result = coordinator_agent.process_files(files)
|
755 |
|
756 |
+
# Wait a moment for processing to complete
|
757 |
import time
|
758 |
time.sleep(3)
|
759 |
|
760 |
+
success_html = """
|
761 |
+
<div class="status-message status-success">
|
762 |
+
β
Documents processed successfully! You can now ask questions.
|
763 |
+
</div>
|
764 |
+
"""
|
765 |
+
return gr.update(value=success_html, visible=True), True
|
766 |
|
767 |
except Exception as e:
|
768 |
+
error_html = f"""
|
769 |
+
<div class="status-message status-error">
|
770 |
+
β Error processing documents: {str(e)}
|
771 |
+
</div>
|
772 |
+
"""
|
773 |
+
return gr.update(value=error_html, visible=True), False
|
774 |
|
775 |
+
def respond(message, history, doc_ready):
|
776 |
+
# Keep original response logic exactly the same
|
777 |
+
if not doc_ready:
|
778 |
+
# Show error message
|
779 |
+
history.append({"role": "user", "content": message})
|
780 |
+
history.append({"role": "assistant", "content": "π Please upload and process documents first using the upload section above."})
|
781 |
return history, ""
|
782 |
|
783 |
+
if not message.strip():
|
784 |
+
return history, message
|
785 |
+
|
786 |
# Add user message
|
787 |
history.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
788 |
history.append({"role": "assistant", "content": ""})
|
789 |
|
790 |
+
# Stream response using original logic
|
791 |
try:
|
792 |
for token in coordinator_agent.handle_query(message, history):
|
793 |
+
history[-1]["content"] += token
|
794 |
+
yield history, ""
|
|
|
795 |
except Exception as e:
|
796 |
+
history[-1]["content"] = f"οΏ½οΏ½ Error: {str(e)}"
|
797 |
yield history, ""
|
798 |
+
|
799 |
+
# Event bindings - keeping original functionality
|
800 |
+
process_btn.click(
|
801 |
+
handle_file_upload_and_process,
|
802 |
+
inputs=[file_upload],
|
803 |
+
outputs=[processing_status, doc_processed],
|
804 |
+
show_progress=True
|
|
|
805 |
)
|
806 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
807 |
send_btn.click(
|
808 |
respond,
|
809 |
+
inputs=[msg_input, chatbot, doc_processed],
|
810 |
+
outputs=[chatbot, msg_input],
|
811 |
+
show_progress=True
|
812 |
)
|
813 |
+
|
|
|
814 |
msg_input.submit(
|
815 |
respond,
|
816 |
+
inputs=[msg_input, chatbot, doc_processed],
|
817 |
+
outputs=[chatbot, msg_input],
|
818 |
+
show_progress=True
|
819 |
)
|
820 |
|
821 |
return iface
|
822 |
+
|
823 |
# Launch the application
|
824 |
if __name__ == "__main__":
|
825 |
demo = create_interface()
|