Spaces:
Sleeping
Sleeping
Upload engine.py
Browse files
engine.py
CHANGED
|
@@ -329,51 +329,44 @@ def normalize_time_question(q):#total-actual date
|
|
| 329 |
|
| 330 |
def is_question_supported(question):
|
| 331 |
q = question.lower()
|
| 332 |
-
tokens = set(q.replace("?", "").replace(",", "").split())
|
| 333 |
|
| 334 |
-
# 1️⃣
|
| 335 |
-
analytic_keywords =
|
| 336 |
"count", "total", "average", "avg", "sum",
|
| 337 |
-
"how many", "number of",
|
| 338 |
-
"
|
| 339 |
-
|
|
|
|
|
|
|
| 340 |
|
| 341 |
if any(k in q for k in analytic_keywords):
|
| 342 |
return True
|
| 343 |
|
| 344 |
-
# 2️⃣
|
| 345 |
schema = load_ai_schema()
|
| 346 |
|
| 347 |
for table, meta in schema.items():
|
| 348 |
-
score = 0
|
| 349 |
table_l = table.lower()
|
| 350 |
|
| 351 |
-
# Table name
|
| 352 |
if table_l in q:
|
| 353 |
-
|
| 354 |
|
| 355 |
-
# Column
|
| 356 |
for col, desc in meta["columns"].items():
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
score += 2
|
| 360 |
-
elif any(tok in col_l for tok in tokens):
|
| 361 |
-
score += 1
|
| 362 |
-
|
| 363 |
-
# Description match
|
| 364 |
-
if meta.get("description"):
|
| 365 |
-
desc_tokens = set(col_desc(meta["description"]).lower().split())
|
| 366 |
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
return True
|
| 372 |
|
| 373 |
return False
|
| 374 |
|
| 375 |
|
| 376 |
|
|
|
|
| 377 |
# =========================
|
| 378 |
# SQL GENERATION
|
| 379 |
# =========================
|
|
|
|
| 329 |
|
| 330 |
def is_question_supported(question):
|
| 331 |
q = question.lower()
|
|
|
|
| 332 |
|
| 333 |
+
# 1️⃣ Always allow analytical / time-based queries
|
| 334 |
+
analytic_keywords = [
|
| 335 |
"count", "total", "average", "avg", "sum",
|
| 336 |
+
"how many", "number of",
|
| 337 |
+
"trend", "increase", "decrease", "compare",
|
| 338 |
+
"last", "latest", "recent", "past",
|
| 339 |
+
"day", "days", "month", "year"
|
| 340 |
+
]
|
| 341 |
|
| 342 |
if any(k in q for k in analytic_keywords):
|
| 343 |
return True
|
| 344 |
|
| 345 |
+
# 2️⃣ Schema-based relevance check
|
| 346 |
schema = load_ai_schema()
|
| 347 |
|
| 348 |
for table, meta in schema.items():
|
|
|
|
| 349 |
table_l = table.lower()
|
| 350 |
|
| 351 |
+
# Table name mentioned
|
| 352 |
if table_l in q:
|
| 353 |
+
return True
|
| 354 |
|
| 355 |
+
# Column or description match
|
| 356 |
for col, desc in meta["columns"].items():
|
| 357 |
+
if col.lower() in q:
|
| 358 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
+
if isinstance(desc, str) and any(
|
| 361 |
+
word in desc.lower() for word in q.split()
|
| 362 |
+
):
|
| 363 |
+
return True
|
|
|
|
| 364 |
|
| 365 |
return False
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
+
|
| 370 |
# =========================
|
| 371 |
# SQL GENERATION
|
| 372 |
# =========================
|