File size: 1,705 Bytes
fc2db1f
 
 
 
 
 
 
 
 
 
ba97ccc
059c9f9
ba97ccc
059c9f9
ba97ccc
059c9f9
 
 
 
 
ba97ccc
059c9f9
ba97ccc
059c9f9
ba97ccc
059c9f9
 
 
 
 
 
 
 
 
 
 
 
 
 
fc2db1f
059c9f9
 
 
ba97ccc
059c9f9
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
---
library_name: peft
base_model: Qwen/Qwen2.5-0.5B-Instruct
tags:
- text-to-sql
- lora
- qwen
- fine-tuned
model_name: Qwen2.5-0.5B-SQL
---

# Qwen2.5-0.5B-SQL LoRA Adapter

This model is a LoRA (Low-Rank Adaptation) adapter for **Qwen2.5-0.5B-Instruct**, specifically fine-tuned to generate SQL queries from natural language questions and database schemas.

## Model Details
- **Base Model:** Qwen/Qwen2.5-0.5B-Instruct
- **Task:** Text-to-SQL
- **Training Data:** b-mc2/sql-create-context
- **Language:** English

## Quick Start (How to use)

To use this adapter, you need to load the base model first and then apply the LoRA weights.

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

model_id = "Qwen/Qwen2.5-0.5B-Instruct"
adapter_id = "azeemazam/Qwen2.5-0.5B-SQL"

tokenizer = AutoTokenizer.from_pretrained(adapter_id)
base_model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map='auto')
model = PeftModel.from_pretrained(base_model, adapter_id)

def generate_sql(schema, question):
    messages = [
        {"role": "user", "content": f"Generate SQL.\\n\\nDatabase Schema:\\n{schema}\\n\\nQuestion:\\n{question}"}    
    ]
    prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    inputs = tokenizer(prompt, return_tensors='pt').to(model.device)
    
    outputs = model.generate(**inputs, max_new_tokens=150)
    return tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)

schema = "CREATE TABLE employees (id INT, name TEXT, salary INT)"
question = "Who earns more than 50000?"
print(generate_sql(schema, question))
```