Spaces:
Sleeping
Sleeping
HusnaManakkot
commited on
Commit
•
7cbc7f5
1
Parent(s):
f1efe67
Update app.py
Browse files
app.py
CHANGED
@@ -1,52 +1,35 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
from datasets import load_dataset
|
4 |
|
5 |
-
# Load the WikiSQL dataset
|
6 |
-
wikisql_dataset = load_dataset("wikisql", split='train[:100]') # Load a subset of the dataset
|
7 |
-
|
8 |
-
# Extract schema information from the dataset
|
9 |
-
table_names = set()
|
10 |
-
column_names = set()
|
11 |
-
for item in wikisql_dataset:
|
12 |
-
table_names.add(item['table']['name'])
|
13 |
-
for column in item['table']['header']:
|
14 |
-
column_names.add(column)
|
15 |
-
|
16 |
# Load tokenizer and model
|
17 |
-
tokenizer = AutoTokenizer.from_pretrained("
|
18 |
-
model = AutoModelForSeq2SeqLM.from_pretrained("
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
# This is just an example and might need to be adapted based on the dataset and model output
|
23 |
-
for table_name in table_names:
|
24 |
-
if "TABLE" in sql_query:
|
25 |
-
sql_query = sql_query.replace("TABLE", table_name)
|
26 |
-
break # Assuming only one table is referenced in the query
|
27 |
-
for column_name in column_names:
|
28 |
-
if "COLUMN" in sql_query:
|
29 |
-
sql_query = sql_query.replace("COLUMN", column_name, 1)
|
30 |
-
return sql_query
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
input_text = "translate English to SQL: " + query
|
35 |
-
inputs = tokenizer(input_text, return_tensors="pt", padding=True)
|
36 |
-
outputs = model.generate(**inputs, max_length=512)
|
37 |
-
sql_query = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
return sql_query
|
42 |
|
|
|
|
|
|
|
43 |
# Create a Gradio interface
|
44 |
interface = gr.Interface(
|
45 |
-
fn=
|
46 |
-
inputs=gr.Textbox(
|
47 |
-
outputs=
|
48 |
-
|
49 |
-
|
|
|
50 |
)
|
51 |
|
52 |
# Launch the app
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
3 |
from datasets import load_dataset
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
# Load tokenizer and model
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("hrshtsharma2012/NL2SQL-Picard-final")
|
7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("hrshtsharma2012/NL2SQL-Picard-final")
|
8 |
|
9 |
+
# Initialize the pipeline
|
10 |
+
nl2sql_pipeline = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Load a part of the WikiSQL dataset
|
13 |
+
wikisql_dataset = load_dataset("wikisql", split='train[:5]')
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
def generate_sql(query):
|
16 |
+
results = nl2sql_pipeline(query)
|
17 |
+
sql_query = results[0]['generated_text']
|
18 |
+
# Post-process the output to ensure it's a valid SQL query
|
19 |
+
sql_query = sql_query.replace('<pad>', '').replace('</s>', '').strip()
|
20 |
return sql_query
|
21 |
|
22 |
+
# Use examples from the WikiSQL dataset
|
23 |
+
example_questions = [(question['question'],) for question in wikisql_dataset]
|
24 |
+
|
25 |
# Create a Gradio interface
|
26 |
interface = gr.Interface(
|
27 |
+
fn=generate_sql,
|
28 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter your natural language query here..."),
|
29 |
+
outputs="text",
|
30 |
+
examples=example_questions,
|
31 |
+
title="NL to SQL with Picard",
|
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
|