File size: 14,755 Bytes
264ec81
92a9089
df4f89b
 
444b43c
264ec81
7edacbb
df4f89b
7edacbb
5de0c48
7edacbb
 
 
 
5de0c48
7edacbb
 
 
 
 
 
5de0c48
7edacbb
 
 
 
 
 
 
 
 
 
5de0c48
 
7edacbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5de0c48
92a9089
 
 
 
 
 
df4f89b
7edacbb
5de0c48
 
92a9089
 
5de0c48
 
92a9089
df4f89b
5de0c48
 
 
 
df4f89b
 
444b43c
264ec81
5de0c48
264ec81
 
df4f89b
 
5de0c48
92a9089
7edacbb
92a9089
 
 
 
 
5de0c48
 
 
 
 
 
92a9089
 
 
df4f89b
5de0c48
92a9089
 
 
 
5de0c48
df4f89b
92a9089
 
 
 
 
 
 
 
 
5de0c48
92a9089
1b3d7c5
df4f89b
 
 
 
 
 
 
 
 
1b3d7c5
92a9089
5de0c48
 
 
 
df4f89b
 
92a9089
df4f89b
 
 
 
 
 
 
 
264ec81
df4f89b
 
 
 
 
 
 
 
 
 
 
 
264ec81
df4f89b
 
 
 
92a9089
5de0c48
92a9089
df4f89b
 
92a9089
df4f89b
1b3d7c5
264ec81
 
 
 
 
 
 
 
 
df4f89b
 
264ec81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df4f89b
264ec81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df4f89b
264ec81
 
df4f89b
264ec81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df4f89b
 
264ec81
 
 
 
df4f89b
264ec81
 
 
 
 
 
 
 
 
df4f89b
264ec81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df4f89b
264ec81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df4f89b
264ec81
 
 
 
 
 
 
 
 
 
 
df4f89b
264ec81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df4f89b
264ec81
 
df4f89b
264ec81
 
 
 
 
df4f89b
264ec81
 
df4f89b
264ec81
 
 
 
 
 
 
 
df4f89b
264ec81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df4f89b
 
 
 
 
 
 
 
 
 
 
264ec81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df4f89b
 
264ec81
 
 
df4f89b
 
264ec81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df4f89b
264ec81
 
 
 
 
 
 
df4f89b
264ec81
 
 
 
 
 
 
 
 
 
 
92a9089
df4f89b
 
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import gradio as gr
import joblib
import os


def load_model():
    """Load model from local files in the Space"""
    try:
        print("Loading model from local files...")
        
        print("\nFiles in current directory:")
        for f in os.listdir("."):
            if f.endswith((".jobilib", ".pkl", ".txt")):
                print(f"  - {f}")
        
       
        model_files = [
            "tfidf_logreg_best.jobilib",
            "model.jobilib",
            "model.pkl"
        ]
        
        model = None
        for mf in model_files:
            if os.path.exists(mf):
                print(f"\nLoading model: {mf}")
                model = joblib.load(mf)
                print(f"✅ Model loaded from {mf}")
                break
        
        if model is None:
            print("❌ No model file found!")
            return None
        
        
        vectorizer_files = [
            "vocab.txt",
            "vocab",
            "vectorizer.jobilib",
            "tfidf.jobilib"
        ]
        
        vectorizer = None
        for vf in vectorizer_files:
            if os.path.exists(vf):
                print(f"Loading vectorizer: {vf}")
                vectorizer = joblib.load(vf)
                print(f"✅ Vectorizer loaded from {vf}")
                break
        
        if vectorizer is None:
            print("⚠️ Vectorizer not found")
        
        # Try to load label encoder
        label_encoder = None
        if os.path.exists("label_encoder.jobilib"):
            print("Loading label encoder...")
            label_encoder = joblib.load("label_encoder.jobilib")
            print("✅ Label encoder loaded")
        
        return {
            "model": model,
            "vectorizer": vectorizer,
            "label_encoder": label_encoder
        }
        
    except Exception as e:
        print(f"❌ Error loading model: {str(e)}")
        import traceback
        print(traceback.format_exc())
        return None

# Load model on startup
print("Starting model loading...")
model_components = load_model()

if model_components is None:
    print("⚠️ Model loading failed!")
