File size: 1,271 Bytes
2c87915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from functools import lru_cache

from transformers import pipeline
import re


@lru_cache(maxsize=1)
def get_classifier():
    return pipeline("text-classification", model="unitary/toxic-bert")

# Custom bad words (Indian context)
bad_words = ["pagal", "chutiya", "boka", "faltu", "idiot", "stupid","nudes","nude","sex","sexy","ass","bitch","dick","pussy","slut","whore","dumb","fool","sucker","loser","jerk","bastard","douchebag","motherfucker","crap","damn"]

def clean_text(text):
    text = text.lower()
    text = text.replace("1", "i").replace("0", "o")
    text = re.sub(r'[^a-zA-Z ]', '', text)
    return text

def has_bad_word(text):
    return any(word in text for word in bad_words)

def predict(text):
    cleaned = clean_text(text)

    classifier = get_classifier()
    result = classifier(cleaned)[0]
    label = result['label']
    score = result['score']
    
    # Convert label to toxicity score
    toxicity_score = score if label == "toxic" else 1 - score

    # Boost score if custom bad word found
    if has_bad_word(cleaned):
        toxicity_score = min(1.0, toxicity_score + 0.2)

    return {
        "input": text,
        "cleaned": cleaned,
        "toxicity_score": round(toxicity_score, 3),
        "is_toxic": toxicity_score > 0.5
    }