Spaces:
Runtime error
Runtime error
Update app.py
Browse filesupload the application file
app.py
CHANGED
|
@@ -37,46 +37,59 @@ else:
|
|
| 37 |
|
| 38 |
# --- 4. HELPER FUNCTIONS ---
|
| 39 |
def clean_sql_query(sql_text):
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
def generate_sql(question):
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
{table_info}
|
| 58 |
|
| 59 |
-
Write a SQLite query to answer
|
| 60 |
|
| 61 |
Rules:
|
| 62 |
-
- Only use tables and columns from the schema
|
| 63 |
-
- Return ONLY the SQL query, no explanations
|
| 64 |
-
- Use proper SQLite syntax
|
| 65 |
-
- Use aggregations (SUM, AVG, COUNT) when needed
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# --- 5. CREATE THE QUERY FUNCTION ---
|
| 82 |
def run_query(question):
|
|
|
|
| 37 |
|
| 38 |
# --- 4. HELPER FUNCTIONS ---
|
| 39 |
def clean_sql_query(sql_text):
|
| 40 |
+
"""Extract and clean SQL query from LLM output"""
|
| 41 |
+
# Remove markdown code blocks
|
| 42 |
+
sql_text = re.sub(r'```sql\s*', '', sql_text)
|
| 43 |
+
sql_text = re.sub(r'```\s*', '', sql_text)
|
| 44 |
+
|
| 45 |
+
# Remove common prefixes
|
| 46 |
+
sql_text = re.sub(r'^(SQL Query:|Query:)\s*', '', sql_text, flags=re.IGNORECASE)
|
| 47 |
+
|
| 48 |
+
# Take only the first SQL statement
|
| 49 |
+
sql_text = sql_text.split(';')[0].strip()
|
| 50 |
+
|
| 51 |
+
return sql_text
|
| 52 |
|
| 53 |
def generate_sql(question):
|
| 54 |
+
"""Generate SQL query using Hugging Face's chat completions"""
|
| 55 |
+
|
| 56 |
+
# System prompt provides the instructions
|
| 57 |
+
system_prompt = f"""Given this database schema:
|
| 58 |
|
| 59 |
{table_info}
|
| 60 |
|
| 61 |
+
Write a SQLite query to answer the user's question.
|
| 62 |
|
| 63 |
Rules:
|
| 64 |
+
- Only use tables and columns from the schema.
|
| 65 |
+
- Return ONLY the SQL query, with no explanations.
|
| 66 |
+
- Use proper SQLite syntax.
|
| 67 |
+
- Use aggregations (SUM, AVG, COUNT) when needed.
|
| 68 |
+
"""
|
| 69 |
+
|
| 70 |
+
# User prompt is just the question
|
| 71 |
+
user_prompt = question
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
# Use the chat completions method
|
| 75 |
+
response = client.chat.completions.create(
|
| 76 |
+
model="mistralai/Mixtral-8x7B-Instruct-v0.1", # Use the model from the first error
|
| 77 |
+
messages=[
|
| 78 |
+
{"role": "system", "content": system_prompt},
|
| 79 |
+
{"role": "user", "content": user_prompt}
|
| 80 |
+
],
|
| 81 |
+
max_tokens=256, # Note: it's max_tokens, not max_new_tokens
|
| 82 |
+
temperature=0.1
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# The response object is different, parse it correctly
|
| 86 |
+
sql_query = response.choices[0].message.content
|
| 87 |
+
return clean_sql_query(sql_query)
|
| 88 |
+
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"Error generating SQL: {e}")
|
| 91 |
+
raise
|
| 92 |
+
|
| 93 |
|
| 94 |
# --- 5. CREATE THE QUERY FUNCTION ---
|
| 95 |
def run_query(question):
|