Views
No views yet
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3from peft import PeftModel
4
5# Define model IDs
6base_model_id = "Qwen/Qwen2.5-1.5B-Instruct"
7finetuned_model_id = "navodPeiris/Vulnerability-Analyst-Qwen2.5-1.5B-Instruct"
8
9# Load tokenizer (trust remote code for Qwen models)
10tokenizer = AutoTokenizer.from_pretrained(finetuned_model_id, trust_remote_code=True)
11
12# Load base model
13model = AutoModelForCausalLM.from_pretrained(
14 base_model_id,
15 device_map="auto",
16 torch_dtype=torch.float16,
17 trust_remote_code=True
18)
19
20# Apply LoRA weights
21model = PeftModel.from_pretrained(model, finetuned_model_id)
22
23# (Optional) Merge LoRA adapters for faster inference
24model = model.merge_and_unload()
25
26# Prompt construction
27system_prompt = (
28 "You are an expert coder with a strong code vulnerability detection and reasoning ability. "
29 "You first think through the reasoning process step-by-step in your mind and then provide the user with the answer."
30)
31
32user_prompt = (
33 "Below is a question that describes a coding related problem. Write a response that appropriately answers the question. "
34 "Show your reasoning in <think> </think> tags. And return the final response in <answer> </answer> tags.\n"
35 "###Question###:\n{question}\n"
36 "###Response###:\n<think>"
37)
38
39# Example question
40question = """Find vulnerabilities in the following PHP code:
41```php
42<?php
43$db = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
44$username = $_GET['username'];
45$password = $_GET['password'];
46$sql = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
47foreach ($db->query($sql) as $row) {
48 print_r($row);
49}
50?>
51```"""
52
53# Apply tokenizer's chat template
54prompt = tokenizer.apply_chat_template(
55 [
56 {"role": "system", "content": system_prompt},
57 {"role": "user", "content": user_prompt.format(question=question)},
58 ],
59 tokenize=False,
60 add_generation_prompt=True,
61)
62
63# Run inference using transformers pipeline
64pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device_map="auto")
65output = pipe(prompt, max_new_tokens=1024, return_full_text=False)[0]["generated_text"]
66
67print("<think>\n" + output)| Parameter | Value |
|---|---|
per_device_train_batch_size | 8 |
gradient_accumulation_steps | 4 |
per_device_eval_batch_size | 16 |
logging_steps | 50 |
eval_steps | 50 |
num_train_epochs | 2 |
warmup_ratio | 0.03 |
learning_rate | 3e-5 |
fp16 | True |
optim | adamw_8bit |
weight_decay | 0.1 |
lr_scheduler_type | cosine |
dataset_text_field | prompt |
max_seq_length | 1024 |
lora_rank (r) | 16 |
target_modules | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
lora_alpha | 32 |
use_gradient_checkpointing | unsloth |
1@misc{vonwerra2022trl,
2 title = {{TRL: Transformer Reinforcement Learning}},
3 author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec},
4 year = 2020,
5 journal = {GitHub repository},
6 publisher = {GitHub},
7 howpublished = {\url{https://github.com/huggingface/trl}}
8}