Spaces:
Sleeping
Sleeping
Update src/sql_generator.py
Browse files1234567891011121314151617181920212223242526272829303132333435362 unmodified lines3940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import osimport requestsimport reimport jsonfrom dotenv import load_dotenvclass SQLGenerator:def __init__(self):load_dotenv()# 1. AUTHENTICATIONraw_key = os.getenv("HF_API_KEY") or os.getenv("HUGGINGFACEHUB_API_TOKEN")self.api_key = raw_key.strip() if raw_key else None# 2. ACTIVE FREE TIER MODELS (2025)# We prioritize "Showcase" models which are kept online by sponsors.self.models = ["Qwen/Qwen2.5-72B-Instruct", # Currently the #1 Free Showcase Model"Qwen/Qwen2.5-7B-Instruct", # Reliable Backup"microsoft/Phi-3.5-mini-instruct", # Newest Microsoft Model (Active)"mistralai/Mistral-Nemo-Instruct-2407" # New Mistral Standard]# 3. ENDPOINTSself.base_url = "https://router.huggingface.co/models/"def generate_followup_questions(self, question, sql_query):return ["Visualize this result", "Export as CSV", "Compare with last year"]def generate_sql(self, question, context, history=None):if history is None: history = []if 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 Layer2 unmodified linesreturn "SELECT 'Error: Blocked by Safety Layer' as status", "Safety Alert", "I cannot execute commands that modify data."# 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"<|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": 512, "temperature": 0.1, "return_full_text": False}}headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}# π RETRY LOOPerrors = []for model in self.models:api_url = f"{self.base_url}{model}"try:print(f" β‘ Attempting: {model}...")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())print(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")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."1234567891011121314151617181920212223242526272829303132332 unmodified lines3637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import osimport reimport jsonfrom dotenv import load_dotenvfrom huggingface_hub import InferenceClientclass SQLGenerator:def __init__(self):load_dotenv()# 1. CLEAN THE KEY (Fixes "Invalid Header" error)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. SETUP CLIENT (Fixes "404/410" errors)# The client automatically handles the complex routing logicif self.api_key:self.client = InferenceClient(api_key=self.api_key)else:self.client = Noneprint(" β FATAL: API Key missing.")# 3. USE THE BEST FREE MODELself.model_id = "Qwen/Qwen2.5-Coder-32B-Instruct"def generate_followup_questions(self, question, sql_query):return ["Visualize this result", "Export as CSV", "Compare with last year"]def generate_sql(self, question, context, history=None):if not self.client:return "SELECT 'Error: HF_API_KEY Missing' as status", "Configuration Error", "Please add HF_API_KEY to your Space Secrets."# π‘οΈ Safety Layer2 unmodified linesreturn "SELECT 'Error: Blocked by Safety Layer' as status", "Safety Alert", "I cannot execute commands that modify data."# Promptmessages = [{"role": "system", "content": f"""You are an SQL Expert.Database Schema:{context}Rules:1. Output valid JSON: {{ "sql": "SELECT ...", "message": "Short text", "explanation": "Brief summary" }}2. Read-only SELECT queries only.3. No markdown formatting."""},{"role": "user", "content": question}]try:print(f" β‘ Generating SQL using {self.model_id}...")# π OFFICIAL CLIENT CALL (The Robust Way)response = self.client.chat.completions.create(model=self.model_id,messages=messages,max_tokens=500,temperature=0.1,stream=False)raw_text = response.choices[0].message.contentreturn self._process_response(raw_text)except Exception as e:print(f" β AI ERROR: {e}")# Failover to backup model if Qwen is busyif "404" in str(e) or "429" in str(e):return self._fallback_generate(messages)return f"SELECT 'Error: {str(e)[:50]}' as status", "System Error", "AI Model unavailable."def _fallback_generate(self, messages):"""Backup using a smaller model if the main one fails"""try:backup_model = "meta-llama/Llama-3.2-3B-Instruct"print(f" β οΈ Switching to backup: {backup_model}...")response = self.client.chat.completions.create(model=backup_model,messages=messages,max_tokens=500)return self._process_response(response.choices[0].message.content)except Exception as e:return "SELECT 'Error: All models failed' as status", "System Error", "Please check your API Key permissions."def _process_response(self, raw_text):sql_query = ""message = "Here is the data."explanation = "Query generated successfully."
src/sql_generator.py+59-59