Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
from datasets import load_dataset
|
|
|
|
| 4 |
|
| 5 |
# Load the WikiSQL dataset
|
| 6 |
-
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return tokenized_questions, tokenized_sql
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 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=
|
| 31 |
inputs=gr.Textbox(label="Enter your natural language query"),
|
| 32 |
-
outputs=gr.Textbox(label="
|
| 33 |
-
title="
|
| 34 |
-
description="Enter a natural language query and get the corresponding SQL query."
|
| 35 |
)
|
| 36 |
|
| 37 |
# Launch the app
|
| 38 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from datasets import load_dataset
|
| 3 |
+
from difflib import get_close_matches
|
| 4 |
|
| 5 |
# Load the WikiSQL dataset
|
| 6 |
+
wikisql_dataset = load_dataset("wikisql", split='train[:100]')
|
| 7 |
|
| 8 |
+
# Create a mapping between natural language queries and SQL queries
|
| 9 |
+
query_sql_mapping = {item['question']: item['sql']['human_readable'] for item in wikisql_dataset}
|
|
|
|
| 10 |
|
| 11 |
+
def find_closest_match(query, dataset):
|
| 12 |
+
questions = [item['question'] for item in dataset]
|
| 13 |
+
matches = get_close_matches(query, questions, n=1)
|
| 14 |
+
return matches[0] if matches else None
|
|
|
|
| 15 |
|
| 16 |
+
def generate_sql_from_user_input(query):
|
| 17 |
+
# Find the closest match in the dataset
|
| 18 |
+
matched_query = find_closest_match(query, wikisql_dataset)
|
| 19 |
+
if not matched_query:
|
| 20 |
+
return "No close match found in the dataset."
|
| 21 |
|
| 22 |
+
# Retrieve the corresponding SQL query from the dataset
|
| 23 |
+
sql_query = query_sql_mapping.get(matched_query, "SQL query not found.")
|
|
|
|
| 24 |
return sql_query
|
| 25 |
|
| 26 |
# Create a Gradio interface
|
| 27 |
interface = gr.Interface(
|
| 28 |
+
fn=generate_sql_from_user_input,
|
| 29 |
inputs=gr.Textbox(label="Enter your natural language query"),
|
| 30 |
+
outputs=gr.Textbox(label="SQL Query from Dataset"),
|
| 31 |
+
title="NL to SQL using WikiSQL Dataset",
|
| 32 |
+
description="Enter a natural language query and get the corresponding SQL query from the WikiSQL dataset."
|
| 33 |
)
|
| 34 |
|
| 35 |
# Launch the app
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
interface.launch()
|