|
import gradio as gr |
|
from transformers import AutoImageProcessor |
|
from transformers import SiglipForImageClassification |
|
from transformers.image_utils import load_image |
|
from PIL import Image |
|
import torch |
|
|
|
|
|
model_name = "prithivMLmods/AI-vs-Deepfake-vs-Real-Siglip2" |
|
model = SiglipForImageClassification.from_pretrained(model_name) |
|
processor = AutoImageProcessor.from_pretrained(model_name) |
|
|
|
def image_classification(image): |
|
"""Classifies an image as AI-generated, deepfake, or real.""" |
|
image = Image.fromarray(image).convert("RGB") |
|
inputs = processor(images=image, return_tensors="pt") |
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
probs = torch.nn.functional.softmax(logits, dim=1).squeeze().tolist() |
|
|
|
labels = model.config.id2label |
|
predictions = {labels[i]: round(probs[i], 3) for i in range(len(probs))} |
|
|
|
return predictions |
|
|
|
|
|
iface = gr.Interface( |
|
fn=image_classification, |
|
inputs=gr.Image(type="numpy"), |
|
outputs=gr.Label(label="Classification Result"), |
|
title="AI vs Deepfake vs Real Image Classification", |
|
description="Upload an image to determine whether it is AI-generated, a deepfake, or a real image." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|