else:
    print("✅ All models loaded successfully!")



def predict(text):
    """Predict cyberbullying category"""
    if not text.strip():
        return "<div class='warn'>⚠️ Please enter some text.</div>"
    
    try:
        # Check if models are loaded
        if model_components is None:
            return "<div class='warn'>❌ Model not loaded. Check logs.</div>"
        
        model = model_components["model"]
        vectorizer = model_components["vectorizer"]
        label_encoder = model_components["label_encoder"]
        
        # Check if vectorizer exists
        if vectorizer is None:
            return "<div class='warn'>❌ Vectorizer not available</div>"
        
        # Vectorize the input text
        text_vector = vectorizer.transform([text])
        
        # Get prediction
        prediction = model.predict(text_vector)[0]
        
        # Get confidence score
        try:
            probabilities = model.predict_proba(text_vector)[0]
            score = max(probabilities)
        except:
            score = 0.8
        
        # Decode label if encoder exists
        if label_encoder is not None:
            try:
                label = label_encoder.inverse_transform([prediction])[0]
            except:
                label = str(prediction)
        else:
            label = str(prediction)
        
        print(f"Prediction: {label}, Score: {score:.4f}")
        
        # Category definitions
        cyberbullying_types = {
            "age": {"emoji": "👶", "color": "#ff6b6b", "text": "Age-Based Cyberbullying"},
            "gender": {"emoji": "⚥️", "color": "#ff8c42", "text": "Gender-Based Cyberbullying"},
            "ethnicity": {"emoji": "🌍", "color": "#ffa502", "text": "Ethnicity-Based Cyberbullying"},
            "religion": {"emoji": "🙏", "color": "#ff6b9d", "text": "Religion-Based Cyberbullying"},
            "other_cyberbullying": {"emoji": "⚠️", "color": "#ff4757", "text": "Other Cyberbullying Detected"},
            "not_cyberbullying": {"emoji": "✅", "color": "#00ff64", "text": "Safe Message"}
        }
        
        # Get category info
        label_lower = str(label).lower().strip()
        category = cyberbullying_types.get(
            label_lower, 
            cyberbullying_types.get(label, cyberbullying_types["not_cyberbullying"])
        )
        
        # Safe message
        if label_lower == "not_cyberbullying":
            return f"""
            <div class='safe'>
                <div class='checkmark'>{category['emoji']}</div>
                <div class='safe-text'>{category['text']}</div>
                <div class='confidence-bar'>
                    <div class='confidence-fill safe-fill' style='width: {score*100}%'></div>
                </div>
                <span class='confidence-score'>Confidence: {score:.2%}</span>
            </div>
            """
        else:
            # Cyberbullying message
            return f"""
            <div class='bully'>
                <div class='warning-icon'>{category['emoji']}</div>
                <div class='bully-text'>{category['text']}</div>
                <div class='label-badge' style='background: {category["color"]}33; border-color: {category["color"]};'>{label}</div>
                <div class='confidence-bar'>
                    <div class='confidence-fill bully-fill' style='width: {score*100}%; background: {category["color"]};'></div>
                </div>
                <span class='confidence-score'>Confidence: {score:.2%}</span>
            </div>
            """
            
    except Exception as e:
        import traceback
        error_msg = traceback.format_exc()
        print(f"ERROR in prediction: {str(e)}")
        print(error_msg)
        return f"<div class='warn'>❌ Error: {str(e)}</div>"



