Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -307,7 +307,7 @@ def get_document_choices(): | |
| 307 | 
             
            def upload_and_process_file(file):
         | 
| 308 | 
             
                """Gradio interface for file upload"""
         | 
| 309 | 
             
                if file is None:
         | 
| 310 | 
            -
                    return "No file uploaded", "", get_document_list(), gr.update(choices=get_document_choices())
         | 
| 311 |  | 
| 312 | 
             
                try:
         | 
| 313 | 
             
                    # Get file path
         | 
| @@ -330,6 +330,7 @@ def upload_and_process_file(file): | |
| 330 | 
             
                            doc_list,
         | 
| 331 | 
             
                            gr.update(choices=doc_choices),
         | 
| 332 | 
             
                            gr.update(choices=doc_choices),
         | 
|  | |
| 333 | 
             
                            gr.update(choices=doc_choices)
         | 
| 334 | 
             
                        )
         | 
| 335 | 
             
                    else:
         | 
| @@ -339,6 +340,7 @@ def upload_and_process_file(file): | |
| 339 | 
             
                            get_document_list(),
         | 
| 340 | 
             
                            gr.update(choices=get_document_choices()),
         | 
| 341 | 
             
                            gr.update(choices=get_document_choices()),
         | 
|  | |
| 342 | 
             
                            gr.update(choices=get_document_choices())
         | 
| 343 | 
             
                        )
         | 
| 344 | 
             
                except Exception as e:
         | 
| @@ -349,6 +351,7 @@ def upload_and_process_file(file): | |
| 349 | 
             
                        get_document_list(),
         | 
| 350 | 
             
                        gr.update(choices=get_document_choices()),
         | 
| 351 | 
             
                        gr.update(choices=get_document_choices()),
         | 
|  | |
| 352 | 
             
                        gr.update(choices=get_document_choices())
         | 
| 353 | 
             
                    )
         | 
| 354 |  | 
| @@ -482,39 +485,48 @@ def ask_question(question): | |
| 482 | 
             
                    return f"β Error: {str(e)}"
         | 
| 483 |  | 
| 484 | 
             
            def delete_document_from_library(document_id):
         | 
| 485 | 
            -
             | 
| 486 | 
            -
             | 
| 487 | 
            -
             | 
| 488 | 
            -
             | 
| 489 | 
            -
             | 
| 490 | 
            -
             | 
| 491 | 
            -
             | 
| 492 | 
            -
             | 
| 493 | 
            -
             | 
| 494 | 
            -
             | 
| 495 | 
            -
             | 
| 496 | 
            -
             | 
| 497 | 
            -
             | 
| 498 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 499 |  | 
| 500 | 
            -
            # Create Gradio Interface
         | 
| 501 | 
             
            # Create Gradio Interface
         | 
| 502 | 
             
            def create_gradio_interface():
         | 
| 503 | 
             
                with gr.Blocks(title="π§  Intelligent Content Organizer MCP Agent", theme=gr.themes.Soft()) as interface:
         | 
| 504 | 
            -
                     | 
| 505 | 
            -
             | 
| 506 | 
            -
             | 
| 507 | 
            -
             | 
|  | |
| 508 |  | 
| 509 | 
            -
             | 
| 510 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
| 511 |  | 
| 512 | 
            -
             | 
| 513 | 
            -
             | 
| 514 | 
            -
             | 
| 515 | 
            -
             | 
| 516 | 
            -
             | 
