Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
662 |
|
663 |
-
|
664 |
-
|
665 |
-
|
666 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
667 |
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
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()
|