Spaces:
Sleeping
Sleeping
HusnaManakkot
commited on
Commit
•
b649083
1
Parent(s):
b8c4f28
Update app.py
Browse files
app.py
CHANGED
@@ -1,40 +1,41 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
from datasets import load_dataset
|
4 |
-
from difflib import get_close_matches
|
5 |
|
6 |
-
# Load the WikiSQL dataset
|
7 |
-
wikisql_dataset = load_dataset("wikisql", split='train
|
8 |
|
9 |
# Load tokenizer and model
|
10 |
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
11 |
model = AutoModelForSeq2SeqLM.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
12 |
|
13 |
-
def
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
17 |
|
18 |
def generate_sql_from_user_input(query):
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
23 |
|
24 |
-
#
|
25 |
for item in wikisql_dataset:
|
26 |
-
if item['
|
27 |
-
return
|
28 |
|
29 |
-
return
|
30 |
|
31 |
# Create a Gradio interface
|
32 |
interface = gr.Interface(
|
33 |
fn=generate_sql_from_user_input,
|
34 |
inputs=gr.Textbox(label="Enter your natural language query"),
|
35 |
-
outputs=[gr.Textbox(label="
|
36 |
title="NL to SQL with T5 using WikiSQL Dataset",
|
37 |
-
description="This model
|
38 |
)
|
39 |
|
40 |
# Launch the app
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
from datasets import load_dataset
|
|
|
4 |
|
5 |
+
# Load the WikiSQL dataset (only the table schemas are needed for validation)
|
6 |
+
wikisql_dataset = load_dataset("wikisql", split='train')
|
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 validate_sql_against_schema(sql_query, schema):
|
13 |
+
# This is a placeholder function. You need to implement the logic to validate
|
14 |
+
# the SQL query against the table schema. The validation can be as simple or as
|
15 |
+
# complex as you need, depending on the requirements.
|
16 |
+
return True # Assume the query is valid for now
|
17 |
|
18 |
def generate_sql_from_user_input(query):
|
19 |
+
# Generate SQL for the user's query
|
20 |
+
input_text = "translate English to SQL: " + query
|
21 |
+
inputs = tokenizer(input_text, return_tensors="pt", padding=True)
|
22 |
+
outputs = model.generate(**inputs, max_length=512)
|
23 |
+
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
24 |
|
25 |
+
# Validate the generated SQL query against the schemas in the dataset
|
26 |
for item in wikisql_dataset:
|
27 |
+
if validate_sql_against_schema(sql_query, item['sql']):
|
28 |
+
return query, sql_query
|
29 |
|
30 |
+
return query, "Generated SQL query is not consistent with the dataset."
|
31 |
|
32 |
# Create a Gradio interface
|
33 |
interface = gr.Interface(
|
34 |
fn=generate_sql_from_user_input,
|
35 |
inputs=gr.Textbox(label="Enter your natural language query"),
|
36 |
+
outputs=[gr.Textbox(label="Your Query"), gr.Textbox(label="Generated SQL Query")],
|
37 |
title="NL to SQL with T5 using WikiSQL Dataset",
|
38 |
+
description="This model generates an SQL query for your natural language input and validates it against the WikiSQL dataset."
|
39 |
)
|
40 |
|
41 |
# Launch the app
|