Downloads · 30 days
22
18% of all-time downloads
Abhisek987/llama-3.2-sql-merged
llama-3.2-sql-merged is a text generation model from Abhisek987. Use it when you need the model to write or continue text. It is set up for transformers. The card lists the license as llama3.2.
This model is a fine-tuned version of meta-llama/Llama-3.2-3B for text-to-SQL generation using LoRA (Low-Rank Adaptation) on the Spider dataset.
Downloads · 30 days
22
18% of all-time downloads
All-time downloads
121
Public
Parameters
3.2B
6.4 GB on disk
Likes
1
Public
Click a slice to open those files.
.safetensors6.4 GB · 100%
From the Hugging Face model README
This model is a fine-tuned version of meta-llama/Llama-3.2-3B for text-to-SQL generation using LoRA (Low-Rank Adaptation) on the Spider dataset.
This model converts natural language questions into SQL queries for various database schemas. It's designed for:
| Metric | Value |
|---|---|
| Initial Loss | 2.50 |
| Final Loss | 0.37 |
| Loss Reduction | 85% |
| Trainable Parameters | 9.17M (0.51% of 1.8B total) |
| Training Time | 47 minutes |
| GPU | NVIDIA A10G (24GB VRAM) |
pip install transformers torch accelerate
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
# Load model and tokenizer
model_name = "Abhisek987/llama-3.2-sql-merged"
model = AutoModelForCausalLM.from_pretrained(
model_name,
device_map="auto",
torch_dtype=torch.float16
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Prepare prompt
database = "employees"
question = "What are the names of all employees who earn more than 50000?"
prompt = f"""### Instruction:
You are a SQL expert. Generate a SQL query to answer the given question for the specified database.
### Input:
Database: {database}
Question: {question}
### Response:
"""
# Generate SQL
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs,
max_new_tokens=256,
temperature=0.1,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
sql_query = result.split("### Response:")[-1].strip()
print(sql_query)
Output:
SELECT name FROM employees WHERE salary > 50000;
def generate_sql_batch(questions_with_db):
"""Generate SQL for multiple questions"""
results = []
for database, question in questions_with_db:
prompt = f"""### Instruction:
You are a SQL expert. Generate a SQL query.
### Input:
Database: {database}
Question: {question}
### Response:
"""
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(**inputs, max_new_tokens=256, temperature=0.1)
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
sql = result.split("### Response:")[-1].strip()
results.append(sql)
return results
# Example usage
queries = [
("employees", "Show all employees"),
("sales", "Top 5 products by revenue"),
("customers", "Count by country")
]
sql_queries = generate_sql_batch(queries)
| Database | Question | Generated SQL |
|---|---|---|
| employees | "Show all employees with salary above 60000" | SELECT name FROM employees WHERE salary > 60000; |
| sales | "Show me the top 5 products by total sales" | SELECT product_id, sum(sales) FROM sales GROUP BY product_id ORDER BY sum(sales) DESC LIMIT 5; |
| customers | "How many customers are from each country?" | SELECT count(*), country FROM customers GROUP BY country; |
| orders | "Find all orders placed in the last 30 days" | SELECT order_id FROM orders WHERE date_order_placed BETWEEN DATE('now') - INTERVAL 30 DAY AND DATE('now') - INTERVAL 1 DAY; |
| concert_singer | "What are the names of all singers ordered by net worth?" | SELECT Name FROM singer ORDER BY Net_Worth; |
Try the model interactively: 🔗 Interactive Demo on HuggingFace Spaces
The demo includes:
Spider Dataset Statistics:
Model was evaluated on the Spider development set with focus on:
Training loss progression demonstrated strong convergence:
If you use this model in your work, please cite:
@misc{llama32-sql-merged,
author = {Abhisek Behera},
title = {Llama 3.2 3B SQL Query Generator - LoRA Fine-tuned},
year = {2025},
publisher = {HuggingFace},
howpublished = {\url{https://huggingface.co/Abhisek987/llama-3.2-sql-merged}},
note = {Fine-tuned on Spider dataset using LoRA}
}
Original Base Model:
@article{llama3,
title={Llama 3 Model Card},
author={Meta AI},
year={2024},
url={https://github.com/meta-llama/llama3}
}
Spider Dataset:
@inproceedings{yu2018spider,
title={Spider: A Large-Scale Human-Labeled Dataset for Complex and Cross-Domain Semantic Parsing and Text-to-SQL Task},
author={Yu, Tao and Zhang, Rui and Yang, Kai and others},
booktitle={EMNLP},
year={2018}
}
This model inherits the Llama 3.2 Community License from the base model. Please review the license terms before commercial use.
For issues, improvements, or questions, please use the Community tab.