AliInamdar commited on
Commit
ac0fd63
Β·
verified Β·
1 Parent(s): 6170650

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +10 -5
streamlit_app.py CHANGED
@@ -28,18 +28,21 @@ if st.button("Generate & Run SQL"):
28
  st.success("βœ… File loaded successfully")
29
  st.dataframe(df.head(3))
30
 
31
- # πŸ“‹ Preview for prompt
32
  preview = df.head(5).to_string(index=False)
33
  prompt = f"""
34
- You are an AI assistant. Generate an SQL query for a pandas DataFrame named 'df' based on the user's question.
35
 
36
  Data preview:
37
  {preview}
38
 
39
- Question:
40
  {user_query}
41
 
42
- Return ONLY the SQL query. Do not include any explanations or Python code.
 
 
 
43
  """
44
 
45
  # πŸ”— Together API request
@@ -53,7 +56,7 @@ Return ONLY the SQL query. Do not include any explanations or Python code.
53
  "max_tokens": 256,
54
  "temperature": 0.3,
55
  "messages": [
56
- {"role": "system", "content": "You generate SQL queries from natural language questions."},
57
  {"role": "user", "content": prompt}
58
  ]
59
  }
@@ -67,7 +70,9 @@ Return ONLY the SQL query. Do not include any explanations or Python code.
67
  if response.status_code != 200:
68
  raise ValueError(f"API Error: {response.status_code} - {response.text}")
69
 
 
70
  sql_query = response.json()['choices'][0]['message']['content'].strip()
 
71
  st.code(sql_query, language='sql')
72
 
73
  # πŸ§ͺ Execute SQL
 
28
  st.success("βœ… File loaded successfully")
29
  st.dataframe(df.head(3))
30
 
31
+ # 🧠 Prompt construction
32
  preview = df.head(5).to_string(index=False)
33
  prompt = f"""
34
+ You are a SQL expert assistant. Generate an SQL query for a pandas DataFrame named 'df' based on the user's question.
35
 
36
  Data preview:
37
  {preview}
38
 
39
+ User question:
40
  {user_query}
41
 
42
+ IMPORTANT:
43
+ - Only return a valid SQL query (no Python, no comments, no markdown, no explanations).
44
+ - Use SQL compatible with SQLite.
45
+ - Table name is 'df'.
46
  """
47
 
48
  # πŸ”— Together API request
 
56
  "max_tokens": 256,
57
  "temperature": 0.3,
58
  "messages": [
59
+ {"role": "system", "content": "You are a SQL assistant. You return only clean SQL code."},
60
  {"role": "user", "content": prompt}
61
  ]
62
  }
 
70
  if response.status_code != 200:
71
  raise ValueError(f"API Error: {response.status_code} - {response.text}")
72
 
73
+ # 🧼 Clean the response
74
  sql_query = response.json()['choices'][0]['message']['content'].strip()
75
+ sql_query = sql_query.replace("```sql", "").replace("```", "").strip()
76
  st.code(sql_query, language='sql')
77
 
78
  # πŸ§ͺ Execute SQL