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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +188 -64
app.py CHANGED
@@ -641,11 +641,11 @@ class AdaptiveMedicalBot:
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 []
@@ -667,8 +667,8 @@ def create_demo():
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"]
@@ -676,98 +676,222 @@ def create_demo():
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:
692
- β€’ Assessing your symptoms
693
- β€’ Finding appropriate care
694
- β€’ Booking appointments
695
- β€’ General medical guidance
696
-
697
- For emergencies, always call 999.
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
 
773
  except Exception as e:
 
641
  logger.error(f"Error updating learning system: {e}")
642
 
643
  def create_demo():
644
+ """Create enhanced Gradio interface with advanced features and styling"""
645
  try:
646
  bot = AdaptiveMedicalBot()
647
 
648
+ def chat(message: str, history: List[Dict[str, str]], urgency_filter: str = "all"):
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 []
 
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]], comment: str = ""):
671
+ """Process feedback with optional comment"""
672
  try:
673
  if history and len(history) >= 2:
674
  last_user_msg = history[-2]["content"]
 
676
  bot.update_from_feedback(
677
  last_user_msg,
678
  last_bot_msg,
679
+ 1 if feedback == "πŸ‘" else -1,
680
+ comment=comment
681
  )
682
  except Exception as e:
683
  logger.error(f"Error processing feedback: {e}")
684
 
685
+ # Create enhanced Gradio interface
686
+ with gr.Blocks(theme=gr.themes.Soft(
687
+ primary_hue="blue",
688
+ secondary_hue="indigo",
689
+ neutral_hue="slate",
690
+ font=gr.themes.GoogleFont("Inter")
691
+ )) as demo:
692
+ # Custom CSS for enhanced styling
693
+ gr.HTML("""
694
+ <style>
695
+ .container { max-width: 900px; margin: auto; }
696
+ .header { text-align: center; padding: 20px; }
697
+ .emergency-banner {
698
+ background-color: #ff4444;
699
+ color: white;
700
+ padding: 10px;
701
+ text-align: center;
702
+ font-weight: bold;
703
+ margin-bottom: 20px;
704
+ }
705
+ .features-grid {
706
+ display: grid;
707
+ grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
708
+ gap: 20px;
709
+ padding: 20px;
710
+ }
711
+ .feature-card {
712
+ background: #f8f9fa;
713
+ padding: 15px;
714
+ border-radius: 10px;
715
+ text-align: center;
716
+ }
717
+ </style>
718
  """)
719
 
720
+ # Emergency Banner
721
+ gr.HTML("""
722
+ <div class="emergency-banner">
723
+ 🚨 For medical emergencies, always call 999 immediately 🚨
724
+ </div>
725
+ """)
726
+
727
+ # Header Section
728
+ with gr.Row(elem_classes="header"):
729
+ gr.Markdown("""
730
+ # GP Medical Triage Assistant - Pearly
731
+
732
+ Welcome to your personal medical triage assistant. I'm here to help assess your symptoms and guide you to appropriate care.
733
+ """)
734
+
735
+ # Main Features Grid
736
+ gr.HTML("""
737
+ <div class="features-grid">
738
+ <div class="feature-card">
739
+ πŸ₯ GP Appointments
740
+ </div>
741
+ <div class="feature-card">
742
+ πŸ” Symptom Assessment
743
+ </div>
744
+ <div class="feature-card">
745
+ ⚑ Urgent Care Guide
746
+ </div>
747
+ <div class="feature-card">
748
+ πŸ’Š Medical Advice
749
+ </div>
750
+ </div>
751
+ """)
752
+
753
+ # Chat Interface
754
  with gr.Row():
