Spaces:
Sleeping
Sleeping
Update src/sql_generator.py
Browse files7 unmodified lines891011121314151617181920212223244 unmodified lines29303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231247 unmodified linesdef __init__(self):load_dotenv()# 1. GET KEY (Cleaned)raw_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACEHUB_API_TOKEN")self.api_key = raw_key.strip() if raw_key else None# 2. USE QWEN 2.5 CODER (7B)# This is the Best "Ungated" Free Model right now.# It does not require a license agreement click, unlike Mistral/Llama.self.repo_id = "Qwen/Qwen2.5-Coder-7B-Instruct"# 3. ROUTER URLself.api_url = f"https://router.huggingface.co/models/{self.repo_id}"def generate_followup_questions(self, question, sql_query):return ["Visualize this result", "Export as CSV", "Compare with last year"]4 unmodified linesif not self.api_key:return "SELECT 'Error: HF_API_KEY Missing' as status", "Configuration Error", "Please add HF_API_KEY to your Space Secrets."# π‘οΈ Safety Layerforbidden = ["DROP", "DELETE", "UPDATE", "INSERT", "ALTER", "TRUNCATE", "GRANT"]if any(word in question.upper() for word in forbidden):return "SELECT 'Error: Blocked by Safety Layer' as status", "Safety Alert", "I cannot execute commands that modify data."# Format Historyhistory_text = ""if history:history_text = "PREVIOUS CONVERSATION:\n" + "\n".join([f"User: {h.get('user')}\nSQL: {h.get('sql')}" for h in history[-2:]])# System Prompt (Qwen ChatML Format)system_prompt = f"""You are an elite SQL Expert.DATABASE SCHEMA:{context}{history_text}RULES:1. Output ONLY a valid JSON object. Format: {{ "sql": "SELECT ...", "message": "Friendly text", "explanation": "Brief summary" }}2. The SQL query MUST be Read-Only (SELECT).3. Do not use markdown formatting."""# Payload (Qwen Specific Format)payload = {"inputs": f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{question}<|im_end|>\n<|im_start|>assistant\n","parameters": {"max_new_tokens": 1024,"temperature": 0.1,"return_full_text": False}}headers = {"Authorization": f"Bearer {self.api_key}","Content-Type": "application/json"}try:print(f" β‘ Generating SQL using {self.repo_id}...")# π DIRECT REQUESTresponse = requests.post(self.api_url, headers=headers, json=payload, timeout=25)if response.status_code != 200:print(f" β API FAILURE: {response.status_code}")print(f" β RESPONSE: {response.text}")return f"SELECT 'Error: API returned {response.status_code}' as status", "API Error", f"Model Error: {response.status_code}"result = response.json()# Helper to extract textif isinstance(result, list) and len(result) > 0:raw_text = result[0].get('generated_text', '')elif isinstance(result, dict):raw_text = result.get('generated_text', '')else:raw_text = str(result)# JSON Parsingsql_query = ""message = "Here is the data."explanation = "Query generated successfully."try:clean_json = re.sub(r"```json|```", "", raw_text).strip()json_match = re.search(r"\{.*\}", clean_json, re.DOTALL)if json_match:data = json.loads(json_match.group(0))sql_query = data.get("sql", "")message = data.get("message", message)explanation = data.get("explanation", explanation)else:# Regex Fallbackmatch = re.search(r"(SELECT[\s\S]+?;)", raw_text, re.IGNORECASE)if match: sql_query = match.group(1)except:match = re.search(r"(SELECT[\s\S]+?;)", raw_text, re.IGNORECASE)if match: sql_query = match.group(1)sql_query = sql_query.strip().replace("\n", " ")if sql_query and not sql_query.endswith(";"): sql_query += ";"clean_check = re.sub(r"/\*.*?\*/|--.*?\n", "", sql_query, flags=re.DOTALL).strip().upper()if not clean_check.startswith("SELECT") and not clean_check.startswith("WITH"):return "SELECT 'Error: Invalid Query Type' as status", "Safety Error", "I can only perform read-only operations."return sql_query, explanation, messageexcept Exception as e:print(f" β SYSTEM EXCEPTION: {e}")safe_e = str(e).replace("'", "").replace('"', "")return f"SELECT 'Error: {safe_e}' as status", "System Error", "An unexpected error occurred."7 unmodified lines89101112131415161718192021222324252627282930313233343536374 unmodified lines424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291307 unmodified linesdef __init__(self):load_dotenv()# 1. AUTHENTICATION (With Debugging)raw_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACEHUB_API_TOKEN")self.api_key = raw_key.strip() if raw_key else Noneif not self.api_key:print(" β FATAL: API Key missing.")else:print(f" β API Key loaded: {self.api_key[:5]}...")# 2. THE "SHOTGUN" MODEL LIST# We try these 5 models in order. One WILL work.self.models = ["microsoft/Phi-3-mini-4k-instruct", # High availability, very fast"google/gemma-1.1-7b-it", # Google's open model (very stable)"mistralai/Mistral-7B-Instruct-v0.3", # Standard free tier workhorse"HuggingFaceH4/zephyr-7b-beta", # Reliable fallback"Qwen/Qwen2.5-Coder-7B-Instruct" # Excellent coder (if online)]# 3. ENDPOINTS (Router + Legacy)self.endpoints = ["https://router.huggingface.co/models/","https://api-inference.huggingface.co/models/"]def generate_followup_questions(self, question, sql_query):return ["Visualize this result", "Export as CSV", "Compare with last year"]4 unmodified linesif not self.api_key:return "SELECT 'Error: HF_API_KEY Missing' as status", "Configuration Error", "Please add HF_API_KEY to your Space Secrets."# π‘οΈ Safetyforbidden = ["DROP", "DELETE", "UPDATE", "INSERT", "ALTER", "TRUNCATE", "GRANT"]if any(word in question.upper() for word in forbidden):return "SELECT 'Error: Blocked by Safety Layer' as status", "Safety Alert", "I cannot execute commands that modify data."# Simple Promptsystem_prompt = f"""You are an SQL Expert.Schema:{context}Rules:1. Output valid JSON: {{ "sql": "SELECT ...", "message": "Short text", "explanation": "Brief summary" }}2. Read-only SELECT queries only.3. No markdown.Question: {question}"""payload = {"inputs": f"<|user|>\n{system_prompt}\n<|end|>\n<|assistant|>\n","parameters": {"max_new_tokens": 512, "temperature": 0.1, "return_full_text": False}}headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}# π ULTRA-ROBUST RETRY LOOPerrors = []for model in self.models:for base_url in self.endpoints:api_url = f"{base_url}{model}"try:print(f" β‘ Attempting: {model}...")response = requests.post(api_url, headers=headers, json=payload, timeout=15)if response.status_code == 200:print(f" β SUCCESS with {model}!")return self._process_response(response.json())# Log failure and continue immediatelyprint(f" β Failed ({response.status_code})")errors.append(f"{model}: {response.status_code}")except Exception as e:print(f" β οΈ Connection Error: {e}")errors.append(f"{model}: Error")# If we get here, literally everything failed (Rare)return f"SELECT 'Error: All models failed' as status", "System Error", f"Debug Info: {', '.join(errors)}"def _process_response(self, result):if isinstance(result, list) and len(result) > 0:raw_text = result[0].get('generated_text', '')elif isinstance(result, dict):raw_text = result.get('generated_text', '')else:raw_text = str(result)sql_query = ""message = "Here is the data."explanation = "Query generated successfully."try:# Clean and Extractclean_json = re.sub(r"```json|```", "", raw_text).strip()# Regex to find JSONjson_match = re.search(r"\{.*\}", clean_json, re.DOTALL)if json_match:data = json.loads(json_match.group(0))sql_query = data.get("sql", "")message = data.get("message", message)explanation = data.get("explanation", explanation)else:match = re.search(r"(SELECT[\s\S]+?;)", raw_text, re.IGNORECASE)if match: sql_query = match.group(1)except:match = re.search(r"(SELECT[\s\S]+?;)", raw_text, re.IGNORECASE)if match: sql_query = match.group(1)sql_query = sql_query.strip().replace("\n", " ")if sql_query and not sql_query.endswith(";"): sql_query += ";"# Fallback for empty SQLif not sql_query:sql_query = "SELECT 'Error: AI generated empty query' as status"return sql_query, explanation, message
src/sql_generator.py+93-87