Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -842,22 +842,45 @@ SELECT
|
|
| 842 |
return self._finalize_sql(fixed_sql, status)
|
| 843 |
|
| 844 |
def _generate_fallback_sql(self, prompt: str) -> str:
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
|
| 862 |
def process_question(self, question: str) -> Tuple[str, str]:
|
| 863 |
"""處理使用者問題"""
|
|
|
|
| 842 |
return self._finalize_sql(fixed_sql, status)
|
| 843 |
|
| 844 |
def _generate_fallback_sql(self, prompt: str) -> str:
|
| 845 |
+
"""當模型不可用時的備用 SQL 生成"""
|
| 846 |
+
prompt_lower = (prompt or "").lower()
|
| 847 |
+
# 統計類:優先使用 JobTimeline.ReportAuthorization,避免不存在的 completed_time 欄位
|
| 848 |
+
if ("統計" in prompt) or ("數量" in prompt) or ("多少" in prompt) or ("count" in prompt_lower):
|
| 849 |
+
if ("月" in prompt) or ("per month" in prompt_lower) or ("monthly" in prompt_lower):
|
| 850 |
+
return (
|
| 851 |
+
"SELECT strftime('%Y-%m', jt.ReportAuthorization) AS month, "
|
| 852 |
+
"COUNT(DISTINCT jt.JobNo) AS count "
|
| 853 |
+
"FROM JobTimeline AS jt "
|
| 854 |
+
"WHERE jt.ReportAuthorization IS NOT NULL "
|
| 855 |
+
"GROUP BY month ORDER BY month;"
|
| 856 |
+
)
|
| 857 |
+
elif ("客戶" in prompt) or ("buyer" in prompt_lower) or ("applicant" in prompt_lower):
|
| 858 |
+
return (
|
| 859 |
+
"SELECT sd.ApplicantName AS applicant, COUNT(DISTINCT jt.JobNo) AS count "
|
| 860 |
+
"FROM JobTimeline AS jt "
|
| 861 |
+
"JOIN TSR53SampleDescription AS sd ON jt.JobNo = sd.JobNo "
|
| 862 |
+
"WHERE jt.ReportAuthorization IS NOT NULL "
|
| 863 |
+
"GROUP BY sd.ApplicantName ORDER BY count DESC;"
|
| 864 |
+
)
|
| 865 |
+
else:
|
| 866 |
+
return (
|
| 867 |
+
"SELECT COUNT(DISTINCT jt.JobNo) AS total_count "
|
| 868 |
+
"FROM JobTimeline AS jt "
|
| 869 |
+
"WHERE jt.ReportAuthorization IS NOT NULL;"
|
| 870 |
+
)
|
| 871 |
+
# 金額彙總
|
| 872 |
+
if ("金額" in prompt) or ("總額" in prompt) or ("amount" in prompt_lower) or ("sum" in prompt_lower):
|
| 873 |
+
return "SELECT SUM(LocalAmount) AS total_amount FROM TSR53Invoice;"
|
| 874 |
+
# 評級分布
|
| 875 |
+
if ("評級" in prompt) or ("rating" in prompt_lower) or ("pass" in prompt_lower) or ("fail" in prompt_lower):
|
| 876 |
+
return "SELECT OverallRating AS rating, COUNT(*) AS count FROM TSR53SampleDescription GROUP BY OverallRating;"
|
| 877 |
+
# 通用後備:最近 10 筆報告
|
| 878 |
+
return (
|
| 879 |
+
"SELECT jt.JobNo, jt.ReportAuthorization "
|
| 880 |
+
"FROM JobTimeline AS jt "
|
| 881 |
+
"WHERE jt.ReportAuthorization IS NOT NULL "
|
| 882 |
+
"ORDER BY jt.ReportAuthorization DESC LIMIT 10;"
|
| 883 |
+
)
|
| 884 |
|
| 885 |
def process_question(self, question: str) -> Tuple[str, str]:
|
| 886 |
"""處理使用者問題"""
|