Crackershoot commited on
Commit
19f6879
Β·
verified Β·
1 Parent(s): ff75bc9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +177 -142
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
- clear_btn = gr.Button(
523
- "🧹 Clear Chat",
524
- variant="secondary",
525
- elem_classes=["clear-action"]
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
- # # Defines the main chat logic as a generator function for streaming output.
597
- # def chat_ui(user_message, chat_history):
598
- # # Initialize chat history if it's the first turn.
599
- # if chat_history is None:
600
- # chat_history = []
601
 
602
- # # Append the user's message to the chat history.
603
- # chat_history.append({
604
- # "role": "user",
605
- # "content": user_message
606
- # })
607
 
608
- # # Append a temporary "Thinking..." message from the assistant.
609
- # chat_history.append({
610
- # "role": "assistant",
611
- # "content": "πŸ€” Dox is thinking..."
612
- # })
613
 
614
- # # `yield` immediately updates the UI with the user's message and "Thinking...".
615
- # # It also clears the user's input textbox.
616
- # yield (
617
- # chat_history,
618
- # None, # No link yet.
619
- # gr.update(value=None, visible=False), # Hide image preview.
620
- # "" # Clear textbox.
621
- # )
622
 
623
- # # Call the agent to get the actual response and PDF link.
624
- # response_text, link = ask_agent(user_message)
625
 
626
- # # Replace the "Thinking..." message with the final response from the agent.
627
- # chat_history[-1] = {
628
- # "role": "assistant",
629
- # "content": response_text
630
- # }
631
 
632
- # # `yield` again to update the UI with the final response.
633
- # yield (
634
- # chat_history,
635
- # link, # Pass the extracted link to the link_state.
636
- # gr.update(value=None, visible=False), # Keep image preview hidden for now.
637
- # "" # Keep textbox clear.
638
- # )
639
 
640
- # # This is a helper function to avoid repeating the event handler chain.
641
- # def submit_chain():
642
- # # It specifies that `chat_ui` is the function to run.
643
- # # It maps the `question` textbox and `chatbot` history as inputs.
644
- # # It maps the outputs to `chatbot` history, `link_state`, `output_image`, and clears the `question` textbox.
645
- # return (
646
- # chat_ui,
647
- # [question, chatbot],
648
- # [chatbot, link_state, output_image, question]
649
- # )
650
-
651
- # def show_pdf_link(link):
652
- # if link:
653
- # return f"[πŸ“₯ Open Full PDF]({link})"
654
- # return ""
655
-
656
- # def clear_chat():
657
- # return [], None, gr.update(value=None, visible=False), gr.update(value=None, visible=False), gr.update(value=None, visible=False)
658
-
659
- # clear_btn.click(
660
- # clear_chat,
661
- # outputs=[chatbot, link_state, output_image, feedback_box, submit_feedback_btn]
662
- # )
663
-
664
- # def show_feedback_box():
665
- # return gr.update(visible=True), gr.update(visible=True)
666
-
667
- # def show_appreciation():
668
- # logger.info("It was helpful!")
669
- # return "βœ… Feedback submitted. Thank you!"
670
 
671
- # thumbs_down.click(
672
- # show_feedback_box,
673
- # outputs=[feedback_box, submit_feedback_btn]
674
- # )
675
-
676
- # thumbs_up.click(
677
- # show_appreciation,
678
- # outputs=feedback_status
679
- # )
680
-
681
- # def handle_feedback(text):
682
- # logger.info(f"User feedback: {text}")
683
- # return "βœ… Feedback submitted. Thank you!"
684
 
685
- # submit_feedback_btn.click(
686
- # handle_feedback,
687
- # inputs=feedback_box,
688
- # outputs=feedback_status
689
- # )
690
-
691
- # examples.dataset.click(
692
- # *submit_chain()
693
- # ).then(
694
- # prepare_pdf_loading,
695
- # inputs=link_state,
696
- # outputs=pdf_status
697
- # ).then(
698
- # display_pdf,
699
- # inputs=link_state,
700
- # outputs=[output_image, pdf_status]
701
- # ).then(
702
- # show_pdf_link,
703
- # inputs=link_state,
704
- # outputs=pdf_link_btn
705
- # )
706
-
707
- # # Set up the event handler for the "Submit" button click.
708
- # ask_btn.click(
709
- # *submit_chain()
710
- # # `.then()` chains subsequent actions after the first one completes.
711
- # ).then(
712
- # # After chat_ui, call `prepare_pdf_loading` to show the "loading" message.
713
- # prepare_pdf_loading,
714
- # inputs=link_state, # Use the link from chat_ui's output.
715
- # outputs=pdf_status # Update the pdf_status text.
716
- # ).then(
717
- # # Finally, call `display_pdf` to render the PDF page.
718
- # display_pdf,
719
- # inputs=link_state, # Use the same link.
720
- # outputs=[output_image, pdf_status] # Update the image and hide the status text.
721
- # ).then(
722
- # show_pdf_link,
723
- # inputs=link_state,
724
- # outputs=pdf_link_btn
725
- # )
726
 
727
- # # Set up the same event handler for when the user presses Enter in the textbox.
728
- # question.submit(
729
- # *submit_chain()
730
- # ).then(
731
- # prepare_pdf_loading,
732
- # inputs=link_state,
733
- # outputs=pdf_status
734
- # ).then(
735
- # display_pdf,
736
- # inputs=link_state,
737
- # outputs=[output_image, pdf_status]
738
- # ).then(
739
- # show_pdf_link,
740
- # inputs=link_state,
741
- # outputs=pdf_link_btn
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__":