Create test_ensemble.py
Browse files- test_ensemble.py +78 -0
test_ensemble.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Basic testing script for the Enhanced Ensemble Model
|
| 4 |
+
"""
|
| 5 |
+
import unittest
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
from app import EnhancedEnsembleMemeAnalyzer
|
| 9 |
+
|
| 10 |
+
class TestEnhancedEnsemble(unittest.TestCase):
|
| 11 |
+
|
| 12 |
+
@classmethod
|
| 13 |
+
def setUpClass(cls):
|
| 14 |
+
"""Initialize the analyzer once for all tests"""
|
| 15 |
+
cls.analyzer = EnhancedEnsembleMemeAnalyzer()
|
| 16 |
+
|
| 17 |
+
# Create a simple test image
|
| 18 |
+
cls.test_image = Image.fromarray(np.random.randint(0, 255, (224, 224, 3), dtype=np.uint8))
|
| 19 |
+
|
| 20 |
+
def test_sentiment_analysis(self):
|
| 21 |
+
"""Test sentiment analysis functionality"""
|
| 22 |
+
|
| 23 |
+
# Test positive sentiment
|
| 24 |
+
positive_result = self.analyzer.analyze_sentiment("I love this content! It's amazing!")
|
| 25 |
+
self.assertIn(positive_result["label"], ["POSITIVE", "NEUTRAL"])
|
| 26 |
+
self.assertGreater(positive_result["score"], 0)
|
| 27 |
+
|
| 28 |
+
# Test negative sentiment
|
| 29 |
+
negative_result = self.analyzer.analyze_sentiment("This is terrible and offensive content")
|
| 30 |
+
self.assertIn(negative_result["label"], ["NEGATIVE", "NEUTRAL"])
|
| 31 |
+
self.assertGreater(negative_result["score"], 0)
|
| 32 |
+
|
| 33 |
+
def test_ocr_extraction(self):
|
| 34 |
+
"""Test OCR text extraction"""
|
| 35 |
+
result = self.analyzer.extract_text_from_image(self.test_image)
|
| 36 |
+
self.assertIsInstance(result, str)
|
| 37 |
+
|
| 38 |
+
def test_multimodal_classification(self):
|
| 39 |
+
"""Test multimodal content classification"""
|
| 40 |
+
result = self.analyzer.classify_multimodal_content(self.test_image, "test text")
|
| 41 |
+
|
| 42 |
+
self.assertIn("is_hateful", result)
|
| 43 |
+
self.assertIn("hate_probability", result)
|
| 44 |
+
self.assertIn("confidence", result)
|
| 45 |
+
self.assertIsInstance(result["is_hateful"], bool)
|
| 46 |
+
self.assertGreaterEqual(result["hate_probability"], 0)
|
| 47 |
+
self.assertLessEqual(result["hate_probability"], 1)
|
| 48 |
+
|
| 49 |
+
def test_ensemble_prediction(self):
|
| 50 |
+
"""Test ensemble prediction functionality"""
|
| 51 |
+
|
| 52 |
+
# Mock sentiment result
|
| 53 |
+
sentiment_result = {
|
| 54 |
+
"label": "NEGATIVE",
|
| 55 |
+
"score": 0.85,
|
| 56 |
+
"probabilities": [0.85, 0.10, 0.05]
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
# Mock multimodal result
|
| 60 |
+
multimodal_result = {
|
| 61 |
+
"is_hateful": True,
|
| 62 |
+
"hate_probability": 0.75,
|
| 63 |
+
"safe_probability": 0.25,
|
| 64 |
+
"confidence": 0.80,
|
| 65 |
+
"detailed_scores": []
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
ensemble_result = self.analyzer.ensemble_prediction(
|
| 69 |
+
sentiment_result, multimodal_result, "test text"
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
self.assertIn("risk_level", ensemble_result)
|
| 73 |
+
self.assertIn("risk_score", ensemble_result)
|
| 74 |
+
self.assertIn("confidence", ensemble_result)
|
| 75 |
+
self.assertIn(ensemble_result["risk_level"], ["HIGH", "MEDIUM", "LOW", "SAFE"])
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
unittest.main()
|