Spaces:
Sleeping
Sleeping
Update src/sql_generator.py
Browse files11 unmodified lines1213141516171819202122232425262728299 unmodified lines394041424344454647484950513 unmodified lines55565758596061626364656667684 unmodified lines73747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314411 unmodified linesraw_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACEHUB_API_TOKEN")self.api_key = raw_key.strip() if raw_key else None# 2. MODEL LIST (The "Shotgun" Strategy)# We try 4 different models. If one fails (404/429), it instantly tries the next.self.models = ["mistralai/Mistral-7B-Instruct-v0.3", # Most popular free model"google/gemma-1.1-7b-it", # Highly reliable backup"microsoft/Phi-3-mini-4k-instruct", # Very fast, almost always online"HuggingFaceH4/zephyr-7b-beta" # Old reliable (last resort)]# 3. BASE URLs (Try Router first, then Legacy)self.router_url = "https://router.huggingface.co/models/"self.legacy_url = "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"]9 unmodified linesif 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."history_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 Promptsystem_prompt = f"""<|system|>You are an elite SQL Expert.DATABASE SCHEMA:{context}3 unmodified lines1. 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.QUESTION: {question} </s><|assistant|>"""payload = {"inputs": system_prompt,"parameters": {"max_new_tokens": 512,"temperature": 0.1,"return_full_text": False}4 unmodified lines"Content-Type": "application/json"}# π ROBUST RETRY LOOPlast_error = ""for model in self.models:# Try Router URL first, then Legacy URLurls_to_try = [self.router_url + model, self.legacy_url + model]for api_url in urls_to_try:try:print(f" β‘ Trying {model} at {api_url}...")response = requests.post(api_url, headers=headers, json=payload, timeout=20)if response.status_code == 200:print(f" β SUCCESS with {model}!")return self._process_response(response.json())# If 404/410/500, we log and continue to nextprint(f" β Failed ({response.status_code}). Trying next...")last_error = f"{response.status_code}: {response.text}"except Exception as e:print(f" β οΈ Connection Error: {e}")last_error = str(e)# If ALL models failreturn f"SELECT 'Error: {last_error}' as status", "System Error", "All AI models are currently unavailable. Check your HF_API_KEY."def _process_response(self, result):"""Helper to parse the AI response cleanly"""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 Markdownclean_json = re.sub(r"```json|```", "", raw_text).strip()# Attempt to find JSON objectjson_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:# Fallback regexmatch = 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)# Final Cleanupsql_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"):# Last resort fallback for clean "SELECT"return "SELECT 'Error: Invalid Query Type' as status", "Safety Error", "I can only perform read-only operations."return sql_query, explanation, message11 unmodified lines121314151617181920212223249 unmodified lines343536373839404142434445463 unmodified lines505152535455565758596061624 unmodified lines67686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312411 unmodified linesraw_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"]9 unmodified linesif 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}3 unmodified lines1. 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}4 unmodified lines"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."
src/sql_generator.py+64-84