Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,42 +1,32 @@
|
|
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
|
7 |
-
|
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 find_closest_match(query, dataset):
|
14 |
-
questions = [item['question'] for item in dataset]
|
15 |
-
matches = get_close_matches(query, questions, n=1)
|
16 |
-
return matches[0] if matches else None
|
17 |
-
|
18 |
def generate_sql_from_user_input(query):
|
19 |
-
#
|
20 |
-
|
21 |
-
if not matched_query:
|
22 |
-
return "No close match found in the dataset.", ""
|
23 |
-
|
24 |
-
# Generate SQL for the matched query
|
25 |
-
input_text = "translate English to SQL: " + matched_query
|
26 |
inputs = tokenizer(input_text, return_tensors="pt", padding=True)
|
27 |
outputs = model.generate(**inputs, max_length=512)
|
28 |
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
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
|
37 |
-
description="This model
|
38 |
)
|
39 |
|
40 |
# Launch the app
|
41 |
if __name__ == "__main__":
|
42 |
interface.launch()
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
from datasets import load_dataset
|
|
|
4 |
|
5 |
+
# Load the WikiSQL dataset (optional, not used directly in SQL generation)
|
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 generate_sql_from_user_input(query):
|
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 query, 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="Your Query"), gr.Textbox(label="Generated SQL Query")],
|
25 |
+
title="NL to SQL with T5 using WikiSQL Dataset",
|
26 |
+
description="This model generates an SQL query for your natural language input."
|
27 |
)
|
28 |
|
29 |
# Launch the app
|
30 |
if __name__ == "__main__":
|
31 |
interface.launch()
|
32 |
+
|