Dub973 commited on
Commit
d73b7bd
·
verified ·
1 Parent(s): aba90dc

Upload 3 files

Browse files
Files changed (1) hide show
  1. app.py +37 -31
app.py CHANGED
@@ -1,15 +1,21 @@
1
- import os, json, sqlite3, time, textwrap, requests
2
  import gradio as gr
3
- from dotenv import load_dotenv
4
- load_dotenv()
5
 
6
- HF_TOKEN = os.getenv("HF_TOKEN") # store in Space Secrets
7
- MODEL_ID = "defog/sqlcoder-7b-2"
8
- API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
9
- HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
10
- DB_PATH = "company.db"
 
11
 
12
- def nlp_to_sql(nl_query: str, schema_ddl: str) -> str:
 
 
 
 
 
 
 
13
  prompt = textwrap.dedent(f"""
14
  ### Task
15
  Translate the following natural language question into ONE valid SQLite SQL query.
@@ -24,48 +30,48 @@ def nlp_to_sql(nl_query: str, schema_ddl: str) -> str:
24
  """)
25
  payload = {"inputs": prompt, "parameters": {"max_new_tokens": 256}}
26
  response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=45)
27
- sql = response.json()[0]["generated_text"].split("### SQL")[-1].strip()
28
- return sql
 
 
 
29
 
30
  def run_pipeline(nl_query):
31
  trace = []
32
  start = time.time()
33
 
34
- # 1. load schema
35
- with open("schema.sql") as f:
 
36
  schema_ddl = f.read()
37
- trace.append(("Load Schema", f"{len(schema_ddl.splitlines())} lines loaded"))
38
 
39
- # 2. NL ➜ SQL
40
  sql_query = nlp_to_sql(nl_query, schema_ddl)
41
- trace.append(("LLM Output", sql_query))
42
 
43
- # 3. Execute SQL
44
  try:
45
  with sqlite3.connect(DB_PATH) as conn:
46
  cursor = conn.execute(sql_query)
47
  rows = cursor.fetchall()
48
- colnames = [d[0] for d in cursor.description] if cursor.description else []
49
  result = {"columns": colnames, "rows": rows}
50
- trace.append(("Execution", f"{len(rows)} rows returned"))
51
  except Exception as e:
52
  result = {"error": str(e)}
53
  trace.append(("Execution Error", str(e)))
54
 
55
- # 4. timing
56
- trace.append(("Latency", f"{time.time() - start:0.2f}s"))
57
-
58
- return sql_query, json.dumps(result, indent=2), "\n".join([f"{s}: {m}" for s, m in trace])
59
 
60
- with gr.Blocks(title="NLP ➜ SQL Demo") as demo:
61
- gr.Markdown("### NLP to SQL Query SQLite Trace Visibility")
62
- nl_input = gr.Textbox(label="Natural-Language Question", placeholder="e.g. List employees in Engineering hired after 2021")
63
  with gr.Row():
64
- sql_out = gr.Code(label="Generated SQL")
65
- result_out= gr.Code(label="Query Result")
66
- trace_out = gr.Textbox(label="Trace", lines=6)
67
- submit = gr.Button("Run")
68
- submit.click(fn=run_pipeline, inputs=nl_input, outputs=[sql_out, result_out, trace_out])
69
 
70
  if __name__ == "__main__":
71
  demo.launch()
 
1
+ import os, sqlite3, textwrap, requests, json, time
2
  import gradio as gr
 
 
3
 
4
+ DB_PATH = "company.db"
5
+ SCHEMA_FILE = "schema.sql"
6
+ MODEL_ID = "defog/sqlcoder-7b-2"
7
+ HF_TOKEN = os.getenv("HF_TOKEN")
8
+ API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
9
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
10
 
11
+ def create_db_if_needed():
12
+ if not os.path.exists(DB_PATH):
13
+ with open(SCHEMA_FILE) as f:
14
+ schema = f.read()
15
+ with sqlite3.connect(DB_PATH) as conn:
16
+ conn.executescript(schema)
17
+
18
+ def nlp_to_sql(nl_query, schema_ddl):
19
  prompt = textwrap.dedent(f"""
20
  ### Task
21
  Translate the following natural language question into ONE valid SQLite SQL query.
 
30
  """)
31
  payload = {"inputs": prompt, "parameters": {"max_new_tokens": 256}}
32
  response = requests.post(API_URL, headers=HEADERS, json=payload, timeout=45)
33
+ try:
34
+ sql = response.json()[0]["generated_text"].split("### SQL")[-1].strip()
35
+ return sql
36
+ except:
37
+ return "Error: " + str(response.json())
38
 
39
  def run_pipeline(nl_query):
40
  trace = []
41
  start = time.time()
42
 
43
+ create_db_if_needed()
44
+
45
+ with open(SCHEMA_FILE) as f:
46
  schema_ddl = f.read()
47
+ trace.append(("Schema Loaded", f"{len(schema_ddl.splitlines())} lines"))
48
 
 
49
  sql_query = nlp_to_sql(nl_query, schema_ddl)
50
+ trace.append(("LLM Generated SQL", sql_query))
51
 
 
52
  try:
53
  with sqlite3.connect(DB_PATH) as conn:
54
  cursor = conn.execute(sql_query)
55
  rows = cursor.fetchall()
56
+ colnames = [desc[0] for desc in cursor.description] if cursor.description else []
57
  result = {"columns": colnames, "rows": rows}
58
+ trace.append(("SQL Executed", f"{len(rows)} rows returned"))
59
  except Exception as e:
60
  result = {"error": str(e)}
61
  trace.append(("Execution Error", str(e)))
62
 
63
+ trace.append(("Total Time", f"{time.time() - start:.2f}s"))
64
+ return sql_query, json.dumps(result, indent=2), "\n".join(f"{step}: {msg}" for step, msg in trace)
 
 
65
 
66
+ with gr.Blocks() as demo:
67
+ gr.Markdown("### NLP to SQL Query (Auto-Build SQLite, No Terminal Needed)")
68
+ input_box = gr.Textbox(label="Enter natural language query", placeholder="e.g., Show all employees in Engineering")
69
  with gr.Row():
70
+ sql_box = gr.Code(label="Generated SQL")
71
+ result_box = gr.Code(label="Query Output")
72
+ trace_box = gr.Textbox(label="Trace Log", lines=6)
73
+ run_btn = gr.Button("Run Query")
74
+ run_btn.click(fn=run_pipeline, inputs=input_box, outputs=[sql_box, result_box, trace_box])
75
 
76
  if __name__ == "__main__":
77
  demo.launch()