not-lain/deepfake-detection
17
1import gradio as gr2import torch3import torch.nn.functional as F4from facenet_pytorch import MTCNN, InceptionResnetV15import os6import numpy as np7from PIL import Image8import zipfile9import cv210from pytorch_grad_cam import GradCAM11from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget12from pytorch_grad_cam.utils.image import show_cam_on_image13from transformers import pipeline14with zipfile.ZipFile("examples.zip","r") as zip_ref:15 zip_ref.extractall(".")16 17pipe = pipeline(model="not-lain/deepfake",trust_remote_code=True)18 19EXAMPLES_FOLDER = 'examples'20examples_names = os.listdir(EXAMPLES_FOLDER)21examples = []22for example_name in examples_names:23 example_path = os.path.join(EXAMPLES_FOLDER, example_name)24 label = example_name.split('_')[0]25 example = {26 'path': example_path,27 'label': label28 }29 examples.append(example)30np.random.shuffle(examples) # shuffle31 32def predict(input_image:Image.Image, true_label:str):33 out = pipe.predict(input_image)34 confidences,face_with_mask = out["confidences"], out["face_with_mask"]35 return confidences, true_label, face_with_mask36 37interface = gr.Interface(38 fn=predict,39 inputs=[40 gr.Image(label="Input Image", type="filepath"),41 "text"42 ],43 outputs=[44 gr.Label(label="Class"),45 "text",46 gr.Image(label="Face with Explainability")47 ],48 examples=[[examples[i]["path"], examples[i]["label"]] for i in range(10)]49).launch()