Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,52 +3,36 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
| 3 |
from datasets import load_dataset
|
| 4 |
|
| 5 |
# Load the WikiSQL dataset
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# Extract schema information from the dataset
|
| 9 |
-
table_names = set()
|
| 10 |
-
column_names = set()
|
| 11 |
-
for item in wikisql_dataset:
|
| 12 |
-
table_names.add(item['table']['name'])
|
| 13 |
-
for column in item['table']['header']:
|
| 14 |
-
column_names.add(column)
|
| 15 |
|
| 16 |
# Load tokenizer and model
|
| 17 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
| 18 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("
|
| 19 |
|
| 20 |
-
def
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
sql_query = sql_query.replace("TABLE", table_name)
|
| 26 |
-
break # Assuming only one table is referenced in the query
|
| 27 |
-
for column_name in column_names:
|
| 28 |
-
if "COLUMN" in sql_query:
|
| 29 |
-
sql_query = sql_query.replace("COLUMN", column_name, 1)
|
| 30 |
-
return sql_query
|
| 31 |
|
| 32 |
-
def
|
| 33 |
-
#
|
| 34 |
input_text = "translate English to SQL: " + query
|
| 35 |
inputs = tokenizer(input_text, return_tensors="pt", padding=True)
|
|
|
|
|
|
|
| 36 |
outputs = model.generate(**inputs, max_length=512)
|
| 37 |
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 38 |
-
|
| 39 |
-
# Post-process the SQL query to match the dataset's schema
|
| 40 |
-
sql_query = post_process_sql_query(sql_query)
|
| 41 |
return sql_query
|
| 42 |
|
| 43 |
# Create a Gradio interface
|
| 44 |
interface = gr.Interface(
|
| 45 |
-
fn=
|
| 46 |
inputs=gr.Textbox(label="Enter your natural language query"),
|
| 47 |
outputs=gr.Textbox(label="Generated SQL Query"),
|
| 48 |
-
title="
|
| 49 |
-
description="
|
| 50 |
)
|
| 51 |
|
| 52 |
# Launch the app
|
| 53 |
-
|
| 54 |
-
interface.launch()
|
|
|
|
| 3 |
from datasets import load_dataset
|
| 4 |
|
| 5 |
# Load the WikiSQL dataset
|
| 6 |
+
dataset = load_dataset("wikisql", split='train[:1000]')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Load tokenizer and model
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained("t5-base")
|
| 10 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
|
| 11 |
|
| 12 |
+
def preprocess_data(dataset):
|
| 13 |
+
# Tokenize the questions and SQL queries
|
| 14 |
+
tokenized_questions = tokenizer(dataset['question'], padding=True, truncation=True, return_tensors="pt")
|
| 15 |
+
tokenized_sql = tokenizer(dataset['sql']['human_readable'], padding=True, truncation=True, return_tensors="pt")
|
| 16 |
+
return tokenized_questions, tokenized_sql
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
def generate_sql(query):
|
| 19 |
+
# Preprocess the input query
|
| 20 |
input_text = "translate English to SQL: " + query
|
| 21 |
inputs = tokenizer(input_text, return_tensors="pt", padding=True)
|
| 22 |
+
|
| 23 |
+
# Generate SQL query using the model
|
| 24 |
outputs = model.generate(**inputs, max_length=512)
|
| 25 |
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
| 26 |
return sql_query
|
| 27 |
|
| 28 |
# Create a Gradio interface
|
| 29 |
interface = gr.Interface(
|
| 30 |
+
fn=generate_sql,
|
| 31 |
inputs=gr.Textbox(label="Enter your natural language query"),
|
| 32 |
outputs=gr.Textbox(label="Generated SQL Query"),
|
| 33 |
+
title="Natural Language to SQL Prototype",
|
| 34 |
+
description="Enter a natural language query and get the corresponding SQL query."
|
| 35 |
)
|
| 36 |
|
| 37 |
# Launch the app
|
| 38 |
+
interface.launch()
|
|
|