Spaces:
Running on Zero
Running on Zero
File size: 3,466 Bytes
ba2940e 3201dc9 ba2940e 2e54c95 821e19f 2e54c95 88f00dc 2e54c95 88f00dc 3201dc9 88f00dc 2e54c95 88f00dc 3201dc9 2e54c95 3201dc9 88f00dc ba2940e 3201dc9 88f00dc ba2940e 88f00dc ba2940e 88f00dc ba2940e 88f00dc ba2940e 88f00dc 3201dc9 ba2940e 2e54c95 ba2940e 88f00dc ba2940e 88f00dc ba2940e 88f00dc ba2940e 88f00dc ba2940e 2e54c95 ba2940e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | """
NL2SQL — live demo Space (ZeroGPU). Model is loaded lazily inside the GPU
function because ZeroGPU only attaches a GPU during an @spaces.GPU call.
"""
import spaces
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
BASE_MODEL = "Qwen/Qwen2.5-Coder-1.5B-Instruct"
ADAPTER_REPO = "JayaHareeswarReddy/nl2sql-qlora"
SYSTEM_PROMPT = (
"You are a text-to-SQL assistant. Given a database schema and a question, "
"respond with a single correct SQL query and nothing else."
)
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
_model = None
def _get_model():
global _model
if _model is None:
base = AutoModelForCausalLM.from_pretrained(
BASE_MODEL, torch_dtype=torch.float16, device_map="cuda"
)
m = PeftModel.from_pretrained(base, ADAPTER_REPO)
m.eval()
_model = m
return _model
@spaces.GPU(duration=120)
def generate_sql(schema: str, question: str):
schema = (schema or "").strip()
question = (question or "").strip()
if not schema or not question:
return "-- Please provide both a schema and a question."
model = _get_model()
messages = [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"Schema:\n{schema}\n\nQuestion: {question}"},
]
prompt = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
with torch.no_grad():
out = model.generate(
**inputs,
max_new_tokens=256,
do_sample=False,
pad_token_id=tokenizer.eos_token_id,
)
text = tokenizer.decode(
out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True
)
return text.strip()
EXAMPLE_SCHEMA = (
"CREATE TABLE employees (id INT, name TEXT, salary INT, department TEXT);\n"
"CREATE TABLE departments (name TEXT, budget INT);"
)
EXAMPLES = [
[EXAMPLE_SCHEMA, "List the names of employees in Sales earning more than 50000."],
[EXAMPLE_SCHEMA, "What is the average salary per department?"],
[EXAMPLE_SCHEMA, "How many employees are in each department, highest first?"],
]
with gr.Blocks(title="NL2SQL", theme=gr.themes.Soft()) as demo:
gr.Markdown(
"""
# 🧮 NL2SQL — Natural Language to SQL
A **Qwen2.5-Coder-1.5B** model fine-tuned with **QLoRA** to turn an
English question and a table schema into a SQL query.
*(First query loads the model and may take ~30s; later ones are fast.)*
"""
)
with gr.Row():
with gr.Column():
schema_in = gr.Textbox(
label="Database schema (CREATE TABLE ...)",
lines=6,
value=EXAMPLE_SCHEMA,
)
question_in = gr.Textbox(label="Question", lines=2)
btn = gr.Button("Generate SQL", variant="primary")
with gr.Column():
sql_out = gr.Code(label="Generated SQL", language="sql")
btn.click(generate_sql, [schema_in, question_in], sql_out)
gr.Examples(EXAMPLES, inputs=[schema_in, question_in])
gr.Markdown(
"<sub>Base: Qwen2.5-Coder-1.5B-Instruct · Fine-tuned with QLoRA on "
"gretelai/synthetic_text_to_sql · Built with Gradio, PEFT & 🤗 Transformers</sub>"
)
if __name__ == "__main__":
demo.launch()
|