Nadeem005 commited on
Commit
c5caf96
·
verified ·
1 Parent(s): f82b5a1

Update app.py

Browse files

upload the application file

Files changed (1) hide show
  1. app.py +46 -33
app.py CHANGED
@@ -37,46 +37,59 @@ else:
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"""
55
- prompt = f"""Given this database schema:
 
 
56
 
57
  {table_info}
58
 
59
- Write a SQLite query to answer: {question}
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
- SQL Query:"""
68
-
69
- try:
70
- response = client.text_generation(
71
- prompt,
72
- model="google/flan-t5-xxl",
73
- max_new_tokens=256,
74
- temperature=0.1
75
- )
76
- return clean_sql_query(response)
77
- except Exception as e:
78
- print(f"Error generating SQL: {e}")
79
- raise
 
 
 
 
 
 
 
 
 
 
 
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):