PD03 commited on
Commit
7704539
Β·
verified Β·
1 Parent(s): 08892e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -25
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 call OpenAI and get back a SQL string
21
  def generate_sql(question: str) -> str:
22
  system = (
23
- f"You are an expert SQL generator for DuckDB table `sap` with columns: {schema}. "
24
- "Translate the user’s question into a valid SQL query. "
25
- "Return ONLY the SQL, no explanation."
26
  )
27
- resp = openai.ChatCompletion.create(
 
 
 
 
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
- # Strip ``` if present
38
  if sql.startswith("```") and sql.endswith("```"):
39
  sql = "\n".join(sql.splitlines()[1:-1])
40
  return sql
41
 
42
- # 5) The function Gradio will call
43
  def answer_profitability(question: str) -> str:
44
- # a) Generate the SQL
45
  try:
46
  sql = generate_sql(question)
47
  except Exception as e:
48
  return f"❌ OpenAI error:\n{e}"
49
 
50
- # b) Execute it in DuckDB
51
  try:
52
- result_df = conn.execute(sql).df()
53
  except Exception as e:
54
- return f"❌ SQL error:\n{e}\n\nGenerated SQL:\n```sql\n{sql}\n```"
 
 
 
55
 
56
- # c) Format the output
57
- if result_df.empty:
58
  return f"No results.\n\nSQL was:\n```sql\n{sql}\n```"
59
- if result_df.shape == (1,1):
60
- return str(result_df.iat[0,0])
61
- return result_df.to_markdown(index=False)
62
 
63
- # 6) Gradio interfaceβ€”**note the explicit outputs=…**
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"),