Spaces:
Sleeping
Sleeping
Update engine.py
Browse files
engine.py
CHANGED
|
@@ -226,6 +226,51 @@ def has_underlying_data(sql):
|
|
| 226 |
cur = conn.cursor()
|
| 227 |
return cur.execute(test_sql).fetchone() is not None
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
# =========================
|
| 230 |
# MAIN ENGINE
|
| 231 |
# =========================
|
|
@@ -234,6 +279,16 @@ def process_question(question):
|
|
| 234 |
global LAST_PROMPT_TYPE, LAST_SUGGESTED_DATE
|
| 235 |
|
| 236 |
q = question.strip().lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
|
| 238 |
# Handle WHY questions
|
| 239 |
if is_why_question(q) and LAST_PROMPT_TYPE == "NO_DATA":
|
|
@@ -311,7 +366,8 @@ def process_question(question):
|
|
| 311 |
"status": "ok",
|
| 312 |
"message": friendly("No data is available for that time period."),
|
| 313 |
"note": f"Available data is only up to {LAST_SUGGESTED_DATE}.",
|
| 314 |
-
"data": []
|
|
|
|
| 315 |
}
|
| 316 |
|
| 317 |
if not rows:
|
|
|
|
| 226 |
cur = conn.cursor()
|
| 227 |
return cur.execute(test_sql).fetchone() is not None
|
| 228 |
|
| 229 |
+
def is_patient_summary_question(text):
|
| 230 |
+
t = text.lower()
|
| 231 |
+
return any(
|
| 232 |
+
phrase in t for phrase in [
|
| 233 |
+
"patient summary",
|
| 234 |
+
"patients summary",
|
| 235 |
+
"patient overview",
|
| 236 |
+
"summary of patients",
|
| 237 |
+
"give a patient summary",
|
| 238 |
+
]
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
# =========================
|
| 242 |
+
# PATIENT SUMMARY
|
| 243 |
+
# =========================
|
| 244 |
+
|
| 245 |
+
def build_patient_summary():
|
| 246 |
+
cur = conn.cursor()
|
| 247 |
+
|
| 248 |
+
total = cur.execute(
|
| 249 |
+
"SELECT COUNT(*) FROM patients"
|
| 250 |
+
).fetchone()[0]
|
| 251 |
+
|
| 252 |
+
genders = cur.execute(
|
| 253 |
+
"SELECT gender, COUNT(*) FROM patients GROUP BY gender"
|
| 254 |
+
).fetchall()
|
| 255 |
+
|
| 256 |
+
msg = "Here’s a quick summary of patients:\n\n"
|
| 257 |
+
msg += f"• Total patients: {total}\n"
|
| 258 |
+
|
| 259 |
+
if genders:
|
| 260 |
+
msg += "• Gender distribution:\n"
|
| 261 |
+
for g, c in genders:
|
| 262 |
+
msg += f" - {g}: {c}\n"
|
| 263 |
+
|
| 264 |
+
msg += (
|
| 265 |
+
"\nYou can also ask:\n"
|
| 266 |
+
"• Patients admitted by year\n"
|
| 267 |
+
"• Patient count by age\n"
|
| 268 |
+
"• Visit trends"
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
return msg
|
| 272 |
+
|
| 273 |
+
|
| 274 |
# =========================
|
| 275 |
# MAIN ENGINE
|
| 276 |
# =========================
|
|
|
|
| 279 |
global LAST_PROMPT_TYPE, LAST_SUGGESTED_DATE
|
| 280 |
|
| 281 |
q = question.strip().lower()
|
| 282 |
+
|
| 283 |
+
# Patient summary intent
|
| 284 |
+
if is_patient_summary_question(q):
|
| 285 |
+
return {
|
| 286 |
+
"status": "ok",
|
| 287 |
+
"message": build_patient_summary(),
|
| 288 |
+
"data": [],
|
| 289 |
+
"sql": None,
|
| 290 |
+
"note": None
|
| 291 |
+
}
|
| 292 |
|
| 293 |
# Handle WHY questions
|
| 294 |
if is_why_question(q) and LAST_PROMPT_TYPE == "NO_DATA":
|
|
|
|
| 366 |
"status": "ok",
|
| 367 |
"message": friendly("No data is available for that time period."),
|
| 368 |
"note": f"Available data is only up to {LAST_SUGGESTED_DATE}.",
|
| 369 |
+
"data": [],
|
| 370 |
+
"sql": None
|
| 371 |
}
|
| 372 |
|
| 373 |
if not rows:
|