Update app.py
Browse files
app.py
CHANGED
|
@@ -519,11 +519,46 @@ with gr.Blocks(title="π€ Dox the Data Professional's Guide π€"#,
|
|
| 519 |
elem_classes=["primary-action"]
|
| 520 |
)
|
| 521 |
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 527 |
|
| 528 |
# # Add titles and descriptions using Markdown.
|
| 529 |
# gr.Markdown("# π€ Dox the Data Professional's Advisor π€")
|
|
@@ -593,153 +628,153 @@ with gr.Blocks(title="π€ Dox the Data Professional's Guide π€"#,
|
|
| 593 |
# )
|
| 594 |
# pdf_link_btn = gr.Markdown("")
|
| 595 |
|
| 596 |
-
#
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
|
| 601 |
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
| 622 |
|
| 623 |
-
|
| 624 |
-
|
| 625 |
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
|
| 640 |
-
#
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
| 652 |
-
|
| 653 |
-
|
| 654 |
-
|
| 655 |
-
|
| 656 |
-
|
| 657 |
-
|
| 658 |
-
|
| 659 |
-
|
| 660 |
-
|
| 661 |
-
|
| 662 |
-
|
| 663 |
-
|
| 664 |
-
|
| 665 |
-
|
| 666 |
-
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
|
| 670 |
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
|
| 674 |
-
|
| 675 |
-
|
| 676 |
-
|
| 677 |
-
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
-
|
| 684 |
|
| 685 |
-
|
| 686 |
-
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
|
| 706 |
-
|
| 707 |
-
#
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
#
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
|
| 727 |
-
#
|
| 728 |
-
|
| 729 |
-
|
| 730 |
-
|
| 731 |
-
|
| 732 |
-
|
| 733 |
-
|
| 734 |
-
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
|
| 738 |
-
|
| 739 |
-
|
| 740 |
-
|
| 741 |
-
|
| 742 |
-
|
| 743 |
|
| 744 |
# This block ensures the code inside only runs when the script is executed directly.
|
| 745 |
if __name__ == "__main__":
|
|
|
|
| 519 |
elem_classes=["primary-action"]
|
| 520 |
)
|
| 521 |
|
| 522 |
+
with gr.Row():
|
| 523 |
+
ask_btn = gr.Button("Submit π€", variant="primary", elem_classes="primary-action")
|
| 524 |
+
clear_btn = gr.Button("π§Ή Clear Chat", elem_classes="clear-action")
|
| 525 |
+
# A section for example questions.
|
| 526 |
+
gr.Markdown("### π‘ Example Questions", elem_classes="component")
|
| 527 |
+
examples = gr.Examples(
|
| 528 |
+
examples=example_questions
|
| 529 |
+
inputs=question,
|
| 530 |
+
outputs=[chatbot, question],
|
| 531 |
+
fn=run_example,
|
| 532 |
+
cache_examples=False
|
| 533 |
+
)
|
| 534 |
+
|
| 535 |
+
# ππ Feedback buttons
|
| 536 |
+
with gr.Row():
|
| 537 |
+
thumbs_up = gr.Button("π Helpful", elem_classes="component")
|
| 538 |
+
thumbs_down = gr.Button("π Not Helpful", elem_classes="component")
|
| 539 |
+
|
| 540 |
+
# Hidden feedback box (only appears on π)
|
| 541 |
+
feedback_box = gr.Textbox(
|
| 542 |
+
placeholder="π¬ Optional: tell us what went wrong...",
|
| 543 |
+
visible=False
|
| 544 |
+
)
|
| 545 |
+
|
| 546 |
+
submit_feedback_btn = gr.Button("π Submit Feedback", visible=False, elem_classes="component")
|
| 547 |
+
feedback_status = gr.Markdown("", elem_classes="component")
|
| 548 |
+
# RIGHT-SIDE COLUMN: for the PDF preview.
|
| 549 |
+
with gr.Column(scale=3):
|
| 550 |
+
gr.Markdown("### π Referenced PDF Document (π Empty for Web Results)", elem_classes="component")
|
| 551 |
+
#gr.Markdown(" π Empty by default", elem_classes="component")
|
| 552 |
+
# A hidden state to store the PDF link found in the agent's response.
|
| 553 |
+
link_state = gr.State()
|
| 554 |
+
# A markdown component to show PDF loading status.
|
| 555 |
+
pdf_status = gr.Markdown(visible=False, elem_classes="component")
|
| 556 |
+
# An image component to display the PDF preview.
|
| 557 |
+
output_image = gr.Image(
|
| 558 |
+
label="β¬οΈ Cheat Sheet Preview",
|
| 559 |
+
visible=False
|
| 560 |
+
)
|
| 561 |
+
pdf_link_btn = gr.Markdown("")
|
| 562 |
|
| 563 |
# # Add titles and descriptions using Markdown.
|
| 564 |
# gr.Markdown("# π€ Dox the Data Professional's Advisor π€")
|
|
|
|
| 628 |
# )
|
| 629 |
# pdf_link_btn = gr.Markdown("")
|
| 630 |
|
| 631 |
+
# Defines the main chat logic as a generator function for streaming output.
|
| 632 |
+
def chat_ui(user_message, chat_history):
|
| 633 |
+
# Initialize chat history if it's the first turn.
|
| 634 |
+
if chat_history is None:
|
| 635 |
+
chat_history = []
|
| 636 |
|
| 637 |
+
# Append the user's message to the chat history.
|
| 638 |
+
chat_history.append({
|
| 639 |
+
"role": "user",
|
| 640 |
+
"content": user_message
|
| 641 |
+
})
|
| 642 |
|
| 643 |
+
# Append a temporary "Thinking..." message from the assistant.
|
| 644 |
+
chat_history.append({
|
| 645 |
+
"role": "assistant",
|
| 646 |
+
"content": "π€ Dox is thinking..."
|
| 647 |
+
})
|
| 648 |
|
| 649 |
+
# `yield` immediately updates the UI with the user's message and "Thinking...".
|
| 650 |
+
# It also clears the user's input textbox.
|
| 651 |
+
yield (
|
| 652 |
+
chat_history,
|
| 653 |
+
None, # No link yet.
|
| 654 |
+
gr.update(value=None, visible=False), # Hide image preview.
|
| 655 |
+
"" # Clear textbox.
|
| 656 |
+
)
|
| 657 |
|
| 658 |
+
# Call the agent to get the actual response and PDF link.
|
| 659 |
+
response_text, link = ask_agent(user_message)
|
| 660 |
|
| 661 |
+
# Replace the "Thinking..." message with the final response from the agent.
|
| 662 |
+
chat_history[-1] = {
|
| 663 |
+
"role": "assistant",
|
| 664 |
+
"content": response_text
|
| 665 |
+
}
|
| 666 |
|
| 667 |
+
# `yield` again to update the UI with the final response.
|
| 668 |
+
yield (
|
| 669 |
+
chat_history,
|
| 670 |
+
link, # Pass the extracted link to the link_state.
|
| 671 |
+
gr.update(value=None, visible=False), # Keep image preview hidden for now.
|
| 672 |
+
"" # Keep textbox clear.
|
| 673 |
+
)
|
| 674 |
|
| 675 |
+
# This is a helper function to avoid repeating the event handler chain.
|
| 676 |
+
def submit_chain():
|
| 677 |
+
# It specifies that `chat_ui` is the function to run.
|
| 678 |
+
# It maps the `question` textbox and `chatbot` history as inputs.
|
| 679 |
+
# It maps the outputs to `chatbot` history, `link_state`, `output_image`, and clears the `question` textbox.
|
| 680 |
+
return (
|
| 681 |
+
chat_ui,
|
| 682 |
+
[question, chatbot],
|
| 683 |
+
[chatbot, link_state, output_image, question]
|
| 684 |
+
)
|
| 685 |
+
|
| 686 |
+
def show_pdf_link(link):
|
| 687 |
+
if link:
|
| 688 |
+
return f"[π₯ Open Full PDF]({link})"
|
| 689 |
+
return ""
|
| 690 |
+
|
| 691 |
+
def clear_chat():
|
| 692 |
+
return [], None, gr.update(value=None, visible=False), gr.update(value=None, visible=False), gr.update(value=None, visible=False)
|
| 693 |
+
|
| 694 |
+
clear_btn.click(
|
| 695 |
+
clear_chat,
|
| 696 |
+
outputs=[chatbot, link_state, output_image, feedback_box, submit_feedback_btn]
|
| 697 |
+
)
|
| 698 |
+
|
| 699 |
+
def show_feedback_box():
|
| 700 |
+
return gr.update(visible=True), gr.update(visible=True)
|
| 701 |
+
|
| 702 |
+
def show_appreciation():
|
| 703 |
+
logger.info("It was helpful!")
|
| 704 |
+
return "β
Feedback submitted. Thank you!"
|
| 705 |
|
| 706 |
+
thumbs_down.click(
|
| 707 |
+
show_feedback_box,
|
| 708 |
+
outputs=[feedback_box, submit_feedback_btn]
|
| 709 |
+
)
|
| 710 |
+
|
| 711 |
+
thumbs_up.click(
|
| 712 |
+
show_appreciation,
|
| 713 |
+
outputs=feedback_status
|
| 714 |
+
)
|
| 715 |
+
|
| 716 |
+
def handle_feedback(text):
|
| 717 |
+
logger.info(f"User feedback: {text}")
|
| 718 |
+
return "β
Feedback submitted. Thank you!"
|
| 719 |
|
| 720 |
+
submit_feedback_btn.click(
|
| 721 |
+
handle_feedback,
|
| 722 |
+
inputs=feedback_box,
|
| 723 |
+
outputs=feedback_status
|
| 724 |
+
)
|
| 725 |
+
|
| 726 |
+
examples.dataset.click(
|
| 727 |
+
*submit_chain()
|
| 728 |
+
).then(
|
| 729 |
+
prepare_pdf_loading,
|
| 730 |
+
inputs=link_state,
|
| 731 |
+
outputs=pdf_status
|
| 732 |
+
).then(
|
| 733 |
+
display_pdf,
|
| 734 |
+
inputs=link_state,
|
| 735 |
+
outputs=[output_image, pdf_status]
|
| 736 |
+
).then(
|
| 737 |
+
show_pdf_link,
|
| 738 |
+
inputs=link_state,
|
| 739 |
+
outputs=pdf_link_btn
|
| 740 |
+
)
|
| 741 |
+
|
| 742 |
+
# Set up the event handler for the "Submit" button click.
|
| 743 |
+
ask_btn.click(
|
| 744 |
+
*submit_chain()
|
| 745 |
+
# `.then()` chains subsequent actions after the first one completes.
|
| 746 |
+
).then(
|
| 747 |
+
# After chat_ui, call `prepare_pdf_loading` to show the "loading" message.
|
| 748 |
+
prepare_pdf_loading,
|
| 749 |
+
inputs=link_state, # Use the link from chat_ui's output.
|
| 750 |
+
outputs=pdf_status # Update the pdf_status text.
|
| 751 |
+
).then(
|
| 752 |
+
# Finally, call `display_pdf` to render the PDF page.
|
| 753 |
+
display_pdf,
|
| 754 |
+
inputs=link_state, # Use the same link.
|
| 755 |
+
outputs=[output_image, pdf_status] # Update the image and hide the status text.
|
| 756 |
+
).then(
|
| 757 |
+
show_pdf_link,
|
| 758 |
+
inputs=link_state,
|
| 759 |
+
outputs=pdf_link_btn
|
| 760 |
+
)
|
| 761 |
|
| 762 |
+
# Set up the same event handler for when the user presses Enter in the textbox.
|
| 763 |
+
question.submit(
|
| 764 |
+
*submit_chain()
|
| 765 |
+
).then(
|
| 766 |
+
prepare_pdf_loading,
|
| 767 |
+
inputs=link_state,
|
| 768 |
+
outputs=pdf_status
|
| 769 |
+
).then(
|
| 770 |
+
display_pdf,
|
| 771 |
+
inputs=link_state,
|
| 772 |
+
outputs=[output_image, pdf_status]
|
| 773 |
+
).then(
|
| 774 |
+
show_pdf_link,
|
| 775 |
+
inputs=link_state,
|
| 776 |
+
outputs=pdf_link_btn
|
| 777 |
+
)
|
| 778 |
|
| 779 |
# This block ensures the code inside only runs when the script is executed directly.
|
| 780 |
if __name__ == "__main__":
|