Omkar1872 commited on
Commit
ed20f1d
·
verified ·
1 Parent(s): ac1d120

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -57
app.py CHANGED
@@ -1,57 +1,49 @@
1
- import gradio as gr
2
- import pandas as pd
3
- import sqlite3
4
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
-
6
- # Load NL2SQL model
7
- MODEL_NAME = "mrm8488/t5-base-finetuned-wikiSQL"
8
- tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
- model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
10
-
11
- def nl_to_sql(nl_query, uploaded_file):
12
- try:
13
- # Load CSV
14
- df = pd.read_csv(uploaded_file.name)
15
- table_name = "user_table"
16
-
17
- # Save DataFrame to SQLite
18
- conn = sqlite3.connect(":memory:")
19
- df.to_sql(table_name, conn, index=False, if_exists='replace')
20
-
21
- # Generate SQL
22
- input_ids = tokenizer(nl_query, return_tensors="pt").input_ids
23
- outputs = model.generate(input_ids)
24
- sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
25
-
26
- # Adjust SQL to use uploaded table name if needed
27
- sql_query = sql_query.replace("table", table_name)
28
-
29
- # Execute SQL safely
30
- try:
31
- result_df = pd.read_sql_query(sql_query, conn)
32
- result = result_df.to_string(index=False)
33
- except Exception as e:
34
- result = f"SQL execution failed:\n{e}\n\nGenerated SQL:\n{sql_query}"
35
-
36
- return sql_query, result
37
-
38
- except Exception as e:
39
- return "", f"Error processing file: {e}"
40
-
41
- # Gradio UI
42
- iface = gr.Interface(
43
- fn=nl_to_sql,
44
- inputs=[
45
- gr.Textbox(label="Enter your question in natural language"),
46
- gr.File(label="Upload CSV", type="file")
47
- ],
48
- outputs=[
49
- gr.Textbox(label="Generated SQL Query"),
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()