Spaces:
Sleeping
Sleeping
App version 1
Browse files- app.py +34 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import ViTForImageClassification
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
import gradio as gr
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
|
8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
9 |
+
|
10 |
+
# Loading in Model
|
11 |
+
model_name = "dima806/ai_vs_real_image_detection"
|
12 |
+
model = ViTForImageClassification.from_pretrained(model_name).to(device)
|
13 |
+
model.to(device)
|
14 |
+
|
15 |
+
|
16 |
+
#Classification function
|
17 |
+
def classify_image(img: Image.Image):
|
18 |
+
inputs = model(images=img, return_tensors="pt").to(device)
|
19 |
+
results = model(inputs)
|
20 |
+
top = results[0]
|
21 |
+
label = top["label"]
|
22 |
+
score = top["score"]
|
23 |
+
return f"Prediction: {label} (Confidence: {score:.2f})"
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
# Interface
|
28 |
+
interface = gr.Interface(
|
29 |
+
fn=classify_image,
|
30 |
+
inputs=gr.Image(type="pil"),
|
31 |
+
outputs="text",
|
32 |
+
title="Real vs AI Image detection",
|
33 |
+
description="Check if your image is Real or AI"
|
34 |
+
)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
transformers
|
4 |
+
pillow
|