Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,35 +1,40 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
from datasets import load_dataset
|
|
|
4 |
|
5 |
-
# Load
|
6 |
-
|
7 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("hrshtsharma2012/NL2SQL-Picard-final")
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
11 |
|
12 |
-
|
13 |
-
|
|
|
|
|
14 |
|
15 |
-
def
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
return sql_query
|
21 |
|
22 |
-
#
|
23 |
-
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Create a Gradio interface
|
26 |
interface = gr.Interface(
|
27 |
-
fn=
|
28 |
-
inputs=gr.Textbox(
|
29 |
-
outputs="
|
30 |
-
|
31 |
-
|
32 |
-
description="This model converts natural language queries into SQL using the WikiSQL dataset. Try one of the example questions or enter your own!"
|
33 |
)
|
34 |
|
35 |
# Launch the app
|
|
|
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 Spider dataset
|
7 |
+
spider_dataset = load_dataset("spider", split='train[:100]') # Increase the number of examples for better matching
|
|
|
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 |
+
# Find the closest match in the dataset
|
20 |
+
matched_query = find_closest_match(query, spider_dataset)
|
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 matched_query, sql_query
|
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="Matched Query from Dataset"), gr.Textbox(label="Generated SQL Query")],
|
36 |
+
title="NL to SQL with T5 using Spider Dataset",
|
37 |
+
description="This model finds the closest match in the Spider dataset for your query and generates the corresponding SQL."
|
|
|
38 |
)
|
39 |
|
40 |
# Launch the app
|