Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
from datasets import load_dataset
|
4 |
|
5 |
-
# Load
|
6 |
-
tokenizer =
|
7 |
-
model =
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
examples.append([question])
|
18 |
-
|
19 |
-
def generate_sql(question):
|
20 |
-
# Format the question for the model if needed. For example:
|
21 |
-
input_text = f"translate English to SQL: {question}"
|
22 |
-
# input_text = f"{question}" # Directly use the question if the model is fine-tuned for SQL generation
|
23 |
-
|
24 |
-
# Tokenize the input text
|
25 |
-
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
26 |
-
|
27 |
-
# Generate the output sequence
|
28 |
-
output_ids = model.generate(input_ids, max_length=512, num_beams=5)[0]
|
29 |
-
|
30 |
-
# Decode the generated ids to get the SQL query
|
31 |
-
sql_query = tokenizer.decode(output_ids, skip_special_tokens=True)
|
32 |
return sql_query
|
33 |
|
34 |
-
|
35 |
-
|
36 |
-
iface = gr.Interface(
|
37 |
fn=generate_sql,
|
38 |
-
inputs=gr.Textbox(lines=2, placeholder="Enter your
|
39 |
-
outputs=
|
40 |
-
title="
|
41 |
-
description="This
|
42 |
-
examples=examples
|
43 |
)
|
44 |
|
45 |
# Launch the app
|
46 |
if __name__ == "__main__":
|
47 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
|
|
3 |
|
4 |
+
# Load tokenizer and model
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("hrshtsharma2012/NL2SQL-Picard-final")
|
6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("hrshtsharma2012/NL2SQL-Picard-final")
|
7 |
|
8 |
+
# Initialize the pipeline
|
9 |
+
nl2sql_pipeline = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
10 |
|
11 |
+
def generate_sql(query):
|
12 |
+
# Use the model to generate SQL from the natural language query
|
13 |
+
results = nl2sql_pipeline(query)
|
14 |
+
# Extract the first result (highest likelihood)
|
15 |
+
sql_query = results[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
return sql_query
|
17 |
|
18 |
+
# Create a Gradio interface
|
19 |
+
interface = gr.Interface(
|
|
|
20 |
fn=generate_sql,
|
21 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your natural language query here..."),
|
22 |
+
outputs="text",
|
23 |
+
title="NL to SQL with Picard",
|
24 |
+
description="This model converts natural language queries into SQL. It's based on the Spider dataset. Enter a query to get started!"
|
|
|
25 |
)
|
26 |
|
27 |
# Launch the app
|
28 |
if __name__ == "__main__":
|
29 |
+
interface.launch()
|