Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2from peft import PeftModel, PeftConfig
3
4# Load the base model
5model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
6tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
7
8# Load the LoRA adapter
9model = PeftModel.from_pretrained(model, "DDiaa/WM-Removal-EXP-Llama-2-7B")
10
11# Prepare the prompt
12
13system_prompt = (
14 "You are an expert copy-editor. Please rewrite the following text in your own voice and paraphrase all "
15 "sentences.\n Ensure that the final output contains the same information as the original text and has "
16 "roughly the same length.\n Do not leave out any important details when rewriting in your own voice. Do "
17 "not include any information that is not present in the original text. Do not respond with a greeting or "
18 "any other extraneous information. Skip the preamble. Just rewrite the text directly."
19)
20
21def paraphrase_text(text):
22 # Prepare prompt
23 prompt = tokenizer.apply_chat_template(
24 [
25 {"role": "system", "content": system_prompt},
26 {"role": "user", "content": f"\n[[START OF TEXT]]\n{text}\n[[END OF TEXT]]"},
27 ],
28 tokenize=False,
29 add_generation_prompt=True,
30 ) + "[[START OF PARAPHRASE]]\n"
31
32 # Generate paraphrase
33 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
34 outputs = model.generate(
35 **inputs,
36 max_new_tokens=512,
37 temperature=1.0,
38 do_sample=True,
39 pad_token_id=tokenizer.pad_token_id
40 )
41
42 # Post-process output
43 paraphrased = tokenizer.decode(outputs[0], skip_special_tokens=True)
44 paraphrased = paraphrased.split("[[START OF PARAPHRASE]]")[1].split("[[END OF")[0].strip()
45
46 return paraphrased1@article{diaa2024optimizing,
2 title={Optimizing adaptive attacks against content watermarks for language models},
3 author={Diaa, Abdulrahman and Aremu, Toluwani and Lukas, Nils},
4 journal={arXiv preprint arXiv:2410.02440},
5 year={2024}
6}