Views
No views yet
| Класс | Порог | F1-score | Precision | Recall |
|---|---|---|---|---|
| Profanity | 0.75 | 0.982 | 0.990 | 0.973 |
| Threat | 0.50 | 1.000 | 1.000 | 1.000 |
| Illegal | 0.10 | 0.997 | 1.000 | 0.994 |
1import torch
2from transformers import AutoTokenizer
3from huggingface_hub import hf_hub_download
4import json
5
6# Загрузка модели и токенизатора
7model_path = hf_hub_download(repo_id="AlucardV/kinopotok-toxicity-multitask-model2", filename="pytorch_model.bin")
8config_path = hf_hub_download(repo_id="AlucardV/kinopotok-toxicity-multitask-model2", filename="config.json")
9tokenizer = AutoTokenizer.from_pretrained("AlucardV/kinopotok-toxicity-multitask-model2")
10
11# Определение класса модели
12class MultiTaskToxicityEncoder(torch.nn.Module):
13 def __init__(self, model_name):
14 super().__init__()
15 from transformers import AutoModel
16 self.encoder = AutoModel.from_pretrained(model_name)
17 hidden_size = self.encoder.config.hidden_size
18 self.profanity_head = torch.nn.Linear(hidden_size, 1)
19 self.threat_head = torch.nn.Linear(hidden_size, 1)
20 self.illegal_head = torch.nn.Linear(hidden_size, 1)
21
22 def forward(self, input_ids, attention_mask):
23 outputs = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
24 cls = outputs.last_hidden_state[:, 0, :]
25 return {
26 "profanity": self.profanity_head(cls).squeeze(-1),
27 "threat": self.threat_head(cls).squeeze(-1),
28 "illegal": self.illegal_head(cls).squeeze(-1),
29 }
30
31# Загрузка весов
32config = json.load(open(config_path))
33model = MultiTaskToxicityEncoder(config["model_name"])
34model.load_state_dict(torch.load(model_path, map_location="cpu"))
35model.eval()
36
37# Функция предсказания
38def predict(text):
39 inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
40 with torch.no_grad():
41 outputs = model(inputs["input_ids"], inputs["attention_mask"])
42 probs = {k: torch.sigmoid(v).item() for k, v in outputs.items()}
43 thresholds = {
44 "profanity": 0.7500000000000002,
45 "threat": 0.5000000000000001,
46 "illegal": 0.1,
47 }
48 results = {
49 k: {
50 "toxic": probs[k] >= thresholds[k],
51 "confidence": f"{probs[k]*100:.1f}%"
52 } for k in probs
53 }
54 return results
55
56# Пример
57print(predict("Ты что, совсем охренел, мудак?"))