Dub973 commited on
Commit
d5ccd98
·
verified ·
1 Parent(s): 3e498a0

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +116 -0
  2. requirements.txt +4 -0
  3. schema.sql +14 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, sys, time, json, sqlite3, textwrap, requests
2
+ import gradio as gr
3
+
4
+ # -------------------------------------------------
5
+ # 1. CONFIGURATION
6
+ # -------------------------------------------------
7
+ MODEL_ID = "gpt2" # always exists; later swap for sqlcoder
8
+ API_URL = f"https://api-inference.huggingface.co/models/{MODEL_ID}"
9
+
10
+ HF_TOKEN = os.getenv("HF_TOKEN")
11
+ if not HF_TOKEN:
12
+ raise RuntimeError(
13
+ "HF_TOKEN not found. Go to Space → Settings → Secrets and add it."
14
+ )
15
+
16
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
17
+
18
+ DB_PATH = "company.db"
19
+ SCHEMA_FILE = "schema.sql"
20
+
21
+ # -------------------------------------------------
22
+ # 2. UTIL: BUILD DB IF NEEDED
23
+ # -------------------------------------------------
24
+ def create_db_if_needed():
25
+ if os.path.exists(DB_PATH):
26
+ return
27
+ with open(SCHEMA_FILE) as f, sqlite3.connect(DB_PATH) as conn:
28
+ conn.executescript(f.read())
29
+
30
+ # -------------------------------------------------
31
+ # 3. UTIL: CALL HF MODEL (with token debug)
32
+ # -------------------------------------------------
33
+ def nlp_to_sql(question: str, schema_ddl: str) -> str:
34
+ prompt = textwrap.dedent(f"""
35
+ Translate the following natural language question into a single valid SQLite SQL query.
36
+
37
+ ### Schema
38
+ {schema_ddl}
39
+
40
+ ### Question
41
+ {question}
42
+
43
+ ### SQL
44
+ """)
45
+ payload = {"inputs": prompt, "parameters": {"max_new_tokens": 64}}
46
+
47
+ # ---------- DEBUG ----------
48
+ print("=" * 60, file=sys.stderr)
49
+ print("DEBUG URL:", API_URL, file=sys.stderr)
50
+ print("DEBUG token starts with:", HF_TOKEN[:8], file=sys.stderr)
51
+ # ---------------------------
52
+
53
+ try:
54
+ r = requests.post(API_URL, headers=HEADERS, json=payload, timeout=60)
55
+ except Exception as e:
56
+ return f"[ConnErr] {e}"
57
+
58
+ # ---------- MORE DEBUG ----------
59
+ print("DEBUG status:", r.status_code, file=sys.stderr)
60
+ print("DEBUG first 200 bytes:", r.text[:200], file=sys.stderr)
61
+ print("=" * 60, file=sys.stderr)
62
+ # -------------------------------
63
+
64
+ if r.status_code != 200:
65
+ return f"[API {r.status_code}] {r.text[:100]}"
66
+
67
+ try:
68
+ generated = r.json()[0]["generated_text"]
69
+ except Exception as e:
70
+ return f"[JSONErr] {e}"
71
+
72
+ return generated.split("### SQL")[-1].strip() or "[Empty SQL]"
73
+
74
+ # -------------------------------------------------
75
+ # 4. PIPELINE
76
+ # -------------------------------------------------
77
+ def run_pipeline(query: str):
78
+ t0, trace = time.time(), []
79
+ create_db_if_needed()
80
+
81
+ with open(SCHEMA_FILE) as f:
82
+ schema = f.read()
83
+ trace.append(("Schema", "loaded"))
84
+
85
+ sql = nlp_to_sql(query, schema)
86
+ trace.append(("LLM", sql))
87
+
88
+ try:
89
+ with sqlite3.connect(DB_PATH) as conn:
90
+ cur = conn.execute(sql)
91
+ rows = cur.fetchall()
92
+ cols = [d[0] for d in cur.description] if cur.description else []
93
+ result = {"columns": cols, "rows": rows}
94
+ trace.append(("Exec", f"{len(rows)} rows"))
95
+ except Exception as e:
96
+ result = {"error": str(e)}
97
+ trace.append(("Exec error", str(e)))
98
+
99
+ trace.append(("Time", f"{time.time() - t0:.2f}s"))
100
+ return sql, json.dumps(result, indent=2), "\n".join(f"{s}: {m}" for s, m in trace)
101
+
102
+ # -------------------------------------------------
103
+ # 5. UI
104
+ # -------------------------------------------------
105
+ with gr.Blocks(title="Debug HF Token & API") as demo:
106
+ gr.Markdown("### Debugging HF TOKEN → API (uses GPT-2)")
107
+ q = gr.Textbox(label="Question", placeholder="e.g., How many employees?")
108
+ with gr.Row():
109
+ sql_box = gr.Code(label="SQL / debug output")
110
+ res_box = gr.Code(label="Result / error")
111
+ trace_box = gr.Textbox(label="Trace")
112
+ btn = gr.Button("Run")
113
+ btn.click(run_pipeline, q, [sql_box, res_box, trace_box])
114
+
115
+ if __name__ == "__main__":
116
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==4.28.3
2
+ requests
3
+ sqlite-utils
4
+ python-dotenv
schema.sql ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ DROP TABLE IF EXISTS employees;
2
+ CREATE TABLE employees (
3
+ emp_id INTEGER PRIMARY KEY,
4
+ name TEXT,
5
+ department TEXT,
6
+ hire_date DATE,
7
+ salary INTEGER
8
+ );
9
+
10
+ INSERT INTO employees (name, department, hire_date, salary) VALUES
11
+ ('Alice', 'Sales', '2022-01-10', 95000),
12
+ ('Bob', 'Engineering','2023-03-14',115000),
13
+ ('Carlos', 'Finance', '2021-07-22',100000),
14
+ ('Dana', 'Engineering','2020-11-05',125000);