Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ DATASET_REPO_ID = "Paul720810/Text-to-SQL-Softline"
|
|
| 16 |
GGUF_REPO_ID = "Paul720810/gguf-models"
|
| 17 |
GGUF_FILENAME = "qwen2.5-coder-1.5b-sql-finetuned.q4_k_m.gguf"
|
| 18 |
|
| 19 |
-
FEW_SHOT_EXAMPLES_COUNT =
|
| 20 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
|
| 22 |
print("=" * 60)
|
|
@@ -33,12 +33,19 @@ def format_log(message: str, level: str = "INFO") -> str:
|
|
| 33 |
return f"[{get_current_time()}] [{level.upper()}] {message}"
|
| 34 |
|
| 35 |
def parse_sql_from_response(response_text: str) -> Optional[str]:
|
| 36 |
-
|
| 37 |
match = re.search(r"```sql\n(.*?)\n```", response_text, re.DOTALL)
|
| 38 |
if match:
|
| 39 |
return match.group(1).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return None
|
| 41 |
|
|
|
|
| 42 |
# ==================== Text-to-SQL 核心類 ====================
|
| 43 |
class TextToSQLSystem:
|
| 44 |
def __init__(self, embed_model='sentence-transformers/paraphrase-multilingual-mpnet-base-v2'):
|
|
@@ -60,8 +67,9 @@ class TextToSQLSystem:
|
|
| 60 |
)
|
| 61 |
self.llm = Llama(
|
| 62 |
model_path=model_path,
|
| 63 |
-
n_ctx=
|
| 64 |
-
n_threads=os.cpu_count()
|
|
|
|
| 65 |
verbose=False
|
| 66 |
)
|
| 67 |
self._log(f"✅ 已載入 GGUF 模型: {GGUF_FILENAME}")
|
|
@@ -138,7 +146,7 @@ class TextToSQLSystem:
|
|
| 138 |
self._log("🧠 使用 GGUF 模型生成 SQL...")
|
| 139 |
output = self.llm(
|
| 140 |
prompt,
|
| 141 |
-
max_tokens=
|
| 142 |
temperature=0.2,
|
| 143 |
stop=["</s>", "```"],
|
| 144 |
echo=False
|
|
|
|
| 16 |
GGUF_REPO_ID = "Paul720810/gguf-models"
|
| 17 |
GGUF_FILENAME = "qwen2.5-coder-1.5b-sql-finetuned.q4_k_m.gguf"
|
| 18 |
|
| 19 |
+
FEW_SHOT_EXAMPLES_COUNT = 1
|
| 20 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
|
| 22 |
print("=" * 60)
|
|
|
|
| 33 |
return f"[{get_current_time()}] [{level.upper()}] {message}"
|
| 34 |
|
| 35 |
def parse_sql_from_response(response_text: str) -> Optional[str]:
|
| 36 |
+
# 1. 先找 ```sql ... ```
|
| 37 |
match = re.search(r"```sql\n(.*?)\n```", response_text, re.DOTALL)
|
| 38 |
if match:
|
| 39 |
return match.group(1).strip()
|
| 40 |
+
|
| 41 |
+
# 2. 如果沒找到,嘗試找最長的 SELECT … 語句
|
| 42 |
+
match = re.search(r"(SELECT .*?;)", response_text, re.DOTALL | re.IGNORECASE)
|
| 43 |
+
if match:
|
| 44 |
+
return match.group(1).strip()
|
| 45 |
+
|
| 46 |
return None
|
| 47 |
|
| 48 |
+
|
| 49 |
# ==================== Text-to-SQL 核心類 ====================
|
| 50 |
class TextToSQLSystem:
|
| 51 |
def __init__(self, embed_model='sentence-transformers/paraphrase-multilingual-mpnet-base-v2'):
|
|
|
|
| 67 |
)
|
| 68 |
self.llm = Llama(
|
| 69 |
model_path=model_path,
|
| 70 |
+
n_ctx=2048, # 少一半上下文 → 快
|
| 71 |
+
n_threads=os.cpu_count(),# 用滿 CPU
|
| 72 |
+
n_batch=512, # 增加 batch size → 減少迭代
|
| 73 |
verbose=False
|
| 74 |
)
|
| 75 |
self._log(f"✅ 已載入 GGUF 模型: {GGUF_FILENAME}")
|
|
|
|
| 146 |
self._log("🧠 使用 GGUF 模型生成 SQL...")
|
| 147 |
output = self.llm(
|
| 148 |
prompt,
|
| 149 |
+
max_tokens=128,
|
| 150 |
temperature=0.2,
|
| 151 |
stop=["</s>", "```"],
|
| 152 |
echo=False
|