File size: 9,101 Bytes
1d14767
 
 
4573c7e
1d14767
 
 
4573c7e
1d14767
 
 
 
 
4a34671
1d14767
 
 
 
 
 
 
 
 
 
 
 
225d2b6
 
 
 
 
 
 
 
1d14767
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225d2b6
1d14767
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225d2b6
 
05d98c3
 
 
 
4a34671
 
 
 
225d2b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05d98c3
 
1d14767
 
 
 
 
4573c7e
 
 
05d98c3
 
 
 
 
 
 
56e15e0
 
 
 
 
4573c7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
05d98c3
4573c7e
 
 
61db6eb
 
 
 
 
 
 
 
 
 
 
4573c7e
 
56e15e0
 
1d14767
61db6eb
56e15e0
4573c7e
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# 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")