| 517 | 
            -
                            """)
         | 
| 518 |  | 
| 519 | 
             
                    with gr.Tabs():
         | 
| 520 | 
             
                        # π Document Library Tab
         | 
| @@ -524,30 +536,31 @@ def create_gradio_interface(): | |
| 524 | 
             
                                    gr.Markdown("### Your Document Collection")
         | 
| 525 | 
             
                                    document_list = gr.Textbox(
         | 
| 526 | 
             
                                        label="Documents in Library",
         | 
| 527 | 
            -
                                        value= | 
| 528 | 
             
                                        lines=20,
         | 
| 529 | 
             
                                        interactive=False
         | 
| 530 | 
             
                                    )
         | 
| 531 | 
             
                                    refresh_btn = gr.Button("π Refresh Library", variant="secondary")
         | 
| 532 |  | 
| 533 | 
            -
                                     | 
| 534 | 
             
                                        label="Select Document to Delete",
         | 
| 535 | 
            -
                                        choices=get_document_choices(), | 
| 536 | 
             
                                        value=None,
         | 
| 537 | 
             
                                        interactive=True,
         | 
| 538 | 
             
                                        allow_custom_value=False
         | 
| 539 | 
             
                                    )
         | 
| 540 | 
             
                                    delete_btn = gr.Button("ποΈ Delete Selected Document", variant="stop")
         | 
|  | |
| 541 |  | 
| 542 | 
             
                            refresh_btn.click(
         | 
| 543 | 
            -
                                fn= | 
| 544 | 
            -
                                outputs=[document_list]
         | 
| 545 | 
             
                            )
         | 
| 546 |  | 
| 547 | 
             
                            delete_btn.click(
         | 
| 548 | 
             
                                delete_document_from_library,
         | 
| 549 | 
            -
                                inputs=[ | 
| 550 | 
            -
                                outputs=[document_list, delete_doc_dropdown]
         | 
| 551 | 
             
                            )
         | 
| 552 |  | 
| 553 | 
             
                        # π Upload Documents Tab
         | 
| @@ -575,7 +588,7 @@ def create_gradio_interface(): | |
| 575 | 
             
                            upload_btn.click(
         | 
| 576 | 
             
                                upload_and_process_file,
         | 
| 577 | 
             
                                inputs=[file_input],
         | 
| 578 | 
            -
                                outputs=[upload_output, doc_id_output]
         | 
| 579 | 
             
                            )
         | 
| 580 |  | 
| 581 | 
             
                        # π Search Documents Tab
         | 
| @@ -615,9 +628,9 @@ def create_gradio_interface(): | |
| 615 | 
             
                                with gr.Column():
         | 
| 616 | 
             
                                    gr.Markdown("### Generate Document Summaries")
         | 
| 617 |  | 
| 618 | 
            -
                                     | 
| 619 | 
             
                                        label="Select Document to Summarize",
         | 
| 620 | 
            -
                                        choices= | 
| 621 | 
             
                                        value=None,
         | 
| 622 | 
             
                                        interactive=True,
         | 
| 623 | 
             
                                        allow_custom_value=False
         | 
| @@ -646,7 +659,7 @@ def create_gradio_interface(): | |
| 646 |  | 
| 647 | 
             
                            summarize_btn.click(
         | 
| 648 | 
             
                                summarize_document,
         | 
| 649 | 
            -
                                inputs=[ | 
| 650 | 
             
                                outputs=[summary_output]
         | 
| 651 | 
             
                            )
         | 
| 652 |  | 
| @@ -656,9 +669,9 @@ def create_gradio_interface(): | |
| 656 | 
             
                                with gr.Column():
         | 
| 657 | 
             
                                    gr.Markdown("### Auto-Generate Document Tags")
         | 
| 658 |  | 
| 659 | 
            -
                                     | 
| 660 | 
             
                                        label="Select Document to Tag",
         | 
| 661 | 
            -
                                        choices= | 
| 662 | 
             
                                        value=None,
         | 
| 663 | 
             
                                        interactive=True,
         | 
| 664 | 
             
                                        allow_custom_value=False
         | 
| @@ -688,7 +701,7 @@ def create_gradio_interface(): | |
| 688 |  | 
| 689 | 
             
                            tag_btn.click(
         | 
| 690 | 
             
                                generate_tags_for_document,
         | 
| 691 | 
            -
                                inputs=[ | 
| 692 | 
             
                                outputs=[tag_output]
         | 
| 693 | 
             
                            )
         | 
| 694 |  | 
| @@ -722,24 +735,32 @@ def create_gradio_interface(): | |
| 722 | 
             
                                outputs=[qa_output]
         | 
| 723 | 
             
                            )
         | 
| 724 |  | 
| 725 | 
            -
                    #  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 726 | 
             
                    interface.load(
         | 
| 727 | 
            -
                        fn= | 
| 728 | 
            -
             | 
| 729 | 
            -
                            get_document_choices(),  # for summarize tab
         | 
| 730 | 
            -
                            get_document_choices(),  # for tag tab
         | 
| 731 | 
            -
                            get_document_choices()   # for delete dropdown
         | 
| 732 | 
            -
                        ),
         | 
| 733 | 
            -
                        outputs=[
         | 
| 734 | 
            -
                            document_list,
         | 
| 735 | 
            -
                            doc_dropdown_sum,
         | 
| 736 | 
            -
                            doc_dropdown_tag,
         | 
| 737 | 
            -
                            delete_doc_dropdown
         | 
| 738 | 
            -
                        ]
         | 
| 739 | 
             
                    )
         | 
| 740 |  | 
| 741 | 
            -
                    return interface
         | 
| 742 | 
            -
                    
         | 
| 743 |  | 
| 744 | 
             
            # Create and launch the interface
         | 
| 745 | 
             
            if __name__ == "__main__":
         | 
|  | |
| 307 | 
             
            def upload_and_process_file(file):
         | 
| 308 | 
             
                """Gradio interface for file upload"""
         | 
| 309 | 
             
                if file is None:
         | 
| 310 | 
            +
                    return "No file uploaded", "", get_document_list(), gr.update(choices=get_document_choices()), gr.update(choices=get_document_choices()), gr.update(choices=get_document_choices()), gr.update(choices=get_document_choices())
         | 
| 311 |  | 
| 312 | 
             
                try:
         | 
| 313 | 
             
                    # Get file path
         | 
|  | |
| 330 | 
             
                            doc_list,
         | 
| 331 | 
             
                            gr.update(choices=doc_choices),
         | 
| 332 | 
             
                            gr.update(choices=doc_choices),
         | 
| 333 | 
            +
                            gr.update(choices=doc_choices),
         | 
| 334 | 
             
                            gr.update(choices=doc_choices)
         | 
| 335 | 
             
                        )
         | 
| 336 | 
             
                    else:
         | 
|  | |
| 340 | 
             
                            get_document_list(),
         | 
| 341 | 
             
                            gr.update(choices=get_document_choices()),
         | 
| 342 | 
             
                            gr.update(choices=get_document_choices()),
         | 
| 343 | 
            +
                            gr.update(choices=get_document_choices()),
         | 
| 344 | 
             
                            gr.update(choices=get_document_choices())
         | 
| 345 | 
             
                        )
         | 
| 346 | 
             
                except Exception as e:
         | 
|  | |
| 351 | 
             
                        get_document_list(),
         | 
| 352 | 
             
                        gr.update(choices=get_document_choices()),
         | 
| 353 | 
             
                        gr.update(choices=get_document_choices()),
         | 
| 354 | 
            +
                        gr.update(choices=get_document_choices()),
         | 
| 355 | 
             
                        gr.update(choices=get_document_choices())
         | 
| 356 | 
             
                    )
         | 
| 357 |  | 
|  | |
| 485 | 
             
                    return f"β Error: {str(e)}"
         | 
| 486 |  | 
| 487 | 
             
            def delete_document_from_library(document_id):
         | 
| 488 | 
            +
                """deleting a document from the library"""
         | 
| 489 | 
            +
                try:
         | 
| 490 | 
            +
                    # Run the async delete_document method
         | 
| 491 | 
            +
                    result = mcp_server.run_async(mcp_server.document_store.delete_document(document_id))
         | 
| 492 | 
            +
                    if result:
         | 
| 493 | 
            +
                        msg = f"ποΈ Document {document_id[:8]}... deleted successfully."
         | 
| 494 | 
            +
                    else:
         | 
| 495 | 
            +
                        msg = f"β Failed to delete document {document_id[:8]}..."
         | 
| 496 | 
            +
                    # Refresh document list and choices
         | 
| 497 | 
            +
                    doc_list = get_document_list()
         | 
| 498 | 
            +
                    doc_choices = get_document_choices()
         | 
| 499 | 
            +
                    return msg, doc_list, gr.update(choices=doc_choices), gr.update(choices=doc_choices), gr.update(choices=doc_choices), gr.update(choices=doc_choices)
         | 
| 500 | 
            +
                except Exception as e:
         | 
| 501 | 
            +
                    return f"β Error: {str(e)}", get_document_list(), gr.update(choices=get_document_choices()), gr.update(choices=get_document_choices()), gr.update(choices=get_document_choices()), gr.update(choices=get_document_choices())
         | 
| 502 | 
            +
             | 
| 503 | 
            +
            def refresh_library():
         | 
| 504 | 
            +
                """Refresh the document library display"""
         | 
| 505 | 
            +
                doc_list = get_document_list()
         | 
| 506 | 
            +
                doc_choices = get_document_choices()
         | 
| 507 | 
            +
                return doc_list, gr.update(choices=doc_choices), gr.update(choices=doc_choices), gr.update(choices=doc_choices), gr.update(choices=doc_choices)
         | 
| 508 |  | 
|  | |
| 509 | 
             
            # Create Gradio Interface
         | 
| 510 | 
             
            def create_gradio_interface():
         | 
| 511 | 
             
                with gr.Blocks(title="π§  Intelligent Content Organizer MCP Agent", theme=gr.themes.Soft()) as interface:
         | 
| 512 | 
            +
                    gr.Markdown("""
         | 
