alokik29 commited on
Commit
d29a670
·
verified ·
1 Parent(s): 37f526a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -3
app.py CHANGED
@@ -39,8 +39,22 @@ def ask_question(user_db, question):
39
  cursor = conn.cursor()
40
 
41
  # Create a Text-to-SQL prompt
42
- prompt = f"Translate this question into an SQLite query:\nQuestion: {question}\nSQL:"
43
- sql_query = sqlcoder_llm(prompt)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  try:
46
  cursor.execute(sql_query)
@@ -51,7 +65,7 @@ def ask_question(user_db, question):
51
  return sql_query, df
52
  except Exception as e:
53
  conn.close()
54
- return f"❌ Error executing query: {e}", None
55
 
56
  # ============================================================
57
  # 🎨 Gradio UI
 
39
  cursor = conn.cursor()
40
 
41
  # Create a Text-to-SQL prompt
42
+ prompt = f"Translate this question into an SQLite query.\nReturn only SQL (no text):\nQuestion: {question}\nSQL:"
43
+
44
+ # Generate text
45
+ response = sqlcoder_llm(prompt)
46
+
47
+ # Some models return objects, ensure it's plain string
48
+ if isinstance(response, dict) and "text" in response:
49
+ response = response["text"]
50
+ elif isinstance(response, list):
51
+ response = response[0]["generated_text"]
52
+
53
+ # Extract SQL from response (clean any extra text)
54
+ sql_query = response.strip().split("SQL:")[-1].strip()
55
+ sql_query = sql_query.split("\n")[0].strip()
56
+ if not sql_query.endswith(";"):
57
+ sql_query += ";"
58
 
59
  try:
60
  cursor.execute(sql_query)
 
65
  return sql_query, df
66
  except Exception as e:
67
  conn.close()
68
+ return f"❌ Error executing query: {e}\n\nGenerated SQL:\n{sql_query}", None
69
 
70
  # ============================================================
71
  # 🎨 Gradio UI