Spaces:
Sleeping
Sleeping
Upload engine.py
Browse files
engine.py
CHANGED
|
@@ -219,6 +219,34 @@ def interpret_empty_result(question: str):
|
|
| 219 |
latest = get_latest_data_date()
|
| 220 |
return f"No results found. Available data is up to {latest}."
|
| 221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
# =========================
|
| 224 |
# ORCHESTRATOR (Single Entry Point)
|
|
@@ -236,7 +264,17 @@ def process_question(question: str):
|
|
| 236 |
}
|
| 237 |
|
| 238 |
# 2. Time relevance
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
# 3. Generate SQL
|
| 242 |
sql = generate_sql(question)
|
|
|
|
| 219 |
latest = get_latest_data_date()
|
| 220 |
return f"No results found. Available data is up to {latest}."
|
| 221 |
|
| 222 |
+
# =========================
|
| 223 |
+
# Data Range Check
|
| 224 |
+
# =========================
|
| 225 |
+
from datetime import datetime
|
| 226 |
+
|
| 227 |
+
def is_request_out_of_data_range(question: str) -> bool:
|
| 228 |
+
latest = get_latest_data_date()
|
| 229 |
+
|
| 230 |
+
if not latest:
|
| 231 |
+
return True
|
| 232 |
+
|
| 233 |
+
latest_date = datetime.fromisoformat(latest.replace("Z", "").split("T")[0])
|
| 234 |
+
now = datetime.now()
|
| 235 |
+
|
| 236 |
+
q = question.lower()
|
| 237 |
+
|
| 238 |
+
if "this year" in q:
|
| 239 |
+
return latest_date.year < now.year
|
| 240 |
+
|
| 241 |
+
if "last month" in q:
|
| 242 |
+
return (now.year, now.month - 1) > (latest_date.year, latest_date.month)
|
| 243 |
+
|
| 244 |
+
if "recent" in q or "last 30" in q:
|
| 245 |
+
return (now - latest_date).days > 30
|
| 246 |
+
|
| 247 |
+
return False
|
| 248 |
+
|
| 249 |
+
|
| 250 |
|
| 251 |
# =========================
|
| 252 |
# ORCHESTRATOR (Single Entry Point)
|
|
|
|
| 264 |
}
|
| 265 |
|
| 266 |
# 2. Time relevance
|
| 267 |
+
# 2. Time relevance check
|
| 268 |
+
if is_request_out_of_data_range(question):
|
| 269 |
+
latest = get_latest_data_date()
|
| 270 |
+
return {
|
| 271 |
+
"status": "ok",
|
| 272 |
+
"message": f"No data available for the requested time period. Latest available data is from {latest}.",
|
| 273 |
+
"data": [],
|
| 274 |
+
"sql": None,
|
| 275 |
+
"note": None
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
|
| 279 |
# 3. Generate SQL
|
| 280 |
sql = generate_sql(question)
|