Spaces:
Sleeping
Sleeping
HusnaManakkot
commited on
Commit
•
0f2dfa7
1
Parent(s):
0e64ed5
Update app.py
Browse files
app.py
CHANGED
@@ -9,18 +9,43 @@ spider_dataset = load_dataset("spider", split='train') # Load a subset of the d
|
|
9 |
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
10 |
model = AutoModelForSeq2SeqLM.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
11 |
|
12 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
# Generate SQL for the user's query
|
14 |
input_text = "translate English to SQL: " + query
|
15 |
inputs = tokenizer(input_text, return_tensors="pt", padding=True)
|
16 |
outputs = model.generate(**inputs, max_length=512)
|
17 |
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
18 |
return sql_query
|
19 |
|
20 |
# Create a Gradio interface
|
21 |
interface = gr.Interface(
|
22 |
fn=generate_sql_from_user_input,
|
23 |
-
inputs=gr.Textbox(label="Enter your natural language query"),
|
24 |
outputs=gr.Textbox(label="Generated SQL Query"),
|
25 |
title="NL to SQL with T5 using Spider Dataset",
|
26 |
description="This model generates an SQL query for your natural language input based on the Spider dataset."
|
|
|
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, db_schema):
|
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 db_schema['table_names']:
|
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 db_schema['column_names']:
|
20 |
+
if "COLUMN" in sql_query:
|
21 |
+
sql_query = sql_query.replace("COLUMN", column_name[1], 1)
|
22 |
+
return sql_query
|
23 |
+
|
24 |
+
def generate_sql_from_user_input(query, db_id):
|
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)
|
38 |
outputs = model.generate(**inputs, max_length=512)
|
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, db_schema)
|
43 |
return sql_query
|
44 |
|
45 |
# Create a Gradio interface
|
46 |
interface = gr.Interface(
|
47 |
fn=generate_sql_from_user_input,
|
48 |
+
inputs=[gr.Textbox(label="Enter your natural language query"), gr.Textbox(label="Enter DB ID")],
|
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."
|