|
|
|
""" |
|
Basic testing script for the Enhanced Ensemble Model |
|
""" |
|
import unittest |
|
from PIL import Image |
|
import numpy as np |
|
from app import EnhancedEnsembleMemeAnalyzer |
|
|
|
class TestEnhancedEnsemble(unittest.TestCase): |
|
|
|
@classmethod |
|
def setUpClass(cls): |
|
"""Initialize the analyzer once for all tests""" |
|
cls.analyzer = EnhancedEnsembleMemeAnalyzer() |
|
|
|
|
|
cls.test_image = Image.fromarray(np.random.randint(0, 255, (224, 224, 3), dtype=np.uint8)) |
|
|
|
def test_sentiment_analysis(self): |
|
"""Test sentiment analysis functionality""" |
|
|
|
|
|
positive_result = self.analyzer.analyze_sentiment("I love this content! It's amazing!") |
|
self.assertIn(positive_result["label"], ["POSITIVE", "NEUTRAL"]) |
|
self.assertGreater(positive_result["score"], 0) |
|
|
|
|
|
negative_result = self.analyzer.analyze_sentiment("This is terrible and offensive content") |
|
self.assertIn(negative_result["label"], ["NEGATIVE", "NEUTRAL"]) |
|
self.assertGreater(negative_result["score"], 0) |
|
|
|
def test_ocr_extraction(self): |
|
"""Test OCR text extraction""" |
|
result = self.analyzer.extract_text_from_image(self.test_image) |
|
self.assertIsInstance(result, str) |
|
|
|
def test_multimodal_classification(self): |
|
"""Test multimodal content classification""" |
|
result = self.analyzer.classify_multimodal_content(self.test_image, "test text") |
|
|
|
self.assertIn("is_hateful", result) |
|
self.assertIn("hate_probability", result) |
|
self.assertIn("confidence", result) |
|
self.assertIsInstance(result["is_hateful"], bool) |
|
self.assertGreaterEqual(result["hate_probability"], 0) |
|
self.assertLessEqual(result["hate_probability"], 1) |
|
|
|
def test_ensemble_prediction(self): |
|
"""Test ensemble prediction functionality""" |
|
|
|
|
|
sentiment_result = { |
|
"label": "NEGATIVE", |
|
"score": 0.85, |
|
"probabilities": [0.85, 0.10, 0.05] |
|
} |
|
|
|
|
|
multimodal_result = { |
|
"is_hateful": True, |
|
"hate_probability": 0.75, |
|
"safe_probability": 0.25, |
|
"confidence": 0.80, |
|
"detailed_scores": [] |
|
} |
|
|
|
ensemble_result = self.analyzer.ensemble_prediction( |
|
sentiment_result, multimodal_result, "test text" |
|
) |
|
|
|
self.assertIn("risk_level", ensemble_result) |
|
self.assertIn("risk_score", ensemble_result) |
|
self.assertIn("confidence", ensemble_result) |
|
self.assertIn(ensemble_result["risk_level"], ["HIGH", "MEDIUM", "LOW", "SAFE"]) |
|
|
|
if __name__ == "__main__": |
|
unittest.main() |