Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
model_name = "orhanemree/ai-image-detector"
|
| 7 |
+
|
| 8 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 10 |
+
|
| 11 |
+
def predict(image):
|
| 12 |
+
if image is None:
|
| 13 |
+
return "No image uploaded", 0, 0
|
| 14 |
+
|
| 15 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 16 |
+
with torch.no_grad():
|
| 17 |
+
logits = model(**inputs).logits
|
| 18 |
+
probs = torch.softmax(logits, dim=1)[0]
|
| 19 |
+
|
| 20 |
+
real_prob = float(probs[0])
|
| 21 |
+
fake_prob = float(probs[1])
|
| 22 |
+
|
| 23 |
+
label = "AI-Generated" if fake_prob > real_prob else "Real Photo"
|
| 24 |
+
|
| 25 |
+
return label, round(real_prob, 3), round(fake_prob, 3)
|
| 26 |
+
|
| 27 |
+
interface = gr.Interface(
|
| 28 |
+
fn=predict,
|
| 29 |
+
inputs=gr.Image(type="pil"),
|
| 30 |
+
outputs=[
|
| 31 |
+
gr.Text(label="Prediction"),
|
| 32 |
+
gr.Number(label="Real Probability"),
|
| 33 |
+
gr.Number(label="AI Probability")
|
| 34 |
+
],
|
| 35 |
+
title="AI vs Real Image Detector",
|
| 36 |
+
description="Upload an image to check if it's AI-generated or real."
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
interface.launch()
|