Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,11 +9,12 @@ from sentence_transformers import SentenceTransformer, util
|
|
| 9 |
import torch
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
from typing import List, Dict, Tuple, Optional
|
|
|
|
| 12 |
|
| 13 |
# ==================== 配置區 ====================
|
| 14 |
HF_TOKEN = os.environ.get("HF_TOKEN", "您的_HuggingFace_Token")
|
| 15 |
DATASET_REPO_ID = "Paul720810/Text-to-SQL-Softline"
|
| 16 |
-
SIMILARITY_THRESHOLD = 0.
|
| 17 |
|
| 18 |
# 多個備用LLM模型
|
| 19 |
LLM_MODELS = [
|
|
@@ -22,109 +23,152 @@ LLM_MODELS = [
|
|
| 22 |
"https://api-inference.huggingface.co/models/microsoft/DialoGPT-small"
|
| 23 |
]
|
| 24 |
|
| 25 |
-
print("=" *
|
| 26 |
-
print("
|
| 27 |
-
print("
|
|
|
|
| 28 |
|
| 29 |
-
# ====================
|
| 30 |
def get_current_time():
|
| 31 |
-
return datetime.now().strftime("%
|
| 32 |
|
| 33 |
def validate_sql(sql_query: str) -> Dict:
|
| 34 |
"""驗證SQL語句的安全性"""
|
| 35 |
if not sql_query or sql_query.strip() == "":
|
| 36 |
-
return {"valid": False, "issues": ["SQL語句為空"], "is_safe": False}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
security_issues = []
|
| 39 |
-
sql_upper =
|
| 40 |
|
| 41 |
# 檢查危險操作
|
| 42 |
dangerous_keywords = ['DROP', 'DELETE', 'INSERT', 'UPDATE', 'ALTER', 'TRUNCATE', 'EXEC', 'EXECUTE']
|
| 43 |
for keyword in dangerous_keywords:
|
| 44 |
if f" {keyword} " in sql_upper:
|
| 45 |
-
security_issues.append(f"
|
| 46 |
|
| 47 |
# 檢查基本語法
|
| 48 |
if "SELECT" not in sql_upper:
|
| 49 |
-
security_issues.append("缺少SELECT
|
| 50 |
|
| 51 |
if "FROM" not in sql_upper:
|
| 52 |
-
security_issues.append("缺少FROM
|
| 53 |
|
| 54 |
return {
|
| 55 |
"valid": len(security_issues) == 0,
|
| 56 |
"issues": security_issues,
|
| 57 |
-
"is_safe": len([i for i in security_issues if '危險' in i]) == 0
|
|
|
|
| 58 |
}
|
| 59 |
|
| 60 |
-
def
|
| 61 |
-
"""
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
return f"SELECT COUNT(*) as count FROM appropriate_table WHERE condition; -- 原始SQL: {original_sql}"
|
| 90 |
else:
|
| 91 |
-
return
|
| 92 |
|
| 93 |
-
elif
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
elif "
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
return original_sql + " FROM products WHERE 1=1;"
|
| 102 |
-
elif "sale" in user_question_lower or "銷售" in user_question_lower:
|
| 103 |
-
return original_sql + " FROM sales WHERE 1=1;"
|
| 104 |
else:
|
| 105 |
-
return
|
| 106 |
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
# ====================
|
| 110 |
-
class
|
| 111 |
def __init__(self, hf_token: str):
|
| 112 |
self.hf_token = hf_token
|
| 113 |
self.questions = []
|
| 114 |
self.sql_answers = []
|
|
|
|
| 115 |
self.schema_data = {}
|
| 116 |
|
| 117 |
-
def
|
| 118 |
-
"""
|
| 119 |
try:
|
| 120 |
-
print(f"[{get_current_time()}]
|
| 121 |
raw_dataset = load_dataset(DATASET_REPO_ID, token=self.hf_token)['train']
|
| 122 |
|
| 123 |
-
print("
|
| 124 |
valid_count = 0
|
|
|
|
| 125 |
invalid_count = 0
|
| 126 |
|
| 127 |
-
for item in raw_dataset:
|
| 128 |
try:
|
| 129 |
if 'messages' in item and len(item['messages']) >= 2:
|
| 130 |
user_content = item['messages'][0]['content']
|
|
@@ -143,29 +187,50 @@ class DataLoader:
|
|
| 143 |
else:
|
| 144 |
sql_query = assistant_content
|
| 145 |
|
| 146 |
-
# 驗證SQL
|
| 147 |
validation = validate_sql(sql_query)
|
| 148 |
-
if not validation["valid"]:
|
| 149 |
-
invalid_count += 1
|
| 150 |
-
print(f"發現無效SQL [{invalid_count}]: {sql_query}")
|
| 151 |
-
# 暫時不修復,等待使用時再智能修復
|
| 152 |
-
|
| 153 |
-
self.questions.append(question)
|
| 154 |
-
self.sql_answers.append(sql_query)
|
| 155 |
-
valid_count += 1
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
except Exception as e:
|
| 158 |
continue
|
| 159 |
|
| 160 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
return True
|
| 162 |
|
| 163 |
except Exception as e:
|
| 164 |
print(f"數據集加載失敗: {e}")
|
| 165 |
-
self.
|
| 166 |
-
self.sql_answers = ["SELECT '數據庫連接就緒' AS status;"]
|
| 167 |
return False
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
def load_schema(self) -> bool:
|
| 170 |
"""加載數據庫Schema"""
|
| 171 |
try:
|
|
@@ -183,60 +248,42 @@ class DataLoader:
|
|
| 183 |
print(f"Schema加載失敗: {e}")
|
| 184 |
self.schema_data = {}
|
| 185 |
return False
|
| 186 |
-
|
| 187 |
-
def build_schema_context(self) -> str:
|
| 188 |
-
"""構建Schema上下文"""
|
| 189 |
-
if not self.schema_data:
|
| 190 |
-
return "/* 無Schema信息 */"
|
| 191 |
-
|
| 192 |
-
context = "/* 數據庫表結構 */\n"
|
| 193 |
-
for table_name, columns in self.schema_data.items():
|
| 194 |
-
if isinstance(columns, list):
|
| 195 |
-
context += f"\n-- 表: {table_name}\n"
|
| 196 |
-
for col in columns:
|
| 197 |
-
col_name = col.get('name', 'unknown')
|
| 198 |
-
col_type = col.get('type', 'TEXT')
|
| 199 |
-
col_desc = col.get('description', '')
|
| 200 |
-
context += f"-- {col_name} ({col_type}) - {col_desc}\n"
|
| 201 |
-
return context
|
| 202 |
|
| 203 |
# ==================== 主系統 ====================
|
| 204 |
-
class
|
| 205 |
def __init__(self, hf_token: str):
|
| 206 |
self.hf_token = hf_token
|
| 207 |
-
self.data_loader =
|
| 208 |
-
self.llm_client = None # 延遲加載
|
| 209 |
self.retrieval_system = RetrievalSystem()
|
| 210 |
|
| 211 |
self.initialize_system()
|
| 212 |
|
| 213 |
def initialize_system(self):
|
| 214 |
"""初始化系統組件"""
|
| 215 |
-
print("
|
| 216 |
|
| 217 |
-
self.data_loader.
|
| 218 |
self.data_loader.load_schema()
|
| 219 |
-
self.retrieval_system.compute_embeddings(self.data_loader.questions)
|
| 220 |
-
self.schema_context = self.data_loader.build_schema_context()
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
""
|
| 227 |
-
if self.llm_client is None:
|
| 228 |
-
self.llm_client = LLMClient(self.hf_token)
|
| 229 |
-
return self.llm_client
|
| 230 |
|
| 231 |
def generate_sql(self, user_question: str) -> Tuple[str, str]:
|
| 232 |
"""生成SQL查詢"""
|
| 233 |
-
log_messages = [f"
|
| 234 |
|
| 235 |
if not user_question or user_question.strip() == "":
|
| 236 |
return "請輸入您的問題。", "錯誤: 問題為空"
|
| 237 |
|
| 238 |
-
#
|
| 239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
hits = self.retrieval_system.retrieve_similar(user_question)
|
| 241 |
|
| 242 |
if hits:
|
|
@@ -245,109 +292,38 @@ class TextToSQLSystem:
|
|
| 245 |
similar_question = self.data_loader.questions[best_hit['corpus_id']]
|
| 246 |
original_sql = self.data_loader.sql_answers[best_hit['corpus_id']]
|
| 247 |
|
| 248 |
-
log_messages.append(f"
|
| 249 |
log_messages.append(f"📊 相似度: {similarity_score:.3f}")
|
| 250 |
|
| 251 |
if similarity_score > SIMILARITY_THRESHOLD:
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
if not validation["valid"]:
|
| 255 |
-
log_messages.append(f"⚠️ 原始SQL有問題: {', '.join(validation['issues'])}")
|
| 256 |
-
log_messages.append("🛠️ 正在智能修復SQL...")
|
| 257 |
-
|
| 258 |
-
# 智能修復
|
| 259 |
-
repaired_sql = intelligent_sql_repair(original_sql, user_question, similar_question)
|
| 260 |
-
log_messages.append(f"✅ 修復完成")
|
| 261 |
-
|
| 262 |
-
# 驗證修復後的SQL
|
| 263 |
-
final_validation = validate_sql(repaired_sql)
|
| 264 |
-
if not final_validation["valid"]:
|
| 265 |
-
log_messages.append(f"❌ 修復後仍有問題: {', '.join(final_validation['issues'])}")
|
| 266 |
-
else:
|
| 267 |
-
log_messages.append("✅ 修復後SQL驗證通過")
|
| 268 |
-
|
| 269 |
-
return repaired_sql, "\n".join(log_messages)
|
| 270 |
-
else:
|
| 271 |
-
log_messages.append(f"✅ 相似度 > {SIMILARITY_THRESHOLD},直接返回")
|
| 272 |
-
return original_sql, "\n".join(log_messages)
|
| 273 |
else:
|
| 274 |
-
log_messages.append(f"ℹ️
|
| 275 |
-
|
| 276 |
-
# 2. LLM生成模式
|
| 277 |
-
log_messages.append("🤖 進入LLM生成模式...")
|
| 278 |
-
|
| 279 |
-
prompt = f"""你是一個SQL專家。請為以下問題生成SQL查詢:
|
| 280 |
-
|
| 281 |
-
問題:{user_question}
|
| 282 |
-
|
| 283 |
-
要求:
|
| 284 |
-
1. 只輸出SQL語句
|
| 285 |
-
2. 必須包含SELECT和FROM
|
| 286 |
-
3. 使用正確的語法
|
| 287 |
-
|
| 288 |
-
SQL查詢:"""
|
| 289 |
-
|
| 290 |
-
generated_sql = self.get_llm_client().call_llm_api(prompt)
|
| 291 |
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
log_messages.append(f"⚠️ LLM生成的SQL有問題: {', '.join(validation['issues'])}")
|
| 297 |
-
generated_sql = intelligent_sql_repair(generated_sql, user_question, user_question)
|
| 298 |
-
|
| 299 |
-
log_messages.append("✅ SQL生成完成")
|
| 300 |
-
return generated_sql, "\n".join(log_messages)
|
| 301 |
-
else:
|
| 302 |
-
# 3. 備用方案
|
| 303 |
-
log_messages.append("❌ 所有LLM模型都失敗,啟用備用方案")
|
| 304 |
-
backup_sql = self.generate_backup_sql(user_question)
|
| 305 |
-
return backup_sql, "\n".join(log_messages)
|
| 306 |
-
|
| 307 |
-
def generate_backup_sql(self, user_question: str) -> str:
|
| 308 |
-
"""生成備用SQL"""
|
| 309 |
-
user_question_lower = user_question.lower()
|
| 310 |
-
|
| 311 |
-
if any(kw in user_question_lower for kw in ['報告', '完成', '份', 'report']):
|
| 312 |
-
return "SELECT strftime('%Y-%m', completion_date) as month, COUNT(*) as report_count FROM reports GROUP BY month ORDER BY month;"
|
| 313 |
-
|
| 314 |
-
elif any(kw in user_question_lower for kw in ['銷售', '業績', '金額', 'sale']):
|
| 315 |
-
return "SELECT product_name, SUM(sales_amount) as total_sales FROM sales GROUP BY product_name ORDER BY total_sales DESC LIMIT 10;"
|
| 316 |
-
|
| 317 |
-
elif any(kw in user_question_lower for kw in ['客戶', '買家', 'customer']):
|
| 318 |
-
return "SELECT customer_name, COUNT(*) as order_count FROM orders GROUP BY customer_name ORDER BY order_count DESC;"
|
| 319 |
-
|
| 320 |
-
elif any(kw in user_question_lower for kw in ['時間', '日期', '月份', 'month']):
|
| 321 |
-
return "SELECT strftime('%Y-%m', order_date) as month, COUNT(*) as orders FROM orders GROUP BY month ORDER BY month DESC;"
|
| 322 |
|
| 323 |
-
|
| 324 |
-
return "SELECT '請提供更詳細的查詢條件' AS status;"
|
| 325 |
|
| 326 |
-
# ====================
|
| 327 |
class LLMClient:
|
| 328 |
def __init__(self, hf_token: str):
|
| 329 |
self.hf_token = hf_token
|
| 330 |
|
| 331 |
-
def call_llm_api(self, prompt: str
|
| 332 |
headers = {"Authorization": f"Bearer {self.hf_token}"}
|
| 333 |
-
payload = {
|
| 334 |
-
"inputs": prompt,
|
| 335 |
-
"parameters": {
|
| 336 |
-
"max_new_tokens": 200,
|
| 337 |
-
"temperature": 0.1,
|
| 338 |
-
"do_sample": False
|
| 339 |
-
}
|
| 340 |
-
}
|
| 341 |
|
| 342 |
-
for model_url in
|
| 343 |
try:
|
| 344 |
-
response = requests.post(model_url, headers=headers, json=payload, timeout=
|
| 345 |
if response.status_code == 200:
|
| 346 |
result = response.json()
|
| 347 |
if isinstance(result, list) and len(result) > 0:
|
| 348 |
-
|
| 349 |
-
generated_text = re.sub(r'^```sql|```$', '', generated_text).strip()
|
| 350 |
-
return generated_text
|
| 351 |
except:
|
| 352 |
continue
|
| 353 |
return None
|
|
@@ -359,9 +335,7 @@ class RetrievalSystem:
|
|
| 359 |
|
| 360 |
def compute_embeddings(self, questions: List[str]) -> None:
|
| 361 |
if questions:
|
| 362 |
-
self.question_embeddings = self.embedder.encode(questions, convert_to_tensor=True
|
| 363 |
-
else:
|
| 364 |
-
self.question_embeddings = torch.Tensor([])
|
| 365 |
|
| 366 |
def retrieve_similar(self, user_question: str, top_k: int = 3) -> List[Dict]:
|
| 367 |
if self.question_embeddings is None or len(self.question_embeddings) == 0:
|
|
@@ -374,36 +348,44 @@ class RetrievalSystem:
|
|
| 374 |
return []
|
| 375 |
|
| 376 |
# ==================== 初始化系統 ====================
|
| 377 |
-
print("
|
| 378 |
-
text_to_sql_system =
|
| 379 |
|
| 380 |
# ==================== Gradio界面 ====================
|
| 381 |
def process_query(user_question: str) -> Tuple[str, str]:
|
| 382 |
sql_result, log_message = text_to_sql_system.generate_sql(user_question)
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
if not final_validation["valid"]:
|
| 386 |
-
debug_info = "❌ 最終SQL驗證失敗:\n" + "\n".join(final_validation["issues"])
|
| 387 |
-
else:
|
| 388 |
-
debug_info = "✅ 最終SQL驗證通過"
|
| 389 |
-
|
| 390 |
-
return sql_result, debug_info, log_message
|
| 391 |
|
| 392 |
with gr.Blocks(title="智能Text-to-SQL系統", theme=gr.themes.Soft()) as demo:
|
| 393 |
gr.Markdown("# 🚀 智能 Text-to-SQL 系統")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
|
| 395 |
with gr.Row():
|
| 396 |
-
question_input = gr.Textbox(label="📝 您的問題", placeholder="例如:查詢2023年每月報告數量", lines=2)
|
| 397 |
submit_btn = gr.Button("🚀 生成SQL", variant="primary")
|
| 398 |
|
| 399 |
with gr.Row():
|
| 400 |
-
sql_output = gr.Code(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 401 |
|
| 402 |
with gr.Row():
|
| 403 |
-
debug_output = gr.Textbox(label="🔍
|
| 404 |
-
log_output = gr.Textbox(label="📋
|
| 405 |
|
| 406 |
-
submit_btn.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
|
| 408 |
if __name__ == "__main__":
|
| 409 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
|
|
|
| 9 |
import torch
|
| 10 |
from huggingface_hub import hf_hub_download
|
| 11 |
from typing import List, Dict, Tuple, Optional
|
| 12 |
+
import numpy as np
|
| 13 |
|
| 14 |
# ==================== 配置區 ====================
|
| 15 |
HF_TOKEN = os.environ.get("HF_TOKEN", "您的_HuggingFace_Token")
|
| 16 |
DATASET_REPO_ID = "Paul720810/Text-to-SQL-Softline"
|
| 17 |
+
SIMILARITY_THRESHOLD = 0.70 # 降低閾值,因為很多數據有問題
|
| 18 |
|
| 19 |
# 多個備用LLM模型
|
| 20 |
LLM_MODELS = [
|
|
|
|
| 23 |
"https://api-inference.huggingface.co/models/microsoft/DialoGPT-small"
|
| 24 |
]
|
| 25 |
|
| 26 |
+
print("=" * 60)
|
| 27 |
+
print("🤖 智能 Text-to-SQL 系統啟動中...")
|
| 28 |
+
print("⚠️ 檢測到大量無效數據,啟用增強修復模式")
|
| 29 |
+
print("=" * 60)
|
| 30 |
|
| 31 |
+
# ==================== 增強工具函數 ====================
|
| 32 |
def get_current_time():
|
| 33 |
+
return datetime.now().strftime("%H:%M:%S")
|
| 34 |
|
| 35 |
def validate_sql(sql_query: str) -> Dict:
|
| 36 |
"""驗證SQL語句的安全性"""
|
| 37 |
if not sql_query or sql_query.strip() == "":
|
| 38 |
+
return {"valid": False, "issues": ["SQL語句為空"], "is_safe": False, "empty": True}
|
| 39 |
+
|
| 40 |
+
sql_clean = sql_query.strip()
|
| 41 |
+
if len(sql_clean) < 10: # 非常短的SQL可能無效
|
| 42 |
+
return {"valid": False, "issues": ["SQL過短"], "is_safe": False, "empty": False}
|
| 43 |
|
| 44 |
security_issues = []
|
| 45 |
+
sql_upper = sql_clean.upper()
|
| 46 |
|
| 47 |
# 檢查危險操作
|
| 48 |
dangerous_keywords = ['DROP', 'DELETE', 'INSERT', 'UPDATE', 'ALTER', 'TRUNCATE', 'EXEC', 'EXECUTE']
|
| 49 |
for keyword in dangerous_keywords:
|
| 50 |
if f" {keyword} " in sql_upper:
|
| 51 |
+
security_issues.append(f"危險操作: {keyword}")
|
| 52 |
|
| 53 |
# 檢查基本語法
|
| 54 |
if "SELECT" not in sql_upper:
|
| 55 |
+
security_issues.append("缺少SELECT")
|
| 56 |
|
| 57 |
if "FROM" not in sql_upper:
|
| 58 |
+
security_issues.append("缺少FROM")
|
| 59 |
|
| 60 |
return {
|
| 61 |
"valid": len(security_issues) == 0,
|
| 62 |
"issues": security_issues,
|
| 63 |
+
"is_safe": len([i for i in security_issues if '危險' in i]) == 0,
|
| 64 |
+
"empty": False
|
| 65 |
}
|
| 66 |
|
| 67 |
+
def analyze_question_type(question: str) -> Dict:
|
| 68 |
+
"""分析問題類型"""
|
| 69 |
+
question_lower = question.lower()
|
| 70 |
+
|
| 71 |
+
analysis = {
|
| 72 |
+
"type": "unknown",
|
| 73 |
+
"keywords": [],
|
| 74 |
+
"has_count": False,
|
| 75 |
+
"has_date": False,
|
| 76 |
+
"has_group": False
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
# 檢測關鍵詞
|
| 80 |
+
keywords_sets = {
|
| 81 |
+
"sales": ["銷售", "業績", "金額", "收入", "sale", "revenue"],
|
| 82 |
+
"customer": ["客戶", "買家", "用戶", "customer", "client"],
|
| 83 |
+
"product": ["產品", "商品", "項目", "product", "item"],
|
| 84 |
+
"time": ["時間", "日期", "月份", "年", "月", "最近", "date", "month", "year"],
|
| 85 |
+
"report": ["報告", "完成", "份", "report", "complete"],
|
| 86 |
+
"count": ["多少", "幾個", "數量", "count", "how many"]
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
for category, keywords in keywords_sets.items():
|
| 90 |
+
for keyword in keywords:
|
| 91 |
+
if keyword in question_lower:
|
| 92 |
+
analysis["keywords"].append(category)
|
| 93 |
+
if category not in analysis["keywords"]:
|
| 94 |
+
analysis["keywords"].append(category)
|
| 95 |
+
|
| 96 |
+
# 特殊檢測
|
| 97 |
+
analysis["has_count"] = any(kw in question_lower for kw in keywords_sets["count"])
|
| 98 |
+
analysis["has_date"] = any(kw in question_lower for kw in keywords_sets["time"])
|
| 99 |
+
analysis["has_group"] = "每" in question_lower or "各" in question_lower or "group" in question_lower
|
| 100 |
+
|
| 101 |
+
# 確定主要類型
|
| 102 |
+
if analysis["keywords"]:
|
| 103 |
+
analysis["type"] = analysis["keywords"][0]
|
| 104 |
|
| 105 |
+
return analysis
|
| 106 |
+
|
| 107 |
+
def generate_intelligent_sql(question: str, analysis: Dict) -> str:
|
| 108 |
+
"""根據問題分析生成智能SQL"""
|
| 109 |
+
question_type = analysis["type"]
|
| 110 |
+
has_count = analysis["has_count"]
|
| 111 |
+
has_date = analysis["has_date"]
|
| 112 |
+
has_group = analysis["has_group"]
|
| 113 |
+
|
| 114 |
+
# 根據問題類型生成相應的SQL
|
| 115 |
+
if question_type == "sales":
|
| 116 |
+
if has_count and has_group and has_date:
|
| 117 |
+
return "SELECT strftime('%Y-%m', sale_date) as month, COUNT(*) as sales_count, SUM(amount) as total_sales FROM sales GROUP BY month ORDER BY month;"
|
| 118 |
+
elif has_count:
|
| 119 |
+
return "SELECT product_name, COUNT(*) as sale_count FROM sales GROUP BY product_name ORDER BY sale_count DESC LIMIT 10;"
|
| 120 |
+
else:
|
| 121 |
+
return "SELECT product_name, SUM(amount) as total_sales FROM sales GROUP BY product_name ORDER BY total_sales DESC LIMIT 10;"
|
| 122 |
|
| 123 |
+
elif question_type == "customer":
|
| 124 |
+
if has_count and has_group:
|
| 125 |
+
return "SELECT customer_name, COUNT(*) as order_count, SUM(amount) as total_spent FROM orders GROUP BY customer_name ORDER BY total_spent DESC;"
|
|
|
|
| 126 |
else:
|
| 127 |
+
return "SELECT customer_name, email, join_date FROM customers ORDER BY join_date DESC LIMIT 10;"
|
| 128 |
|
| 129 |
+
elif question_type == "product":
|
| 130 |
+
if has_count:
|
| 131 |
+
return "SELECT category, COUNT(*) as product_count FROM products GROUP BY category ORDER BY product_count DESC;"
|
| 132 |
+
else:
|
| 133 |
+
return "SELECT product_name, price, stock_quantity FROM products WHERE stock_quantity > 0 ORDER BY price DESC LIMIT 10;"
|
| 134 |
|
| 135 |
+
elif question_type == "report" or question_type == "time":
|
| 136 |
+
if has_count and has_group and has_date:
|
| 137 |
+
return "SELECT strftime('%Y-%m', report_date) as month, COUNT(*) as report_count FROM reports GROUP BY month ORDER BY month;"
|
| 138 |
+
elif has_date:
|
| 139 |
+
return "SELECT report_id, report_name, report_date FROM reports ORDER BY report_date DESC LIMIT 10;"
|
|
|
|
|
|
|
|
|
|
| 140 |
else:
|
| 141 |
+
return "SELECT report_type, COUNT(*) as count FROM reports GROUP BY report_type ORDER BY count DESC;"
|
| 142 |
|
| 143 |
+
# 默認SQL
|
| 144 |
+
if has_count and has_group:
|
| 145 |
+
return "SELECT category, COUNT(*) as item_count FROM items GROUP BY category ORDER BY item_count DESC;"
|
| 146 |
+
elif has_count:
|
| 147 |
+
return "SELECT COUNT(*) as total_count FROM records;"
|
| 148 |
+
else:
|
| 149 |
+
return "SELECT * FROM data_table LIMIT 10;"
|
| 150 |
|
| 151 |
+
# ==================== 智能數據加載模塊 ====================
|
| 152 |
+
class SmartDataLoader:
|
| 153 |
def __init__(self, hf_token: str):
|
| 154 |
self.hf_token = hf_token
|
| 155 |
self.questions = []
|
| 156 |
self.sql_answers = []
|
| 157 |
+
self.valid_indices = [] # 記錄有效數據的索引
|
| 158 |
self.schema_data = {}
|
| 159 |
|
| 160 |
+
def load_and_clean_dataset(self) -> bool:
|
| 161 |
+
"""加載並清理數據集"""
|
| 162 |
try:
|
| 163 |
+
print(f"[{get_current_time()}] 加載數據集 '{DATASET_REPO_ID}'...")
|
| 164 |
raw_dataset = load_dataset(DATASET_REPO_ID, token=self.hf_token)['train']
|
| 165 |
|
| 166 |
+
print("解析 messages 格式並過濾無效數據...")
|
| 167 |
valid_count = 0
|
| 168 |
+
empty_count = 0
|
| 169 |
invalid_count = 0
|
| 170 |
|
| 171 |
+
for i, item in enumerate(raw_dataset):
|
| 172 |
try:
|
| 173 |
if 'messages' in item and len(item['messages']) >= 2:
|
| 174 |
user_content = item['messages'][0]['content']
|
|
|
|
| 187 |
else:
|
| 188 |
sql_query = assistant_content
|
| 189 |
|
| 190 |
+
# 驗證SQL - 只保留真正有效的數據
|
| 191 |
validation = validate_sql(sql_query)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
+
if validation["valid"]:
|
| 194 |
+
self.questions.append(question)
|
| 195 |
+
self.sql_answers.append(sql_query)
|
| 196 |
+
self.valid_indices.append(i)
|
| 197 |
+
valid_count += 1
|
| 198 |
+
elif validation["empty"]:
|
| 199 |
+
empty_count += 1
|
| 200 |
+
else:
|
| 201 |
+
invalid_count += 1
|
| 202 |
+
|
| 203 |
except Exception as e:
|
| 204 |
continue
|
| 205 |
|
| 206 |
+
print(f"數據清理完成: {valid_count} 有效, {empty_count} 空, {invalid_count} 無效")
|
| 207 |
+
|
| 208 |
+
# 如果有效數據太少,添加一些備用問題
|
| 209 |
+
if valid_count < 100:
|
| 210 |
+
print("有效數據過少,添加備用問題...")
|
| 211 |
+
self.add_backup_examples()
|
| 212 |
+
|
| 213 |
return True
|
| 214 |
|
| 215 |
except Exception as e:
|
| 216 |
print(f"數據集加載失敗: {e}")
|
| 217 |
+
self.add_backup_examples()
|
|
|
|
| 218 |
return False
|
| 219 |
|
| 220 |
+
def add_backup_examples(self):
|
| 221 |
+
"""添加備用範例"""
|
| 222 |
+
backup_data = [
|
| 223 |
+
{"question": "查詢銷售額最高的產品", "sql": "SELECT product_name, SUM(sales_amount) as total_sales FROM sales GROUP BY product_name ORDER BY total_sales DESC LIMIT 10;"},
|
| 224 |
+
{"question": "顯示最近30天的訂單", "sql": "SELECT * FROM orders WHERE order_date >= date('now', '-30 days') ORDER BY order_date DESC;"},
|
| 225 |
+
{"question": "統計每個客戶的訂單數量", "sql": "SELECT customer_name, COUNT(*) as order_count FROM orders GROUP BY customer_name ORDER BY order_count DESC;"},
|
| 226 |
+
{"question": "2023年每月銷售額", "sql": "SELECT strftime('%Y-%m', sale_date) as month, SUM(amount) as monthly_sales FROM sales WHERE strftime('%Y', sale_date) = '2023' GROUP BY month ORDER BY month;"},
|
| 227 |
+
{"question": "庫存不足的商品", "sql": "SELECT product_name, stock_quantity FROM products WHERE stock_quantity < 10 ORDER BY stock_quantity ASC;"}
|
| 228 |
+
]
|
| 229 |
+
|
| 230 |
+
for data in backup_data:
|
| 231 |
+
self.questions.append(data["question"])
|
| 232 |
+
self.sql_answers.append(data["sql"])
|
| 233 |
+
|
| 234 |
def load_schema(self) -> bool:
|
| 235 |
"""加載數據庫Schema"""
|
| 236 |
try:
|
|
|
|
| 248 |
print(f"Schema加載失敗: {e}")
|
| 249 |
self.schema_data = {}
|
| 250 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
# ==================== 主系統 ====================
|
| 253 |
+
class EnhancedTextToSQLSystem:
|
| 254 |
def __init__(self, hf_token: str):
|
| 255 |
self.hf_token = hf_token
|
| 256 |
+
self.data_loader = SmartDataLoader(hf_token)
|
|
|
|
| 257 |
self.retrieval_system = RetrievalSystem()
|
| 258 |
|
| 259 |
self.initialize_system()
|
| 260 |
|
| 261 |
def initialize_system(self):
|
| 262 |
"""初始化系統組件"""
|
| 263 |
+
print("初始化系統組件...")
|
| 264 |
|
| 265 |
+
self.data_loader.load_and_clean_dataset()
|
| 266 |
self.data_loader.load_schema()
|
|
|
|
|
|
|
| 267 |
|
| 268 |
+
# 只為有效數據計算向量
|
| 269 |
+
if self.data_loader.questions:
|
| 270 |
+
self.retrieval_system.compute_embeddings(self.data_loader.questions)
|
| 271 |
+
|
| 272 |
+
print(f"系統初始化完成,可用有效問題: {len(self.data_loader.questions)}")
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
def generate_sql(self, user_question: str) -> Tuple[str, str]:
|
| 275 |
"""生成SQL查詢"""
|
| 276 |
+
log_messages = [f"⏰ {get_current_time()} 開始處理"]
|
| 277 |
|
| 278 |
if not user_question or user_question.strip() == "":
|
| 279 |
return "請輸入您的問題。", "錯誤: 問題為空"
|
| 280 |
|
| 281 |
+
# 分析問題
|
| 282 |
+
question_analysis = analyze_question_type(user_question)
|
| 283 |
+
log_messages.append(f"🔍 問題分析: {question_analysis['type']}類型")
|
| 284 |
+
|
| 285 |
+
# 1. 嘗試檢索相似問題(只在有有效數據時)
|
| 286 |
+
if self.data_loader.questions:
|
| 287 |
hits = self.retrieval_system.retrieve_similar(user_question)
|
| 288 |
|
| 289 |
if hits:
|
|
|
|
| 292 |
similar_question = self.data_loader.questions[best_hit['corpus_id']]
|
| 293 |
original_sql = self.data_loader.sql_answers[best_hit['corpus_id']]
|
| 294 |
|
| 295 |
+
log_messages.append(f"📋 檢索到: '{similar_question}'")
|
| 296 |
log_messages.append(f"📊 相似度: {similarity_score:.3f}")
|
| 297 |
|
| 298 |
if similarity_score > SIMILARITY_THRESHOLD:
|
| 299 |
+
log_messages.append(f"✅ 相似度 > {SIMILARITY_THRESHOLD},使用預先SQL")
|
| 300 |
+
return original_sql, "\n".join(log_messages)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
else:
|
| 302 |
+
log_messages.append(f"ℹ️ 相似度不足,嘗試其他方法")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
+
# 2. 智能生成SQL
|
| 305 |
+
log_messages.append("🤖 智能生成SQL...")
|
| 306 |
+
intelligent_sql = generate_intelligent_sql(user_question, question_analysis)
|
| 307 |
+
log_messages.append("✅ 智能生成完成")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
+
return intelligent_sql, "\n".join(log_messages)
|
|
|
|
| 310 |
|
| 311 |
+
# ==================== 其他類定義 ====================
|
| 312 |
class LLMClient:
|
| 313 |
def __init__(self, hf_token: str):
|
| 314 |
self.hf_token = hf_token
|
| 315 |
|
| 316 |
+
def call_llm_api(self, prompt: str) -> Optional[str]:
|
| 317 |
headers = {"Authorization": f"Bearer {self.hf_token}"}
|
| 318 |
+
payload = {"inputs": prompt, "parameters": {"max_new_tokens": 200, "temperature": 0.1}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
|
| 320 |
+
for model_url in LLM_MODELS:
|
| 321 |
try:
|
| 322 |
+
response = requests.post(model_url, headers=headers, json=payload, timeout=15)
|
| 323 |
if response.status_code == 200:
|
| 324 |
result = response.json()
|
| 325 |
if isinstance(result, list) and len(result) > 0:
|
| 326 |
+
return result[0]['generated_text'].strip()
|
|
|
|
|
|
|
| 327 |
except:
|
| 328 |
continue
|
| 329 |
return None
|
|
|
|
| 335 |
|
| 336 |
def compute_embeddings(self, questions: List[str]) -> None:
|
| 337 |
if questions:
|
| 338 |
+
self.question_embeddings = self.embedder.encode(questions, convert_to_tensor=True)
|
|
|
|
|
|
|
| 339 |
|
| 340 |
def retrieve_similar(self, user_question: str, top_k: int = 3) -> List[Dict]:
|
| 341 |
if self.question_embeddings is None or len(self.question_embeddings) == 0:
|
|
|
|
| 348 |
return []
|
| 349 |
|
| 350 |
# ==================== 初始化系統 ====================
|
| 351 |
+
print("正在初始化增強版Text-to-SQL系統...")
|
| 352 |
+
text_to_sql_system = EnhancedTextToSQLSystem(HF_TOKEN)
|
| 353 |
|
| 354 |
# ==================== Gradio界面 ====================
|
| 355 |
def process_query(user_question: str) -> Tuple[str, str]:
|
| 356 |
sql_result, log_message = text_to_sql_system.generate_sql(user_question)
|
| 357 |
+
return sql_result, "✅ SQL生成完成", log_message
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
with gr.Blocks(title="智能Text-to-SQL系統", theme=gr.themes.Soft()) as demo:
|
| 360 |
gr.Markdown("# 🚀 智能 Text-to-SQL 系統")
|
| 361 |
+
gr.Markdown("💡 針對大量無效數據優化的增強版本")
|
| 362 |
+
|
| 363 |
+
with gr.Row():
|
| 364 |
+
question_input = gr.Textbox(
|
| 365 |
+
label="📝 輸入問題",
|
| 366 |
+
placeholder="例如:查詢2023年每月報告數量",
|
| 367 |
+
lines=2
|
| 368 |
+
)
|
| 369 |
|
| 370 |
with gr.Row():
|
|
|
|
| 371 |
submit_btn = gr.Button("🚀 生成SQL", variant="primary")
|
| 372 |
|
| 373 |
with gr.Row():
|
| 374 |
+
sql_output = gr.Code(
|
| 375 |
+
label="📊 生成的SQL",
|
| 376 |
+
language="sql",
|
| 377 |
+
lines=6
|
| 378 |
+
)
|
| 379 |
|
| 380 |
with gr.Row():
|
| 381 |
+
debug_output = gr.Textbox(label="🔍 狀態", lines=2, interactive=False)
|
| 382 |
+
log_output = gr.Textbox(label="📋 詳細日誌", lines=4, interactive=False)
|
| 383 |
|
| 384 |
+
submit_btn.click(
|
| 385 |
+
process_query,
|
| 386 |
+
inputs=question_input,
|
| 387 |
+
outputs=[sql_output, debug_output, log_output]
|
| 388 |
+
)
|
| 389 |
|
| 390 |
if __name__ == "__main__":
|
| 391 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|