from transformers import pipeline from PIL import Image print("⏳ Loading AI Model...") pipe = pipeline("image-classification", model="dima806/deepfake_vs_real_image_detection") print("✅ Model Loaded Successfully!") def predict_deepfake(image_path): try: img = Image.open(image_path) results = pipe(img) # DEBUG: Print the raw output print(f"🔍 Raw AI Output: {results}") fake_score = 0.0 real_score = 0.0 for res in results: label_fixed = res['label'].lower() # FORCE LOWERCASE if "fake" in label_fixed or "ai" in label_fixed: fake_score = res['score'] elif "real" in label_fixed: real_score = res['score'] print(f"🧐 Analysis -> Real: {real_score:.5f} | Fake: {fake_score:.5f}") # --- CORRECTION: STANDARD THRESHOLD --- # Changed from 0.001 to 0.50. # This means the model must be at least 50% sure it's fake to flag it. THRESHOLD = 0.50 if fake_score > THRESHOLD: return { "is_fake": True, "confidence": f"{fake_score * 100:.1f}%", "message": "⚠️ DEEPFAKE DETECTED" } else: return { "is_fake": False, # Return the REAL score if it is real "confidence": f"{real_score * 100:.1f}%", "message": "✅ VERIFIED REAL" } except Exception as e: print(f"Error: {e}") return {"is_fake": False, "confidence": "0%", "message": "Error"}