Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files
app.py
CHANGED
|
@@ -1,15 +1,21 @@
|
|
| 1 |
-
import os,
|
| 2 |
import gradio as gr
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
load_dotenv()
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def run_pipeline(nl_query):
|
| 31 |
trace = []
|
| 32 |
start = time.time()
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
schema_ddl = f.read()
|
| 37 |
-
trace.append(("
|
| 38 |
|
| 39 |
-
# 2. NL ➜ SQL
|
| 40 |
sql_query = nlp_to_sql(nl_query, schema_ddl)
|
| 41 |
-
trace.append(("LLM
|
| 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 = [
|
| 49 |
result = {"columns": colnames, "rows": rows}
|
| 50 |
-
trace.append(("
|
| 51 |
except Exception as e:
|
| 52 |
result = {"error": str(e)}
|
| 53 |
trace.append(("Execution Error", str(e)))
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 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(
|
| 61 |
-
gr.Markdown("### NLP to SQL Query
|
| 62 |
-
|
| 63 |
with gr.Row():
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 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()
|