PearlIsa commited on
Commit
d5166a8
β€’
1 Parent(s): 424ddbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -42
app.py CHANGED
@@ -641,49 +641,51 @@ class AdaptiveMedicalBot:
641
  logger.error(f"Error updating learning system: {e}")
642
 
643
  def create_demo():
644
- """Create Gradio interface with proper initialization"""
645
  try:
646
  bot = AdaptiveMedicalBot()
647
 
648
- def chat(message, history, feedback=None):
649
  try:
650
- # Handle feedback if provided
651
- if feedback and history:
652
- last_interaction = history[-1]
653
- bot.update_from_feedback(
654
- last_interaction[0],
655
- last_interaction[1],
656
- 1 if feedback == "πŸ‘" else -1
657
- )
658
-
659
- # Generate response using the correct method
660
- response_data = bot.generate_adaptive_response(message, history)
661
  response = response_data['response']
662
-
663
- # Update the chat history
664
- history = history + [(message, response)]
665
-
666
- # Return both the response and the updated history for Gradio
667
- return response, history
668
 
669
  except Exception as e:
670
  logger.error(f"Chat error: {e}")
671
- return "I apologize, but I'm experiencing technical difficulties. For emergencies, please call 999.", history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
672
 
673
- # Create Gradio interface with proper state management and updated type
674
- demo = gr.Interface(
675
- fn=chat,
676
- inputs=[
677
- gr.Textbox(label="Your message", placeholder="Type your message here...", lines=2),
678
- gr.State([]), # For chat history (input)
679
- gr.Radio(choices=["πŸ‘", "πŸ‘Ž"], label="Was this response helpful?", visible=True, value=None)
680
- ],
681
- outputs=[
682
- gr.Chatbot(label="GP Medical Triage Assistant", height=500, type='messages'),
683
- gr.State([]) # For chat history (output)
684
- ],
685
- title="GP Medical Triage Assistant - Pearly",
686
- description="""
687
  πŸ‘‹ Hello! I'm Pearly, your GP medical assistant.
688
 
689
  I can help you with:
@@ -696,14 +698,75 @@ def create_demo():
696
  For urgent concerns, call 111.
697
 
698
  Please describe your concerns below.
699
- """,
700
- examples=[
701
- ["I've been having headaches recently"],
702
- ["I need to book a routine checkup"],
703
- ["I'm feeling very anxious lately"]
704
- ],
705
- theme="soft"
706
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
707
 
708
  return demo
709
 
 
641
  logger.error(f"Error updating learning system: {e}")
642
 
643
  def create_demo():
644
+ """Create Gradio interface with proper message formatting"""
645
  try:
646
  bot = AdaptiveMedicalBot()
647
 
648
+ def chat(message: str, history: List[Dict[str, str]]):
649
  try:
650
+ # Convert history to the format expected by the bot
651
+ bot_history = [(h["user"], h["bot"]) for h in history] if history else []
652
+
653
+ # Generate response
654
+ response_data = bot.generate_adaptive_response(message, bot_history)
 
 
 
 
 
 
655
  response = response_data['response']
656
+
657
+ # Format response for Gradio chat
658
+ history.append({"role": "user", "content": message})
659
+ history.append({"role": "assistant", "content": response})
660
+
661
+ return history
662
 
663
  except Exception as e:
664
  logger.error(f"Chat error: {e}")
665
+ return history + [
666
+ {"role": "user", "content": message},
667
+ {"role": "assistant", "content": "I apologize, but I'm experiencing technical difficulties. For emergencies, please call 999."}
668
+ ]
669
+
670
+ def process_feedback(feedback: str, history: List[Dict[str, str]]):
671
+ """Process feedback when received"""
672
+ try:
673
+ if history and len(history) >= 2:
674
+ last_user_msg = history[-2]["content"]
675
+ last_bot_msg = history[-1]["content"]
676
+ bot.update_from_feedback(
677
+ last_user_msg,
678
+ last_bot_msg,
679
+ 1 if feedback == "πŸ‘" else -1
680
+ )
681
+ except Exception as e:
682
+ logger.error(f"Error processing feedback: {e}")
683
 
684
+ # Create Gradio interface with proper message handling
685
+ with gr.Blocks(theme="soft") as demo:
686
+ gr.Markdown("""
687
+ # GP Medical Triage Assistant - Pearly
688
+
 
 
 
 
 
 
 
 
 
689
  πŸ‘‹ Hello! I'm Pearly, your GP medical assistant.
690
 
691
  I can help you with:
 
698
  For urgent concerns, call 111.
699
 
700
  Please describe your concerns below.
701
+ """)
702
+
703
+ # Initialize chat components
704
+ chatbot = gr.Chatbot(
705
+ value=[{"role": "assistant", "content": "Hello! I'm Pearly, your GP medical assistant. How can I help you today?"}],
706
+ height=500,
707
+ type="messages"
708
+ )
709
+ msg = gr.Textbox(
710
+ label="Your message",
711
+ placeholder="Type your message here...",
712
+ lines=2
713
+ )
714
+ with gr.Row():
715
+ submit = gr.Button("Submit")
716
+ clear = gr.Button("Clear")
717
+
718
+ feedback = gr.Radio(
719
+ choices=["πŸ‘", "πŸ‘Ž"],
720
+ label="Was this response helpful?",
721
+ visible=True
722
+ )
723
+
724
+ # Set up event handlers
725
+ submit.click(
726
+ chat,
727
+ inputs=[msg, chatbot],
728
+ outputs=[chatbot],
729
+ queue=False
730
+ ).then(
731
+ lambda: "",
732
+ None,
733
+ msg # Clear input box after sending
734
+ )
735
+
736
+ msg.submit(
737
+ chat,
738
+ inputs=[msg, chatbot],
739
+ outputs=[chatbot],
740
+ queue=False
741
+ ).then(
742
+ lambda: "",
743
+ None,
744
+ msg # Clear input box after sending
745
+ )
746
+
747
+ clear.click(
748
+ lambda: [[], ""],
749
+ None,
750
+ [chatbot, msg],
751
+ queue=False
752
+ )
753
+
754
+ feedback.change(
755
+ process_feedback,
756
+ inputs=[feedback, chatbot],
757
+ outputs=[],
758
+ queue=False
759
+ )
760
+
761
+ # Add example messages
762
+ gr.Examples(
763
+ examples=[
764
+ "I've been having headaches recently",
765
+ "I need to book a routine checkup",
766
+ "I'm feeling very anxious lately"
767
+ ],
768
+ inputs=msg
769
+ )
770
 
771
  return demo
772