Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,49 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return sql_query
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
gr.Textbox(label="Query Result")
|
| 51 |
-
],
|
| 52 |
-
title="Natural Language to SQL",
|
| 53 |
-
description="Upload a CSV dataset and ask questions in English. SQL queries and results will be generated automatically."
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
if __name__ == "__main__":
|
| 57 |
-
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 4 |
+
|
| 5 |
+
# Load NL2SQL model from Hugging Face (no API key needed)
|
| 6 |
+
model_name = "PaulGan1/t5-small-spider-sql"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Global dataframe
|
| 11 |
+
df = None
|
| 12 |
+
|
| 13 |
+
def load_data(file):
|
| 14 |
+
global df
|
| 15 |
+
df = pd.read_csv(file.name)
|
| 16 |
+
return f"✅ Data uploaded successfully! Columns: {', '.join(df.columns)}"
|
| 17 |
+
|
| 18 |
+
def generate_sql(question):
|
| 19 |
+
if df is None:
|
| 20 |
+
return "⚠️ Please upload a CSV file first."
|
| 21 |
+
|
| 22 |
+
# Create prompt
|
| 23 |
+
columns = ", ".join(df.columns)
|
| 24 |
+
prompt = f"translate English to SQL: {question} | table: {columns}"
|
| 25 |
+
|
| 26 |
+
# Generate SQL
|
| 27 |
+
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
| 28 |
+
outputs = model.generate(**inputs, max_length=128)
|
| 29 |
+
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
+
|
| 31 |
+
# Execute SQL
|
| 32 |
+
try:
|
| 33 |
+
result = pd.read_sql_query(sql_query, con=df)
|
| 34 |
+
return f"🧠 SQL Query:\n{sql_query}\n\n📊 Result:\n{result.head()}"
|
| 35 |
+
except Exception:
|
| 36 |
+
return f"🧠 SQL Query:\n{sql_query}\n\n⚠️ Unable to execute SQL. (Demo only)"
|
| 37 |
+
|
| 38 |
+
# Gradio interface
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("## 🧠 Natural Language to SQL Query Generator")
|
| 41 |
+
file_input = gr.File(label="Upload your CSV file")
|
| 42 |
+
upload_output = gr.Textbox(label="Upload Status")
|
| 43 |
+
question = gr.Textbox(label="Ask your question in natural language:")
|
| 44 |
+
sql_output = gr.Textbox(label="Generated SQL Query & Output", lines=10)
|
| 45 |
+
|
| 46 |
+
file_input.change(load_data, inputs=file_input, outputs=upload_output)
|
| 47 |
+
question.submit(generate_sql, inputs=question, outputs=sql_output)
|
| 48 |
+
|
| 49 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|