Spaces:
Running
Running
Update src/sql_generator.py
Browse files- src/sql_generator.py +59 -59
src/sql_generator.py
CHANGED
|
@@ -1,36 +1,33 @@
|
|
| 1 |
import os
|
| 2 |
-
import requests
|
| 3 |
import re
|
| 4 |
import json
|
| 5 |
from dotenv import load_dotenv
|
|
|
|
| 6 |
|
| 7 |
class SQLGenerator:
|
| 8 |
def __init__(self):
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
-
# 1.
|
| 12 |
raw_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 13 |
self.api_key = raw_key.strip() if raw_key else None
|
| 14 |
|
| 15 |
-
# 2.
|
| 16 |
-
#
|
| 17 |
-
self.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
"
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
self.base_url = "https://router.huggingface.co/models/"
|
| 26 |
|
| 27 |
def generate_followup_questions(self, question, sql_query):
|
| 28 |
return ["Visualize this result", "Export as CSV", "Compare with last year"]
|
| 29 |
|
| 30 |
def generate_sql(self, question, context, history=None):
|
| 31 |
-
if
|
| 32 |
-
|
| 33 |
-
if not self.api_key:
|
| 34 |
return "SELECT 'Error: HF_API_KEY Missing' as status", "Configuration Error", "Please add HF_API_KEY to your Space Secrets."
|
| 35 |
|
| 36 |
# 🛡️ Safety Layer
|
|
@@ -39,53 +36,56 @@ class SQLGenerator:
|
|
| 39 |
return "SELECT 'Error: Blocked by Safety Layer' as status", "Safety Alert", "I cannot execute commands that modify data."
|
| 40 |
|
| 41 |
# Prompt
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
"inputs": f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{question}<|im_end|>\n<|im_start|>assistant\n",
|
| 55 |
-
"parameters": {"max_new_tokens": 512, "temperature": 0.1, "return_full_text": False}
|
| 56 |
-
}
|
| 57 |
-
|
| 58 |
-
headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
except Exception as e:
|
| 76 |
-
print(f" ⚠️ Connection Error: {e}")
|
| 77 |
-
errors.append(f"{model}: Error")
|
| 78 |
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
def
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
|
|
|
| 89 |
sql_query = ""
|
| 90 |
message = "Here is the data."
|
| 91 |
explanation = "Query generated successfully."
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import re
|
| 3 |
import json
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
+
from huggingface_hub import InferenceClient
|
| 6 |
|
| 7 |
class SQLGenerator:
|
| 8 |
def __init__(self):
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
+
# 1. CLEAN THE KEY (Fixes "Invalid Header" error)
|
| 12 |
raw_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 13 |
self.api_key = raw_key.strip() if raw_key else None
|
| 14 |
|
| 15 |
+
# 2. SETUP CLIENT (Fixes "404/410" errors)
|
| 16 |
+
# The client automatically handles the complex routing logic
|
| 17 |
+
if self.api_key:
|
| 18 |
+
self.client = InferenceClient(api_key=self.api_key)
|
| 19 |
+
else:
|
| 20 |
+
self.client = None
|
| 21 |
+
print(" ❌ FATAL: API Key missing.")
|
| 22 |
+
|
| 23 |
+
# 3. USE THE BEST FREE MODEL
|
| 24 |
+
self.model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"
|
|
|
|
| 25 |
|
| 26 |
def generate_followup_questions(self, question, sql_query):
|
| 27 |
return ["Visualize this result", "Export as CSV", "Compare with last year"]
|
| 28 |
|
| 29 |
def generate_sql(self, question, context, history=None):
|
| 30 |
+
if not self.client:
|
|
|
|
|
|
|
| 31 |
return "SELECT 'Error: HF_API_KEY Missing' as status", "Configuration Error", "Please add HF_API_KEY to your Space Secrets."
|
| 32 |
|
| 33 |
# 🛡️ Safety Layer
|
|
|
|
| 36 |
return "SELECT 'Error: Blocked by Safety Layer' as status", "Safety Alert", "I cannot execute commands that modify data."
|
| 37 |
|
| 38 |
# Prompt
|
| 39 |
+
messages = [
|
| 40 |
+
{"role": "system", "content": f"""You are an SQL Expert.
|
| 41 |
+
Database Schema:
|
| 42 |
+
{context}
|
| 43 |
+
|
| 44 |
+
Rules:
|
| 45 |
+
1. Output valid JSON: {{ "sql": "SELECT ...", "message": "Short text", "explanation": "Brief summary" }}
|
| 46 |
+
2. Read-only SELECT queries only.
|
| 47 |
+
3. No markdown formatting.
|
| 48 |
+
"""},
|
| 49 |
+
{"role": "user", "content": question}
|
| 50 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
try:
|
| 53 |
+
print(f" ⚡ Generating SQL using {self.model_id}...")
|
| 54 |
+
|
| 55 |
+
# 🚀 OFFICIAL CLIENT CALL (The Robust Way)
|
| 56 |
+
response = self.client.chat.completions.create(
|
| 57 |
+
model=self.model_id,
|
| 58 |
+
messages=messages,
|
| 59 |
+
max_tokens=500,
|
| 60 |
+
temperature=0.1,
|
| 61 |
+
stream=False
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
raw_text = response.choices[0].message.content
|
| 65 |
+
return self._process_response(raw_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f" ❌ AI ERROR: {e}")
|
| 69 |
+
# Failover to backup model if Qwen is busy
|
| 70 |
+
if "404" in str(e) or "429" in str(e):
|
| 71 |
+
return self._fallback_generate(messages)
|
| 72 |
+
return f"SELECT 'Error: {str(e)[:50]}' as status", "System Error", "AI Model unavailable."
|
| 73 |
|
| 74 |
+
def _fallback_generate(self, messages):
|
| 75 |
+
"""Backup using a smaller model if the main one fails"""
|
| 76 |
+
try:
|
| 77 |
+
backup_model = "meta-llama/Llama-3.2-3B-Instruct"
|
| 78 |
+
print(f" ⚠️ Switching to backup: {backup_model}...")
|
| 79 |
+
response = self.client.chat.completions.create(
|
| 80 |
+
model=backup_model,
|
| 81 |
+
messages=messages,
|
| 82 |
+
max_tokens=500
|
| 83 |
+
)
|
| 84 |
+
return self._process_response(response.choices[0].message.content)
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return "SELECT 'Error: All models failed' as status", "System Error", "Please check your API Key permissions."
|
| 87 |
|
| 88 |
+
def _process_response(self, raw_text):
|
| 89 |
sql_query = ""
|
| 90 |
message = "Here is the data."
|
| 91 |
explanation = "Query generated successfully."
|