File size: 7,035 Bytes
3f65026
df80a2b
 
 
 
 
 
 
 
 
 
3f65026
6ed4adf
 
df80a2b
3f65026
df80a2b
3f65026
df80a2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed4adf
df80a2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed4adf
df80a2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed4adf
df80a2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed4adf
df80a2b
 
 
6ed4adf
df80a2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ed4adf
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
---
license: mit
datasets:
- gretelai/synthetic_text_to_sql
language:
- en
base_model:
- google/gemma-3-1b-it
pipeline_tag: reinforcement-learning
tags:
- text-to-sql
---
library: gemma3-text-to-sql
---
# Gemma 3 Text-to-SQL

A powerful LoRA-fine-tuned adapter for Gemma 3 that converts natural language questions into SQL queries with high accuracy and contextual understanding.

[![Hugging Face](https://img.shields.io/badge/%F0%9F%A4%97%20Hugging%20Face-Spaces-blue)](https://huggingface.co/spaces)
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0)

## Overview

This model is a specialized adapter built on top of Gemma 3 27B that has been fine-tuned to bridge the gap between natural language and SQL. It allows users to describe their data queries in plain English and receive accurate SQL code in return.

Key capabilities:
- Converts natural language questions to SQL queries
- Understands database schema context when provided
- Generates clean, optimized SQL with proper table joins
- Handles complex queries including aggregations, filters, and sorting

## Model Details

- **Base Model**: [lmstudio-community/gemma-3-27b-it-GGUF](https://huggingface.co/lmstudio-community/gemma-3-27b-it-GGUF)
- **Fine-tuning Method**: LoRA (Low-Rank Adaptation)
- **LoRA Configuration**:
  - Rank: 16
  - Alpha: 16
  - Dropout: 0.05
  - Target Modules: q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj
- **Training Data**: Synthetic and curated text-to-SQL datasets
- **Model Size**: Base (27B parameters) + Adapter (~70MB)
- **Format**: SafeTensors

## Usage

### Using Transformers Library

```python
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

# Load base model and tokenizer
model_id = "lmstudio-community/gemma-3-27b-it-GGUF"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)

# Load adapter
adapter_path = "parole-study-viper/gemma-3-text-to-sql"  # Replace with your HF model path
model = PeftModel.from_pretrained(model, adapter_path)

# Format prompt
question = "Find all customers who made a purchase over $1000 in the last month"
prompt = f"Convert the following natural language query to SQL: {question}"

# Generate response
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
    inputs.input_ids,
    max_new_tokens=200,
    temperature=0.7,
    do_sample=True
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(response)
```

### Using MLX (Apple Silicon)

For Apple Silicon users, you can use MLX for efficient inference:

```python
import mlx.core as mx
from mlx_lm.utils import get_model_path

# Setup paths
model_path = "lmstudio-community/gemma-3-27b-it-GGUF"
adapter_path = "parole-study-viper/gemma-3-text-to-sql/adapter_model.safetensors"

# Run generation
prompt = "Convert the following natural language query to SQL: Find all customers in New York"
command = f"""
python -m mlx_lm.generate \\
    --model {model_path} \\
    --adapter {adapter_path} \\
    --prompt "{prompt}" \\
    --max-tokens 200 \\
    --temp 0.7
"""
```

### API Usage with Inference Endpoint

You can also use the Hugging Face Inference API:

```python
import requests

API_URL = "https://api-inference.huggingface.co/models/parole-study-viper/gemma-3-text-to-sql"
headers = {"Authorization": f"Bearer {API_TOKEN}"}

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()
    
output = query({
    "inputs": "Convert to SQL: List all customers who placed orders in the last 30 days",
    "parameters": {"max_new_tokens": 200, "temperature": 0.7}
})
```

## Example Inputs and Outputs

### Example 1: Basic Query

**Input:**
```
Find all customers in New York
```

**Output:**
```sql
SELECT * 
FROM customers 
WHERE state = 'NY' OR city = 'New York';
```

### Example 2: Complex Query with Joins

**Input:**
```
List the top 5 products by revenue in the last quarter
```

**Output:**
```sql
SELECT p.product_id, p.product_name, SUM(oi.quantity * oi.unit_price) as revenue
FROM products p
JOIN order_items oi ON p.product_id = oi.product_id
JOIN orders o ON oi.order_id = o.order_id
WHERE o.order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 3 MONTH)
GROUP BY p.product_id, p.product_name
ORDER BY revenue DESC
LIMIT 5;
```

### Example 3: With Schema Context

**Input:**
```
Schema:
CREATE TABLE employees (
  employee_id INT PRIMARY KEY,
  name VARCHAR(100),
  department VARCHAR(100),
  salary INT,
  hire_date DATE
);

Query: Find the average salary by department
```

**Output:**
```sql
SELECT department, AVG(salary) as average_salary
FROM employees
GROUP BY department
ORDER BY average_salary DESC;
```

## Training Details

This model was fine-tuned using LoRA, a parameter-efficient fine-tuning technique that significantly reduces the number of trainable parameters while maintaining performance. The training process involved:

1. **Dataset Preparation**: A combination of synthetic and curated text-to-SQL pairs
2. **Training Configuration**:
   - Learning Rate: 5e-5
   - Batch Size: 8
   - Training Steps: 1000
   - LoRA Rank: 16
   - Gradient Checkpointing: True
3. **Hardware**: Apple Silicon with MLX acceleration

## Limitations

- The model performs best when the database schema is included in the prompt
- Complex nested queries may require refining the prompt
- Performance varies based on domain-specific terminology
- The model may occasionally generate SQL syntax that is specific to certain database systems

## Ethical Considerations

This model is designed as a productivity tool for database queries and should be used responsibly:

- Always review and test generated SQL before executing in production environments
- Be aware that the model may reflect biases present in its training data
- The model should not be used to generate queries intended to exploit database vulnerabilities

## Citation

If you use this model in your research or applications, please cite:

```bibtex
@misc{gemma3-text-to-sql,
  author = {parole-study-viper},
  title = {Gemma 3 Text-to-SQL: A LoRA-fine-tuned adapter for natural language to SQL conversion},
  year = {2025},
  publisher = {HuggingFace},
  howpublished = {\url{https://huggingface.co/parole-study-viper/gemma-3-text-to-sql}}
}
```

## License

This model adapter is licensed under the Apache 2.0 License. Usage of the base Gemma 3 model is subject to Google's Gemma license terms.

## Acknowledgements

We thank Google for releasing the Gemma 3 models and the Hugging Face team for their transformers library and model hosting. We also acknowledge the contributions of the MLX team at Apple for enabling efficient inference on Apple Silicon.

---

If you find any issues or have suggestions for improvement, please open an issue on the GitHub repository or reach out on the Hugging Face community forums.


This model created by [@parole-study-viper]