Spaces:
Running
on
Zero
Running
on
Zero
Update prompt_check.py
Browse files- prompt_check.py +15 -24
prompt_check.py
CHANGED
|
@@ -1,35 +1,26 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
Answer (only yes or no):"""
|
| 15 |
-
messages = [
|
| 16 |
-
{"role": "user", "content": prompt}
|
| 17 |
-
]
|
| 18 |
|
| 19 |
text = tokenizer.apply_chat_template(
|
| 20 |
messages,
|
| 21 |
tokenize=False,
|
| 22 |
add_generation_prompt=True,
|
| 23 |
-
enable_thinking=False
|
| 24 |
)
|
| 25 |
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
**model_inputs,
|
| 30 |
-
max_new_tokens=10
|
| 31 |
-
)
|
| 32 |
-
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
|
| 33 |
|
| 34 |
content = tokenizer.decode(output_ids, skip_special_tokens=True).strip("\n")
|
| 35 |
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def clean_model_output(text):
|
| 5 |
+
text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL)
|
| 6 |
+
text = re.sub(r"\n*(assistant|user)\n*", "", text)
|
| 7 |
+
text = re.sub(r"\n+", "\n", text).strip()
|
| 8 |
+
return text
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def is_unsafe_prompt(model, tokenizer, system_prompt=None, user_prompt=None, max_new_token=10):
|
| 12 |
+
messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
text = tokenizer.apply_chat_template(
|
| 15 |
messages,
|
| 16 |
tokenize=False,
|
| 17 |
add_generation_prompt=True,
|
| 18 |
+
enable_thinking=False,
|
| 19 |
)
|
| 20 |
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 21 |
|
| 22 |
+
generated_ids = model.generate(**model_inputs, max_new_tokens=max_new_token)
|
| 23 |
+
output_ids = generated_ids[0][-max_new_token:].tolist()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
content = tokenizer.decode(output_ids, skip_special_tokens=True).strip("\n")
|
| 26 |
|