Views
No views yet

pip install -U transformers accelerate bitsandbytes, then copy the snippet from the section that is relevant for your usecase.1from transformers import AutoTokenizer, AutoModelForCausalLM
2
3model_name = "UBC-NLP/DetoxLLM-7B"
4
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6model = AutoModelForCausalLM.from_pretrained(model_name)
7
8prompt = "Rewrite the following toxic input into non-toxic version. Let's break the input down step by step to rewrite the non-toxic version. You should first think about the expanation of why the input text is toxic. Then generate the detoxic output. You must preserve the original meaning as much as possible.\nInput: "
9
10input = "Those shithead should stop talking and get the f*ck out of this place"
11input_text = prompt+input+"\n"
12
13input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
14
15outputs = model.generate(**input_ids, do_sample=False)
16print(tokenizer.decode(outputs[0]))1from transformers import AutoTokenizer, AutoModelForCausalLM
2
3model_name = "UBC-NLP/DetoxLLM-7B"
4
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
7
8
9prompt = "Rewrite the following toxic input into non-toxic version. Let's break the input down step by step to rewrite the non-toxic version. You should first think about the expanation of why the input text is toxic. Then generate the detoxic output. You must preserve the original meaning as much as possible.\nInput: "
10
11input = "Those shithead should stop talking and get the f*ck out of this place"
12input_text = prompt+input+"\n"
13
14input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
15
16outputs = model.generate(**input_ids, do_sample=False)
17print(tokenizer.decode(outputs[0]))torch.float161# pip install accelerate
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4model_name = "UBC-NLP/DetoxLLM-7B"
5
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.float16)
8
9prompt = "Rewrite the following toxic input into non-toxic version. Let's break the input down step by step to rewrite the non-toxic version. You should first think about the expanation of why the input text is toxic. Then generate the detoxic output. You must preserve the original meaning as much as possible.\nInput: "
10
11input = "Those shithead should stop talking and get the f*ck out of this place"
12input_text = prompt+input+"\n"
13
14input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
15
16outputs = model.generate(**input_ids, do_sample=False)
17print(tokenizer.decode(outputs[0]))torch.bfloat161from transformers import AutoTokenizer, AutoModelForCausalLM
2
3model_name = "UBC-NLP/DetoxLLM-7B"
4
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype=torch.bfloat16)
7
8prompt = "Rewrite the following toxic input into non-toxic version. Let's break the input down step by step to rewrite the non-toxic version. You should first think about the expanation of why the input text is toxic. Then generate the detoxic output. You must preserve the original meaning as much as possible.\nInput: "
9
10input = "Those shithead should stop talking and get the f*ck out of this place"
11input_text = prompt+input+"\n"
12
13input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
14
15outputs = model.generate(**input_ids, do_sample=False)
16print(tokenizer.decode(outputs[0]))bitsandbytes1from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
2
3quantization_config = BitsAndBytesConfig(load_in_8bit=True)
4
5model_name = "UBC-NLP/DetoxLLM-7B"
6
7tokenizer = AutoTokenizer.from_pretrained(model_name)
8model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=quantization_config)
9
10prompt = "Rewrite the following toxic input into non-toxic version. Let's break the input down step by step to rewrite the non-toxic version. You should first think about the expanation of why the input text is toxic. Then generate the detoxic output. You must preserve the original meaning as much as possible.\nInput: "
11
12input = "Those shithead should stop talking and get the f*ck out of this place"
13input_text = prompt+input+"\n"
14
15input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
16
17outputs = model.generate(**input_ids, do_sample=False)
18print(tokenizer.decode(outputs[0]))1from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
2
3quantization_config = BitsAndBytesConfig(load_in_4bit=True)
4
5model_name = "UBC-NLP/DetoxLLM-7B"
6
7tokenizer = AutoTokenizer.from_pretrained(model_name)
8model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=quantization_config)
9
10prompt = "Rewrite the following toxic input into non-toxic version. Let's break the input down step by step to rewrite the non-toxic version. You should first think about the expanation of why the input text is toxic. Then generate the detoxic output. You must preserve the original meaning as much as possible.\nInput: "
11
12input = "Those shithead should stop talking and get the f*ck out of this place"
13input_text = prompt+input+"\n"
14
15input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
16
17outputs = model.generate(**input_ids, do_sample=False)
18print(tokenizer.decode(outputs[0]))@inproceedings{khondaker-etal-2024-detoxllm,
title = "{D}etox{LLM}: A Framework for Detoxification with Explanations",
author = "Khondaker, Md Tawkat Islam and
Abdul-Mageed, Muhammad and
Lakshmanan, Laks V. S.",
editor = "Al-Onaizan, Yaser and
Bansal, Mohit and
Chen, Yun-Nung",
booktitle = "Proceedings of the 2024 Conference on Empirical Methods in Natural Language Processing",
month = nov,
year = "2024",
address = "Miami, Florida, USA",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2024.emnlp-main.1066",
pages = "19112--19139",
}