Views
No views yet
| Metric | Value |
|---|---|
| Validation Loss (Initial) | 5.210 |
| Validation Loss (Final) | 0.061 |
| Loss Reduction | 98.9% |
| Dummy DB Test Accuracy | 100% (4/4) |
| Training Time | ~13 hours (RTX 3060) |
pip install transformers peft torch bitsandbytes1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel
3import torch
4
5# Load base model with 4-bit quantization
6base_model = AutoModelForCausalLM.from_pretrained(
7 "Qwen/Qwen2.5-Coder-3B-Instruct",
8 device_map="auto",
9 trust_remote_code=True,
10 torch_dtype=torch.float16,
11 load_in_4bit=True
12)
13
14# Load LoRA adapter
15model = PeftModel.from_pretrained(
16 base_model,
17 "YOUR_USERNAME/qwen-text-to-sql-lora"
18)
19
20# Load tokenizer
21tokenizer = AutoTokenizer.from_pretrained(
22 "Qwen/Qwen2.5-Coder-3B-Instruct",
23 trust_remote_code=True
24)1def generate_sql(question: str, schema: str) -> str:
2 """Generate SQL from natural language question."""
3 prompt = f"""You are an expert SQL generator. Given a database schema and a question, generate a SQL query.
4
5Database Schema:
6{schema}
7
8Question: {question}
9
10SQL Query:"""
11
12 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
13
14 outputs = model.generate(
15 **inputs,
16 max_new_tokens=1024,
17 temperature=0.1,
18 top_p=0.9,
19 do_sample=True,
20 pad_token_id=tokenizer.eos_token_id
21 )
22
23 sql = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
24 return sql.strip()
25
26# Example usage
27schema = """
28CREATE TABLE customers (
29 customer_id INT PRIMARY KEY,
30 name TEXT,
31 email TEXT,
32 signup_date DATE
33);
34
35CREATE TABLE orders (
36 order_id INT PRIMARY KEY,
37 customer_id INT,
38 order_date DATE,
39 total_amount DECIMAL,
40 FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
41);
42"""
43
44question = "What is the total revenue by month in 2024?"
45
46sql = generate_sql(question, schema)
47print(f"Generated SQL:\n{sql}")1SELECT
2 strftime('%Y-%m', order_date) AS month,
3 SUM(total_amount) AS total_revenue
4FROM orders
5WHERE strftime('%Y', order_date) = '2024'
6GROUP BY month
7ORDER BY month;1# LoRA Configuration
2lora_config = LoraConfig(
3 r=16, # Rank
4 lora_alpha=32, # Scaling factor
5 target_modules=[
6 "q_proj", "k_proj", "v_proj", "o_proj"
7 ],
8 lora_dropout=0.05,
9 bias="none",
10 task_type="CAUSAL_LM"
11)
12
13# Training Arguments
14training_args = TrainingArguments(
15 num_train_epochs=3,
16 per_device_train_batch_size=8,
17 gradient_accumulation_steps=4,
18 learning_rate=2e-4,
19 fp16=True,
20 optim="paged_adamw_8bit",
21 lr_scheduler_type="cosine",
22 warmup_ratio=0.1,
23)1@software{qwen_text_to_sql_lora_2026,
2 author = Jireh Fessenden,
3 title = {Qwen2.5-Coder-3B Text-to-SQL LoRA Adapter},
4 year = {2026},
5 url = {https://huggingface.co/YOUR_USERNAME/qwen-text-to-sql-lora},
6 note = {LoRA adapter for Text-to-SQL generation}
7}1@article{qwen2.5coder,
2 title={Qwen2.5-Coder Technical Report},
3 author={Hui, Binyuan and others},
4 year={2024}
5}
6
7@article{li2024bird,
8 title={Can LLM Already Serve as a Database Interface?},
9 author={Li, Jinyang and others},
10 journal={arXiv preprint arXiv:2305.03111},
11 year={2024}
12}