Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,63 +4,67 @@ import pandas as pd
|
|
| 4 |
import duckdb
|
| 5 |
import openai
|
| 6 |
|
| 7 |
-
# 1) Load your OpenAI key from the Spaceβs Secrets
|
| 8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
if not openai.api_key:
|
| 10 |
raise RuntimeError("Missing OPENAI_API_KEY secret in your Space settings")
|
| 11 |
|
| 12 |
-
# 2) Load your CSV into DuckDB
|
| 13 |
df = pd.read_csv("synthetic_profit.csv")
|
| 14 |
conn = duckdb.connect(":memory:")
|
| 15 |
conn.register("sap", df)
|
| 16 |
|
| 17 |
-
# 3) Build a one-line schema string for prompting
|
| 18 |
-
schema = ", ".join(df.columns)
|
| 19 |
|
| 20 |
-
# 4) Function to
|
| 21 |
def generate_sql(question: str) -> str:
|
| 22 |
system = (
|
| 23 |
-
f"You are an expert SQL generator for DuckDB table `sap`
|
| 24 |
-
"
|
| 25 |
-
"
|
| 26 |
)
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
model="gpt-3.5-turbo",
|
| 29 |
-
messages=
|
| 30 |
-
{"role": "system", "content": system},
|
| 31 |
-
{"role": "user", "content": question}
|
| 32 |
-
],
|
| 33 |
temperature=0.0,
|
| 34 |
max_tokens=150,
|
| 35 |
)
|
| 36 |
sql = resp.choices[0].message.content.strip()
|
| 37 |
-
#
|
| 38 |
if sql.startswith("```") and sql.endswith("```"):
|
| 39 |
sql = "\n".join(sql.splitlines()[1:-1])
|
| 40 |
return sql
|
| 41 |
|
| 42 |
-
# 5)
|
| 43 |
def answer_profitability(question: str) -> str:
|
| 44 |
-
# a)
|
| 45 |
try:
|
| 46 |
sql = generate_sql(question)
|
| 47 |
except Exception as e:
|
| 48 |
return f"β OpenAI error:\n{e}"
|
| 49 |
|
| 50 |
-
# b)
|
| 51 |
try:
|
| 52 |
-
|
| 53 |
except Exception as e:
|
| 54 |
-
return
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
# c)
|
| 57 |
-
if
|
| 58 |
return f"No results.\n\nSQL was:\n```sql\n{sql}\n```"
|
| 59 |
-
if
|
| 60 |
-
return str(
|
| 61 |
-
return
|
| 62 |
|
| 63 |
-
# 6) Gradio interface
|
| 64 |
iface = gr.Interface(
|
| 65 |
fn=answer_profitability,
|
| 66 |
inputs=gr.Textbox(lines=2, placeholder="Ask a questionβ¦", label="Question"),
|
|
|
|
| 4 |
import duckdb
|
| 5 |
import openai
|
| 6 |
|
| 7 |
+
# βββ 1) Load your OpenAI key from the Spaceβs Secrets ββββββββββββββββββββββββ
|
| 8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
if not openai.api_key:
|
| 10 |
raise RuntimeError("Missing OPENAI_API_KEY secret in your Space settings")
|
| 11 |
|
| 12 |
+
# βββ 2) Load your CSV into DuckDB βββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
df = pd.read_csv("synthetic_profit.csv")
|
| 14 |
conn = duckdb.connect(":memory:")
|
| 15 |
conn.register("sap", df)
|
| 16 |
|
| 17 |
+
# βββ 3) Build a one-line schema string for prompting ββββββββββββββββββββββββ
|
| 18 |
+
schema = ", ".join(df.columns) # e.g. "Region,Product,FiscalYear, ..."
|
| 19 |
|
| 20 |
+
# βββ 4) Function to generate SQL via OpenAIβs new chat API ββββββββββββββββββ
|
| 21 |
def generate_sql(question: str) -> str:
|
| 22 |
system = (
|
| 23 |
+
f"You are an expert SQL generator for a DuckDB table named `sap` "
|
| 24 |
+
f"with columns: {schema}. "
|
| 25 |
+
"Translate the user's question into a valid SQL query and return ONLY the SQL."
|
| 26 |
)
|
| 27 |
+
messages = [
|
| 28 |
+
{"role": "system", "content": system},
|
| 29 |
+
{"role": "user", "content": question},
|
| 30 |
+
]
|
| 31 |
+
resp = openai.chat.completions.create(
|
| 32 |
model="gpt-3.5-turbo",
|
| 33 |
+
messages=messages,
|
|
|
|
|
|
|
|
|
|
| 34 |
temperature=0.0,
|
| 35 |
max_tokens=150,
|
| 36 |
)
|
| 37 |
sql = resp.choices[0].message.content.strip()
|
| 38 |
+
# strip ``` if the model wrapped it
|
| 39 |
if sql.startswith("```") and sql.endswith("```"):
|
| 40 |
sql = "\n".join(sql.splitlines()[1:-1])
|
| 41 |
return sql
|
| 42 |
|
| 43 |
+
# βββ 5) Core Q&A function: NL β SQL β execute β format βββββββββββββββββββββ
|
| 44 |
def answer_profitability(question: str) -> str:
|
| 45 |
+
# a) generate SQL
|
| 46 |
try:
|
| 47 |
sql = generate_sql(question)
|
| 48 |
except Exception as e:
|
| 49 |
return f"β OpenAI error:\n{e}"
|
| 50 |
|
| 51 |
+
# b) execute it in DuckDB
|
| 52 |
try:
|
| 53 |
+
df_out = conn.execute(sql).df()
|
| 54 |
except Exception as e:
|
| 55 |
+
return (
|
| 56 |
+
f"β SQL error:\n{e}\n\n"
|
| 57 |
+
f"Generated SQL:\n```sql\n{sql}\n```"
|
| 58 |
+
)
|
| 59 |
|
| 60 |
+
# c) format the result
|
| 61 |
+
if df_out.empty:
|
| 62 |
return f"No results.\n\nSQL was:\n```sql\n{sql}\n```"
|
| 63 |
+
if df_out.shape == (1,1):
|
| 64 |
+
return str(df_out.iat[0,0])
|
| 65 |
+
return df_out.to_markdown(index=False)
|
| 66 |
|
| 67 |
+
# βββ 6) Gradio interface with explicit outputs ββββββββββββββββββββββββββββββ
|
| 68 |
iface = gr.Interface(
|
| 69 |
fn=answer_profitability,
|
| 70 |
inputs=gr.Textbox(lines=2, placeholder="Ask a questionβ¦", label="Question"),
|