File size: 27,537 Bytes
f8c08a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0df90de
f8c08a4
 
 
 
 
 
 
 
 
 
 
0df90de
f8c08a4
 
 
0df90de
f8c08a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# app.py - Enhanced Ensemble Model for Meme and Text Analysis
import gradio as gr
import torch
import torch.nn as nn
import numpy as np
from PIL import Image
import requests
from io import BytesIO
import easyocr
import cv2
import re
from urllib.parse import urlparse
import json
import logging
from typing import Dict, List, Tuple, Optional
import warnings
warnings.filterwarnings("ignore")

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Import transformers components
from transformers import (
    AutoTokenizer, AutoModelForSequenceClassification,
    AutoProcessor, AutoModel, SiglipVisionModel,
    SiglipProcessor, pipeline
)

class EnhancedEnsembleMemeAnalyzer:
    def __init__(self):
        """Initialize the enhanced ensemble model with best available models"""
        self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
        logger.info(f"Using device: {self.device}")
        
        # Initialize models
        self.setup_models()
        self.setup_ocr()
        self.setup_ensemble_weights()
        
    def setup_models(self):
        """Initialize BERT and SigLIP models with error handling"""
        try:
            # Load your fine-tuned BERT model (93% accuracy)
            logger.info("Loading fine-tuned BERT model...")
            self.bert_tokenizer = AutoTokenizer.from_pretrained("./fine_tuned_bert_sentiment")
            self.bert_model = AutoModelForSequenceClassification.from_pretrained("./fine_tuned_bert_sentiment")
            self.bert_model.to(self.device)
            logger.info("βœ… Fine-tuned BERT loaded successfully!")
            
        except Exception as e:
            logger.warning(f"⚠️ Could not load custom BERT model: {e}")
            logger.info("Loading fallback BERT model...")
            # Fallback to high-performance public model
            self.bert_tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
            self.bert_model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment-latest")
            self.bert_model.to(self.device)
        
        try:
            # Load the best available SigLIP model (Large version)
            logger.info("Loading SigLIP-Large model...")
            self.siglip_processor = AutoProcessor.from_pretrained("google/siglip-large-patch16-384")
            self.siglip_model = AutoModel.from_pretrained("google/siglip-large-patch16-384")
            self.siglip_model.to(self.device)
            
            # Enhanced hate speech classifier on top of SigLIP features
            self.hate_classifier = nn.Sequential(
                nn.Linear(1152, 512),  # SigLIP-Large has 1152 dim features
                nn.ReLU(),
                nn.Dropout(0.3),
                nn.Linear(512, 256),
                nn.ReLU(),
                nn.Dropout(0.2),
                nn.Linear(256, 4)  # Multi-class: safe, hateful, offensive, spam
            ).to(self.device)
            
            logger.info("βœ… SigLIP-Large loaded successfully!")
            
        except Exception as e:
            logger.warning(f"⚠️ Could not load SigLIP-Large, trying base model: {e}")
            # Fallback to base model
            self.siglip_processor = AutoProcessor.from_pretrained("google/siglip-base-patch16-224")
            self.siglip_model = AutoModel.from_pretrained("google/siglip-base-patch16-224")
            self.siglip_model.to(self.device)
            
            self.hate_classifier = nn.Sequential(
                nn.Linear(768, 256),
                nn.ReLU(),
                nn.Dropout(0.2),
                nn.Linear(256, 4)
            ).to(self.device)
    
    def setup_ocr(self):
        """Initialize OCR with multiple engines for better accuracy"""
        try:
            # Primary OCR: EasyOCR (good for memes)
            self.ocr_reader = easyocr.Reader(['en'], gpu=torch.cuda.is_available())
            logger.info("βœ… EasyOCR initialized")
            
            # Backup OCR: We'll use cv2 + basic text detection as fallback
            self.use_easyocr = True
            
        except Exception as e:
            logger.warning(f"⚠️ OCR initialization issue: {e}")
            self.use_easyocr = False
    
    def setup_ensemble_weights(self):
        """Initialize ensemble weights and thresholds"""
        self.ensemble_weights = {
            'text_sentiment': 0.4,
            'image_content': 0.35,
            'multimodal_context': 0.25
        }
        
        self.risk_thresholds = {
            'high_risk': 0.8,
            'medium_risk': 0.6,
            'low_risk': 0.4
        }
        
        # Hate speech keywords for additional context
        self.hate_keywords = [
            'hate', 'kill', 'death', 'violence', 'attack', 
            'discriminate', 'racist', 'nazi', 'terrorist'
        ]
    
    def extract_text_from_image(self, image: Image.Image) -> str:
        """Enhanced OCR text extraction with multiple methods"""
        extracted_texts = []
        
        try:
            if self.use_easyocr:
                # Method 1: EasyOCR
                img_array = np.array(image)
                results = self.ocr_reader.readtext(img_array, detail=0)
                if results:
                    easyocr_text = ' '.join(results)
                    extracted_texts.append(easyocr_text)
                    logger.info(f"EasyOCR extracted: {easyocr_text[:100]}...")
            
            # Method 2: Basic OpenCV preprocessing + simple text detection
            img_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
            gray = cv2.cvtColor(img_cv, cv2.COLOR_BGR2GRAY)
            
            # Enhance text regions
            kernel = np.ones((1,1), np.uint8)
            processed = cv2.morphologyEx(gray, cv2.MORPH_CLOSE, kernel)
            
            # This is a simplified approach - in production you'd use more sophisticated methods
            
        except Exception as e:
            logger.error(f"OCR Error: {e}")
        
        # Combine and clean extracted text
        final_text = ' '.join(extracted_texts) if extracted_texts else ""
        return self.clean_text(final_text)
    
    def clean_text(self, text: str) -> str:
        """Clean and preprocess text"""
        if not text:
            return ""
        
        # Remove extra whitespace and special characters
        text = re.sub(r'\s+', ' ', text)
        text = re.sub(r'[^\w\s\.\!\?\,\-\:\;\(\)]', '', text)
        
        return text.strip().lower()
    
    def analyze_sentiment(self, text: str) -> Dict:
        """Analyze sentiment using fine-tuned BERT with confidence calibration"""
        if not text.strip():
            return {"label": "NEUTRAL", "score": 0.5, "probabilities": [0.33, 0.34, 0.33]}
        
        try:
            inputs = self.bert_tokenizer(
                text, 
                return_tensors="pt", 
                truncation=True, 
                padding=True, 
                max_length=512
            ).to(self.device)
            
            with torch.no_grad():
                outputs = self.bert_model(**inputs)
                probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
                
            # Get predictions
            predicted_class = torch.argmax(probabilities, dim=-1).item()
            confidence = torch.max(probabilities).item()
            probs_list = probabilities[0].cpu().tolist()
            
            # Map to sentiment labels (adjust based on your model's configuration)
            if len(probs_list) == 3:
                label_mapping = {0: "NEGATIVE", 1: "NEUTRAL", 2: "POSITIVE"}
            else:
                label_mapping = {0: "NEGATIVE", 1: "POSITIVE"}
            
            return {
                "label": label_mapping.get(predicted_class, "UNKNOWN"),
                "score": confidence,
                "probabilities": probs_list
            }
            
        except Exception as e:
            logger.error(f"Sentiment analysis error: {e}")
            return {"label": "NEUTRAL", "score": 0.5, "probabilities": [0.5, 0.5]}
    
    def classify_multimodal_content(self, image: Image.Image, text: str = "") -> Dict:
        """Enhanced multimodal classification using SigLIP"""
        try:
            # Prepare comprehensive text queries for zero-shot classification
            hate_queries = [
                "hateful meme targeting specific groups",
                "discriminatory content with offensive imagery",
                "violent or threatening visual content",
                "meme promoting hatred or discrimination",
                "offensive visual propaganda",
                "cyberbullying visual content"
            ]
            
            safe_queries = [
                "harmless funny meme",
                "positive social media content",
                "safe entertainment image",
                "normal social media post",
                "friendly humorous content",
                "non-offensive visual content"
            ]
            
            # Include context from extracted text
            if text:
                context_query = f"image with text saying: {text[:100]}"
                hate_queries.append(f"hateful {context_query}")
                safe_queries.append(f"harmless {context_query}")
            
            all_queries = hate_queries + safe_queries
            
            # Process with SigLIP
            inputs = self.siglip_processor(
                text=all_queries, 
                images=image, 
                return_tensors="pt", 
                padding=True
            ).to(self.device)
            
            with torch.no_grad():
                outputs = self.siglip_model(**inputs)
                logits_per_image = outputs.logits_per_image
                probs = torch.softmax(logits_per_image, dim=-1)
            
            # Calculate hate vs safe probabilities
            hate_prob = torch.sum(probs[0][:len(hate_queries)]).item()
            safe_prob = torch.sum(probs[0][len(hate_queries):]).item()
            
            # Normalize probabilities
            total_prob = hate_prob + safe_prob
            if total_prob > 0:
                hate_prob /= total_prob
                safe_prob /= total_prob
            
            # Additional keyword-based adjustment
            keyword_boost = self.check_hate_keywords(text)
            hate_prob = min(1.0, hate_prob + keyword_boost * 0.1)
            
            return {
                "is_hateful": hate_prob > 0.5,
                "hate_probability": hate_prob,
                "safe_probability": safe_prob,
                "confidence": abs(hate_prob - 0.5) * 2,
                "detailed_scores": probs[0].cpu().tolist()
            }
            
        except Exception as e:
            logger.error(f"Multimodal classification error: {e}")
            return {
                "is_hateful": False,
                "hate_probability": 0.3,
                "safe_probability": 0.7,
                "confidence": 0.5,
                "detailed_scores": []
            }
    
    def check_hate_keywords(self, text: str) -> float:
        """Check for hate speech keywords and return boost factor"""
        if not text:
            return 0.0
        
        text_lower = text.lower()
        keyword_count = sum(1 for keyword in self.hate_keywords if keyword in text_lower)
        
        return min(1.0, keyword_count * 0.2)  # Cap at 1.0
    
    def fetch_social_media_content(self, url: str) -> Dict:
        """Enhanced social media content fetching with better error handling"""
        try:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
            }
            
            response = requests.get(url, headers=headers, timeout=15)
            response.raise_for_status()
            
            content_type = response.headers.get('content-type', '').lower()
            
            # Handle direct image URLs
            if any(img_type in content_type for img_type in ['image/jpeg', 'image/png', 'image/gif', 'image/webp']):
                image = Image.open(BytesIO(response.content))
                return {"type": "image", "content": image, "url": url}
            
            # Handle HTML content (simplified scraping)
            elif 'text/html' in content_type:
                html_content = response.text
                
                # Extract images from HTML
                img_urls = re.findall(r'<img[^>]+src=["\']([^"\']+)["\']', html_content)
                
                # Try to get the first valid image
                for img_url in img_urls[:3]:  # Try first 3 images
                    try:
                        if not img_url.startswith('http'):
                            img_url = requests.compat.urljoin(url, img_url)
                        
                        img_response = requests.get(img_url, headers=headers, timeout=10)
                        img_response.raise_for_status()
                        
                        image = Image.open(BytesIO(img_response.content))
                        
                        # Extract text content from HTML
                        text_content = re.sub(r'<[^>]+>', ' ', html_content)
                        text_content = re.sub(r'\s+', ' ', text_content)[:500]
                        
                        return {
                            "type": "webpage", 
                            "content": image, 
                            "text": text_content,
                            "url": url
                        }
                        
                    except Exception as img_e:
                        logger.warning(f"Failed to fetch image {img_url}: {img_e}")
                        continue
                
                # If no images found, return text content
                text_content = re.sub(r'<[^>]+>', ' ', html_content)
                text_content = re.sub(r'\s+', ' ', text_content)[:1000]
                
                return {"type": "text", "content": text_content, "url": url}
            
            else:
                return {"type": "error", "content": f"Unsupported content type: {content_type}"}
                
        except requests.RequestException as e:
            logger.error(f"Request error for URL {url}: {e}")
            return {"type": "error", "content": f"Failed to fetch URL: {str(e)}"}
        except Exception as e:
            logger.error(f"General error fetching {url}: {e}")
            return {"type": "error", "content": f"Error processing content: {str(e)}"}
    
    def ensemble_prediction(self, sentiment_result: Dict, multimodal_result: Dict, extracted_text: str = "") -> Dict:
        """Advanced ensemble prediction with risk stratification"""
        
        # Convert sentiment to risk score
        sentiment_risk = self.sentiment_to_risk_score(sentiment_result["label"], sentiment_result["score"])
        
        # Get multimodal risk score
        multimodal_risk = multimodal_result["hate_probability"]
        
        # Context-aware weighting
        text_weight = self.ensemble_weights['text_sentiment']
        multimodal_weight = self.ensemble_weights['image_content'] + self.ensemble_weights['multimodal_context']
        
        # Adjust weights based on text availability
        if not extracted_text.strip():
            text_weight *= 0.5
            multimodal_weight = 1.0 - text_weight
        
        # Calculate combined risk score
        combined_risk = (text_weight * sentiment_risk + multimodal_weight * multimodal_risk)
        
        # Risk stratification
        if combined_risk >= self.risk_thresholds['high_risk']:
            risk_level = "HIGH"
            risk_description = "Potentially harmful content requiring immediate attention"
        elif combined_risk >= self.risk_thresholds['medium_risk']:
            risk_level = "MEDIUM"
            risk_description = "Concerning content that may require review"
        elif combined_risk >= self.risk_thresholds['low_risk']:
            risk_level = "LOW"
            risk_description = "Mildly concerning content, likely safe"
        else:
            risk_level = "SAFE"
            risk_description = "Content appears safe and non-harmful"
        
        # Confidence calculation
        confidence = self.calculate_ensemble_confidence(sentiment_result, multimodal_result)
        
        return {
            "risk_level": risk_level,
            "risk_score": combined_risk,
            "risk_description": risk_description,
            "confidence": confidence,
            "sentiment_analysis": sentiment_result,
            "multimodal_analysis": multimodal_result,
            "explanation": self.generate_explanation(sentiment_result, multimodal_result, risk_level)
        }
    
    def sentiment_to_risk_score(self, sentiment_label: str, confidence: float) -> float:
        """Convert sentiment analysis to risk score"""
        base_scores = {"NEGATIVE": 0.7, "NEUTRAL": 0.3, "POSITIVE": 0.1}
        base_score = base_scores.get(sentiment_label, 0.3)
        
        # Adjust based on confidence
        return base_score * confidence + (1 - confidence) * 0.3
    
    def calculate_ensemble_confidence(self, sentiment_result: Dict, multimodal_result: Dict) -> float:
        """Calculate overall ensemble confidence"""
        sentiment_conf = sentiment_result["score"]
        multimodal_conf = multimodal_result["confidence"]
        
        # Weighted average of confidences
        overall_conf = (sentiment_conf + multimodal_conf) / 2
        
        # Boost confidence if both models agree
        sentiment_negative = sentiment_result["label"] == "NEGATIVE"
        multimodal_hateful = multimodal_result["is_hateful"]
        
        if sentiment_negative == multimodal_hateful:
            overall_conf = min(1.0, overall_conf * 1.2)
        
        return overall_conf
    
    def generate_explanation(self, sentiment_result: Dict, multimodal_result: Dict, risk_level: str) -> str:
        """Generate human-readable explanation of the decision"""
        explanations = []
        
        # Sentiment explanation
        sentiment_label = sentiment_result["label"]
        sentiment_conf = sentiment_result["score"]
        explanations.append(f"Text sentiment: {sentiment_label} (confidence: {sentiment_conf:.1%})")
        
        # Multimodal explanation
        hate_prob = multimodal_result["hate_probability"]
        explanations.append(f"Visual content analysis: {hate_prob:.1%} probability of harmful content")
        
        # Risk level explanation
        explanations.append(f"Overall risk assessment: {risk_level}")
        
        return " | ".join(explanations)

