-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoxigen_hatebert.py
61 lines (47 loc) · 1.73 KB
/
toxigen_hatebert.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import argparse
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from guardbench import benchmark
def moderate(
conversations: list[list[dict[str, str]]],
tokenizer: AutoTokenizer,
model: AutoModelForSequenceClassification,
) -> list[float]:
# Convert conversations to single texts by concatenation
texts = ["\n".join([y["content"] for y in x]) for x in conversations]
# Tokenize texts
input = tokenizer(
texts,
max_length=512,
padding=True,
truncation=True,
return_tensors="pt",
)
# Move input to model device
input = {k: v.to(model.device) for k, v in input.items()}
# Compute logits
logits = model(**input).logits
# Compute "unsafe" probabilities
return torch.softmax(logits, dim=-1)[:, 1].tolist()
def main(device: str, datasets: list[str], batch_size: int) -> None:
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
model = AutoModelForSequenceClassification.from_pretrained("tomh/toxigen_hatebert")
model = model.to(device)
model = model.eval()
benchmark(
moderate=moderate,
model_name="ToxiGen HateBERT",
batch_size=batch_size,
datasets=datasets,
# Moderate kwargs
model=model,
tokenizer=tokenizer,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--device", default="cuda", type=str, help="Device")
parser.add_argument("--datasets", nargs="+", default="all", help="Datasets")
parser.add_argument("--batch_size", default=512, type=int, help="Batch size")
args = parser.parse_args()
with torch.no_grad():
main(args.device, args.datasets, args.batch_size)