File size: 1,146 Bytes
86afc12
 
 
 
 
 
 
 
 
 
 
 
 
a721c4c
 
 
 
 
86afc12
a721c4c
86afc12
f4f5e0f
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import gradio as gr
import torch
from PIL import Image
from transformers import AutoImageProcessor, AutoModelForImageClassification

# Load the Hugging Face model and processor for deepfake detection.
processor = AutoImageProcessor.from_pretrained("Smogy/SMOGY-Ai-images-detector")
model = AutoModelForImageClassification.from_pretrained("Smogy/SMOGY-Ai-images-detector")

def detect_deepfake(image: Image.Image) -> str:
    inputs = processor(images=image, return_tensors="pt")
    with torch.no_grad():
        outputs = model(**inputs)
    probs = torch.softmax(outputs.logits, dim=1)
    idx = probs.argmax(dim=1).item()
    label = model.config.id2label[idx]
    conf = probs[0, idx].item()
    return f"The image is {label} with confidence {conf:.2f}"

# Build Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# Deepfake Detection App")
    gr.Markdown("### Upload an image to detect deepfake content.")
    img_in = gr.Image(type="pil", label="Upload Image")
    txt_out = gr.Textbox(label="Result")
    gr.Button("Detect").click(fn=detect_deepfake, inputs=img_in, outputs=txt_out)

if __name__ == "__main__":
    demo.launch()