Omkar1872 commited on
Commit
a22c47e
Β·
verified Β·
1 Parent(s): f2d90f8

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +27 -13
  2. app.py +56 -0
  3. requirements.txt +5 -0
README.md CHANGED
@@ -1,13 +1,27 @@
1
- ---
2
- title: NLP SQL
3
- emoji: πŸ“š
4
- colorFrom: green
5
- colorTo: purple
6
- sdk: gradio
7
- sdk_version: 5.49.1
8
- app_file: app.py
9
- pinned: false
10
- license: apache-2.0
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 🧠 Natural Language β†’ SQL Generator App
2
+
3
+ This app converts **natural language questions** into **SQL queries** and runs them directly on your uploaded CSV data.
4
+
5
+ ## πŸš€ Features
6
+ - Upload any CSV file
7
+ - Ask questions in plain English
8
+ - Auto-generates SQL queries
9
+ - Executes SQL and shows the result instantly
10
+ - Runs 100% on Hugging Face β€” **no API keys required**
11
+
12
+ ## 🧩 Tech Stack
13
+ - Python 🐍
14
+ - Transformers (Hugging Face)
15
+ - Gradio (UI)
16
+ - SQLite + Pandas
17
+
18
+ ## 🧠 Model Used
19
+ [`mrm8488/t5-base-finetuned-wikiSQL`](https://huggingface.co/mrm8488/t5-base-finetuned-wikiSQL)
20
+
21
+ ## πŸͺ„ Example
22
+ **Input:**
23
+ > Show employees older than 25
24
+
25
+ **Output SQL:**
26
+ ```sql
27
+ SELECT * FROM data_table WHERE age > 25;
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
4
+ import sqlite3
5
+
6
+ # Load model and tokenizer
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 generate_sql_query(natural_language, data):
12
+ # Load uploaded CSV
13
+ df = pd.read_csv(data.name)
14
+
15
+ # Create in-memory SQLite DB
16
+ conn = sqlite3.connect(":memory:")
17
+ df.to_sql("data_table", conn, index=False, if_exists="replace")
18
+
19
+ # Create schema description
20
+ schema = ", ".join([f"{col}" for col in df.columns])
21
+
22
+ # Combine user query and schema
23
+ input_text = f"translate English to SQL: {natural_language} | table columns: {schema}"
24
+
25
+ # Generate SQL query
26
+ inputs = tokenizer(input_text, return_tensors="pt")
27
+ outputs = model.generate(**inputs, max_length=256)
28
+ sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
29
+
30
+ try:
31
+ # Execute the generated SQL query
32
+ result_df = pd.read_sql_query(sql_query, conn)
33
+ except Exception as e:
34
+ result_df = pd.DataFrame({"Error": [str(e)]})
35
+
36
+ conn.close()
37
+ return sql_query, result_df.head()
38
+
39
+ # Gradio UI
40
+ iface = gr.Interface(
41
+ fn=generate_sql_query,
42
+ inputs=[
43
+ gr.Textbox(label="Enter your question (Natural Language)", placeholder="e.g., Show customers with age > 30"),
44
+ gr.File(label="Upload CSV dataset")
45
+ ],
46
+ outputs=[
47
+ gr.Textbox(label="Generated SQL Query"),
48
+ gr.Dataframe(label="Query Result")
49
+ ],
50
+ title="🧠 Natural Language to SQL Generator",
51
+ description="Upload a CSV file and ask questions in plain English. The app converts them into SQL and shows the result.",
52
+ allow_flagging="never"
53
+ )
54
+
55
+ if __name__ == "__main__":
56
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ transformers==4.41.2
2
+ torch
3
+ gradio
4
+ pandas
5
+ sqlite3-binary