Text Generation
Transformers
Safetensors
gemma3_text
text-to-sql
sql
oracle
postgresql
gemma3
trl
sft
conversational
text-generation-inference
Instructions to use chabab/gemma-3-270m-text2sql-oracle-postgres with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use chabab/gemma-3-270m-text2sql-oracle-postgres with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="chabab/gemma-3-270m-text2sql-oracle-postgres") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("chabab/gemma-3-270m-text2sql-oracle-postgres") model = AutoModelForCausalLM.from_pretrained("chabab/gemma-3-270m-text2sql-oracle-postgres", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use chabab/gemma-3-270m-text2sql-oracle-postgres with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "chabab/gemma-3-270m-text2sql-oracle-postgres" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "chabab/gemma-3-270m-text2sql-oracle-postgres", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/chabab/gemma-3-270m-text2sql-oracle-postgres
- SGLang
How to use chabab/gemma-3-270m-text2sql-oracle-postgres with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "chabab/gemma-3-270m-text2sql-oracle-postgres" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "chabab/gemma-3-270m-text2sql-oracle-postgres", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "chabab/gemma-3-270m-text2sql-oracle-postgres" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "chabab/gemma-3-270m-text2sql-oracle-postgres", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use chabab/gemma-3-270m-text2sql-oracle-postgres with Docker Model Runner:
docker model run hf.co/chabab/gemma-3-270m-text2sql-oracle-postgres
Add model card with eval results
Browse files
README.md
CHANGED
|
@@ -1,59 +1,133 @@
|
|
| 1 |
---
|
| 2 |
base_model: google/gemma-3-270m-it
|
|
|
|
|
|
|
| 3 |
library_name: transformers
|
| 4 |
-
|
|
|
|
| 5 |
tags:
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
-
-
|
| 9 |
-
-
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
-
#
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
```python
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
```
|
| 28 |
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
| 32 |
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
- Pytorch: 2.13.0
|
| 41 |
-
- Datasets: 5.0.1
|
| 42 |
-
- Tokenizers: 0.22.2
|
| 43 |
|
| 44 |
-
##
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
```bibtex
|
| 51 |
-
@misc{vonwerra2022trl,
|
| 52 |
-
title = {{TRL: Transformer Reinforcement Learning}},
|
| 53 |
-
author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec},
|
| 54 |
-
year = 2020,
|
| 55 |
-
journal = {GitHub repository},
|
| 56 |
-
publisher = {GitHub},
|
| 57 |
-
howpublished = {\url{https://github.com/huggingface/trl}}
|
| 58 |
-
}
|
| 59 |
-
```
|
|
|
|
| 1 |
---
|
| 2 |
base_model: google/gemma-3-270m-it
|
| 3 |
+
datasets:
|
| 4 |
+
- chabab/text2sql-oracle-postgres
|
| 5 |
library_name: transformers
|
| 6 |
+
license: apache-2.0
|
| 7 |
+
pipeline_tag: text-generation
|
| 8 |
tags:
|
| 9 |
+
- text-to-sql
|
| 10 |
+
- sql
|
| 11 |
+
- oracle
|
| 12 |
+
- postgresql
|
| 13 |
+
- gemma3
|
| 14 |
+
- trl
|
| 15 |
+
- sft
|
| 16 |
---
|
| 17 |
|
| 18 |
+
# gemma-3-270m-text2sql-oracle-postgres
|
| 19 |
|
| 20 |
+
[`google/gemma-3-270m-it`](https://huggingface.co/google/gemma-3-270m-it) fine-tuned to turn a
|
| 21 |
+
schema + a natural-language question into **one** dialect-correct SQL statement — Oracle or
|
| 22 |
+
PostgreSQL — with no markdown fences and no commentary.
|
| 23 |
|
| 24 |
+
At 270M parameters it runs on CPU and quantizes to ~290 MB.
|
| 25 |
+
|
| 26 |
+
## Results
|
| 27 |
+
|
| 28 |
+
Held-out test split (60 examples), greedy decoding, normalized exact match against gold SQL:
|
| 29 |
+
|
| 30 |
+
| Slice | Exact match |
|
| 31 |
+
|---|---|
|
| 32 |
+
| **Overall** | **78.3%** (47/60) |
|
| 33 |
+
| Oracle | 86.7% (39/45) |
|
| 34 |
+
| PostgreSQL | 53.3% (8/15) |
|
| 35 |
+
| easy | 93.3% (14/15) |
|
| 36 |
+
| medium | 69.2% (27/39) |
|
| 37 |
+
| hard | 100% (6/6) |
|
| 38 |
+
|
| 39 |
+
Per-example predictions are in [`eval_results.json`](./eval_results.json).
|
| 40 |
+
|
| 41 |
+
Caveats worth knowing before you rely on these numbers:
|
| 42 |
+
|
| 43 |
+
- The test split is skewed 45 Oracle / 15 PostgreSQL, so the PostgreSQL figure rests on 15
|
| 44 |
+
examples and has a wide error bar.
|
| 45 |
+
- Exact match is strict. Several "failures" are valid SQL that differs from gold — an extra
|
| 46 |
+
`LIMIT`, a different but equivalent predicate. Real semantic accuracy is higher than 78.3%.
|
| 47 |
+
- The most common genuine error is dialect leakage: emitting `LIKE` where PostgreSQL gold uses
|
| 48 |
+
`ILIKE`. If case-insensitive matching matters to you, check that specific pattern.
|
| 49 |
+
- Only the 7 schemas in the training set (hr, sales, banking, inventory, tickets, university,
|
| 50 |
+
logistics) are represented. Generalization to unseen schemas is untested.
|
| 51 |
+
|
| 52 |
+
## Usage
|
| 53 |
+
|
| 54 |
+
The model expects the system prompt naming the dialect, then a `Schema:` block and a `Question:`
|
| 55 |
+
block — the same shape as the training data.
|
| 56 |
|
| 57 |
```python
|
| 58 |
+
import torch
|
| 59 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 60 |
+
|
| 61 |
+
model_id = "chabab/gemma-3-270m-text2sql-oracle-postgres"
|
| 62 |
+
tok = AutoTokenizer.from_pretrained(model_id)
|
| 63 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 64 |
+
model_id,
|
| 65 |
+
dtype=torch.bfloat16,
|
| 66 |
+
attn_implementation="eager", # Gemma-3 needs eager attention for correct generation
|
| 67 |
+
device_map="auto",
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
messages = [
|
| 71 |
+
{"role": "system", "content": "You convert natural language into PostgreSQL SQL. Use only tables and columns from the provided schema. Reply with one SQL statement and nothing else. No markdown fences. No commentary."},
|
| 72 |
+
{"role": "user", "content": """Schema:
|
| 73 |
+
employees(
|
| 74 |
+
employee_id INTEGER PK,
|
| 75 |
+
first_name VARCHAR(50),
|
| 76 |
+
last_name VARCHAR(50),
|
| 77 |
+
hire_date DATE,
|
| 78 |
+
salary NUMERIC(12,2),
|
| 79 |
+
department_id INTEGER FK->departments.department_id
|
| 80 |
+
)
|
| 81 |
|
| 82 |
+
Question:
|
| 83 |
+
Show the five employees with the largest salary. Return only the SQL."""},
|
| 84 |
+
]
|
| 85 |
+
|
| 86 |
+
ids = tok.apply_chat_template(messages, add_generation_prompt=True,
|
| 87 |
+
return_tensors="pt", return_dict=True).to(model.device)
|
| 88 |
+
out = model.generate(**ids, max_new_tokens=256, do_sample=False)
|
| 89 |
+
print(tok.decode(out[0][ids["input_ids"].shape[-1]:], skip_special_tokens=True).strip())
|
| 90 |
+
# SELECT first_name, last_name, salary FROM employees ORDER BY salary DESC LIMIT 5;
|
| 91 |
```
|
| 92 |
|
| 93 |
+
Two things matter for output quality:
|
| 94 |
+
|
| 95 |
+
- **Use greedy decoding** (`do_sample=False`). The task has one right answer; sampling only adds
|
| 96 |
+
drift.
|
| 97 |
+
- **Use `attn_implementation="eager"`.** Gemma-3 generates degenerate repeated tokens under the
|
| 98 |
+
default SDPA path in some configurations.
|
| 99 |
+
|
| 100 |
+
## GGUF / local inference
|
| 101 |
|
| 102 |
+
Quantized builds for Ollama, LM Studio, and llama.cpp:
|
| 103 |
+
[`chabab/gemma-3-270m-text2sql-oracle-postgres-GGUF`](https://huggingface.co/chabab/gemma-3-270m-text2sql-oracle-postgres-GGUF)
|
| 104 |
|
| 105 |
+
## Training
|
| 106 |
|
| 107 |
+
Full-parameter SFT (no LoRA — the model is small enough to tune end to end) with TRL `SFTTrainer`
|
| 108 |
+
on one L4 GPU, about 10 minutes.
|
| 109 |
|
| 110 |
+
| | |
|
| 111 |
+
|---|---|
|
| 112 |
+
| Base | `google/gemma-3-270m-it` |
|
| 113 |
+
| Data | `chabab/text2sql-oracle-postgres` — 684 train / 60 validation / 60 test |
|
| 114 |
+
| Epochs | 5 |
|
| 115 |
+
| Effective batch size | 16 (4 × 4 grad accum) |
|
| 116 |
+
| Learning rate | 5e-5, cosine, 10 warmup steps |
|
| 117 |
+
| Max sequence length | 1024 |
|
| 118 |
+
| Precision | bf16 |
|
| 119 |
|
| 120 |
+
Final metrics: train loss **0.0256**, eval loss **0.0683**, eval token accuracy **98.5%**.
|
| 121 |
+
Eval loss fell monotonically through training with no divergence.
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
## Limitations
|
| 124 |
|
| 125 |
+
Generated SQL is not validated against a live database. The model can produce syntactically valid
|
| 126 |
+
statements that reference the wrong table or misread the intent — two of the observed test
|
| 127 |
+
failures do exactly that. Review output before executing it, and never run generated SQL against
|
| 128 |
+
production with write permissions.
|
| 129 |
|
| 130 |
+
## License
|
| 131 |
|
| 132 |
+
Apache 2.0, inheriting the [Gemma terms of use](https://ai.google.dev/gemma/terms) from the base
|
| 133 |
+
model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|