import gradio as gr from PIL import Image def detect_deepfake(image): # TRANSFORM tensor = predict_transform(image).unsqueeze(0).to(device) # PREDICT model.eval() with torch.no_grad(): outputs = model(tensor) probs = torch.softmax(outputs, dim=1)[0] pred_idx = torch.argmax(probs).item() labels = ["FAKE", "REAL"] prediction = labels[pred_idx] fake_prob = probs[0].item() * 100 real_prob = probs[1].item() * 100 result = { "FAKE 🔴": round(fake_prob / 100, 3), "REAL 🟢": round(real_prob / 100, 3) } verdict = f"⚠️ FAKE — {fake_prob:.2f}% confidence" if prediction == "FAKE" \ else f"✅ REAL — {real_prob:.2f}% confidence" return verdict, result # BUILD APP app = gr.Interface( fn=detect_deepfake, inputs=gr.Image(type="pil", label="Upload Face Image"), outputs=[ gr.Text(label="Verdict"), gr.Label(label="Confidence Scores") ], title="🕵️ Deepfake Detector", description="Upload any face image to detect if it's REAL or FAKE using EfficientNet-B4", examples=[ [real_img], [fake_img] ], theme=gr.themes.Soft() ) app.launch(share=True)