with gr.Blocks(css="""
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"/>

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    background: linear-gradient(-45deg, #7d00ff, #5500ff, #4b7fff, #0099ff, #00bfff);
    background-size: 500% 500%;
    animation: gradientBG 15s ease infinite;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    min-height: 100vh;
}

@keyframes gradientBG {
    0% {background-position: 0% 50%;}
    25% {background-position: 100% 50%;}
    50% {background-position: 100% 100%;}
    75% {background-position: 0% 100%;}
    100% {background-position: 0% 50%;}
}

.gradio-container {
    background: rgba(20, 20, 40, 0.85) !important;
    border-radius: 25px !important;
    box-shadow: 0 25px 80px rgba(0, 0, 0, 0.4), inset 0 0 30px rgba(255, 255, 255, 0.1) !important;
    backdrop-filter: blur(15px) !important;
    border: 1px solid rgba(255, 255, 255, 0.15) !important;
    padding: 40px !important;
}

.title {
    font-size: 52px;
    font-weight: 900;
    text-align: center;
    background: linear-gradient(135deg, #7d00ff, #5500ff, #4b7fff, #0099ff);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    animation: slideInDown 1s ease-out, glow 3s ease-in-out infinite;
    margin-bottom: 10px;
}

@keyframes slideInDown {
    from {
        opacity: 0;
        transform: translateY(-50px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes glow {
    0%, 100% {
        text-shadow: 0 0 20px rgba(125, 0, 255, 0.5), 0 0 40px rgba(75, 127, 255, 0.3);
    }
    50% {
        text-shadow: 0 0 30px rgba(75, 127, 255, 0.6), 0 0 60px rgba(0, 153, 255, 0.4);
    }
}

.subtitle {
    text-align: center;
    color: #e0e0ff;
    margin-bottom: 30px;
    font-style: italic;
    font-size: 16px;
    animation: fadeInUp 1s ease-out 0.2s both;
    letter-spacing: 1px;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

textarea {
    background: rgba(255, 255, 255, 0.08) !important;
    border: 2px solid rgba(75, 127, 255, 0.3) !important;
    border-radius: 15px !important;
    padding: 18px !important;
    font-size: 16px !important;
    color: #e0e0ff !important;
    transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) !important;
    box-shadow: 0 8px 32px rgba(75, 127, 255, 0.1), inset 0 0 20px rgba(255, 255, 255, 0.05) !important;
    backdrop-filter: blur(10px) !important;
}

textarea::placeholder {
    color: rgba(224, 224, 255, 0.5) !important;
}

textarea:focus {
    border-color: #0099ff !important;
    box-shadow: 0 15px 50px rgba(0, 153, 255, 0.4), inset 0 0 30px rgba(0, 153, 255, 0.1) !important;
    transform: translateY(-5px) scale(1.02);
}

.btn-primary {
    background: linear-gradient(135deg, #7d00ff, #5500ff, #4b7fff, #0099ff) !important;
    background-size: 300% 300% !important;
    border: 2px solid rgba(75, 127, 255, 0.5) !important;
    color: white !important;
    font-weight: 700 !important;
    font-size: 16px !important;
    padding: 14px 40px !important;
    border-radius: 50px !important;
    cursor: pointer !important;
    transition: all 0.4s ease !important;
    box-shadow: 0 10px 40px rgba(75, 127, 255, 0.4) !important;
    animation: bounceIn 0.8s cubic-bezier(0.68, -0.55, 0.265, 1.55) 0.4s both;
    position: relative;
    overflow: hidden;
}

@keyframes bounceIn {
    0% {
        opacity: 0;
        transform: scale(0.1) rotateZ(-45deg);
    }
    50% {
        opacity: 1;
        transform: scale(1.1) rotateZ(10deg);
    }
    100% {
        transform: scale(1) rotateZ(0deg);
    }
}

.btn-primary:hover {
    transform: translateY(-5px) scale(1.05);
    box-shadow: 0 20px 60px rgba(0, 153, 255, 0.6) !important;
    background-position: 100% 0 !important;
}

.btn-primary:active {
    transform: translateY(-2px) scale(0.98);
}

.safe {
    background: linear-gradient(135deg, rgba(0, 255, 100, 0.15), rgba(100, 255, 150, 0.08));
    border: 2px solid rgba(0, 255, 100, 0.4);
    padding: 35px;
    border-radius: 20px;
    color: #00ff64;
    text-align: center;
    animation: slideInRight 0.8s cubic-bezier(0.34, 1.56, 0.64, 1), cardGlowGreen 3s ease-in-out infinite;
    box-shadow: 0 20px 60px rgba(0, 255, 100, 0.25);
    backdrop-filter: blur(15px);
}

.bully {
    background: linear-gradient(135deg, rgba(255, 0, 0, 0.15), rgba(255, 100, 100, 0.08));
    border: 2px solid rgba(255, 107, 107, 0.4);
    padding: 35px;
    border-radius: 20px;
    color: #ff6b6b;
    text-align: center;
    animation: slideInRight 0.8s cubic-bezier(0.34, 1.56, 0.64, 1), cardGlowRed 2s ease-in-out infinite;
    box-shadow: 0 20px 60px rgba(255, 107, 107, 0.25);
    backdrop-filter: blur(15px);
}

@keyframes slideInRight {
    from {
        opacity: 0;
        transform: translateX(100px) rotateY(20deg);
    }
    to {
        opacity: 1;
        transform: translateX(0) rotateY(0deg);
    }
}

@keyframes cardGlowGreen {
    0%, 100% {
        box-shadow: 0 20px 60px rgba(0, 255, 100, 0.25);
    }
    50% {
        box-shadow: 0 30px 80px rgba(0, 255, 100, 0.4);
    }
}

@keyframes cardGlowRed {
    0%, 100% {
        box-shadow: 0 20px 60px rgba(255, 107, 107, 0.25);
    }
    50% {
        box-shadow: 0 30px 80px rgba(255, 107, 107, 0.4);
    }
}

.warn {
    color: #ffb700;
    text-align: center;
    font-weight: 700;
    font-size: 18px;
    animation: shake 0.5s ease-in-out;
    padding: 20px;
}

@keyframes shake {
    0%, 100% {transform: translateX(0);}
    10%, 30%, 50%, 70%, 90% {transform: translateX(-8px);}
    20%, 40%, 60%, 80% {transform: translateX(8px);}
}

.checkmark, .warning-icon {
    font-size: 56px;
    margin-bottom: 15px;
    animation: bounce 0.8s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    display: inline-block;
}

@keyframes bounce {
    0% {
        opacity: 0;
        transform: scale(0) rotateZ(-45deg);
    }
    50% {
        transform: scale(1.2) rotateZ(15deg);
    }
    100% {
        opacity: 1;
        transform: scale(1) rotateZ(0deg);
    }
}

.safe-text, .bully-text {
    font-size: 28px;
    font-weight: 800;
    margin-bottom: 15px;
    animation: fadeInDown 0.8s ease-out 0.2s both;
}

.label-badge {
    display: inline-block;
    background: rgba(255, 107, 107, 0.2);
    border: 2px solid rgba(255, 107, 107, 0.5);
    padding: 10px 20px;
    border-radius: 25px;
    margin-bottom: 18px;
    font-size: 14px;
    font-weight: 700;
    animation: zoomIn 0.7s ease-out 0.3s both;
}

@keyframes zoomIn {
    from {
        opacity: 0;
        transform: scale(0.3);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes fadeInDown {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.confidence-bar {
    width: 100%;
    height: 10px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    margin-bottom: 15px;
    overflow: hidden;
    animation: fadeIn 0.8s ease-out 0.1s both;
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.confidence-fill {
    height: 100%;
    border-radius: 12px;
    transition: width 1.2s cubic-bezier(0.34, 1.56, 0.64, 1);
    animation: fillWidth 1.2s ease-out;
}

@keyframes fillWidth {
    from {
        width: 0 !important;
    }
}

.safe-fill {
    background: linear-gradient(90deg, #00ff64, #00d452);
    box-shadow: 0 0 25px rgba(0, 255, 100, 0.8);
}

.bully-fill {
    background: linear-gradient(90deg, #ff6b6b, #ff4444);
    box-shadow: 0 0 25px rgba(255, 107, 107, 0.8);
}

.confidence-score {
    font-size: 15px;
    opacity: 0.9;
    animation: fadeIn 0.8s ease-out 0.4s both;
    font-weight: 600;
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@media (max-width: 768px) {
    .title {
        font-size: 36px;
    }
    
    .safe, .bully {
        padding: 25px;
    }
}
""") as demo:
    
    gr.Markdown("<div class='title'>🛡️ Cyberbullying Detection System</div>")
    gr.Markdown("<div class='subtitle'>End-to-end NLP system for detecting cyberbullying, including religion-based abuse</div>")
    
    with gr.Group():
        text_input = gr.Textbox(
            lines=4,
            placeholder="Enter a message to analyze...",
            label="Input Text"
        )
        detect_btn = gr.Button("🔍 Detect", variant="primary")
    
    output = gr.HTML()
    
    detect_btn.click(
        fn=predict,
        inputs=text_input,
        outputs=output
    )


if __name__ == "__main__":
    demo.launch()