Spaces:
Running
Running
| import os | |
| import requests | |
| import io | |
| from PIL import Image | |
| from app import get_image_detector | |
| def download_image(url): | |
| response = requests.get(url, headers={"User-Agent": "Mozilla/5.0"}) | |
| response.raise_for_status() | |
| return Image.open(io.BytesIO(response.content)).convert("RGB") | |
| def run_tests(): | |
| detector = get_image_detector() | |
| test_cases = [ | |
| { | |
| "name": "Real Image - Dog", | |
| "url": "https://images.unsplash.com/photo-1543466835-00a7907e9de1?q=80&w=800&auto=format&fit=crop", | |
| "expected": "REAL" | |
| }, | |
| { | |
| "name": "Fake Image - AI Face", | |
| "url": "https://thispersondoesnotexist.com/", | |
| "expected": "FAKE" | |
| }, | |
| { | |
| "name": "Real Image - Architecture", | |
| "url": "https://upload.wikimedia.org/wikipedia/commons/4/47/New_york_times_square-terabass.jpg", | |
| "expected": "REAL" | |
| }, | |
| { | |
| "name": "Real Image - People", | |
| "url": "https://images.unsplash.com/photo-1517841905240-472988babdf9?ixlib=rb-4.0.3&q=80&w=800&auto=format&fit=crop", | |
| "expected": "REAL" | |
| } | |
| ] | |
| print("starting accuracy tests...") | |
| correct = 0 | |
| for case in test_cases: | |
| print(f"\nTesting: {case['name']} (Expected: {case['expected']})") | |
| print(f"URL: {case['url']}") | |
| try: | |
| img = download_image(case['url']) | |
| result, _ = detector.predict_with_visuals(img, include_gradcam=False, include_fft=False, include_result_card=False) | |
| label = result['label'] | |
| print(f"Result: {label}") | |
| print(f"Fake Prob: {result['fake_prob']:.4f}") | |
| print(f"Real Prob: {result['real_prob']:.4f}") | |
| for model, score in result['scores'].items(): | |
| print(f" {model}: {score:.4f}") | |
| if (label == case['expected']) or (label == "FAKE" and case['expected'] == "FAKE") or (label == "REAL" and case['expected'] == "REAL"): | |
| print("[Correct]") | |
| correct += 1 | |
| else: | |
| print("[Incorrect]") | |
| except Exception as e: | |
| print(f"[Error] testing {case['name']}: {e}") | |
| print(f"\nAccuracy: {correct}/{len(test_cases)} ({correct/len(test_cases)*100:.1f}%)") | |
| if __name__ == "__main__": | |
| run_tests() | |