File size: 869 Bytes
1e271fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
from transformers import ViTForImageClassification
from PIL import Image
import torch
import gradio as gr
from transformers import pipeline


device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Loading in Model
model_name = "dima806/ai_vs_real_image_detection"
model = ViTForImageClassification.from_pretrained(model_name).to(device)
model.to(device)


#Classification function
def classify_image(img: Image.Image):
    inputs = model(images=img, return_tensors="pt").to(device)
    results = model(inputs)
    top = results[0]
    label = top["label"]
    score = top["score"]
    return f"Prediction: {label} (Confidence: {score:.2f})"



# Interface
interface = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="Real vs AI Image detection",
    description="Check if your image is Real or AI"
)