Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,53 @@
|
|
1 |
-
import torch
|
2 |
-
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
3 |
import gradio as gr
|
|
|
|
|
4 |
|
5 |
-
# Load model from local folder
|
6 |
-
|
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 |
-
|
|
|
|
|
13 |
model.eval()
|
14 |
|
15 |
-
|
16 |
-
|
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 |
-
|
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
submit.click(fn=generate_sql, inputs=[schema, instructions, user_query], outputs=output)
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
4 |
|
5 |
+
# Load tokenizer and model from local folder
|
6 |
+
model_path = "./"
|
|
|
|
|
|
|
|
|
7 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
8 |
+
|
9 |
+
tokenizer = T5Tokenizer.from_pretrained(model_path)
|
10 |
+
model = T5ForConditionalGeneration.from_pretrained(model_path).to(device)
|
11 |
model.eval()
|
12 |
|
13 |
+
def generate_sql(schema, user_query, date_info=""):
|
14 |
+
prompt = f"""Given the following database schema and requirements, generate a PostgreSQL query:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
{schema}
|
|
|
|
|
|
|
17 |
|
18 |
+
User Query: "{user_query}"
|
|
|
|
|
19 |
|
20 |
+
IMPORTANT REQUIREMENTS:
|
21 |
+
- Always filter by user_id = $1 for security
|
22 |
+
- The current year is 2025. You are working in this year!
|
23 |
+
- CRITICAL: Use ONLY the dates provided in the input parameters. Do NOT infer or change dates on your own!
|
24 |
+
- Single date provided: {date_info if date_info else '[none provided]'}
|
25 |
+
- If date range is provided, use DATE(created_at) BETWEEN 'startDate' AND 'endDate'
|
26 |
+
- If single date is provided, use DATE(created_at) = 'YYYY-MM-DD'
|
27 |
+
- NEVER use hardcoded years like 2024 - always use the provided dates exactly as given
|
28 |
+
- Generate ONLY simple SELECT, INSERT, UPDATE, DELETE statements.
|
29 |
+
- NO WITH clauses, NO CTEs, NO complex subqueries.
|
30 |
+
- Use RETURNING * for INSERT, UPDATE, DELETE when applicable.
|
31 |
|
32 |
+
SQL Query:"""
|
|
|
33 |
|
34 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, padding=True).to(device)
|
35 |
+
with torch.no_grad():
|
36 |
+
outputs = model.generate(**inputs, max_length=256)
|
37 |
+
generated_sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
38 |
+
return generated_sql
|
39 |
+
|
40 |
+
# Gradio UI
|
41 |
+
iface = gr.Interface(
|
42 |
+
fn=generate_sql,
|
43 |
+
inputs=[
|
44 |
+
gr.Textbox(label="Database Schema", lines=12, placeholder="CREATE TABLE ..."),
|
45 |
+
gr.Textbox(label="User Query", placeholder="How much did I spend on food last week?"),
|
46 |
+
gr.Textbox(label="Date Info (optional)", placeholder="2025-06-12 or 2025-06-01 to 2025-06-07")
|
47 |
+
],
|
48 |
+
outputs=gr.Textbox(label="Generated SQL Query"),
|
49 |
+
title="HISAB AI - Natural Language to SQL",
|
50 |
+
description="Enter your schema, user query and date (optional). Model will output SQL query."
|
51 |
+
)
|
52 |
+
|
53 |
+
iface.launch()
|