-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest-model.py
27 lines (21 loc) · 1.04 KB
/
test-model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
model_path = "modified_model"
model_path = "augmxnt/Qwen2-7B-Instruct-deccp"
model_path = "Qwen/Qwen2-7B-Instruct"
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, device_map="auto", torch_dtype=torch.bfloat16)
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
streamer = TextStreamer(tokenizer)
with open("harmful.txt", "r") as f:
harmful = f.readlines()
for prompt in harmful:
print('===')
print(prompt)
print('---')
conversation=[]
conversation.append({"role": "user", "content": prompt})
toks = tokenizer.apply_chat_template(conversation=conversation,
add_generation_prompt=True, return_tensors="pt")
gen = model.generate(toks.to(model.device), streamer=streamer, max_new_tokens=200)
decoded = tokenizer.batch_decode(gen, skip_special_tokens=True)
conversation.append({"role": "assistant", "content": decoded})