Gemma-2-2B Text-to-SQL Expert
A specialized SQL generation model fine-tuned on 78k+ samples. Built with Unsloth for efficiency and speed.
Overview
This model is a fine-tuned version of Gemma-2-2B-Instruct, specialized in translating natural language questions into SQL queries. It has been trained to understand database schemas and generate precise SQL commands without conversational filler.
- Model Architecture: Gemma 2 (2B Parameters)
- Fine-tuning Method: QLoRA (via Unsloth)
- Dataset: b-mc2/sql-create-context
- Output: Raw SQL Query
Quick Start
1. Install Dependencies
pip install "unsloth[colab-new] @ git+[https://github.com/unslothai/unsloth.git](https://github.com/unslothai/unsloth.git)"
pip install --no-deps xformers "trl<0.9.0" peft accelerate bitsandbytes
2. Run Inference (Python)
from unsloth import FastLanguageModel
import torch
# 1. Load Model & Tokenizer
model_name = "adamwhite625/gemma-2-2b-finetuned-text2sql"
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = model_name,
max_seq_length = 2048,
dtype = None,
load_in_4bit = True,
)
FastLanguageModel.for_inference(model)
# 2. Define Schema & Question
question = "Find the name and address of the employee who is the oldest."
schema = "CREATE TABLE employee (name VARCHAR, address VARCHAR, age INTEGER)"
# 3. Format Prompt (Strict Format)
prompt = f"""Write a SQL query to answer the following question given the database schema:
{question}
Schema:
{schema}"""
# 4. Generate SQL
inputs = tokenizer.apply_chat_template(
[{"role": "user", "content": prompt}],
add_generation_prompt = True,
return_tensors = "pt",
).to("cuda")
outputs = model.generate(input_ids=inputs, max_new_tokens=128, use_cache=True)
result = tokenizer.batch_decode(outputs)
# Print only the generated SQL (extracting from the response)
print(result[0].split("<start_of_turn>model")[-1].replace("<end_of_turn>", "").strip())
Example Output
Input:
Find the name and address of the employee who is the oldest. Schema: CREATE TABLE employee (name VARCHAR, address VARCHAR, age INTEGER)
Output:
SELECT name, address FROM employee ORDER BY age DESC LIMIT 1
- Downloads last month
- -
Model tree for adamwhite625/gemma-2-2b-finetuned-text2sql
Base model
unsloth/gemma-2-2b-it-bnb-4bit