Spaces:
Running
Running
Commit
·
455e0c8
1
Parent(s):
9af961d
Made changes to chatbot.py
Browse files- app/chatbot.py +35 -4
app/chatbot.py
CHANGED
@@ -76,6 +76,31 @@ def is_affirmative(answer):
|
|
76 |
answer_lower = answer.lower()
|
77 |
return any(word in answer_lower for word in ["yes", "yeah", "yep", "certainly", "sometimes", "a little"])
|
78 |
import random
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
# -------------------------------
|
80 |
# Chatbot session class
|
81 |
# -------------------------------
|
@@ -132,7 +157,7 @@ class ChatbotSession:
|
|
132 |
if unexplored_symptoms:
|
133 |
symptom = random.choice(unexplored_symptoms)
|
134 |
self.current_detail_symptom = symptom
|
135 |
-
self.symptom_details[symptom] = {}
|
136 |
self.state = "symptom_detail"
|
137 |
return f"Doctor: About your '{symptom}', when did it start? (e.g., 2 days ago)"
|
138 |
else:
|
@@ -149,7 +174,13 @@ class ChatbotSession:
|
|
149 |
|
150 |
if self.current_detail_symptom and 'severity' not in self.symptom_details[self.current_detail_symptom]:
|
151 |
self.symptom_details[self.current_detail_symptom]['severity'] = message
|
152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
if self.current_detail_symptom and 'location' not in self.symptom_details[self.current_detail_symptom]:
|
155 |
self.symptom_details[self.current_detail_symptom]['location'] = message
|
@@ -182,11 +213,11 @@ class ChatbotSession:
|
|
182 |
matches = len(set(self.reported_symptoms) & set(symptoms))
|
183 |
if matches > 0:
|
184 |
score = matches / len(symptoms)
|
185 |
-
if matches >= 2:
|
186 |
match_scores.append((disease, score))
|
187 |
|
188 |
match_scores.sort(key=lambda x: x[1], reverse=True)
|
189 |
-
self.predicted_diseases = match_scores[:2]
|
190 |
|
191 |
def _generate_summary(self):
|
192 |
report_lines = []
|
|
|
76 |
answer_lower = answer.lower()
|
77 |
return any(word in answer_lower for word in ["yes", "yeah", "yep", "certainly", "sometimes", "a little"])
|
78 |
import random
|
79 |
+
# Map symptoms to associated body parts
|
80 |
+
SYMPTOM_BODY_PARTS = {
|
81 |
+
"headache": "head",
|
82 |
+
"fever": None,
|
83 |
+
"cold": None,
|
84 |
+
"cough": "throat",
|
85 |
+
"chest pain": "chest",
|
86 |
+
"stomach ache": "abdomen",
|
87 |
+
"sore throat": "throat",
|
88 |
+
"back pain": "back",
|
89 |
+
"ear pain": "ear",
|
90 |
+
"toothache": "mouth",
|
91 |
+
"nausea": "stomach",
|
92 |
+
"vomiting": "stomach",
|
93 |
+
"diarrhea": "abdomen",
|
94 |
+
"shortness of breath": "chest",
|
95 |
+
"dizziness": "head",
|
96 |
+
"rash": "skin",
|
97 |
+
"eye pain": "eye",
|
98 |
+
"abdominal pain": "abdomen",
|
99 |
+
"joint pain": "joint",
|
100 |
+
"muscle pain": "muscle",
|
101 |
+
"neck pain": "neck"
|
102 |
+
}
|
103 |
+
|
104 |
# -------------------------------
|
105 |
# Chatbot session class
|
106 |
# -------------------------------
|
|
|
157 |
if unexplored_symptoms:
|
158 |
symptom = random.choice(unexplored_symptoms)
|
159 |
self.current_detail_symptom = symptom
|
160 |
+
self.symptom_details[symptom] = {}
|
161 |
self.state = "symptom_detail"
|
162 |
return f"Doctor: About your '{symptom}', when did it start? (e.g., 2 days ago)"
|
163 |
else:
|
|
|
174 |
|
175 |
if self.current_detail_symptom and 'severity' not in self.symptom_details[self.current_detail_symptom]:
|
176 |
self.symptom_details[self.current_detail_symptom]['severity'] = message
|
177 |
+
body_part = SYMPTOM_BODY_PARTS.get(self.current_detail_symptom)
|
178 |
+
if body_part:
|
179 |
+
self.symptom_details[self.current_detail_symptom]['location'] = body_part
|
180 |
+
self.state = "intake"
|
181 |
+
return "Doctor: Thank you. Are there any more symptoms you'd like to mention?"
|
182 |
+
else:
|
183 |
+
return f"Doctor: Where exactly do you feel the '{self.current_detail_symptom}'? (e.g., forehead, chest, abdomen)"
|
184 |
|
185 |
if self.current_detail_symptom and 'location' not in self.symptom_details[self.current_detail_symptom]:
|
186 |
self.symptom_details[self.current_detail_symptom]['location'] = message
|
|
|
213 |
matches = len(set(self.reported_symptoms) & set(symptoms))
|
214 |
if matches > 0:
|
215 |
score = matches / len(symptoms)
|
216 |
+
if matches >= 2:
|
217 |
match_scores.append((disease, score))
|
218 |
|
219 |
match_scores.sort(key=lambda x: x[1], reverse=True)
|
220 |
+
self.predicted_diseases = match_scores[:2]
|
221 |
|
222 |
def _generate_summary(self):
|
223 |
report_lines = []
|