PearlIsa commited on
Commit
73f83b1
β€’
1 Parent(s): 999be6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -26
app.py CHANGED
@@ -644,35 +644,97 @@ class AdaptiveMedicalBot:
644
  logger.error(f"Error updating learning system: {e}")
645
 
646
  def create_demo():
647
- """Create Gradio interface"""
648
- bot = AdaptiveMedicalBot()
649
-
650
- def chat(message, history):
651
- try:
652
- response_data = bot.generate_response(message, history)
653
- return response_data['response']
654
- except Exception as e:
655
- logger.error(f"Chat error: {e}")
656
- return "I apologize, but I'm experiencing technical difficulties. For emergencies, please call 999."
657
-
658
- def handle_feedback(feedback: str, history: List[tuple]):
659
- if history:
660
- last_interaction = history[-1]
661
- bot.update_from_feedback(last_interaction[0], last_interaction[1], 1 if feedback == "πŸ‘" else -1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662
 
663
- demo = gr.ChatInterface(
664
- fn=chat,
665
- title="NHS Medical Triage Assistant",
666
- description="Hello! I'm Pearly, I'm here to help assess your symptoms and provide appropriate healthcare guidance. Please describe your concerns.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
667
 
668
- additional_inputs=[
669
- gr.Radio(["πŸ‘", "πŸ‘Ž"], label="Was this response helpful?", visible=True)
670
- ],
671
- additional_inputs_update_fun=handle_feedback
672
- )
673
-
674
- return demo
675
 
676
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
677
  demo = create_demo()
678
  demo.launch()
 
644
  logger.error(f"Error updating learning system: {e}")
645
 
646
  def create_demo():
647
+ """Create Gradio interface with proper initialization"""
648
+ try:
649
+ bot = AdaptiveMedicalBot()
650
+
651
+ def chat(message, history, feedback=None):
652
+ try:
653
+ # Handle feedback if provided
654
+ if feedback and history:
655
+ last_interaction = history[-1]
656
+ bot.update_from_feedback(
657
+ last_interaction[0],
658
+ last_interaction[1],
659
+ 1 if feedback == "πŸ‘" else -1
660
+ )
661
+
662
+ # For first message, provide introduction
663
+ if not history:
664
+ return "Hello! I'm Pearly, your NHS medical assistant. How can I help you today?"
665
+
666
+ response = bot.generate_response(message, history)
667
+
668
+ # Clean up response formatting
669
+ response = response.strip()
670
+ if response.startswith("Response:"):
671
+ response = response[9:].strip()
672
+
673
+ return response
674
+
675
+ except Exception as e:
676
+ logger.error(f"Chat error: {e}")
677
+ return "I apologize, but I'm experiencing technical difficulties. For emergencies, please call 999."
678
 
679
+ # Create Gradio interface with better styling
680
+ demo = gr.Interface(
681
+ fn=chat,
682
+ inputs=[
683
+ gr.Textbox(
684
+ label="Your message",
685
+ placeholder="Type your message here...",
686
+ lines=2
687
+ ),
688
+ gr.State([]), # For chat history
689
+ gr.Radio(
690
+ choices=["πŸ‘", "πŸ‘Ž"],
691
+ label="Was this response helpful?",
692
+ visible=True,
693
+ value=None
694
+ )
695
+ ],
696
+ outputs=gr.Chatbot(
697
+ label="NHS Medical Triage Assistant",
698
+ height=500
699
+ ),
700
+ title="NHS Medical Triage Assistant - Pearly",
701
+ description="""
702
+ πŸ‘‹ Hello! I'm Pearly, your NHS medical assistant.
703
+
704
+ I can help you with:
705
+ β€’ Assessing your symptoms
706
+ β€’ Finding appropriate care
707
+ β€’ Booking appointments
708
+ β€’ General medical guidance
709
+
710
+ For emergencies, always call 999.
711
+ For urgent concerns, call NHS 111.
712
+
713
+ Please describe your concerns below.
714
+ """,
715
+ examples=[
716
+ ["I've been having headaches recently"],
717
+ ["I need to book a routine checkup"],
718
+ ["I'm feeling very anxious lately"]
719
+ ],
720
+ theme="soft"
721
+ )
722
 
723
+ return demo
724
+
725
+ except Exception as e:
726
+ logger.error(f"Error creating demo: {e}")
727
+ raise
 
 
728
 
729
  if __name__ == "__main__":
730
+ # Initialize environment
731
+ load_dotenv()
732
+
733
+ # Set up HuggingFace login if token exists
734
+ hf_token = os.getenv("HF_TOKEN")
735
+ if hf_token:
736
+ login(token=hf_token)
737
+
738
+ # Launch demo
739
  demo = create_demo()
740
  demo.launch()