CoolFace
Modelpublic

AuricErgeson/Antelope-textTosql

sourceHugging Facemitupdated 5mo agoView on Hugging Face
1likes22downloads
Model Card

๐ŸฆŒ Antelope Text-to-SQL

Convert plain English questions into SQL queries instantly. Lightweight, fast, and runs on CPU. No database expertise needed.

๐Ÿš€ [Try the live demo โ†’](https://huggingface.co/spaces/AuricErgeson/Antelope-textTosql-demo)


What it does

QuestionDatabaseOutput
How many employees are there?companySELECT COUNT(*) FROM employees
List all customers from GermanystoreSELECT * FROM customers WHERE country = 'Germany'
What is the average salary by department?hrSELECT department, AVG(salary) FROM employees GROUP BY department

Quick Start

python
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model_id = "AuricErgeson/Antelope-textTosql"

tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.float16,
    device_map="auto",
    trust_remote_code=True
)

def generate_sql(question, db=""):
    prompt = (
        f"### Task: Convert question to SQL. Use only what the question asks. Simple questions need simple SQL.\n"
        f"### Database: {db}\n"
        f"### Question: {question}\n"
        f"### SQL:"
    )
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    with torch.no_grad():
        out = model.generate(
            **inputs,
            max_new_tokens=128,
            temperature=0.1,
            do_sample=True,
            pad_token_id=tokenizer.eos_token_id
        )
    decoded = tokenizer.decode(out[0], skip_special_tokens=True)
    return decoded.split("### SQL:")[-1].strip().split("\n")[0]

print(generate_sql("How many employees are in each department?", db="company"))
# โ†’ SELECT department, COUNT(*) FROM employees GROUP BY department

Why use this model?

  • โ€”โœ… Small โ€” 2.7B params, runs on modest hardware
  • โ€”โœ… Fast โ€” CPU inference possible, GPU recommended
  • โ€”โœ… Open โ€” MIT license, use anywhere
  • โ€”โœ… No setup โ€” works out of the box with transformers
  • โ€”โœ… Cross-domain โ€” trained on 200+ database schemas

Prompt Format

### Task: Convert question to SQL. Use only what the question asks. Simple questions need simple SQL.
### Database: {your_database_name}
### Question: {your_question}
### SQL:

Via HuggingFace Inference API

python
import requests

API_URL = "https://api-inference.huggingface.co/models/AuricErgeson/Antelope-textTosql"
headers = {"Authorization": "Bearer YOUR_HF_TOKEN"}

response = requests.post(API_URL, headers=headers, json={
    "inputs": "### Task: Convert question to SQL. Use only what the question asks. Simple questions need simple SQL.\n### Database: company\n### Question: How many employees?\n### SQL:"
})
print(response.json())

Training Details

PropertyValue
Base modelmicrosoft/phi-2 (2.7B)
MethodQLoRA (4-bit + LoRA)
DatasetSpider (7,000+ examples, 200+ databases)
LoRA rank16
LoRA alpha32
Target modulesqproj, vproj
Learning rate2e-4
Epochs3
HardwareNVIDIA A100 (Google Colab)
Training time~1.5 hours
Adapter size~21MB
Merged model size~5.56GB

Limitations

  • โ€”Not recommended for production databases without output validation
  • โ€”Complex multi-join queries may be inaccurate
  • โ€”Does not infer table/column names โ€” provide your database name for best results

Roadmap

  • โ€”[x] Text-to-SQL (this model)
  • โ€”[ ] Text-to-Regex (coming soon)
  • โ€”[ ] Text-to-Shell (planned)

Author

Auric Ergeson Nitonde ๐Ÿ“ง auricergesonnitonde@gmail.com ๐Ÿค— HuggingFace Profile

If this model helped you, consider leaving a like โญ โ€” it helps others find it.