Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import torchvision.transforms as transforms
|
4 |
+
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
import cv2
|
7 |
+
import os
|
8 |
+
from sklearn.svm import SVC
|
9 |
+
from scipy.fftpack import fft2, fftshift
|
10 |
+
import pretrainedmodels
|
11 |
+
|
12 |
+
# 🔌 Transform pour CNN
|
13 |
+
transform = transforms.Compose([
|
14 |
+
transforms.Resize((299, 299)),
|
15 |
+
transforms.ToTensor(),
|
16 |
+
transforms.Normalize([0.5]*3, [0.5]*3)
|
17 |
+
])
|
18 |
+
|
19 |
+
class ProxyXceptionNet(torch.nn.Module):
|
20 |
+
def __init__(self):
|
21 |
+
super().__init__()
|
22 |
+
self.model = pretrainedmodels.__dict__["xception"](num_classes=1000, pretrained="imagenet")
|
23 |
+
self.model.eval()
|
24 |
+
|
25 |
+
def predict(self, image_pil):
|
26 |
+
img = transform(image_pil).unsqueeze(0)
|
27 |
+
with torch.no_grad():
|
28 |
+
out = self.model(img)
|
29 |
+
prob = torch.softmax(out, dim=1).numpy()
|
30 |
+
# Simule une décision deepfake basée sur la classe dominante : purement démonstratif !
|
31 |
+
pred_class = np.argmax(prob)
|
32 |
+
return int(pred_class % 2 == 0) # pseudo-détection arbitraire
|
33 |
+
|
34 |
+
cnn_model = ProxyXceptionNet()
|
35 |
+
|
36 |
+
# ⚖️ SVM avec FFT (entraînement simple sur données factices)
|
37 |
+
fft_train = np.random.rand(20, 1000)
|
38 |
+
y_train = [0]*10 + [1]*10
|
39 |
+
svm = SVC(probability=True)
|
40 |
+
svm.fit(fft_train, y_train)
|
41 |
+
|
42 |
+
# 📈 FFT Feature Extraction
|
43 |
+
def extract_fft_features_pil(image):
|
44 |
+
img = image.convert("L").resize((256, 256))
|
45 |
+
img_np = np.array(img)
|
46 |
+
f = fft2(img_np)
|
47 |
+
fshift = fftshift(f)
|
48 |
+
magnitude_spectrum = 20 * np.log(np.abs(fshift) + 1)
|
49 |
+
return magnitude_spectrum.flatten()[:1000]
|
50 |
+
|
51 |
+
# 🔄 Fonction principale Gradio
|
52 |
+
def predict(image, method):
|
53 |
+
if method == "CNN (Xception)":
|
54 |
+
result = cnn_model.predict(image)
|
55 |
+
label = "Deepfake ❌" if result == 1 else "Réel ✅"
|
56 |
+
else:
|
57 |
+
features = extract_fft_features_pil(image)
|
58 |
+
pred = svm.predict([features])[0]
|
59 |
+
label = "Deepfake ❌" if pred == 1 else "Réel ✅"
|
60 |
+
|
61 |
+
return label
|
62 |
+
|
63 |
+
demo = gr.Interface(
|
64 |
+
fn=predict,
|
65 |
+
inputs=[
|
66 |
+
gr.Image(type="pil", label="Image à analyser"),
|
67 |
+
gr.Radio(["CNN (Xception)", "FFT + SVM"], label="Méthode de détection")
|
68 |
+
],
|
69 |
+
outputs=gr.Textbox(label="Résultat"),
|
70 |
+
title="Détection de Deepfakes (Image)",
|
71 |
+
description="Upload une image et choisis la méthode pour tester si c'est un deepfake."
|
72 |
+
)
|
73 |
+
|
74 |
+
demo.launch(share=True)
|