Spaces:
Sleeping
Sleeping
| # import gradio as gr | |
| # from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| # import torch | |
| # tokenizer = AutoTokenizer.from_pretrained("sourabh5500/hate-speech-muril") | |
| # model = AutoModelForSequenceClassification.from_pretrained("sourabh5500/hate-speech-muril") | |
| # model.eval() | |
| # # BLOCKLIST = [ | |
| # # # English profanity | |
| # # "fuck", "fucking", "fucker", "shit", "bitch", "asshole", | |
| # # "dick", "pussy", "whore", "slut", "cock", "cunt", "bastard", | |
| # # "damn", "crap", "piss", "nigger", "nigga", "faggot", | |
| # # # Hindi / Hinglish slang & profanity | |
| # # "bsdk", "bhosdike", "bhosdi", "mc", "madarchod", "madar", | |
| # # "bc", "behenchod", "behen", "chutiya", "chutiye", "chuti", | |
| # # "gand", "gaand", "lund", "loda", "lauda", "laude", | |
| # # "harami", "kaminey", "kamine", "bewakoof", "bhak", | |
| # # "aabe", "abe", "suar", "kutty", "kutte", "kutta", "kuttiya", | |
| # # "randi", "randi ke", "bache", "saale", "saala", "gandu", | |
| # # "gandi", "bhen", "behen ke", "chod", "chodu", "chodne", | |
| # # "tatte", "tatti", "bhadwa", "bhadve", "pataak", "chinal", | |
| # # ] | |
| # BLOCKLIST = [ | |
| # # Original Hinglish / Hindi Slang | |
| # "bsdk", "bhosdike", "bhosdi", "mc", "madarchod", "madar", | |
| # "bc", "behenchod", "behen", "chutiya", "chutiye", "chuti", | |
| # "gand", "gaand", "lund", "loda", "lauda", "laude", | |
| # "harami", "kaminey", "kamine", "bewakoof", "bhak", | |
| # "aabe", "abe", "suar", "kutty", "kutte", "kutta", "kuttiya", | |
| # "randi", "randi ke", "bache", "saale", "saala", "gandu", | |
| # "gandi", "bhen", "behen ke", "chod", "chodu", "chodne", | |
| # "tatte", "tatti", "bhadwa", "bhadve", "pataak", "chinal", | |
| # # Additional Hinglish / Hindi | |
| # "jhaat", "jhant", "jhantu", "bakland", "gashti", "gasti", "ghasti", | |
| # "chut", "chutiyo", "chutia", "chutiyappa", | |
| # "madarchodh", "madrchod", "maderchod", "madarchhod", | |
| # "behenchodh", "bhenchod", "behenchhod", | |
| # "bhosadike", "bhosdika", "bhosdiki", | |
| # "gaandu", "gaandfat", "gaandmasti", | |
| # "kameena", "kaminee", "kamin", "kamina", "kamini", | |
| # "lavda", "lavde", "lawda", "lounde", "lunda", | |
| # "haramzada", "haramkhor", "haraami", | |
| # "najayaz", "nalayak", | |
| # "teri maa", "teri behen", "maa ka bhosda", | |
| # "ullu ke pathe", "kutte ki jat", "bhains ki aulad", | |
| # "saale kutta", "saali kutti", | |
| # # English Profanity (Cleaned of false positives) | |
| # "fuck", "fucking", "fucker", "fucked", "fck", "fuk", "fuuck", | |
| # "shit", "shitty", "shyt", "shithead", | |
| # "bitch", "bitches", "bytch", "b!tch", "biatch", | |
| # "asshole", "assholes", "ashole", "dumbass", "jackass", | |
| # "bastard", "bastards", | |
| # "dick", "dickhead", "d!ck", "dickweed", | |
| # "pussy", "cunt", "kunt", | |
| # "slut", "slutty", "sluts", | |
| # "whore", | |
| # "nigger", "nigga", | |
| # "faggot", "fag", | |
| # "retard", "retarded", | |
| # "cock", "cockhead", | |
| # "twat", | |
| # "damn", "dammit", "goddamn", "goddammit", | |
| # "piss", "pissed", "pissoff", | |
| # "bollocks", "bugger", | |
| # "bullshit", "crap", "crappy", | |
| # "cum", "cumshot", | |
| # "dyke", "homo", | |
| # "jackoff", "jerkoff", "jizz", | |
| # "milf", "mofo", "motherfucker", | |
| # "prick", "scrotum", "semen", | |
| # "tit", "tits", "titty", | |
| # "turd", "wank", "wanker", | |
| # ] | |
| # def predict(text: str, check_type: str = "note") -> dict: | |
| # if not text or not text.strip(): | |
| # return {"is_abusive": False, "confidence": 0} | |
| # text_lower = text.lower() | |
| # # Check blocklist first (instant detection) | |
| # for word in BLOCKLIST: | |
| # if word in text_lower: | |
| # return {"is_abusive": True, "confidence": 1.0} | |
| # # β For names, only use blocklist (skip AI to avoid false positives) | |
| # if check_type == "name": | |
| # return {"is_abusive": False, "confidence": 0} | |
| # # Model inference for notes | |
| # inputs = tokenizer( | |
| # text, | |
| # return_tensors="pt", | |
| # max_length=128, | |
| # truncation=True, | |
| # padding=True | |
| # ) | |
| # with torch.no_grad(): | |
| # outputs = model(**inputs) | |
| # probs = torch.softmax(outputs.logits, dim=1)[0] | |
| # hof_score = probs[1].item() | |
| # return { | |
| # "is_abusive": hof_score > 0.5, | |
| # "confidence": round(hof_score, 3) | |
| # } | |
| # iface = gr.Interface( | |
| # fn=predict, | |
| # inputs=[ | |
| # gr.Textbox(lines=3, placeholder="Enter text to check..."), | |
| # gr.Radio(["note", "name"], value="note", label="Check type") | |
| # ], | |
| # outputs="json", | |
| # title="SafeShield AI - Hate Speech Detection", | |
| # description="Detects hate speech in Hinglish/English text" | |
| # ) | |
| # iface.launch(server_name="0.0.0.0") | |
| import os | |
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification | |
| import torch | |
| tokenizer = AutoTokenizer.from_pretrained("sourabh5500/hate-speech-muril") | |
| model = AutoModelForSequenceClassification.from_pretrained("sourabh5500/hate-speech-muril") | |
| model.eval() | |
| BLOCKLIST = [ | |
| # Original Hinglish / Hindi Slang | |
| "bsdk", "bhosdike", "bhosdi", "mc", "madarchod", "madar", | |
| "bc", "behenchod", "behen", "chutiya", "chutiye", "chuti", | |
| "gand", "gaand", "lund", "loda", "lauda", "laude", | |
| "harami", "kaminey", "kamine", "bewakoof", "bhak", | |
| "aabe", "abe", "suar", "kutty", "kutte", "kutta", "kuttiya", | |
| "randi", "randi ke", "bache", "saale", "saala", "gandu", | |
| "gandi", "bhen", "behen ke", "chod", "chodu", "chodne", | |
| "tatte", "tatti", "bhadwa", "bhadve", "pataak", "chinal", | |
| # Additional Hinglish / Hindi | |
| "jhaat", "jhant", "jhantu", "bakland", "gashti", "gasti", "ghasti", | |
| "chut", "chutiyo", "chutia", "chutiyappa", | |
| "madarchodh", "madrchod", "maderchod", "madarchhod", | |
| "behenchodh", "bhenchod", "behenchhod", | |
| "bhosadike", "bhosdika", "bhosdiki", | |
| "gaandu", "gaandfat", "gaandmasti", | |
| "kameena", "kaminee", "kamin", "kamina", "kamini", | |
| "lavda", "lavde", "lawda", "lounde", "lunda", | |
| "haramzada", "haramkhor", "haraami", | |
| "najayaz", "nalayak", | |
| "teri maa", "teri behen", "maa ka bhosda", | |
| "ullu ke pathe", "kutte ki jat", "bhains ki aulad", | |
| "saale kutta", "saali kutti", | |
| # English Profanity (Cleaned of false positives) | |
| "fuck", "fucking", "fucker", "fucked", "fck", "fuk", "fuuck", | |
| "shit", "shitty", "shyt", "shithead", | |
| "bitch", "bitches", "bytch", "b!tch", "biatch", | |
| "asshole", "assholes", "ashole", "dumbass", "jackass", | |
| "bastard", "bastards", | |
| "dick", "dickhead", "d!ck", "dickweed", | |
| "pussy", "cunt", "kunt", | |
| "slut", "slutty", "sluts", | |
| "whore", | |
| "nigger", "nigga", | |
| "faggot", "fag", | |
| "retard", "retarded", | |
| "cock", "cockhead", | |
| "twat", | |
| "damn", "dammit", "goddamn", "goddammit", | |
| "piss", "pissed", "pissoff", | |
| "bollocks", "bugger", | |
| "bullshit", "crap", "crappy", | |
| "cum", "cumshot", | |
| "dyke", "homo", | |
| "jackoff", "jerkoff", "jizz", | |
| "milf", "mofo", "motherfucker", | |
| "prick", "scrotum", "semen", | |
| "tit", "tits", "titty", | |
| "turd", "wank", "wanker", | |
| ] | |
| def predict(text: str, check_type: str = "note", secret_key: str = "") -> dict: | |
| # π Secret key validation | |
| if secret_key != os.environ.get("HOPIN_API_SECRET"): | |
| return {"is_abusive": False, "confidence": 0, "error": "Unauthorized"} | |
| if not text or not text.strip(): | |
| return {"is_abusive": False, "confidence": 0} | |
| text_lower = text.lower() | |
| # Check blocklist first (instant detection) | |
| for word in BLOCKLIST: | |
| if word in text_lower: | |
| return {"is_abusive": True, "confidence": 1.0} | |
| # β For names, only use blocklist (skip AI to avoid false positives) | |
| if check_type == "name": | |
| return {"is_abusive": False, "confidence": 0} | |
| # Model inference for notes | |
| inputs = tokenizer( | |
| text, | |
| return_tensors="pt", | |
| max_length=128, | |
| truncation=True, | |
| padding=True | |
| ) | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| probs = torch.softmax(outputs.logits, dim=1)[0] | |
| hof_score = probs[1].item() | |
| return { | |
| "is_abusive": hof_score > 0.5, | |
| "confidence": round(hof_score, 3) | |
| } | |
| # iface = gr.Interface( | |
| # fn=predict, | |
| # inputs=[ | |
| # gr.Textbox(lines=3, placeholder="Enter text to check..."), | |
| # gr.Radio(["note", "name"], value="note", label="Check type"), | |
| # gr.Textbox(visible=False, label="Secret Key") # Hidden from UI but required for API | |
| # ], | |
| # outputs="json", | |
| # title="SafeShield AI - Hate Speech Detection", | |
| # description="Detects hate speech in Hinglish/English text" | |
| # ) | |
| iface = gr.Interface( | |
| fn=predict, | |
| inputs=[ | |
| gr.Textbox(lines=3, placeholder="Enter text to check..."), | |
| gr.Radio(["note", "name"], value="note", label="Check type"), | |
| gr.Textbox(label="π Secret Key (required)", placeholder="Enter your API secret key") # Made visible! | |
| ], | |
| outputs="json", | |
| title="SafeShield AI - Hate Speech Detection", | |
| description="Detects hate speech in Hinglish/English text" | |
| ) | |
| iface.launch(server_name="0.0.0.0") |