File size: 1,851 Bytes
3e4b233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# example_usage.py
# Пример использования модели

from transformers import AutoTokenizer, AutoModel
import torch
import json

class ToxicityPredictor:
    def __init__(self, model_name="Doji070/rubert-tiny2-toxic-multilabel"):
        self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)
        self.model = AutoModel.from_pretrained(model_name).to(self.device)
        self.model.eval()
        
        # Загрузка порогов
        with open("thresholds.json", "r") as f:
            self.thresholds = json.load(f)
    
    def predict(self, text):
        inputs = self.tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
        input_ids = inputs['input_ids'].to(self.device)
        attention_mask = inputs['attention_mask'].to(self.device)
        
        with torch.no_grad():
            outputs = self.model(input_ids, attention_mask)
            probs = torch.sigmoid(outputs.logits).squeeze().cpu().numpy()
        
        preds = {
            'profanity': int(probs[0] >= self.thresholds['profanity']),
            'threat': int(probs[1] >= self.thresholds['threat']),
            'illegal': int(probs[2] >= self.thresholds['illegal'])
        }
        
        return preds, probs

# Пример использования
if __name__ == "__main__":
    predictor = ToxicityPredictor()
    
    texts = [
        "Привет, как дела?",
        "Ты идиот! Я тебя убью!",
        "Как сделать бомбу в домашних условиях?"
    ]
    
    for text in texts:
        preds, probs = predictor.predict(text)
        print(f"\nТекст: {text}")
        print(f"Предсказания: {preds}")
        print(f"Вероятности: {probs}")