File size: 3,415 Bytes
6a489d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4321f9
 
6a489d1
c4321f9
142f504
6a489d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()