Downloads · 30 days
29
27% of all-time downloads
anmol-unitmole/schema-aware-text-to-sql-codet5p-770m
schema-aware-text-to-sql-codet5p-770m is a other model from anmol-unitmole. Use it for the other task on the model card, and read the license before you ship it in a product. It is set up for transformers. The card lists the license as bsd-3-clause.
This repository contains a schema-aware Text-to-SQL encoder-decoder model based on Salesforce/codet5p-770m.
Downloads · 30 days
29
27% of all-time downloads
All-time downloads
109
Public
Parameters
738M
1.5 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors1.5 GB · 100%
From the Hugging Face model README
This repository contains a schema-aware Text-to-SQL encoder-decoder model based
on Salesforce/codet5p-770m.
The model converts a natural-language question and a serialized relational database schema into one read-only SQLite query.
It was fine-tuned using PEFT and LoRA on Spider 1.0 together with a small set of curated rule-based and synthetic portfolio examples. The final LoRA adapter was merged into the base model so that the model can be loaded directly with Hugging Face Transformers without requiring PEFT during inference.
| Property | Value |
|---|---|
| Selected experiment | CodeT5+ 770M LoRA r32 |
| Base model | Salesforce/codet5p-770m |
| Architecture | Encoder-decoder Transformer |
| Task | Schema-aware natural-language-to-SQL generation |
| Fine-tuning method | PEFT / LoRA |
| LoRA rank | 32 |
| LoRA alpha | 64 |
| LoRA dropout | 0.05 |
| Training precision | BF16 |
| Training hardware | NVIDIA GeForce RTX 5090 |
| Target SQL dialect | SQLite |
| Output policy | One read-only SELECT or WITH query |
| Validation examples | 628 |
| Held-out test examples | 1,040 |
The selected model was evaluated on 628 validation examples.
| Metric | Result |
|---|---|
| Execution accuracy | 60.99% |
| Valid-SQL rate | 94.90% |
| Exact match | 42.04% |
| Schema-linking precision | 88.89% |
| Schema-linking recall | 100.00% |
| Schema-linking F1 | 93.33% |
| Average generation latency | 1,104.62 ms |
| Median generation latency | 850.23 ms |
| P95 generation latency | 2,639.82 ms |
The final held-out evaluation used 1,040 examples.
| Metric | Result |
|---|---|
| Execution accuracy | 56.92% |
| Valid-SQL rate | 92.69% |
| Exact match | 38.37% |
| Schema-linking precision | 96.67% |
| Schema-linking recall | 100.00% |
| Schema-linking F1 | 98.15% |
| Average generation latency | 1,184.16 ms |
| Median generation latency | 932.17 ms |
| P95 generation latency | 2,849.17 ms |
The final project quality gate required:
| Requirement | Minimum | Achieved |
|---|---|---|
| Held-out execution accuracy | 50.00% | 56.92% |
| Held-out valid-SQL rate | 90.00% | 92.69% |
| Improvement over the base model | 3 percentage points | Passed |
| Held-out evaluation examples | 500 | 1,040 |
The model passed all required portfolio-readiness checks.
The complete training corpus contained 8,070 examples.
| Split | Examples |
|---|---|
| Training | 6,402 |
| Validation | 628 |
| Held-out test | 1,040 |
| Total | 8,070 |
The corpus included:
The evaluation process used database-aware splitting and leakage checks.
The completed leakage audit found:
Spider database files and private database files are not redistributed with this model repository.
The model expects one prompt containing:
Example input:
Task:
Generate one valid SQLite query for the given business question.
Database schema:
Table: sales
Columns:
- sale_id INTEGER PRIMARY KEY
- region TEXT
- sale_date TEXT
- sales_amount REAL
Question:
What is the total sales amount for each region?
Rules:
- Use only tables and columns present in the schema.
- Generate exactly one read-only SELECT or WITH query.
- Return SQL only.
The expected output is SQL text without an explanation:
SELECT region,
SUM(sales_amount) AS total_sales
FROM sales
GROUP BY region;
The final ablation study was performed on the complete 1,040-example held-out test set.
| Condition | Exact match | Valid SQL | Execution accuracy |
|---|---|---|---|
| Base model without schema | 0.00% | 0.29% | 0.00% |
| Base model with schema | 0.00% | 2.98% | 0.00% |
| Fine-tuned model with schema | 38.37% | 92.69% | 56.92% |
| Fine-tuned model with schema and conservative repair | 38.37% | 92.69% | 56.92% |
The experiment demonstrates that both domain fine-tuning and structured schema conditioning were necessary for reliable Text-to-SQL generation.
The conservative repair condition produced the same results as the condition without repair:
The repair layer should therefore be described as a conservative SQL formatting and safety mechanism rather than as an accuracy-improvement component.
No repair-related performance gain is claimed for this experiment.
from __future__ import annotations
import torch
from transformers import (
AutoModelForSeq2SeqLM,
AutoTokenizer,
)
MODEL_ID = (
"anmol-unitmole/"
"schema-aware-text-to-sql-codet5p-770m"
)
tokenizer = AutoTokenizer.from_pretrained(
MODEL_ID,
use_fast=True,
)
dtype = (
torch.bfloat16
if torch.cuda.is_available()
and torch.cuda.is_bf16_supported()
else torch.float32
)
model = AutoModelForSeq2SeqLM.from_pretrained(
MODEL_ID,
torch_dtype=dtype,
)
device = torch.device(
"cuda"
if torch.cuda.is_available()
else "cpu"
)
model = model.to(device)
model.eval()
prompt = """
Task:
Generate one valid SQLite query for the given business question.
Database schema:
Table: sales
Columns:
- sale_id INTEGER PRIMARY KEY
- region TEXT
- sale_date TEXT
- sales_amount REAL
Question:
What is the total sales amount for each region?
Rules:
- Use only tables and columns present in the schema.
- Generate exactly one read-only SELECT or WITH query.
- Return SQL only.
""".strip()
inputs = tokenizer(
prompt,
return_tensors="pt",
truncation=True,
max_length=768,
)
inputs = {
key: value.to(device)
for key, value in inputs.items()
}
with torch.no_grad():
generated = model.generate(
**inputs,
max_new_tokens=256,
num_beams=4,
do_sample=False,
early_stopping=True,
)
sql = tokenizer.decode(
generated[0],
skip_special_tokens=True,
)
print(sql)
The model can be loaded on CPU, but inference will be significantly slower:
import torch
from transformers import (
AutoModelForSeq2SeqLM,
AutoTokenizer,
)
MODEL_ID = (
"anmol-unitmole/"
"schema-aware-text-to-sql-codet5p-770m"
)
tokenizer = AutoTokenizer.from_pretrained(
MODEL_ID
)
model = AutoModelForSeq2SeqLM.from_pretrained(
MODEL_ID,
torch_dtype=torch.float32,
)
model = model.to("cpu")
model.eval()
For interactive inference, a CUDA-capable GPU is recommended.
The model itself generates text and does not independently guarantee safe SQL.
The accompanying project applies a separate SQL safety layer that includes:
Generated SQL should always be validated before execution.
This model is intended for:
The model is not intended for:
The model can still generate SQL that is:
Complex multi-table joins, correlated subqueries, nested aggregations and semantically ambiguous questions remain challenging.
Exact-match accuracy is lower than execution accuracy because multiple SQL queries can be textually different while returning equivalent results.
Human review is required before using generated SQL for real decisions.
Execution accuracy compares the shape and normalized returned values of the generated and reference queries.
Output aliases and SQLite-generated column labels are not required to match when the returned values are equivalent.
The SQL validator masks quoted string literals before applying schema-aware
identifier checks. This prevents values such as "JetBlue Airways" or
"Presentation" from being incorrectly classified as column names.
The selected experiment used approximately the following configuration:
| Parameter | Value |
|---|---|
| Base model | Salesforce/codet5p-770m |
| Fine-tuning mode | LoRA |
| LoRA rank | 32 |
| LoRA alpha | 64 |
| LoRA dropout | 0.05 |
| Source length | 768 tokens |
| Target length | 256 tokens |
| Generation beams | 4 |
| Precision | BF16 |
| TF32 | Enabled |
| Gradient checkpointing | Enabled |
| Optimizer | Fused AdamW |
| Learning-rate schedule | Cosine |
| Evaluation strategy | Per epoch |
| Model-selection metric | Validation exact match |
| Component | Version or value |
|---|---|
| Operating system | Windows 11 |
| Python | 3.12.10 |
| PyTorch | 2.11.0 with CUDA 12.8 |
| Transformers | 4.57.6 |
| GPU | NVIDIA GeForce RTX 5090 |
| GPU memory | Approximately 31.84 GB |
| Compute capability | 12.0 |
| BF16 support | Yes |
| TF32 support | Yes |
The complete project includes:
Source repository:
https://github.com/unit-mole/encoder-decoder-projects
Project directory:
01-schema-aware-text-to-sql-encoder-decoder
Five final candidates were evaluated:
Candidate ranking used:
CodeT5+ 770M LoRA rank 32 was selected as the final model.
This model is derived from:
Salesforce/codet5p-770m
The base model and this merged derivative use the BSD 3-Clause license.
Users should also review the original base-model documentation and comply with all applicable dataset, model and software licenses.
A formal research-paper citation is not currently associated with this portfolio model.
When referencing the implementation, cite the GitHub repository and this Hugging Face model page.
This model is provided for research, educational and portfolio-demonstration purposes.
The model authors do not guarantee the correctness, completeness, safety or business suitability of generated SQL. Users are responsible for validating queries and protecting all connected databases.