CoolFace
Modelpublic

vindows/qwen2.5-7b-text-to-sql

sourceHugging Faceapache-2.0updated 9mo agoView on Hugging Face
0likes17downloads
Model Card

Qwen2.5-7B LoRA Fine-tuned for Text-to-SQL

This is a LoRA adapter for Qwen/Qwen2.5-7B-Instruct fine-tuned on natural language to SQL conversion.

Quick Links

Model Performance

Training Metrics (54 test examples)

MetricBase ModelFine-tunedImprovement
Loss2.13010.409880.76% โฌ†๏ธ
Perplexity8.41551.506482.10% โฌ†๏ธ

Spider Benchmark Results (200 examples)

MetricScore
Exact Match0.00%
Normalized Match0.50%
Component Accuracy92.60%
Average Similarity25.47%

Note: The model shows strong component understanding but tends to append explanatory text after SQL queries, affecting exact match scores. See limitations below.

Training Details

  • โ€”Training Time: 6 minutes 15 seconds
  • โ€”Epochs: 3
  • โ€”LoRA Rank: 16
  • โ€”LoRA Alpha: 32
  • โ€”Learning Rate: 2e-4
  • โ€”Dataset: 425 training examples, 54 validation, 54 test

Usage

With PEFT (Recommended for fine-tuning/adapters)

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

# Load base model
base_model = AutoModelForCausalLM.from_pretrained(
    "Qwen/Qwen2.5-7B-Instruct",
    device_map="auto",
    torch_dtype=torch.bfloat16,
    trust_remote_code=True
)

# Load LoRA adapter
model = PeftModel.from_pretrained(base_model, "vindows/qwen2.5-7b-text-to-sql")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct", trust_remote_code=True)

# Generate SQL
prompt = "Convert the following natural language question to SQL:\n\nDatabase: concert_singer\nQuestion: How many singers do we have?\n\nSQL:"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=128, temperature=0.1)
sql = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(sql)

Using the Merged Model (Easier)

For easier usage without loading base + adapter separately, use the merged model:

python
from transformers import AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained(
    "vindows/qwen2.5-7b-text-to-sql-merged",
    device_map="auto",
    trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained("vindows/qwen2.5-7b-text-to-sql-merged")

Limitations

  1. 1.Appends Explanatory Text: The model tends to append explanatory text or context after generating SQL queries. Post-processing to extract only the SQL statement is recommended.
  2. 2.Hallucinated Table Names (0.5B): The smaller 0.5B model sometimes invents table names not present in the schema.
  3. 3.Training Data Distribution: Best performance on queries similar to training examples.

Recommended Post-Processing

python
def extract_sql(generated_text):
    # Extract SQL after the "SQL:" marker
    if "SQL:" in generated_text:
        sql = generated_text.split("SQL:")[-1].strip()
    else:
        sql = generated_text

    # Take only the first SQL statement (before extra text)
    if '\n\n' in sql:
        sql = sql.split('\n\n')[0].strip()

    # Remove trailing semicolon if present
    sql = sql.rstrip(';').strip()

    return sql

Files Included

  • โ€”adapter_config.json - LoRA configuration
  • โ€”adapter_model.safetensors - LoRA weights
  • โ€”README.md - This file

Citation

bibtex
@misc{qwen2.5-7b-text-to-sql,
  title = {Qwen2.5-7B LoRA Fine-tuned for Text-to-SQL},
  year = {2024},
  publisher = {Hugging Face},
  url = {https://huggingface.co/vindows/qwen2.5-7b-text-to-sql}
}

License

Apache 2.0 (inherits from base Qwen2.5 model)