Update app.py
Browse files
app.py
CHANGED
|
@@ -111,7 +111,7 @@ class ContentOrganizerMCPServer:
|
|
| 111 |
"error": str(e),
|
| 112 |
"message": "Failed to process document"
|
| 113 |
}
|
| 114 |
-
|
| 115 |
async def get_document_content_async(self, document_id: str) -> Optional[str]:
|
| 116 |
"""Get document content by ID"""
|
| 117 |
try:
|
|
@@ -457,22 +457,6 @@ def generate_tags_for_document(doc_choice, custom_text, max_tags):
|
|
| 457 |
logger.error(f"Tag generation error: {str(e)}")
|
| 458 |
return f"β Error: {str(e)}"
|
| 459 |
|
| 460 |
-
def delete_document_from_library(document_id):
|
| 461 |
-
"""deleting a document from the library"""
|
| 462 |
-
try:
|
| 463 |
-
# Run the async delete_document method
|
| 464 |
-
result = mcp_server.run_async(mcp_server.document_store.delete_document(document_id))
|
| 465 |
-
if result:
|
| 466 |
-
msg = f"ποΈ Document {document_id[:8]}... deleted successfully."
|
| 467 |
-
else:
|
| 468 |
-
msg = f"β Failed to delete document {document_id[:8]}..."
|
| 469 |
-
# Refresh document list and choices
|
| 470 |
-
doc_list = get_document_list()
|
| 471 |
-
doc_choices = get_document_choices()
|
| 472 |
-
return msg, doc_list, gr.update(choices=doc_choices)
|
| 473 |
-
except Exception as e:
|
| 474 |
-
return f"β Error: {str(e)}", get_document_list(), gr.update(choices=get_document_choices())
|
| 475 |
-
|
| 476 |
def ask_question(question):
|
| 477 |
"""Gradio interface for Q&A"""
|
| 478 |
if not question.strip():
|
|
@@ -497,28 +481,40 @@ def ask_question(question):
|
|
| 497 |
except Exception as e:
|
| 498 |
return f"β Error: {str(e)}"
|
| 499 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 500 |
# Create Gradio Interface
|
| 501 |
def create_gradio_interface():
|
| 502 |
with gr.Blocks(title="π§ Intelligent Content Organizer MCP Agent", theme=gr.themes.Soft()) as interface:
|
| 503 |
gr.Markdown("""
|
| 504 |
# π§ Intelligent Content Organizer MCP Agent
|
| 505 |
-
|
| 506 |
A powerful MCP (Model Context Protocol) server for intelligent content management with semantic search,
|
| 507 |
summarization, and Q&A capabilities powered by Anthropic Claude and Mistral AI.
|
| 508 |
-
|
| 509 |
## π Quick Start:
|
| 510 |
-
1. **Upload Documents** β Go to "π Upload Documents" tab
|
| 511 |
-
2. **Search Your Content** β Use "π Search Documents" to find information
|
| 512 |
-
3. **Get Summaries** β Select any document in "π Summarize" tab
|
| 513 |
-
4. **Ask Questions** β Get answers from your documents in "β Ask Questions" tab
|
| 514 |
-
|
| 515 |
""")
|
| 516 |
-
|
| 517 |
-
# Shared components for document selection
|
| 518 |
-
doc_choices = gr.State(get_document_choices())
|
| 519 |
-
|
| 520 |
with gr.Tabs():
|
| 521 |
-
# Document Library Tab
|
| 522 |
with gr.Tab("π Document Library"):
|
| 523 |
with gr.Row():
|
| 524 |
with gr.Column():
|
|
@@ -530,26 +526,28 @@ def create_gradio_interface():
|
|
| 530 |
interactive=False
|
| 531 |
)
|
| 532 |
refresh_btn = gr.Button("π Refresh Library", variant="secondary")
|
| 533 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 534 |
refresh_btn.click(
|
| 535 |
fn=get_document_list,
|
| 536 |
outputs=[document_list]
|
| 537 |
)
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
delete_btn.click(
|
| 547 |
-
delete_document_from_library,
|
| 548 |
-
inputs=[delete_doc_dropdown],
|
| 549 |
-
outputs=[document_list, document_list, delete_doc_dropdown]
|
| 550 |
-
)
|
| 551 |
-
|
| 552 |
-
# Document Ingestion Tab
|
| 553 |
with gr.Tab("π Upload Documents"):
|
| 554 |
with gr.Row():
|
| 555 |
with gr.Column():
|
|
@@ -570,25 +568,21 @@ def create_gradio_interface():
|
|
| 570 |
label="Document ID",
|
| 571 |
placeholder="Document ID will appear here after processing..."
|
| 572 |
)
|
| 573 |
-
|
| 574 |
-
# Hidden dropdowns for updating
|
| 575 |
-
doc_dropdown_sum = gr.Dropdown(label="Hidden", visible=False)
|
| 576 |
-
doc_dropdown_tag = gr.Dropdown(label="Hidden", visible=False)
|
| 577 |
-
|
| 578 |
upload_btn.click(
|
| 579 |
upload_and_process_file,
|
| 580 |
inputs=[file_input],
|
| 581 |
-
outputs=[upload_output, doc_id_output
|
| 582 |
)
|
| 583 |
-
|
| 584 |
-
#
|
| 585 |
with gr.Tab("π Search Documents"):
|
| 586 |
with gr.Row():
|
| 587 |
with gr.Column(scale=1):
|
| 588 |
gr.Markdown("### Search Your Document Library")
|
| 589 |
search_query = gr.Textbox(
|
| 590 |
label="What are you looking for?",
|
| 591 |
-
placeholder="Enter your search query...
|
| 592 |
lines=2
|
| 593 |
)
|
| 594 |
search_top_k = gr.Slider(
|
|
@@ -605,35 +599,33 @@ def create_gradio_interface():
|
|
| 605 |
lines=20,
|
| 606 |
placeholder="Search results will appear here..."
|
| 607 |
)
|
| 608 |
-
|
| 609 |
search_btn.click(
|
| 610 |
perform_search,
|
| 611 |
inputs=[search_query, search_top_k],
|
| 612 |
outputs=[search_output]
|
| 613 |
)
|
| 614 |
-
|
| 615 |
-
#
|
| 616 |
with gr.Tab("π Summarize"):
|
| 617 |
with gr.Row():
|
| 618 |
with gr.Column():
|
| 619 |
gr.Markdown("### Generate Document Summaries")
|
| 620 |
-
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
)
|
| 636 |
-
|
| 637 |
summary_style = gr.Dropdown(
|
| 638 |
label="Summary Style",
|
| 639 |
choices=["concise", "detailed", "bullet_points", "executive"],
|
|
@@ -641,42 +633,40 @@ def create_gradio_interface():
|
|
| 641 |
info="Choose how you want the summary formatted"
|
| 642 |
)
|
| 643 |
summarize_btn = gr.Button("π Generate Summary", variant="primary", size="lg")
|
| 644 |
-
|
| 645 |
with gr.Column():
|
| 646 |
summary_output = gr.Textbox(
|
| 647 |
label="Generated Summary",
|
| 648 |
lines=20,
|
| 649 |
placeholder="Summary will appear here..."
|
| 650 |
)
|
| 651 |
-
|
| 652 |
summarize_btn.click(
|
| 653 |
summarize_document,
|
| 654 |
inputs=[doc_dropdown_sum, summary_text, summary_style],
|
| 655 |
outputs=[summary_output]
|
| 656 |
)
|
| 657 |
-
|
| 658 |
-
#
|
| 659 |
with gr.Tab("π·οΈ Generate Tags"):
|
| 660 |
with gr.Row():
|
| 661 |
with gr.Column():
|
| 662 |
gr.Markdown("### Auto-Generate Document Tags")
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
)
|
| 679 |
-
|
| 680 |
max_tags = gr.Slider(
|
| 681 |
label="Number of Tags",
|
| 682 |
minimum=3,
|
|
@@ -685,57 +675,61 @@ def create_gradio_interface():
|
|
| 685 |
step=1
|
| 686 |
)
|
| 687 |
tag_btn = gr.Button("π·οΈ Generate Tags", variant="primary", size="lg")
|
| 688 |
-
|
| 689 |
with gr.Column():
|
| 690 |
tag_output = gr.Textbox(
|
| 691 |
label="Generated Tags",
|
| 692 |
lines=10,
|
| 693 |
placeholder="Tags will appear here..."
|
| 694 |
)
|
| 695 |
-
|
| 696 |
tag_btn.click(
|
| 697 |
generate_tags_for_document,
|
| 698 |
inputs=[doc_dropdown_tag, tag_text, max_tags],
|
| 699 |
outputs=[tag_output]
|
| 700 |
)
|
| 701 |
-
|
| 702 |
-
#
|
| 703 |
with gr.Tab("β Ask Questions"):
|
| 704 |
with gr.Row():
|
| 705 |
with gr.Column():
|
| 706 |
gr.Markdown("""
|
| 707 |
### Ask Questions About Your Documents
|
| 708 |
-
|
| 709 |
The AI will search through all your uploaded documents to find relevant information
|
| 710 |
and provide comprehensive answers with sources.
|
| 711 |
""")
|
| 712 |
qa_question = gr.Textbox(
|
| 713 |
label="Your Question",
|
| 714 |
-
placeholder="Ask anything about your documents...
|
| 715 |
lines=3
|
| 716 |
)
|
| 717 |
qa_btn = gr.Button("β Get Answer", variant="primary", size="lg")
|
| 718 |
-
|
| 719 |
with gr.Column():
|
| 720 |
qa_output = gr.Textbox(
|
| 721 |
label="AI Answer",
|
| 722 |
lines=20,
|
| 723 |
placeholder="Answer will appear here with sources..."
|
| 724 |
)
|
| 725 |
-
|
| 726 |
qa_btn.click(
|
| 727 |
ask_question,
|
| 728 |
inputs=[qa_question],
|
| 729 |
outputs=[qa_output]
|
| 730 |
)
|
| 731 |
-
|
| 732 |
-
# Auto-refresh
|
| 733 |
interface.load(
|
| 734 |
-
fn=lambda: (
|
|
|
|
|
|
|
|
|
|
|
|
|
| 735 |
outputs=[document_list, doc_dropdown_sum, doc_dropdown_tag]
|
| 736 |
)
|
| 737 |
-
|
| 738 |
-
return interface
|
| 739 |
|
| 740 |
# Create and launch the interface
|
| 741 |
if __name__ == "__main__":
|
|
|
|
| 111 |
"error": str(e),
|
| 112 |
"message": "Failed to process document"
|
| 113 |
}
|
| 114 |
+
|
| 115 |
async def get_document_content_async(self, document_id: str) -> Optional[str]:
|
| 116 |
"""Get document content by ID"""
|
| 117 |
try:
|
|
|
|
| 457 |
logger.error(f"Tag generation error: {str(e)}")
|
| 458 |
return f"β Error: {str(e)}"
|
| 459 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 460 |
def ask_question(question):
|
| 461 |
"""Gradio interface for Q&A"""
|
| 462 |
if not question.strip():
|
|
|
|
| 481 |
except Exception as e:
|
| 482 |
return f"β Error: {str(e)}"
|
| 483 |
|
| 484 |
+
def delete_document_from_library(document_id):
|
| 485 |
+
"""deleting a document from the library"""
|
| 486 |
+
try:
|
| 487 |
+
# Run the async delete_document method
|
| 488 |
+
result = mcp_server.run_async(mcp_server.document_store.delete_document(document_id))
|
| 489 |
+
if result:
|
| 490 |
+
msg = f"ποΈ Document {document_id[:8]}... deleted successfully."
|
| 491 |
+
else:
|
| 492 |
+
msg = f"β Failed to delete document {document_id[:8]}..."
|
| 493 |
+
# Refresh document list and choices
|
| 494 |
+
doc_list = get_document_list()
|
| 495 |
+
doc_choices = get_document_choices()
|
| 496 |
+
return msg, doc_list, gr.update(choices=doc_choices)
|
| 497 |
+
except Exception as e:
|
| 498 |
+
return f"β Error: {str(e)}", get_document_list(), gr.update(choices=get_document_choices())
|
| 499 |
+
|
| 500 |
# Create Gradio Interface
|
| 501 |
def create_gradio_interface():
|
| 502 |
with gr.Blocks(title="π§ Intelligent Content Organizer MCP Agent", theme=gr.themes.Soft()) as interface:
|
| 503 |
gr.Markdown("""
|
| 504 |
# π§ Intelligent Content Organizer MCP Agent
|
| 505 |
+
|
| 506 |
A powerful MCP (Model Context Protocol) server for intelligent content management with semantic search,
|
| 507 |
summarization, and Q&A capabilities powered by Anthropic Claude and Mistral AI.
|
| 508 |
+
|
| 509 |
## π Quick Start:
|
| 510 |
+
1. **Upload Documents** β Go to "π Upload Documents" tab
|
| 511 |
+
2. **Search Your Content** β Use "π Search Documents" to find information
|
| 512 |
+
3. **Get Summaries** β Select any document in "π Summarize" tab
|
| 513 |
+
4. **Ask Questions** β Get answers from your documents in "β Ask Questions" tab
|
|
|
|
| 514 |
""")
|
| 515 |
+
|
|
|
|
|
|
|
|
|
|
| 516 |
with gr.Tabs():
|
| 517 |
+
# π Document Library Tab
|
| 518 |
with gr.Tab("π Document Library"):
|
| 519 |
with gr.Row():
|
| 520 |
with gr.Column():
|
|
|
|
| 526 |
interactive=False
|
| 527 |
)
|
| 528 |
refresh_btn = gr.Button("π Refresh Library", variant="secondary")
|
| 529 |
+
|
| 530 |
+
delete_doc_dropdown = gr.Dropdown(
|
| 531 |
+
label="Select Document to Delete",
|
| 532 |
+
choices=get_document_choices(),
|
| 533 |
+
value=None,
|
| 534 |
+
interactive=True,
|
| 535 |
+
allow_custom_value=False
|
| 536 |
+
)
|
| 537 |
+
delete_btn = gr.Button("ποΈ Delete Selected Document", variant="stop")
|
| 538 |
+
|
| 539 |
refresh_btn.click(
|
| 540 |
fn=get_document_list,
|
| 541 |
outputs=[document_list]
|
| 542 |
)
|
| 543 |
+
|
| 544 |
+
delete_btn.click(
|
| 545 |
+
delete_document_from_library,
|
| 546 |
+
inputs=[delete_doc_dropdown],
|
| 547 |
+
outputs=[document_list, delete_doc_dropdown]
|
| 548 |
+
)
|
| 549 |
+
|
| 550 |
+
# π Upload Documents Tab
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 551 |
with gr.Tab("π Upload Documents"):
|
| 552 |
with gr.Row():
|
| 553 |
with gr.Column():
|
|
|
|
| 568 |
label="Document ID",
|
| 569 |
placeholder="Document ID will appear here after processing..."
|
| 570 |
)
|
| 571 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 572 |
upload_btn.click(
|
| 573 |
upload_and_process_file,
|
| 574 |
inputs=[file_input],
|
| 575 |
+
outputs=[upload_output, doc_id_output]
|
| 576 |
)
|
| 577 |
+
|
| 578 |
+
# π Search Documents Tab
|
| 579 |
with gr.Tab("π Search Documents"):
|
| 580 |
with gr.Row():
|
| 581 |
with gr.Column(scale=1):
|
| 582 |
gr.Markdown("### Search Your Document Library")
|
| 583 |
search_query = gr.Textbox(
|
| 584 |
label="What are you looking for?",
|
| 585 |
+
placeholder="Enter your search query...",
|
| 586 |
lines=2
|
| 587 |
)
|
| 588 |
search_top_k = gr.Slider(
|
|
|
|
| 599 |
lines=20,
|
| 600 |
placeholder="Search results will appear here..."
|
| 601 |
)
|
| 602 |
+
|
| 603 |
search_btn.click(
|
| 604 |
perform_search,
|
| 605 |
inputs=[search_query, search_top_k],
|
| 606 |
outputs=[search_output]
|
| 607 |
)
|
| 608 |
+
|
| 609 |
+
# π Summarize Tab
|
| 610 |
with gr.Tab("π Summarize"):
|
| 611 |
with gr.Row():
|
| 612 |
with gr.Column():
|
| 613 |
gr.Markdown("### Generate Document Summaries")
|
| 614 |
+
|
| 615 |
+
doc_dropdown_sum = gr.Dropdown(
|
| 616 |
+
label="Select Document to Summarize",
|
| 617 |
+
choices=get_document_choices(),
|
| 618 |
+
value=None,
|
| 619 |
+
interactive=True,
|
| 620 |
+
allow_custom_value=False
|
| 621 |
+
)
|
| 622 |
+
|
| 623 |
+
summary_text = gr.Textbox(
|
| 624 |
+
label="Or Paste Text to Summarize",
|
| 625 |
+
placeholder="Paste any text here to summarize...",
|
| 626 |
+
lines=8
|
| 627 |
+
)
|
| 628 |
+
|
|
|
|
|
|
|
| 629 |
summary_style = gr.Dropdown(
|
| 630 |
label="Summary Style",
|
| 631 |
choices=["concise", "detailed", "bullet_points", "executive"],
|
|
|
|
| 633 |
info="Choose how you want the summary formatted"
|
| 634 |
)
|
| 635 |
summarize_btn = gr.Button("π Generate Summary", variant="primary", size="lg")
|
| 636 |
+
|
| 637 |
with gr.Column():
|
| 638 |
summary_output = gr.Textbox(
|
| 639 |
label="Generated Summary",
|
| 640 |
lines=20,
|
| 641 |
placeholder="Summary will appear here..."
|
| 642 |
)
|
| 643 |
+
|
| 644 |
summarize_btn.click(
|
| 645 |
summarize_document,
|
| 646 |
inputs=[doc_dropdown_sum, summary_text, summary_style],
|
| 647 |
outputs=[summary_output]
|
| 648 |
)
|
| 649 |
+
|
| 650 |
+
# π·οΈ Generate Tags Tab
|
| 651 |
with gr.Tab("π·οΈ Generate Tags"):
|
| 652 |
with gr.Row():
|
| 653 |
with gr.Column():
|
| 654 |
gr.Markdown("### Auto-Generate Document Tags")
|
| 655 |
+
|
| 656 |
+
doc_dropdown_tag = gr.Dropdown(
|
| 657 |
+
label="Select Document to Tag",
|
| 658 |
+
choices=get_document_choices(),
|
| 659 |
+
value=None,
|
| 660 |
+
interactive=True,
|
| 661 |
+
allow_custom_value=False
|
| 662 |
+
)
|
| 663 |
+
|
| 664 |
+
tag_text = gr.Textbox(
|
| 665 |
+
label="Or Paste Text to Generate Tags",
|
| 666 |
+
placeholder="Paste any text here to generate tags...",
|
| 667 |
+
lines=8
|
| 668 |
+
)
|
| 669 |
+
|
|
|
|
|
|
|
| 670 |
max_tags = gr.Slider(
|
| 671 |
label="Number of Tags",
|
| 672 |
minimum=3,
|
|
|
|
| 675 |
step=1
|
| 676 |
)
|
| 677 |
tag_btn = gr.Button("π·οΈ Generate Tags", variant="primary", size="lg")
|
| 678 |
+
|
| 679 |
with gr.Column():
|
| 680 |
tag_output = gr.Textbox(
|
| 681 |
label="Generated Tags",
|
| 682 |
lines=10,
|
| 683 |
placeholder="Tags will appear here..."
|
| 684 |
)
|
| 685 |
+
|
| 686 |
tag_btn.click(
|
| 687 |
generate_tags_for_document,
|
| 688 |
inputs=[doc_dropdown_tag, tag_text, max_tags],
|
| 689 |
outputs=[tag_output]
|
| 690 |
)
|
| 691 |
+
|
| 692 |
+
# β Ask Questions Tab
|
| 693 |
with gr.Tab("β Ask Questions"):
|
| 694 |
with gr.Row():
|
| 695 |
with gr.Column():
|
| 696 |
gr.Markdown("""
|
| 697 |
### Ask Questions About Your Documents
|
| 698 |
+
|
| 699 |
The AI will search through all your uploaded documents to find relevant information
|
| 700 |
and provide comprehensive answers with sources.
|
| 701 |
""")
|
| 702 |
qa_question = gr.Textbox(
|
| 703 |
label="Your Question",
|
| 704 |
+
placeholder="Ask anything about your documents...",
|
| 705 |
lines=3
|
| 706 |
)
|
| 707 |
qa_btn = gr.Button("β Get Answer", variant="primary", size="lg")
|
| 708 |
+
|
| 709 |
with gr.Column():
|
| 710 |
qa_output = gr.Textbox(
|
| 711 |
label="AI Answer",
|
| 712 |
lines=20,
|
| 713 |
placeholder="Answer will appear here with sources..."
|
| 714 |
)
|
| 715 |
+
|
| 716 |
qa_btn.click(
|
| 717 |
ask_question,
|
| 718 |
inputs=[qa_question],
|
| 719 |
outputs=[qa_output]
|
| 720 |
)
|
| 721 |
+
|
| 722 |
+
# Auto-refresh dropdowns when the app loads
|
| 723 |
interface.load(
|
| 724 |
+
fn=lambda: (
|
| 725 |
+
get_document_list(),
|
| 726 |
+
get_document_choices(),
|
| 727 |
+
get_document_choices()
|
| 728 |
+
),
|
| 729 |
outputs=[document_list, doc_dropdown_sum, doc_dropdown_tag]
|
| 730 |
)
|
| 731 |
+
|
| 732 |
+
return interface
|
| 733 |
|
| 734 |
# Create and launch the interface
|
| 735 |
if __name__ == "__main__":
|