# Initialize the analyzer
analyzer = EnhancedEnsembleMemeAnalyzer()

def analyze_content(input_type: str, text_input: str, image_input: Image.Image, url_input: str) -> Tuple[str, str, str]:
    """Main analysis function for Gradio interface"""
    try:
        extracted_text = ""
        image_content = None
        source_info = ""
        
        # Handle different input types
        if input_type == "Text Only" and text_input:
            extracted_text = text_input
            source_info = "Direct text input"
            
        elif input_type == "Image Only" and image_input:
            image_content = image_input
            extracted_text = analyzer.extract_text_from_image(image_input)
            source_info = "Direct image upload"
            
        elif input_type == "URL" and url_input:
            content = analyzer.fetch_social_media_content(url_input)
            source_info = f"Content from: {url_input}"
            
            if content["type"] == "image":
                image_content = content["content"]
                extracted_text = analyzer.extract_text_from_image(content["content"])
            elif content["type"] == "webpage":
                image_content = content["content"]
                extracted_text = content.get("text", "") + " " + analyzer.extract_text_from_image(content["content"])
            elif content["type"] == "text":
                extracted_text = content["content"]
            else:
                return f"❌ Error: {content['content']}", "", ""
                
        elif input_type == "Text + Image" and text_input and image_input:
            extracted_text = text_input + " " + analyzer.extract_text_from_image(image_input)
            image_content = image_input
            source_info = "Combined text and image input"
            
        else:
            return "⚠️ Please provide appropriate input based on the selected type.", "", ""
        
        # Perform analysis
        sentiment_result = analyzer.analyze_sentiment(extracted_text)
        
        if image_content:
            multimodal_result = analyzer.classify_multimodal_content(image_content, extracted_text)
        else:
            # Default multimodal analysis for text-only content
            multimodal_result = {
                "is_hateful": False,
                "hate_probability": 0.2,
                "safe_probability": 0.8,
                "confidence": 0.5,
                "detailed_scores": []
            }
        
        # Get ensemble prediction
        final_result = analyzer.ensemble_prediction(sentiment_result, multimodal_result, extracted_text)
        
        # Format comprehensive results
        risk_emoji = {"HIGH": "🚨", "MEDIUM": "⚠️", "LOW": "🟑", "SAFE": "βœ…"}
        
        result_text = f"""
# πŸ€– Enhanced Ensemble Analysis Results

## {risk_emoji[final_result['risk_level']]} Overall Assessment
**Risk Level**: {final_result['risk_level']} 
**Risk Score**: {final_result['risk_score']:.1%}
**Confidence**: {final_result['confidence']:.1%}
**Description**: {final_result['risk_description']}

---

## πŸ“Š Detailed Analysis

### πŸ“ Text Analysis
**Source**: {source_info}
**Extracted Text**: {extracted_text[:300]}{'...' if len(extracted_text) > 300 else ''}
**Sentiment**: {sentiment_result['label']} ({sentiment_result['score']:.1%} confidence)

### πŸ–ΌοΈ Visual Content Analysis  
**Contains Harmful Content**: {'Yes' if multimodal_result['is_hateful'] else 'No'}
**Harm Probability**: {multimodal_result['hate_probability']:.1%}
**Safe Probability**: {multimodal_result['safe_probability']:.1%}
**Visual Analysis Confidence**: {multimodal_result['confidence']:.1%}

### 🧠 Ensemble Decision Process
{final_result['explanation']}

---

## πŸ’‘ Recommendations
{analyzer.get_recommendations(final_result['risk_level'])}
        """
        
        # Prepare detailed output for inspection
        detailed_output = json.dumps({
            "risk_assessment": {
                "level": final_result['risk_level'],
                "score": final_result['risk_score'],
                "confidence": final_result['confidence']
            },
            "text_analysis": sentiment_result,
            "visual_analysis": multimodal_result,
            "extracted_text": extracted_text
        }, indent=2)
        
        return result_text, extracted_text, detailed_output
        
    except Exception as e:
        logger.error(f"Analysis error: {e}")
        return f"❌ Error during analysis: {str(e)}", "", ""

