Crackershoot commited on
Commit
8d811fe
·
verified ·
1 Parent(s): b537b8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -137
app.py CHANGED
@@ -541,153 +541,153 @@ with gr.Blocks(
541
  # )
542
  # pdf_link_btn = gr.Markdown("")
543
 
544
- # Defines the main chat logic as a generator function for streaming output.
545
- def chat_ui(user_message, chat_history):
546
- # Initialize chat history if it's the first turn.
547
- if chat_history is None:
548
- chat_history = []
549
 
550
- # Append the user's message to the chat history.
551
- chat_history.append({
552
- "role": "user",
553
- "content": user_message
554
- })
555
 
556
- # Append a temporary "Thinking..." message from the assistant.
557
- chat_history.append({
558
- "role": "assistant",
559
- "content": "🤔 Dox is thinking..."
560
- })
561
 
562
- # `yield` immediately updates the UI with the user's message and "Thinking...".
563
- # It also clears the user's input textbox.
564
- yield (
565
- chat_history,
566
- None, # No link yet.
567
- gr.update(value=None, visible=False), # Hide image preview.
568
- "" # Clear textbox.
569
- )
570
 
571
- # Call the agent to get the actual response and PDF link.
572
- response_text, link = ask_agent(user_message)
573
 
574
- # Replace the "Thinking..." message with the final response from the agent.
575
- chat_history[-1] = {
576
- "role": "assistant",
577
- "content": response_text
578
- }
579
 
580
- # `yield` again to update the UI with the final response.
581
- yield (
582
- chat_history,
583
- link, # Pass the extracted link to the link_state.
584
- gr.update(value=None, visible=False), # Keep image preview hidden for now.
585
- "" # Keep textbox clear.
586
- )
587
 
588
- # This is a helper function to avoid repeating the event handler chain.
589
- def submit_chain():
590
- # It specifies that `chat_ui` is the function to run.
591
- # It maps the `question` textbox and `chatbot` history as inputs.
592
- # It maps the outputs to `chatbot` history, `link_state`, `output_image`, and clears the `question` textbox.
593
- return (
594
- chat_ui,
595
- [question, chatbot],
596
- [chatbot, link_state, output_image, question]
597
- )
598
-
599
- def show_pdf_link(link):
600
- if link:
601
- return f"[📥 Open Full PDF]({link})"
602
- return ""
603
-
604
- def clear_chat():
605
- return [], None, gr.update(value=None, visible=False), gr.update(value=None, visible=False), gr.update(value=None, visible=False)
606
-
607
- clear_btn.click(
608
- clear_chat,
609
- outputs=[chatbot, link_state, output_image, feedback_box, submit_feedback_btn]
610
- )
611
-
612
- def show_feedback_box():
613
- return gr.update(visible=True), gr.update(visible=True)
614
-
615
- def show_appreciation():
616
- logger.info("It was helpful!")
617
- return "✅ Feedback submitted. Thank you!"
618
 
619
- thumbs_down.click(
620
- show_feedback_box,
621
- outputs=[feedback_box, submit_feedback_btn]
622
- )
623
-
624
- thumbs_up.click(
625
- show_appreciation,
626
- outputs=feedback_status
627
- )
628
-
629
- def handle_feedback(text):
630
- logger.info(f"User feedback: {text}")
631
- return "✅ Feedback submitted. Thank you!"
632
 
633
- submit_feedback_btn.click(
634
- handle_feedback,
635
- inputs=feedback_box,
636
- outputs=feedback_status
637
- )
638
-
639
- examples.dataset.click(
640
- *submit_chain()
641
- ).then(
642
- prepare_pdf_loading,
643
- inputs=link_state,
644
- outputs=pdf_status
645
- ).then(
646
- display_pdf,
647
- inputs=link_state,
648
- outputs=[output_image, pdf_status]
649
- ).then(
650
- show_pdf_link,
651
- inputs=link_state,
652
- outputs=pdf_link_btn
653
- )
654
-
655
- # Set up the event handler for the "Submit" button click.
656
- ask_btn.click(
657
- *submit_chain()
658
- # `.then()` chains subsequent actions after the first one completes.
659
- ).then(
660
- # After chat_ui, call `prepare_pdf_loading` to show the "loading" message.
661
- prepare_pdf_loading,
662
- inputs=link_state, # Use the link from chat_ui's output.
663
- outputs=pdf_status # Update the pdf_status text.
664
- ).then(
665
- # Finally, call `display_pdf` to render the PDF page.
666
- display_pdf,
667
- inputs=link_state, # Use the same link.
668
- outputs=[output_image, pdf_status] # Update the image and hide the status text.
669
- ).then(
670
- show_pdf_link,
671
- inputs=link_state,
672
- outputs=pdf_link_btn
673
- )
674
 
675
- # Set up the same event handler for when the user presses Enter in the textbox.
676
- question.submit(
677
- *submit_chain()
678
- ).then(
679
- prepare_pdf_loading,
680
- inputs=link_state,
681
- outputs=pdf_status
682
- ).then(
683
- display_pdf,
684
- inputs=link_state,
685
- outputs=[output_image, pdf_status]
686
- ).then(
687
- show_pdf_link,
688
- inputs=link_state,
689
- outputs=pdf_link_btn
690
- )
691
 
692
  # This block ensures the code inside only runs when the script is executed directly.
693
  if __name__ == "__main__":
 
541
  # )
542
  # pdf_link_btn = gr.Markdown("")
543
 
544
+ # # Defines the main chat logic as a generator function for streaming output.
545
+ # def chat_ui(user_message, chat_history):
546
+ # # Initialize chat history if it's the first turn.
547
+ # if chat_history is None:
548
+ # chat_history = []
549
 
550
+ # # Append the user's message to the chat history.
551
+ # chat_history.append({
552
+ # "role": "user",
553
+ # "content": user_message
554
+ # })
555
 
556
+ # # Append a temporary "Thinking..." message from the assistant.
557
+ # chat_history.append({
558
+ # "role": "assistant",
559
+ # "content": "🤔 Dox is thinking..."
560
+ # })
561
 
562
+ # # `yield` immediately updates the UI with the user's message and "Thinking...".
563
+ # # It also clears the user's input textbox.
564
+ # yield (
565
+ # chat_history,
566
+ # None, # No link yet.
567
+ # gr.update(value=None, visible=False), # Hide image preview.
568
+ # "" # Clear textbox.
569
+ # )
570
 
571
+ # # Call the agent to get the actual response and PDF link.
572
+ # response_text, link = ask_agent(user_message)
573
 
574
+ # # Replace the "Thinking..." message with the final response from the agent.
575
+ # chat_history[-1] = {
576
+ # "role": "assistant",
577
+ # "content": response_text
578
+ # }
579
 
580
+ # # `yield` again to update the UI with the final response.
581
+ # yield (
582
+ # chat_history,
583
+ # link, # Pass the extracted link to the link_state.
584
+ # gr.update(value=None, visible=False), # Keep image preview hidden for now.
585
+ # "" # Keep textbox clear.
586
+ # )
587
 
588
+ # # This is a helper function to avoid repeating the event handler chain.
589
+ # def submit_chain():
590
+ # # It specifies that `chat_ui` is the function to run.
591
+ # # It maps the `question` textbox and `chatbot` history as inputs.
592
+ # # It maps the outputs to `chatbot` history, `link_state`, `output_image`, and clears the `question` textbox.
593
+ # return (
594
+ # chat_ui,
595
+ # [question, chatbot],
596
+ # [chatbot, link_state, output_image, question]
597
+ # )
598
+
599
+ # def show_pdf_link(link):
600
+ # if link:
601
+ # return f"[📥 Open Full PDF]({link})"
602
+ # return ""
603
+
604
+ # def clear_chat():
605
+ # return [], None, gr.update(value=None, visible=False), gr.update(value=None, visible=False), gr.update(value=None, visible=False)
606
+
607
+ # clear_btn.click(
608
+ # clear_chat,
609
+ # outputs=[chatbot, link_state, output_image, feedback_box, submit_feedback_btn]
610
+ # )
611
+
612
+ # def show_feedback_box():
613
+ # return gr.update(visible=True), gr.update(visible=True)
614
+
615
+ # def show_appreciation():
616
+ # logger.info("It was helpful!")
617
+ # return "✅ Feedback submitted. Thank you!"
618
 
619
+ # thumbs_down.click(
620
+ # show_feedback_box,
621
+ # outputs=[feedback_box, submit_feedback_btn]
622
+ # )
623
+
624
+ # thumbs_up.click(
625
+ # show_appreciation,
626
+ # outputs=feedback_status
627
+ # )
628
+
629
+ # def handle_feedback(text):
630
+ # logger.info(f"User feedback: {text}")
631
+ # return "✅ Feedback submitted. Thank you!"
632
 
633
+ # submit_feedback_btn.click(
634
+ # handle_feedback,
635
+ # inputs=feedback_box,
636
+ # outputs=feedback_status
637
+ # )
638
+
639
+ # examples.dataset.click(
640
+ # *submit_chain()
641
+ # ).then(
642
+ # prepare_pdf_loading,
643
+ # inputs=link_state,
644
+ # outputs=pdf_status
645
+ # ).then(
646
+ # display_pdf,
647
+ # inputs=link_state,
648
+ # outputs=[output_image, pdf_status]
649
+ # ).then(
650
+ # show_pdf_link,
651
+ # inputs=link_state,
652
+ # outputs=pdf_link_btn
653
+ # )
654
+
655
+ # # Set up the event handler for the "Submit" button click.
656
+ # ask_btn.click(
657
+ # *submit_chain()
658
+ # # `.then()` chains subsequent actions after the first one completes.
659
+ # ).then(
660
+ # # After chat_ui, call `prepare_pdf_loading` to show the "loading" message.
661
+ # prepare_pdf_loading,
662
+ # inputs=link_state, # Use the link from chat_ui's output.
663
+ # outputs=pdf_status # Update the pdf_status text.
664
+ # ).then(
665
+ # # Finally, call `display_pdf` to render the PDF page.
666
+ # display_pdf,
667
+ # inputs=link_state, # Use the same link.
668
+ # outputs=[output_image, pdf_status] # Update the image and hide the status text.
669
+ # ).then(
670
+ # show_pdf_link,
671
+ # inputs=link_state,
672
+ # outputs=pdf_link_btn
673
+ # )
674
 
675
+ # # Set up the same event handler for when the user presses Enter in the textbox.
676
+ # question.submit(
677
+ # *submit_chain()
678
+ # ).then(
679
+ # prepare_pdf_loading,
680
+ # inputs=link_state,
681
+ # outputs=pdf_status
682
+ # ).then(
683
+ # display_pdf,
684
+ # inputs=link_state,
685
+ # outputs=[output_image, pdf_status]
686
+ # ).then(
687
+ # show_pdf_link,
688
+ # inputs=link_state,
689
+ # outputs=pdf_link_btn
690
+ # )
691
 
692
  # This block ensures the code inside only runs when the script is executed directly.
693
  if __name__ == "__main__":