Spaces:
Sleeping
Sleeping
HusnaManakkot
commited on
Commit
•
769f777
1
Parent(s):
0f2dfa7
Update app.py
Browse files
app.py
CHANGED
@@ -5,33 +5,32 @@ from datasets import load_dataset
|
|
5 |
# Load the Spider dataset
|
6 |
spider_dataset = load_dataset("spider", split='train') # Load a subset of the dataset
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# Load tokenizer and model
|
9 |
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
10 |
model = AutoModelForSeq2SeqLM.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
11 |
|
12 |
-
def post_process_sql_query(sql_query
|
13 |
# Modify the SQL query to match the dataset's schema
|
14 |
# This is just an example and might need to be adapted based on the dataset and model output
|
15 |
-
for table_name in
|
16 |
if "TABLE" in sql_query:
|
17 |
sql_query = sql_query.replace("TABLE", table_name)
|
18 |
break # Assuming only one table is referenced in the query
|
19 |
-
for column_name in
|
20 |
if "COLUMN" in sql_query:
|
21 |
-
sql_query = sql_query.replace("COLUMN", column_name
|
22 |
return sql_query
|
23 |
|
24 |
-
def generate_sql_from_user_input(query
|
25 |
-
# Find the corresponding database schema
|
26 |
-
db_schema = None
|
27 |
-
for item in spider_dataset:
|
28 |
-
if item['db_id'] == db_id:
|
29 |
-
db_schema = item
|
30 |
-
break
|
31 |
-
|
32 |
-
if db_schema is None:
|
33 |
-
return "Database schema not found for the given DB ID."
|
34 |
-
|
35 |
# Generate SQL for the user's query
|
36 |
input_text = "translate English to SQL: " + query
|
37 |
inputs = tokenizer(input_text, return_tensors="pt", padding=True)
|
@@ -39,13 +38,13 @@ def generate_sql_from_user_input(query, db_id):
|
|
39 |
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
40 |
|
41 |
# Post-process the SQL query to match the dataset's schema
|
42 |
-
sql_query = post_process_sql_query(sql_query
|
43 |
return sql_query
|
44 |
|
45 |
# Create a Gradio interface
|
46 |
interface = gr.Interface(
|
47 |
fn=generate_sql_from_user_input,
|
48 |
-
inputs=
|
49 |
outputs=gr.Textbox(label="Generated SQL Query"),
|
50 |
title="NL to SQL with T5 using Spider Dataset",
|
51 |
description="This model generates an SQL query for your natural language input based on the Spider dataset."
|
|
|
5 |
# Load the Spider dataset
|
6 |
spider_dataset = load_dataset("spider", split='train') # Load a subset of the dataset
|
7 |
|
8 |
+
# Extract schema information from the dataset
|
9 |
+
db_table_names = set()
|
10 |
+
column_names = set()
|
11 |
+
for item in spider_dataset:
|
12 |
+
for table in item['db']['table_names_original']:
|
13 |
+
db_table_names.add((item['db_id'], table))
|
14 |
+
for column in item['db']['column_names_original']:
|
15 |
+
column_names.add(column[1])
|
16 |
+
|
17 |
# Load tokenizer and model
|
18 |
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
19 |
model = AutoModelForSeq2SeqLM.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
20 |
|
21 |
+
def post_process_sql_query(sql_query):
|
22 |
# Modify the SQL query to match the dataset's schema
|
23 |
# This is just an example and might need to be adapted based on the dataset and model output
|
24 |
+
for db_id, table_name in db_table_names:
|
25 |
if "TABLE" in sql_query:
|
26 |
sql_query = sql_query.replace("TABLE", table_name)
|
27 |
break # Assuming only one table is referenced in the query
|
28 |
+
for column_name in column_names:
|
29 |
if "COLUMN" in sql_query:
|
30 |
+
sql_query = sql_query.replace("COLUMN", column_name, 1)
|
31 |
return sql_query
|
32 |
|
33 |
+
def generate_sql_from_user_input(query):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
# Generate SQL for the user's query
|
35 |
input_text = "translate English to SQL: " + query
|
36 |
inputs = tokenizer(input_text, return_tensors="pt", padding=True)
|
|
|
38 |
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
39 |
|
40 |
# Post-process the SQL query to match the dataset's schema
|
41 |
+
sql_query = post_process_sql_query(sql_query)
|
42 |
return sql_query
|
43 |
|
44 |
# Create a Gradio interface
|
45 |
interface = gr.Interface(
|
46 |
fn=generate_sql_from_user_input,
|
47 |
+
inputs=gr.Textbox(label="Enter your natural language query"),
|
48 |
outputs=gr.Textbox(label="Generated SQL Query"),
|
49 |
title="NL to SQL with T5 using Spider Dataset",
|
50 |
description="This model generates an SQL query for your natural language input based on the Spider dataset."
|