Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,28 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
-
import sqlite3
|
| 5 |
-
|
| 6 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 7 |
|
|
|
|
| 8 |
model_name = "mrm8488/t5-base-finetuned-wikiSQL"
|
| 9 |
|
| 10 |
-
# Force slow tokenizer
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
|
| 12 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return f"Error reading CSV: {e}", pd.DataFrame()
|
| 20 |
-
|
| 21 |
-
# Create SQLite DB
|
| 22 |
-
conn = sqlite3.connect(":memory:")
|
| 23 |
-
df.to_sql("data_table", conn, index=False, if_exists="replace")
|
| 24 |
-
|
| 25 |
-
schema = ", ".join(df.columns)
|
| 26 |
-
text = f"translate English to SQL: {question} | table columns: {schema}"
|
| 27 |
-
|
| 28 |
-
# Tokenize with slow tokenizer
|
| 29 |
-
inputs = tokenizer(text, return_tensors="pt")
|
| 30 |
-
outputs = model.generate(**inputs, max_length=256)
|
| 31 |
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
gr.Textbox(label="Ask your question (Natural Language)", placeholder="e.g., Show customers older than 30"),
|
| 45 |
-
gr.File(label="Upload your CSV file")
|
| 46 |
-
],
|
| 47 |
-
outputs=[
|
| 48 |
-
gr.Textbox(label="Generated SQL Query"),
|
| 49 |
-
gr.Dataframe(label="Result Preview")
|
| 50 |
-
],
|
| 51 |
-
title="🧠 NL to SQL Generator",
|
| 52 |
-
description="Upload a CSV and ask questions in plain English. Generates SQL and shows results."
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
if __name__ == "__main__":
|
| 56 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
|
| 4 |
+
# Public model fine-tuned on WikiSQL
|
| 5 |
model_name = "mrm8488/t5-base-finetuned-wikiSQL"
|
| 6 |
|
| 7 |
+
# Force slow tokenizer to avoid tiktoken issues
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
|
| 9 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 10 |
|
| 11 |
+
def nl_to_sql(nl_query):
|
| 12 |
+
# Add prefix required for this model
|
| 13 |
+
input_text = "translate English to SQL: " + nl_query
|
| 14 |
+
inputs = tokenizer.encode(input_text, return_tensors="pt")
|
| 15 |
+
outputs = model.generate(inputs, max_length=512)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 17 |
+
return sql_query
|
| 18 |
+
|
| 19 |
+
# Gradio UI
|
| 20 |
+
with gr.Blocks() as demo:
|
| 21 |
+
gr.Markdown("## 🧠 Natural Language to SQL Generator")
|
| 22 |
+
with gr.Row():
|
| 23 |
+
nl_input = gr.Textbox(label="Enter your query in English")
|
| 24 |
+
sql_output = gr.Textbox(label="Generated SQL")
|
| 25 |
+
btn = gr.Button("Generate SQL")
|
| 26 |
+
btn.click(nl_to_sql, inputs=nl_input, outputs=sql_output)
|
| 27 |
+
|
| 28 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|