Update app.py
Browse files
app.py
CHANGED
|
@@ -53,13 +53,33 @@ class StopOnTokens(StoppingCriteria):
|
|
| 53 |
# Medical Keywords and Validation
|
| 54 |
# =============================
|
| 55 |
MEDICAL_KEYWORDS = [
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
"fever", "cough", "cold", "flu", "infection", "allergy", "diabetes", "pressure",
|
| 58 |
-
"
|
| 59 |
-
|
| 60 |
-
"
|
| 61 |
-
"
|
| 62 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
]
|
| 64 |
|
| 65 |
CASUAL_ONLY_PATTERNS = [
|
|
@@ -77,8 +97,12 @@ def is_medical_query(message):
|
|
| 77 |
if keyword in message_lower:
|
| 78 |
return True
|
| 79 |
|
| 80 |
-
#
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
return True
|
| 83 |
|
| 84 |
return False
|
|
@@ -104,9 +128,9 @@ def is_only_greeting(message):
|
|
| 104 |
def get_response(user_input, history_context):
|
| 105 |
"""Generate response from ChatDoctor model"""
|
| 106 |
|
| 107 |
-
#
|
| 108 |
-
if
|
| 109 |
-
return "Hello! I'm ChatDoctor, an AI medical assistant. I
|
| 110 |
|
| 111 |
human_invitation = "Patient: "
|
| 112 |
doctor_invitation = "ChatDoctor: "
|
|
|
|
| 53 |
# Medical Keywords and Validation
|
| 54 |
# =============================
|
| 55 |
MEDICAL_KEYWORDS = [
|
| 56 |
+
# Symptoms
|
| 57 |
+
"pain", "ache", "symptom", "hurt", "sore", "discomfort", "suffering",
|
| 58 |
+
# Common conditions
|
| 59 |
"fever", "cough", "cold", "flu", "infection", "allergy", "diabetes", "pressure",
|
| 60 |
+
"asthma", "migraine", "nausea", "vomit", "diarrhea", "constipation",
|
| 61 |
+
# Body parts
|
| 62 |
+
"heart", "stomach", "head", "back", "chest", "throat", "lung", "kidney",
|
| 63 |
+
"liver", "brain", "skin", "eye", "ear", "nose", "tooth", "teeth", "joint",
|
| 64 |
+
"muscle", "bone", "neck", "shoulder", "knee", "ankle", "foot", "hand",
|
| 65 |
+
# Medical terms
|
| 66 |
+
"doctor", "hospital", "clinic", "emergency", "ambulance", "medication",
|
| 67 |
+
"medicine", "prescription", "diagnosis", "treatment", "therapy", "cure",
|
| 68 |
+
"sick", "ill", "disease", "condition", "disorder", "syndrome",
|
| 69 |
+
# Injuries
|
| 70 |
+
"injury", "wound", "cut", "bruise", "fracture", "sprain", "burn", "bleed",
|
| 71 |
+
# Vitals and tests
|
| 72 |
+
"blood", "pressure", "temperature", "pulse", "breathing", "test", "scan",
|
| 73 |
+
# Mental health
|
| 74 |
+
"stress", "anxiety", "depression", "mental", "sleep", "insomnia", "tired",
|
| 75 |
+
"fatigue", "exhausted", "mood", "panic", "worry",
|
| 76 |
+
# Lifestyle/wellness
|
| 77 |
+
"diet", "nutrition", "exercise", "weight", "vitamin", "supplement", "healthy",
|
| 78 |
+
"wellness", "fitness", "eating", "appetite",
|
| 79 |
+
# Serious conditions
|
| 80 |
+
"cancer", "tumor", "surgery", "stroke", "attack", "seizure", "diabetic",
|
| 81 |
+
# Questions about health
|
| 82 |
+
"health", "medical", "feel", "feeling", "comfortable", "uncomfortable"
|
| 83 |
]
|
| 84 |
|
| 85 |
CASUAL_ONLY_PATTERNS = [
|
|
|
|
| 97 |
if keyword in message_lower:
|
| 98 |
return True
|
| 99 |
|
| 100 |
+
# Check for question words combined with longer messages (might be medical)
|
| 101 |
+
question_words = ["what", "how", "why", "when", "where", "can", "should", "is", "are", "do", "does"]
|
| 102 |
+
has_question = any(q in message_lower.split()[:3] for q in question_words)
|
| 103 |
+
|
| 104 |
+
# If it has a question word and is longer than 5 words, might be medical
|
| 105 |
+
if has_question and len(message.split()) > 5:
|
| 106 |
return True
|
| 107 |
|
| 108 |
return False
|
|
|
|
| 128 |
def get_response(user_input, history_context):
|
| 129 |
"""Generate response from ChatDoctor model"""
|
| 130 |
|
| 131 |
+
# STRICT FILTERING: Only allow medical queries to reach the model
|
| 132 |
+
if not is_medical_query(user_input):
|
| 133 |
+
return "Hello! I'm ChatDoctor, an AI medical assistant specialized in health and medical topics. I can help you with:\n\n• Symptoms and health concerns\n• Medical conditions and treatments\n• General health advice\n• Wellness and prevention\n\nPlease describe any health-related symptoms or medical questions you have, and I'll do my best to assist you."
|
| 134 |
|
| 135 |
human_invitation = "Patient: "
|
| 136 |
doctor_invitation = "ChatDoctor: "
|