Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,39 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Load model and tokenizer from local files
|
| 6 |
-
model_path = "./"
|
| 7 |
-
tokenizer = T5Tokenizer.from_pretrained(model_path)
|
| 8 |
-
model = T5ForConditionalGeneration.from_pretrained(model_path)
|
| 9 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
-
model
|
| 11 |
model.eval()
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
with torch.no_grad():
|
| 16 |
outputs = model.generate(**inputs, max_length=512)
|
|
|
|
| 17 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Load model from local folder
|
| 6 |
+
model_dir = "./"
|
| 7 |
+
|
| 8 |
+
tokenizer = T5Tokenizer.from_pretrained(model_dir)
|
| 9 |
+
model = T5ForConditionalGeneration.from_pretrained(model_dir)
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
+
model.to(device)
|
| 13 |
model.eval()
|
| 14 |
|
| 15 |
+
# Inference function
|
| 16 |
+
def generate_sql(schema, instructions, user_query):
|
| 17 |
+
combined_input = f"{instructions.strip()}\n\n{schema.strip()}\n\nUser Query: \"{user_query.strip()}\"\n\nSQL Query:"
|
| 18 |
+
inputs = tokenizer(combined_input, padding=True, truncation=True, return_tensors="pt").to(device)
|
| 19 |
+
|
| 20 |
with torch.no_grad():
|
| 21 |
outputs = model.generate(**inputs, max_length=512)
|
| 22 |
+
|
| 23 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
|
| 25 |
+
# UI layout
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("# 🧠 Text-to-SQL Generator")
|
| 28 |
+
gr.Markdown("Enter the **schema**, **prompt/instructions**, and a **user query** to get the SQL output.")
|
| 29 |
+
|
| 30 |
+
schema = gr.Textbox(label="Database Schema", lines=10, placeholder="CREATE TABLE students (...) ...")
|
| 31 |
+
instructions = gr.Textbox(label="SQL Instructions / Prompt", lines=15, placeholder="Explain how to generate SQL queries...")
|
| 32 |
+
user_query = gr.Textbox(label="User Query", placeholder="e.g., Show me students who never attended class")
|
| 33 |
+
|
| 34 |
+
output = gr.Textbox(label="Generated SQL Query")
|
| 35 |
+
|
| 36 |
+
submit = gr.Button("Generate SQL")
|
| 37 |
+
submit.click(fn=generate_sql, inputs=[schema, instructions, user_query], outputs=output)
|
| 38 |
+
|
| 39 |
+
demo.launch()
|