This LoRA adapter enhances the Qwen2.5-7B-Instruct model for the specialized task of extracting security controls from compliance framework documents. The adapter was trained using a custom weighted loss function that penalizes false positives more heavily than false negatives, addressing the critical requirement in compliance auditing where incorrectly identifying a control is more problematic than missing one.
1from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
2from peft import PeftModel
3import torch
4
5# Quantization config (optional, for memory efficiency)
6bnb_config = BitsAndBytesConfig(
7 load_in_4bit=True,
8 bnb_4bit_use_double_quant=True,
9 bnb_4bit_quant_type="nf4",
10 bnb_4bit_compute_dtype=torch.bfloat16
11)
12
13# Load base model
14base_model = AutoModelForCausalLM.from_pretrained(
15 "Qwen/Qwen2.5-7B-Instruct",
16 quantization_config=bnb_config,
17 device_map="auto",
18 trust_remote_code=True
19)
20
21# Load tokenizer
22tokenizer = AutoTokenizer.from_pretrained("path/to/final_adapter")
23
24# Load LoRA adapter
25model = PeftModel.from_pretrained(base_model, "path/to/final_adapter")
1system_prompt = """You are a senior Compliance Auditor and Regulatory Analyst specialized in ISO, NIST, and statutory frameworks."""
2
3messages = [
4 {"role": "system", "content": system_prompt},
5 {"role": "user", "content": f"Analyze this text:\n\n{page_text}"}
6]
7
8input_ids = tokenizer.apply_chat_template(
9 messages,
10 add_generation_prompt=True,
11 return_tensors="pt"
12).to(model.device)
13
14outputs = model.generate(
15 input_ids,
16 max_new_tokens=512,
17 temperature=0.1,
18 do_sample=False
19)
20
21response = tokenizer.decode(outputs[0], skip_special_tokens=True)
1[
2 {
3 "control_id": "AC-1",
4 "control_title": "Access Control Policy and Procedures",
5 "control_desc": "Description of the control..."
6 }
7]
8<END>
1# Samples with controls are weighted 2x during loss computation
2weights = torch.where(has_control, 2.0, 1.0)
3weighted_loss = (sample_loss * weights).mean()
For licensing inquiries, please contact via the channels below.
1@misc{sharma2026nist-control-extraction,
2 title={NIST Control Extraction LoRA Adapter for Qwen2.5-7B},
3 author={Sharma, Rishit},
4 year={2026},
5 publisher={GitHub},
6 howpublished={\url{https://github.com/rishit836/control-extraction-using-llm-finetuned}}
7}