MDS_demonstrator / verify_all_option.py
AMontiB
update ALL
c4321f9
import os
from PIL import Image
import matplotlib.pyplot as plt
import json
import sys
from unittest.mock import patch
# Add current directory to path
sys.path.append(os.getcwd())
import app
from app import predict, DETECTORS
def create_dummy_image(path):
img = Image.new('RGB', (512, 512), color = 'red')
img.save(path)
return path
def mock_run_single_detection(image_path, detector_name):
# Mock results
results = {
'R50_TF': {'prediction': 'fake', 'confidence': 0.9, 'elapsed_time': 0.1},
'R50_nodown': {'prediction': 'real', 'confidence': 0.2, 'elapsed_time': 0.1}, # Score 0.8 (Real)
'CLIP-D': {'prediction': 'fake', 'confidence': 0.8, 'elapsed_time': 0.1},
'P2G': {'prediction': 'real', 'confidence': 0.45, 'elapsed_time': 0.1}, # Score 0.55 (Real) - Invalid Vote
'NPR': {'prediction': 'fake', 'confidence': 0.95, 'elapsed_time': 0.1}
}
return results.get(detector_name)
def test_all_option():
print("Testing 'ALL' option with MOCKED results...")
img_path = create_dummy_image("test_image.jpg")
with patch('app.run_single_detection', side_effect=mock_run_single_detection):
try:
# Run predict with 'ALL'
text, fig = predict(img_path, 'ALL')
print(f"Output type: {type(text)}, {type(fig)}")
print(f"Text output: {text}")
if isinstance(fig, plt.Figure):
print("Figure created successfully.")
if "weighted confidence" in text:
print("Text output confirms weighted confidence is reported.")
else:
print("Text output MISSING weighted confidence confirmation.")
expected_verdict = "produced by a generative AI" # Majority Fake
if expected_verdict in text:
print("Verdict seems correct (Fake majority).")
else:
print(f"Unexpected verdict. Expected '{expected_verdict}' in text.")
except Exception as e:
print(f"Test failed with exception: {e}")
finally:
if os.path.exists(img_path):
os.remove(img_path)
def test_single_option():
print("\nTesting 'R50_TF' option with MOCKED results...")
img_path = create_dummy_image("test_image_single.jpg")
with patch('app.run_single_detection', side_effect=mock_run_single_detection):
try:
# Run predict with single detector
text, fig = predict(img_path, 'R50_TF')
print(f"Output type: {type(text)}, {type(fig)}")
print(f"Text output: {text}")
if fig is None:
print("Figure is None as expected.")
else:
print("Figure should be None for single detector.")
try:
json_out = json.loads(text)
print("JSON output parsed successfully.")
if json_out.get("Prediction") == "fake":
print("JSON content seems correct.")
except:
print("Failed to parse JSON output.")
except Exception as e:
print(f"Test failed with exception: {e}")
finally:
if os.path.exists(img_path):
os.remove(img_path)
if __name__ == "__main__":
test_all_option()
test_single_option()