phamthanhfd's picture
Add QLoRA adapter — Qwen2.5-3B contract analysis
2a9358c verified
|
Raw
History Blame Contribute Delete
2.05 kB
---
language:
- en
license: apache-2.0
base_model: Qwen/Qwen2.5-3B-Instruct
tags:
- legal
- contract-analysis
- lora
- qlora
- peft
- qwen2.5
datasets:
- theatticusproject/cuad-qa
---
# Contract Analysis — LoRA Adapter (Qwen2.5-3B)
QLoRA fine-tuned adapter for legal contract clause analysis on CUAD dataset.
| | |
|---|---|
| **Base Model** | Qwen/Qwen2.5-3B-Instruct |
| **Method** | QLoRA (4-bit NF4, LoRA r=16 alpha=32) |
| **Eval Loss** | 0.2167 |
| **Perplexity** | 1.24 ✅ Excellent |
| **Accuracy** | 80% |
## Output Format
```json
{"category": "confidentiality", "summary": "Employee must not disclose company secrets."}
```
**Categories:** `salary` · `payment` · `confidentiality` · `liability` · `termination` · `insurance` · `dispute_resolution` · `other`
## Usage
```python
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
from peft import PeftModel
import torch, json
bnb = BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16)
tokenizer = AutoTokenizer.from_pretrained("phamthanhfd/contract-analysis-lora-adapter")
base = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-3B-Instruct",
quantization_config=bnb, device_map="auto")
model = PeftModel.from_pretrained(base, "phamthanhfd/contract-analysis-lora-adapter")
SYSTEM = "You are a legal contract expert. Return JSON with category and summary."
clause = "The employee shall not disclose confidential information."
messages = [{"role":"system","content":SYSTEM},
{"role":"user","content":f"Analyze: {clause}"}]
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
out = model.generate(**inputs, max_new_tokens=150, temperature=0.1, do_sample=True)
gen = out[0][inputs["input_ids"].shape[-1]:]
print(json.loads(tokenizer.decode(gen, skip_special_tokens=True)))
```