| 513 | 
            +
                    # π§  Intelligent Content Organizer MCP Agent
         | 
| 514 | 
            +
             | 
| 515 | 
            +
                    A powerful MCP (Model Context Protocol) server for intelligent content management with semantic search, 
         | 
| 516 | 
            +
                    summarization, and Q&A capabilities powered by Anthropic Claude and Mistral AI.
         | 
| 517 |  | 
| 518 | 
            +
                    ## π Quick Start:
         | 
| 519 | 
            +
                    1. **Upload Documents** β Go to "π Upload Documents" tab  
         | 
| 520 | 
            +
                    2. **Search Your Content** β Use "π Search Documents" to find information  
         | 
| 521 | 
            +
                    3. **Get Summaries** β Select any document in "π Summarize" tab  
         | 
| 522 | 
            +
                    4. **Ask Questions** β Get answers from your documents in "β Ask Questions" tab  
         | 
| 523 | 
            +
                    """)
         | 
| 524 |  | 
| 525 | 
            +
                    # State components for dropdowns
         | 
| 526 | 
            +
                    with gr.Row(visible=False):
         | 
| 527 | 
            +
                        doc_dropdown_sum = gr.Dropdown(label="Hidden", choices=get_document_choices())
         | 
| 528 | 
            +
                        doc_dropdown_tag = gr.Dropdown(label="Hidden", choices=get_document_choices())
         | 
| 529 | 
            +
                        delete_doc_dropdown = gr.Dropdown(label="Hidden", choices=get_document_choices())
         | 
|  | |
| 530 |  | 
| 531 | 
             
                    with gr.Tabs():
         | 
| 532 | 
             
                        # π Document Library Tab
         | 
|  | |
| 536 | 
             
                                    gr.Markdown("### Your Document Collection")
         | 
| 537 | 
             
                                    document_list = gr.Textbox(
         | 
| 538 | 
             
                                        label="Documents in Library",
         | 
| 539 | 
            +
                                        value=get_document_list(),
         | 
| 540 | 
             
                                        lines=20,
         | 
| 541 | 
             
                                        interactive=False
         | 
| 542 | 
             
                                    )
         | 
| 543 | 
             
                                    refresh_btn = gr.Button("π Refresh Library", variant="secondary")
         | 
| 544 |  | 
| 545 | 
            +
                                    delete_doc_dropdown_visible = gr.Dropdown(
         | 
| 546 | 
             
                                        label="Select Document to Delete",
         | 
| 547 | 
            +
                                        choices=get_document_choices(),
         | 
| 548 | 
             
                                        value=None,
         | 
| 549 | 
             
                                        interactive=True,
         | 
| 550 | 
             
                                        allow_custom_value=False
         | 
| 551 | 
             
                                    )
         | 
| 552 | 
             
                                    delete_btn = gr.Button("ποΈ Delete Selected Document", variant="stop")
         | 
| 553 | 
            +
                                    delete_output = gr.Textbox(label="Delete Status", visible=True)
         | 
| 554 |  | 
| 555 | 
             
                            refresh_btn.click(
         | 
| 556 | 
            +
                                fn=refresh_library,
         | 
| 557 | 
            +
                                outputs=[document_list, delete_doc_dropdown_visible, doc_dropdown_sum, doc_dropdown_tag, delete_doc_dropdown]
         | 
| 558 | 
             
                            )
         | 
| 559 |  | 
| 560 | 
             
                            delete_btn.click(
         | 
| 561 | 
             
                                delete_document_from_library,
         | 
| 562 | 
            +
                                inputs=[delete_doc_dropdown_visible],
         | 
| 563 | 
            +
                                outputs=[delete_output, document_list, delete_doc_dropdown_visible, doc_dropdown_sum, doc_dropdown_tag, delete_doc_dropdown]
         | 
| 564 | 
             
                            )
         | 
| 565 |  | 
| 566 | 
             
                        # π Upload Documents Tab
         | 
|  | |
| 588 | 
             
                            upload_btn.click(
         | 
| 589 | 
             
                                upload_and_process_file,
         | 
| 590 | 
             
                                inputs=[file_input],
         | 
| 591 | 
            +
                                outputs=[upload_output, doc_id_output, document_list, delete_doc_dropdown_visible, doc_dropdown_sum, doc_dropdown_tag, delete_doc_dropdown]
         | 
| 592 | 
             
                            )
         | 
| 593 |  | 
| 594 | 
             
                        # π Search Documents Tab
         | 
|  | |
| 628 | 
             
                                with gr.Column():
         | 
| 629 | 
             
                                    gr.Markdown("### Generate Document Summaries")
         | 
| 630 |  | 
| 631 | 
            +
                                    doc_dropdown_sum_visible = gr.Dropdown(
         | 
| 632 | 
             
                                        label="Select Document to Summarize",
         | 
| 633 | 
            +
                                        choices=get_document_choices(),
         | 
| 634 | 
             
                                        value=None,
         | 
| 635 | 
             
                                        interactive=True,
         | 
| 636 | 
             
                                        allow_custom_value=False
         | 
|  | |
| 659 |  | 
| 660 | 
             
                            summarize_btn.click(
         | 
| 661 | 
             
                                summarize_document,
         | 
| 662 | 
            +
                                inputs=[doc_dropdown_sum_visible, summary_text, summary_style],
         | 
| 663 | 
             
                                outputs=[summary_output]
         | 
| 664 | 
             
                            )
         | 
| 665 |  | 
|  | |
| 669 | 
             
                                with gr.Column():
         | 
| 670 | 
             
                                    gr.Markdown("### Auto-Generate Document Tags")
         | 
| 671 |  | 
| 672 | 
            +
                                    doc_dropdown_tag_visible = gr.Dropdown(
         | 
| 673 | 
             
                                        label="Select Document to Tag",
         | 
| 674 | 
            +
                                        choices=get_document_choices(),
         | 
| 675 | 
             
                                        value=None,
         | 
| 676 | 
             
                                        interactive=True,
         | 
| 677 | 
             
                                        allow_custom_value=False
         | 
|  | |
| 701 |  | 
| 702 | 
             
                            tag_btn.click(
         | 
| 703 | 
             
                                generate_tags_for_document,
         | 
| 704 | 
            +
                                inputs=[doc_dropdown_tag_visible, tag_text, max_tags],
         | 
| 705 | 
             
                                outputs=[tag_output]
         | 
| 706 | 
             
                            )
         | 
| 707 |  | 
|  | |
| 735 | 
             
                                outputs=[qa_output]
         | 
| 736 | 
             
                            )
         | 
| 737 |  | 
| 738 | 
            +
                    # Update hidden dropdowns when visible ones change
         | 
| 739 | 
            +
                    doc_dropdown_sum_visible.change(
         | 
| 740 | 
            +
                        lambda x: x,
         | 
| 741 | 
            +
                        inputs=[doc_dropdown_sum_visible],
         | 
| 742 | 
            +
                        outputs=[doc_dropdown_sum]
         | 
| 743 | 
            +
                    )
         | 
| 744 | 
            +
                    
         | 
| 745 | 
            +
                    doc_dropdown_tag_visible.change(
         | 
| 746 | 
            +
                        lambda x: x,
         | 
| 747 | 
            +
                        inputs=[doc_dropdown_tag_visible],
         | 
| 748 | 
            +
                        outputs=[doc_dropdown_tag]
         | 
| 749 | 
            +
                    )
         | 
| 750 | 
            +
                    
         | 
| 751 | 
            +
                    delete_doc_dropdown_visible.change(
         | 
| 752 | 
            +
                        lambda x: x,
         | 
| 753 | 
            +
                        inputs=[delete_doc_dropdown_visible],
         | 
| 754 | 
            +
                        outputs=[delete_doc_dropdown]
         | 
| 755 | 
            +
                    )
         | 
| 756 | 
            +
             | 
| 757 | 
            +
                    # Auto-refresh dropdowns when the app loads
         | 
| 758 | 
             
                    interface.load(
         | 
| 759 | 
            +
                        fn=refresh_library,
         | 
| 760 | 
            +
                        outputs=[document_list, delete_doc_dropdown_visible, doc_dropdown_sum_visible, doc_dropdown_tag_visible, delete_doc_dropdown]
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 761 | 
             
                    )
         | 
| 762 |  | 
| 763 | 
            +
                    return interface           
         | 
|  | |
| 764 |  | 
| 765 | 
             
            # Create and launch the interface
         | 
| 766 | 
             
            if __name__ == "__main__":
         | 

