Update app.py
Browse files
app.py
CHANGED
|
@@ -35,7 +35,6 @@ import spaces
|
|
| 35 |
plt.set_loglevel("ERROR")
|
| 36 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 37 |
|
| 38 |
-
# Deep-fake specialist
|
| 39 |
_face_det = MTCNN(select_largest=False, post_process=False, device=device).eval().to(device)
|
| 40 |
_df_model = InceptionResnetV1(pretrained="vggface2", classify=True, num_classes=1, device=device)
|
| 41 |
_df_model.load_state_dict(torch.load("resnet_inception.pth", map_location="cpu")["model_state_dict"])
|
|
@@ -88,7 +87,6 @@ def _extract_landmarks(rgb: np.ndarray) -> Tuple[np.ndarray, np.ndarray | None]:
|
|
| 88 |
def _overlay_cam(cam, base):
|
| 89 |
if torch.is_tensor(cam):
|
| 90 |
cam = cam.detach().cpu().numpy()
|
| 91 |
-
# ------------------------------------------------------
|
| 92 |
|
| 93 |
cam = (cam - cam.min()) / (cam.max() - cam.min() + 1e-6)
|
| 94 |
heat = Image.fromarray(
|
|
@@ -113,13 +111,13 @@ def _render_pdf(title: str, verdict: str, conf: dict, pages: List[Image.Image])
|
|
| 113 |
pdf.cell(0, 6, f"Figure {idx+1}", ln=True)
|
| 114 |
|
| 115 |
tmp = Path(tempfile.mktemp(suffix=".jpg"))
|
| 116 |
-
img.convert("RGB").save(tmp, format="JPEG")
|
| 117 |
pdf.image(str(tmp), x=10, w=90)
|
| 118 |
tmp.unlink(missing_ok=True)
|
| 119 |
pdf.output(out)
|
| 120 |
return str(out)
|
| 121 |
|
| 122 |
-
|
| 123 |
def _susy_cam(tensor: torch.Tensor, class_idx: int) -> np.ndarray:
|
| 124 |
sal = Saliency(_susy_mod)
|
| 125 |
grad = sal.attribute(tensor, target=class_idx).abs().mean(1, keepdim=True)
|
|
@@ -146,7 +144,6 @@ def _susy_predict(img: Image.Image):
|
|
| 146 |
probs = _susy_mod(tens.to(device)).softmax(-1).mean(0).cpu().numpy()[1:]
|
| 147 |
return dict(zip(_GEN_CLASSES, probs))
|
| 148 |
|
| 149 |
-
# βββββββββββββββββββββββββββββ fusion math βββββββββββββββββββββββββββββ
|
| 150 |
def _fuse(p_ai: float, p_df: float) -> float:
|
| 151 |
return 1 - (1 - p_ai) * (1 - p_df)
|
| 152 |
|
|
@@ -201,7 +198,7 @@ def _predict_image(pil: Image.Image):
|
|
| 201 |
|
| 202 |
return verdict, conf, gallery, bar_plot, pdf
|
| 203 |
|
| 204 |
-
def _sample_idx(n):
|
| 205 |
return list(range(n)) if n <= MAX_SAMPLES else np.linspace(0, n-1, MAX_SAMPLES, dtype=int)
|
| 206 |
|
| 207 |
@spaces.GPU
|
|
@@ -232,7 +229,8 @@ with gr.Blocks(css=_css, title="AI-Fake & Deepfake Analyser") as demo:
|
|
| 232 |
gr.Markdown("""
|
| 233 |
## Deepfake detector
|
| 234 |
Upload an **image** or a short **video**.
|
| 235 |
-
The app fuses two complementary models, then shows heat-maps & a PDF report.
|
|
|
|
| 236 |
""")
|
| 237 |
|
| 238 |
with gr.Tab("Image"):
|
|
|
|
| 35 |
plt.set_loglevel("ERROR")
|
| 36 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 37 |
|
|
|
|
| 38 |
_face_det = MTCNN(select_largest=False, post_process=False, device=device).eval().to(device)
|
| 39 |
_df_model = InceptionResnetV1(pretrained="vggface2", classify=True, num_classes=1, device=device)
|
| 40 |
_df_model.load_state_dict(torch.load("resnet_inception.pth", map_location="cpu")["model_state_dict"])
|
|
|
|
| 87 |
def _overlay_cam(cam, base):
|
| 88 |
if torch.is_tensor(cam):
|
| 89 |
cam = cam.detach().cpu().numpy()
|
|
|
|
| 90 |
|
| 91 |
cam = (cam - cam.min()) / (cam.max() - cam.min() + 1e-6)
|
| 92 |
heat = Image.fromarray(
|
|
|
|
| 111 |
pdf.cell(0, 6, f"Figure {idx+1}", ln=True)
|
| 112 |
|
| 113 |
tmp = Path(tempfile.mktemp(suffix=".jpg"))
|
| 114 |
+
img.convert("RGB").save(tmp, format="JPEG")
|
| 115 |
pdf.image(str(tmp), x=10, w=90)
|
| 116 |
tmp.unlink(missing_ok=True)
|
| 117 |
pdf.output(out)
|
| 118 |
return str(out)
|
| 119 |
|
| 120 |
+
|
| 121 |
def _susy_cam(tensor: torch.Tensor, class_idx: int) -> np.ndarray:
|
| 122 |
sal = Saliency(_susy_mod)
|
| 123 |
grad = sal.attribute(tensor, target=class_idx).abs().mean(1, keepdim=True)
|
|
|
|
| 144 |
probs = _susy_mod(tens.to(device)).softmax(-1).mean(0).cpu().numpy()[1:]
|
| 145 |
return dict(zip(_GEN_CLASSES, probs))
|
| 146 |
|
|
|
|
| 147 |
def _fuse(p_ai: float, p_df: float) -> float:
|
| 148 |
return 1 - (1 - p_ai) * (1 - p_df)
|
| 149 |
|
|
|
|
| 198 |
|
| 199 |
return verdict, conf, gallery, bar_plot, pdf
|
| 200 |
|
| 201 |
+
def _sample_idx(n):
|
| 202 |
return list(range(n)) if n <= MAX_SAMPLES else np.linspace(0, n-1, MAX_SAMPLES, dtype=int)
|
| 203 |
|
| 204 |
@spaces.GPU
|
|
|
|
| 229 |
gr.Markdown("""
|
| 230 |
## Deepfake detector
|
| 231 |
Upload an **image** or a short **video**.
|
| 232 |
+
The app fuses two complementary models, then shows heat-maps & a PDF report.Made by Code Alchemists
|
| 233 |
+
Which is (Brijesh Khanoolkar, Shreeya Dessai, Slevin Rodrigues , Rafan Khan)
|
| 234 |
""")
|
| 235 |
|
| 236 |
with gr.Tab("Image"):
|