Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,33 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
from datasets import load_dataset
|
4 |
-
import json
|
5 |
-
import os
|
6 |
|
7 |
# Load the Spider dataset
|
8 |
spider_dataset = load_dataset("spider", split='train') # Load a subset of the dataset
|
9 |
|
10 |
-
# Load the database schemas
|
11 |
-
db_schemas = {}
|
12 |
-
database_dir = 'path/to/database/folder'
|
13 |
-
for filename in os.listdir(database_dir):
|
14 |
-
if filename.endswith('.json'):
|
15 |
-
with open(os.path.join(database_dir, filename), 'r') as file:
|
16 |
-
db_schema = json.load(file)
|
17 |
-
db_schemas[db_schema['db_id']] = db_schema
|
18 |
-
|
19 |
# Load tokenizer and model
|
20 |
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
21 |
model = AutoModelForSeq2SeqLM.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
22 |
|
23 |
-
def
|
24 |
-
#
|
25 |
-
if
|
26 |
-
|
27 |
-
for table_name in db_schema['table_names_original']:
|
28 |
-
if "TABLE" in sql_query:
|
29 |
-
sql_query = sql_query.replace("TABLE", table_name)
|
30 |
-
break # Assuming only one table is referenced in the query
|
31 |
-
for column_name in db_schema['column_names_original']:
|
32 |
-
if "COLUMN" in sql_query:
|
33 |
-
sql_query = sql_query.replace("COLUMN", column_name[1], 1)
|
34 |
-
return sql_query
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
input_text = "translate English to SQL: " + query
|
39 |
inputs = tokenizer(input_text, return_tensors="pt", padding=True)
|
40 |
outputs = model.generate(**inputs, max_length=512)
|
41 |
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
42 |
-
|
43 |
-
# Post-process the SQL query to match the dataset's schema
|
44 |
-
sql_query = post_process_sql_query(sql_query, db_id)
|
45 |
-
return sql_query
|
46 |
|
47 |
# Create a Gradio interface
|
48 |
interface = gr.Interface(
|
49 |
-
fn=
|
50 |
-
inputs=
|
51 |
-
outputs=gr.Textbox(label="Generated SQL Query"),
|
52 |
title="NL to SQL using Spider Dataset",
|
53 |
-
description="This interface generates an SQL query from
|
54 |
)
|
55 |
|
56 |
# Launch the app
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
from datasets import load_dataset
|
|
|
|
|
4 |
|
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 generate_sql_from_spider_query(index):
|
13 |
+
# Get the natural language query from the Spider dataset
|
14 |
+
if index < 0 or index >= len(spider_dataset):
|
15 |
+
return "Invalid index. Please enter a value between 0 and {}.".format(len(spider_dataset) - 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
nl_query = spider_dataset[index]['question']
|
18 |
+
input_text = "translate English to SQL: " + nl_query
|
|
|
19 |
inputs = tokenizer(input_text, return_tensors="pt", padding=True)
|
20 |
outputs = model.generate(**inputs, max_length=512)
|
21 |
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
22 |
+
return {"Natural Language Query": nl_query, "Generated SQL Query": sql_query}
|
|
|
|
|
|
|
23 |
|
24 |
# Create a Gradio interface
|
25 |
interface = gr.Interface(
|
26 |
+
fn=generate_sql_from_spider_query,
|
27 |
+
inputs=gr.Number(label="Enter the index of the query in the Spider dataset (0 to {})".format(len(spider_dataset) - 1)),
|
28 |
+
outputs=[gr.Textbox(label="Natural Language Query"), gr.Textbox(label="Generated SQL Query")],
|
29 |
title="NL to SQL using Spider Dataset",
|
30 |
+
description="This interface generates an SQL query from a natural language query in the Spider dataset."
|
31 |
)
|
32 |
|
33 |
# Launch the app
|