Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -29,7 +29,7 @@ sql_gen = pipeline(
|
|
29 |
)
|
30 |
|
31 |
def answer_profitability(question: str) -> str:
|
32 |
-
# 1)
|
33 |
prompt = (
|
34 |
f"-- Translate to SQL for table `sap` ({schema})\n"
|
35 |
f"Question: {question}\n"
|
@@ -37,8 +37,26 @@ def answer_profitability(question: str) -> str:
|
|
37 |
)
|
38 |
sql = sql_gen(prompt)[0]['generated_text'].strip()
|
39 |
|
40 |
-
# 2) Try to
|
41 |
try:
|
42 |
df_out = con.execute(sql).df()
|
43 |
except Exception as e:
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
)
|
30 |
|
31 |
def answer_profitability(question: str) -> str:
|
32 |
+
# 1) Generate SQL
|
33 |
prompt = (
|
34 |
f"-- Translate to SQL for table `sap` ({schema})\n"
|
35 |
f"Question: {question}\n"
|
|
|
37 |
)
|
38 |
sql = sql_gen(prompt)[0]['generated_text'].strip()
|
39 |
|
40 |
+
# 2) Try to execute it
|
41 |
try:
|
42 |
df_out = con.execute(sql).df()
|
43 |
except Exception as e:
|
44 |
+
# Use a normal f-string with explicit \n for newlines
|
45 |
+
return (
|
46 |
+
f"❌ **SQL Error**\n"
|
47 |
+
f"```\n{e}\n```\n\n"
|
48 |
+
f"**Generated SQL**\n"
|
49 |
+
f"```sql\n{sql}\n```"
|
50 |
+
)
|
51 |
+
|
52 |
+
# 3) Format successful result
|
53 |
+
if df_out.empty:
|
54 |
+
return (
|
55 |
+
"No rows returned.\n\n"
|
56 |
+
f"**Generated SQL**\n```sql\n{sql}\n```"
|
57 |
+
)
|
58 |
+
|
59 |
+
if df_out.shape == (1,1):
|
60 |
+
return str(df_out.iat[0,0])
|
61 |
+
|
62 |
+
return df_out.to_markdown(index=False)
|