| import gradio as gr |
| from PIL import Image |
|
|
| def detect_deepfake(image): |
| |
| tensor = predict_transform(image).unsqueeze(0).to(device) |
|
|
| |
| 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 |
|
|
|
|
| |
| 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) |