File size: 6,007 Bytes
798cb15
f9c3b3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dca809a
f9c3b3c
 
 
 
 
 
 
 
 
 
dca809a
 
f9c3b3c
 
 
 
 
 
 
 
 
 
dca809a
 
 
 
 
 
 
 
 
 
f9c3b3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dca809a
 
 
f9c3b3c
 
 
 
dca809a
f9c3b3c
 
 
 
 
dca809a
f9c3b3c
 
 
dca809a
 
 
f9c3b3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dca809a
f9c3b3c
 
dca809a
 
f9c3b3c
dca809a
 
f9c3b3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
---
license: mit
datasets:
- sirunchained/text-to-sql-dataset
language:
- en
base_model:
- google/gemma-3-270m
pipeline_tag: text-generation
library_name: transformers
tags:
- LoRA
- peft
- gemma
- trl
- sql
---

# Text-to-SQL Model v2

## πŸš€ Model Description

This is **Version 2** of the `sirunchained/text-to-sql-model`, fine-tuned from [google/gemma-3-270m-it](https://huggingface.co/google/gemma-3-270m-it) for Text-to-SQL generation.  
In this version, the model is **merged** with the LoRA adapter – you can load it directly with `pipeline()` (no PEFT required).

**Key improvements in v2:**

| Feature | v1 (LoRA Adapter) | v2 (Merged) |
|---------|-------------------|-------------|
| **Load method** | Required PEFT + base model | Direct `pipeline()` |
| **Model size** | ~10 MB (adapter only) | ~536 MB (full model) |
| **Inference speed** | Slower (requires adapter load) | Faster |
| **Ease of use** | Complex | Simple |
| **Performance** | 89.7% accuracy | βœ… Same |

---

## 🧠 Task

**Text-to-SQL Generation**  
Converts natural language questions into SQL queries. Supports:
- βœ… `SELECT` queries (with JOINs, aggregations, subqueries)
- βœ… `INSERT` operations
- βœ… `UPDATE` operations
- βœ… `DELETE` operations (currently weak at this)

---

## πŸ“Š Training Details

| Item | Value |
|------|-------|
| **Base Model** | `google/gemma-3-270m-it` |
| **Fine-tuning Method** | LoRA + 4-bit quantization (QLoRA) |
| **Framework** | `trl` (SFTTrainer) |
| **Dataset** | `sirunchained/text-to-sql-dataset` (4518 samples training, 200 validation, 200 test) |
| **Training Epochs** | 5 |
| **Batch Size** | 32 |
| **Learning Rate** | 5e-5 |
| **LoRA Rank (r)** | 8 |
| **LoRA Alpha** | 16 |
| **Optimizer** | AdamW (fused) |

---

## πŸ“ˆ Training Performance

| Epoch | Training Loss | Validation Loss | Mean Token Accuracy |
|-------|---------------|-----------------|---------------------|
| 1     | 0.800         | 0.700           | 83.8%               |
| 2     | 0.650         | 0.680           | 84.2%               |
| 3     | 0.500         | 0.650           | 84.6%               |
| 4     | 0.350         | **0.640**       | **85.1%**           |
| 5     | 0.550         | **0.640**       | 83.5%               |

> **Best validation loss** was achieved at **epoch 4 & 5** (`0.640`).  
> **Highest mean token accuracy** on validation was at **epoch 4** (`85.1%`).

---

## πŸ’» Quick Start

### Using Pipeline (Recommended)

```python
from transformers import pipeline

generator = pipeline(
    "text-generation",
    model="sirunchained/text-to-sql-model-v2",
    device=0  # or "cuda"
)

# Example with schema
prompt = """<start_of_turn>user
# Schema
customers(id, name, email, country)
# Text
Find customers from USA.<end_of_turn>
<start_of_turn>model
"""
result = generator(prompt, max_new_tokens=128)
print(result[0]["generated_text"])
```

### With Chat Template

```python
from transformers import pipeline

pipe = pipeline("text-generation", model="sirunchained/text-to-sql-model-v2")

messages = [
    {"role": "user", "content": "# Schema\ncustomers(id, name, email)\n\n# Text\nFind customers with gmail emails."}
]

outputs = pipe(
    pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True),
    max_new_tokens=128
)
print(outputs[0]["generated_text"])
```

---

## 🎯 Dataset

The model was trained on [sirunchained/text-to-sql-dataset](https://huggingface.co/datasets/sirunchained/text-to-sql-dataset):

| Split | Size |
|-------|------|
| Train | 4,518 samples |
| Validation | 200 samples |
| Test | 200 samples |

**Dataset format:**
- `text`: Natural language question
- `schema`: Optional database schema
- `query`: Target SQL query

---

## πŸ§ͺ Evaluation Results

**Test Set Performance (Epoch 5 model):**

| Metric | Value |
|--------|-------|
| **Test Loss** | 0.638 |
| **Mean Token Accuracy** | **0.830** |
| **Entropy** | 0.636 |

---

## πŸ“ Version History

| Version | Date | Description |
|---------|------|-------------|
| **v1** | 2026-07-22 | LoRA adapter only (not directly loadable with pipeline) |
| **v2** | **2026-07-23** | **Merged version – fully loadable with `pipeline()`** |

---

## πŸ› οΈ Training Configuration

```python
# LoRA Configuration
LoraConfig(
    r=8,
    lora_alpha=16,
    lora_dropout=0.05,
    bias="none",
    task_type=TaskType.CAUSAL_LM,
)

# Training Configuration
SFTConfig(
    num_train_epochs=5,
    per_device_train_batch_size=32,
    learning_rate=5e-5,
    lr_scheduler_type="constant",
    weight_decay=0.0,
    load_best_model_at_end=True,
    metric_for_best_model="mean_token_accuracy",
    greater_is_better=True,
)
```

---

## ⚠️ Important Notes

- This is a **small language model** (270M parameters) – works on T4 GPUs
- Provide schema **only when needed** – works with or without it
- For non-SQL requests, the model outputs `INVALID_QUERY` (trained with negative samples)
- The model handles INSERT, UPDATE, and DELETE queries correctly

---

## πŸ”— Links

- **Base Model**: [google/gemma-3-270m-it](https://huggingface.co/google/gemma-3-270m-it)
- **Dataset**: [sirunchained/text-to-sql-dataset](https://huggingface.co/datasets/sirunchained/text-to-sql-dataset)
- **v1 (Adapter)**: [sirunchained/text-to-sql-model](https://huggingface.co/sirunchained/text-to-sql-model-Ψ±Ϋ±)

---

## πŸ™ Acknowledgments

Built with:
- [Hugging Face Transformers](https://github.com/huggingface/transformers)
- [TRL (Transformer Reinforcement Learning)](https://github.com/huggingface/trl)
- [PEFT (Parameter-Efficient Fine-Tuning)](https://github.com/huggingface/peft)
- [bitsandbytes](https://github.com/TimDettmers/bitsandbytes)
- [Gradio](https://gradio.app/) for the demo interface which you can use [here](https://huggingface.co/spaces/sirunchained/text-to-sql-gradio)

---

## πŸ“„ License

This model is released under the same license as Google's Gemma model. See the [Gemma model card](https://huggingface.co/google/gemma-3-270m-it) for details.