bhavika24 commited on
Commit
8d22257
·
verified ·
1 Parent(s): 9496a38

Update engine.py

Browse files
Files changed (1) hide show
  1. engine.py +31 -10
engine.py CHANGED
@@ -16,6 +16,7 @@ conn = sqlite3.connect("hospital.db", check_same_thread=False)
16
  # =========================
17
 
18
  LAST_PROMPT_TYPE = None
 
19
 
20
  # =========================
21
  # HUMAN RESPONSE HELPERS
@@ -30,6 +31,9 @@ def friendly(text):
30
  def is_confirmation(text):
31
  return text.strip().lower() in ["yes", "yep", "yeah", "ok", "okay", "sure"]
32
 
 
 
 
33
  # =========================
34
  # SPELL CORRECTION
35
  # =========================
@@ -227,12 +231,29 @@ def has_underlying_data(sql):
227
  # =========================
228
 
229
  def process_question(question):
230
- global LAST_PROMPT_TYPE
 
 
231
 
232
- question = question.strip().lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
 
234
- # Handle confirmation replies like "yes"
235
- if is_confirmation(question) and LAST_PROMPT_TYPE == "NO_DATA":
236
  return {
237
  "status": "ok",
238
  "message": (
@@ -241,8 +262,7 @@ def process_question(question):
241
  "• How many patients were admitted in 2021?\n"
242
  "• Patient count by gender\n"
243
  "• Total visits by month\n"
244
- "• Most common conditions\n\n"
245
- "Just type one of these or ask your own question."
246
  ),
247
  "data": []
248
  }
@@ -286,25 +306,26 @@ def process_question(question):
286
 
287
  if is_aggregate_only_query(sql) and not has_underlying_data(sql):
288
  LAST_PROMPT_TYPE = "NO_DATA"
289
- latest = get_latest_data_date()
290
  return {
291
  "status": "ok",
292
  "message": friendly("No data is available for that time period."),
293
- "note": f"Available data is only up to {latest}.",
294
  "data": []
295
  }
296
 
297
  if not rows:
298
  LAST_PROMPT_TYPE = "NO_DATA"
299
- latest = get_latest_data_date()
300
  return {
301
  "status": "ok",
302
  "message": friendly("No records found."),
303
- "note": f"Available data is only up to {latest}.",
304
  "data": []
305
  }
306
 
307
  LAST_PROMPT_TYPE = None
 
308
 
309
  return {
310
  "status": "ok",
 
16
  # =========================
17
 
18
  LAST_PROMPT_TYPE = None
19
+ LAST_SUGGESTED_DATE = None
20
 
21
  # =========================
22
  # HUMAN RESPONSE HELPERS
 
31
  def is_confirmation(text):
32
  return text.strip().lower() in ["yes", "yep", "yeah", "ok", "okay", "sure"]
33
 
34
+ def is_why_question(text):
35
+ return text.strip().lower().startswith("why")
36
+
37
  # =========================
38
  # SPELL CORRECTION
39
  # =========================
 
231
  # =========================
232
 
233
  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":
240
+ return {
241
+ "status": "ok",
242
+ "message": (
243
+ f"I suggested **{LAST_SUGGESTED_DATE[:4]}** because that’s the most recent year "
244
+ f"for which data exists in the system.\n\n"
245
+ "Your database doesn’t contain newer records, so questions like "
246
+ "“today” or “this year” can’t be answered yet.\n\n"
247
+ "You can still explore:\n"
248
+ "• Data from 2021\n"
249
+ "• Trends over time\n"
250
+ "• Patient summaries"
251
+ ),
252
+ "data": []
253
+ }
254
 
255
+ # Handle confirmations
256
+ if is_confirmation(q) and LAST_PROMPT_TYPE == "NO_DATA":
257
  return {
258
  "status": "ok",
259
  "message": (
 
262
  "• How many patients were admitted in 2021?\n"
263
  "• Patient count by gender\n"
264
  "• Total visits by month\n"
265
+ "• Most common conditions"
 
266
  ),
267
  "data": []
268
  }
 
306
 
307
  if is_aggregate_only_query(sql) and not has_underlying_data(sql):
308
  LAST_PROMPT_TYPE = "NO_DATA"
309
+ LAST_SUGGESTED_DATE = get_latest_data_date()
310
  return {
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:
318
  LAST_PROMPT_TYPE = "NO_DATA"
319
+ LAST_SUGGESTED_DATE = get_latest_data_date()
320
  return {
321
  "status": "ok",
322
  "message": friendly("No records found."),
323
+ "note": f"Available data is only up to {LAST_SUGGESTED_DATE}.",
324
  "data": []
325
  }
326
 
327
  LAST_PROMPT_TYPE = None
328
+ LAST_SUGGESTED_DATE = None
329
 
330
  return {
331
  "status": "ok",