Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,51 +2,53 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
# Function to handle JSON prompt for SQL generation
|
| 11 |
def generate_sql(payload):
|
|
|
|
| 12 |
question = payload.get("question", "")
|
| 13 |
schema = payload.get("schema", "")
|
| 14 |
sample_rows = payload.get("sample_rows", [])
|
| 15 |
|
| 16 |
sample_str = "\n".join([str(row) for row in sample_rows]) if sample_rows else ""
|
| 17 |
-
|
| 18 |
prompt = f"""
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
{sample_str}
|
| 24 |
-
Question: {question}
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 29 |
outputs = model.generate(
|
| 30 |
**inputs,
|
| 31 |
-
|
| 32 |
-
do_sample=
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
pad_token_id=tokenizer.eos_token_id
|
| 36 |
)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
return generated_sql
|
| 42 |
|
| 43 |
-
# Launch Gradio interface
|
| 44 |
demo = gr.Interface(
|
| 45 |
fn=generate_sql,
|
| 46 |
inputs=gr.JSON(label="Input JSON (question, schema, sample_rows)"),
|
| 47 |
outputs="text",
|
| 48 |
-
title="Text
|
| 49 |
-
description="
|
| 50 |
)
|
| 51 |
|
| 52 |
-
demo.launch()
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
model_path = "defog/sqlcoder-7b-2"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, torch_dtype=torch.float16, device_map="auto")
|
| 8 |
+
|
| 9 |
|
|
|
|
| 10 |
def generate_sql(payload):
|
| 11 |
+
|
| 12 |
question = payload.get("question", "")
|
| 13 |
schema = payload.get("schema", "")
|
| 14 |
sample_rows = payload.get("sample_rows", [])
|
| 15 |
|
| 16 |
sample_str = "\n".join([str(row) for row in sample_rows]) if sample_rows else ""
|
| 17 |
+
|
| 18 |
prompt = f"""
|
| 19 |
+
### Task
|
| 20 |
+
Generate a SQL query to answer [QUESTION]{question}[/QUESTION]
|
| 21 |
+
|
| 22 |
+
### Database Schema
|
| 23 |
+
The query will run on a database with the following schema:
|
| 24 |
+
{schema}
|
| 25 |
+
|
| 26 |
+
### Sample Rows
|
| 27 |
{sample_str}
|
|
|
|
| 28 |
|
| 29 |
+
### Answer
|
| 30 |
+
Given the database schema, here is the SQL query that [QUESTION]{question}[/QUESTION]
|
| 31 |
+
[SQL]
|
| 32 |
+
""".strip()
|
| 33 |
|
| 34 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 35 |
outputs = model.generate(
|
| 36 |
**inputs,
|
| 37 |
+
max_length=512,
|
| 38 |
+
do_sample=False,
|
| 39 |
+
num_beams=4,
|
| 40 |
+
early_stopping=True
|
|
|
|
| 41 |
)
|
| 42 |
+
sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 43 |
+
return sql.split("[SQL]")[-1].strip()
|
| 44 |
+
|
|
|
|
|
|
|
| 45 |
|
|
|
|
| 46 |
demo = gr.Interface(
|
| 47 |
fn=generate_sql,
|
| 48 |
inputs=gr.JSON(label="Input JSON (question, schema, sample_rows)"),
|
| 49 |
outputs="text",
|
| 50 |
+
title="SQLCoder - Text to SQL",
|
| 51 |
+
description="Enter a JSON object with 'question', 'schema', and optional 'sample_rows'. The model will generate SQL using Defog's sqlcoder-7b-2."
|
| 52 |
)
|
| 53 |
|
| 54 |
+
demo.launch()
|