Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -37,48 +37,6 @@ interface = gr.Interface(
|
|
37 |
description="This model finds the closest match in the WikiSQL dataset for your query and generates the corresponding SQL."
|
38 |
)
|
39 |
|
40 |
-
# Launch the app
|
41 |
-
if __name__ == "__main__":
|
42 |
-
interface.launch()
|
43 |
-
import gradio as gr
|
44 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
45 |
-
from datasets import load_dataset
|
46 |
-
from difflib import get_close_matches
|
47 |
-
|
48 |
-
# Load the WikiSQL dataset
|
49 |
-
wikisql_dataset = load_dataset("wikisql", split='train[:100]') # Increase the number of examples for better matching
|
50 |
-
|
51 |
-
# Load tokenizer and model
|
52 |
-
tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
53 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("mrm8488/t5-base-finetuned-wikiSQL")
|
54 |
-
|
55 |
-
def find_closest_match(query, dataset):
|
56 |
-
questions = [item['question'] for item in dataset]
|
57 |
-
matches = get_close_matches(query, questions, n=1)
|
58 |
-
return matches[0] if matches else None
|
59 |
-
|
60 |
-
def generate_sql_from_user_input(query):
|
61 |
-
# Find the closest match in the dataset
|
62 |
-
matched_query = find_closest_match(query, wikisql_dataset)
|
63 |
-
if not matched_query:
|
64 |
-
return "No close match found in the dataset.", ""
|
65 |
-
|
66 |
-
# Generate SQL for the matched query
|
67 |
-
input_text = "translate English to SQL: " + matched_query
|
68 |
-
inputs = tokenizer(input_text, return_tensors="pt", padding=True)
|
69 |
-
outputs = model.generate(**inputs, max_length=512)
|
70 |
-
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
71 |
-
return matched_query, sql_query
|
72 |
-
|
73 |
-
# Create a Gradio interface
|
74 |
-
interface = gr.Interface(
|
75 |
-
fn=generate_sql_from_user_input,
|
76 |
-
inputs=gr.Textbox(label="Enter your natural language query"),
|
77 |
-
outputs=[gr.Textbox(label="Matched Query from Dataset"), gr.Textbox(label="Generated SQL Query")],
|
78 |
-
title="NL to SQL with T5 using WikiSQL Dataset",
|
79 |
-
description="This model finds the closest match in the WikiSQL dataset for your query and generates the corresponding SQL."
|
80 |
-
)
|
81 |
-
|
82 |
# Launch the app
|
83 |
if __name__ == "__main__":
|
84 |
interface.launch()
|
|
|
37 |
description="This model finds the closest match in the WikiSQL dataset for your query and generates the corresponding SQL."
|
38 |
)
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# Launch the app
|
41 |
if __name__ == "__main__":
|
42 |
interface.launch()
|