# Add recommendation method to analyzer class
def get_recommendations(self, risk_level: str) -> str:
    """Get recommendations based on risk level"""
    recommendations = {
        "HIGH": "🚨 **Immediate Action Required**: This content should be reviewed by moderators and potentially removed. Consider issuing warnings or taking enforcement action.",
        "MEDIUM": "⚠️ **Review Recommended**: Content may violate community guidelines. Manual review suggested before taking action.",
        "LOW": "🟑 **Monitor**: Content shows some concerning signals but may be acceptable. Consider additional context before action.",
        "SAFE": "βœ… **No Action Needed**: Content appears safe and compliant with community standards."
    }
    return recommendations.get(risk_level, "No specific recommendations available.")

# Add the method to the class
EnhancedEnsembleMemeAnalyzer.get_recommendations = get_recommendations

# Create enhanced Gradio interface
with gr.Blocks(title="Enhanced Ensemble Meme & Text Analyzer", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # πŸ€– Enhanced Ensemble Meme & Text Analyzer
    
    **Advanced AI system combining:**
    - 🎯 Fine-tuned BERT (93% accuracy) for sentiment analysis
    - πŸ‘οΈ SigLIP-Large for visual content understanding  
    - πŸ” Advanced OCR for text extraction
    - 🧠 Intelligent ensemble decision making
    
    **Analyzes content risk across multiple dimensions with explainable AI**
    """)
    
    with gr.Row():
        input_type = gr.Dropdown(
            choices=["Text Only", "Image Only", "URL", "Text + Image"],
            value="Text Only",
            label="πŸ“₯ Input Type"
        )
    
    with gr.Row():
        with gr.Column(scale=2):
            text_input = gr.Textbox(
                label="πŸ“ Text Input",
                placeholder="Enter text content to analyze (tweets, posts, comments)...",
                lines=4
            )
            image_input = gr.Image(
                label="πŸ–ΌοΈ Image Input",
                type="pil"
            )
            url_input = gr.Textbox(
                label="πŸ”— URL Input",
                placeholder="Enter social media URL (Twitter, Reddit, etc.)..."
            )
            
        with gr.Column(scale=1):
            analyze_btn = gr.Button("πŸš€ Analyze Content", variant="primary", size="lg")
            
            gr.Markdown("""
            ### 🎯 Model Information
            - **BERT**: Fine-tuned sentiment analysis (93% accuracy)
            - **SigLIP**: Large-scale vision-language model
            - **OCR**: Multi-engine text extraction
            - **Ensemble**: Weighted decision fusion
            """)
    
    with gr.Row():
        output_analysis = gr.Markdown(label="πŸ“Š Analysis Results")
        
    with gr.Row():
        with gr.Column():
            output_text = gr.Textbox(label="πŸ“ Extracted Text", lines=4)
        with gr.Column():
            output_detailed = gr.Code(label="πŸ”§ Detailed Results (JSON)", language="json")
    
    # Enhanced examples
    gr.Examples(
        examples=[
            ["Text Only", "This meme is so offensive and targets innocent people. Absolutely disgusting!", None, ""],
            ["Text Only", "Haha this meme made my day! So funny and clever πŸ˜‚", None, ""],
            ["URL", "", None, "https://i.imgur.com/example.jpg"],
            ["Text + Image", "Check out this hilarious meme I found!", None, ""]
        ],
        inputs=[input_type, text_input, image_input, url_input],
        label="πŸ’‘ Try these examples"
    )
    
    analyze_btn.click(
        fn=analyze_content,
        inputs=[input_type, text_input, image_input, url_input],
        outputs=[output_analysis, output_text, output_detailed]
    )

if __name__ == "__main__":
    demo.launch(
        share=True,
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True
    )