Views
No views yet
1import time
2import torch
3from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
4
5device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
6finetuned_model = AutoModelForCausalLM.from_pretrained("AquilaX-AI/security_assistant_2")
7tokenizer = AutoTokenizer.from_pretrained("AquilaX-AI/security_assistant")
8
9finetuned_model.to(device)
10
11prompt = """<|im_start|>system
12You are a helpful AI assistant named Securitron<|im_end|>
13<|im_start|>user
14cwe_id:CWE-20
15cwe_name:Improper Input Validation
16affected_line:Pattern Undefined (v3)
17partial_code:example: c4d5ea2f-81a2-4a05-bcd3-202126ae21df
18 name:
19 type: string
20 example: Toolbox
21 serial:
22file_name:itemit_openapi.yaml
23status:True Positive
24reason: There is no pattern property that could lead to insufficient input validation.
25remediation_action: Always define a pattern to ensure strict input validation.
26
27How to fix this?<|im_end|>
28<|im_start|>assistant
29"""
30
31s = time.time()
32
33encodeds = tokenizer(prompt, return_tensors="pt",truncation=True).input_ids.to(device)
34text_streamer = TextStreamer(tokenizer, skip_prompt = True)
35
36# Increase max_new_tokens if needed
37response = finetuned_model.generate(
38 input_ids=encodeds,
39 streamer=text_streamer,
40 max_new_tokens=512,
41 use_cache=True,
42 pad_token_id=151645,
43 eos_token_id=151645,
44 num_return_sequences=1
45 )
46e = time.time()
47print(f'time taken:{e-s}')