phamthanhfd/contract-analysis-lora-adapter
08
Contract Analysis — LoRA Adapter (Qwen2.5-3B)
QLoRA fine-tuned adapter for legal contract clause analysis on CUAD dataset.
Output Format
{"category": "confidentiality", "summary": "Employee must not disclose company secrets."}Categories: salary · payment · confidentiality · liability · termination · insurance · dispute_resolution · other
Usage
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)))