755
+ with gr.Column(scale=4):
756
+ chatbot = gr.Chatbot(
757
+ value=[{
758
+ "role": "assistant",
759
+ "content": "Hello! I'm Pearly, your GP medical assistant. How can I help you today?"
760
+ }],
761
+ height=500,
762
+ elem_id="chatbot",
763
+ type="messages",
764
+ show_label=False
765
+ )
766
+
767
+ with gr.Row():
768
+ msg = gr.Textbox(
769
+ label="Your message",
770
+ placeholder="Type your message here...",
771
+ lines=2,
772
+ scale=4
773
+ )
774
+ submit = gr.Button("Send", variant="primary", scale=1)
775
+
776
+ with gr.Column(scale=1):
777
+ # Quick Actions Panel
778
+ gr.Markdown("### Quick Actions")
779
+ emergency_btn = gr.Button("🚨 Emergency Info", variant="secondary")
780
+ nhs_111_btn = gr.Button("πŸ“ž NHS 111 Info", variant="secondary")
781
+ booking_btn = gr.Button("πŸ“… GP Booking", variant="secondary")
782
+
783
+ # Conversation Controls
784
+ gr.Markdown("### Controls")
785
+ clear = gr.Button("πŸ—‘οΈ Clear Chat")
786
+
787
+ # Feedback Section
788
+ gr.Markdown("### Feedback")
789
+ feedback = gr.Radio(
790
+ choices=["πŸ‘", "πŸ‘Ž"],
791
+ label="Was this response helpful?",
792
+ visible=True
793
+ )
794
+ feedback_text = gr.Textbox(
795
+ label="Additional comments (optional)",
796
+ placeholder="Tell us more about your experience...",
797
+ lines=2
798
+ )
799
+
800
+ # Examples Section
801
+ with gr.Accordion("Example Messages", open=False):
802
+ gr.Examples(
803
+ examples=[
804
+ ["I've been having severe headaches for the past week"],
805
+ ["I need to book a routine checkup"],
806
+ ["I'm feeling very anxious lately and need help"],
807
+ ["My child has had a fever for 2 days"],
808
+ ["I need information about COVID-19 testing"]
809
+ ],
810
+ inputs=msg
811
+ )
812
+
813
+ # Information Accordions
814
+ with gr.Accordion("NHS Services Guide", open=False):
815
+ gr.Markdown("""
816
+ ### Emergency Services (999)
817
+ - Life-threatening emergencies
818
+ - Severe injuries
819
+ - Suspected heart attack or stroke
820
+
821
+ ### NHS 111
822
+ - Urgent but non-emergency situations
823
+ - Medical advice needed
824
+ - Unsure where to go
825
+
826
+ ### GP Services
827
+ - Routine check-ups
828
+ - Non-urgent medical issues
829
+ - Prescription renewals
830
+ """)
831
+
832
+ # Event Handlers
833
  submit.click(
834
  chat,
835
  inputs=[msg, chatbot],
836
+ outputs=[chatbot]
 
837
  ).then(
838
  lambda: "",
839
  None,
840
+ msg
841
  )
842
 
843
  msg.submit(
844
  chat,
845
  inputs=[msg, chatbot],
846
+ outputs=[chatbot]
 
847
  ).then(
848
  lambda: "",
849
  None,
850
+ msg
851
  )
852
 
853
  clear.click(
854
  lambda: [[], ""],
855
  None,
856
+ [chatbot, msg]
 
857
  )
858
 
859
  feedback.change(
860
  process_feedback,
861
+ inputs=[feedback, chatbot, feedback_text],
862
+ outputs=[]
 
863
  )
864
 
865
+ # Quick Action Button Handlers
866
+ def show_emergency_info():
867
+ return """🚨 Emergency Services (999)
868
+ - For life-threatening emergencies
869
+ - Severe chest pain
870
+ - Difficulty breathing
871
+ - Severe bleeding
872
+ - Loss of consciousness
873
+ """
874
+
875
+ def show_nhs_111_info():
876
+ return """πŸ“ž NHS 111 Service
877
+ - Available 24/7
878
+ - Medical advice
879
+ - Local service information
880
+ - Urgent care guidance
881
+ """
882
+
883
+ def show_booking_info():
884
+ return """πŸ“… GP Booking Options
885
+ - Online booking
886
+ - Phone booking
887
+ - Routine appointments
888
+ - Urgent appointments
889
+ """
890
+
891
+ emergency_btn.click(lambda: show_emergency_info(), outputs=[msg])
892
+ nhs_111_btn.click(lambda: show_nhs_111_info(), outputs=[msg])
893
+ booking_btn.click(lambda: show_booking_info(), outputs=[msg])
894
+
895
  return demo
896
 
897
  except Exception as e: