Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
model_name = "prithivMLmods/open-deepfake-detection"
|
| 7 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForImageClassification.from_pretrained(model_name)
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
def predict(img):
|
| 12 |
+
inputs = processor(images=img, return_tensors="pt")
|
| 13 |
+
with torch.no_grad():
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
logits = outputs.logits
|
| 16 |
+
probs = torch.softmax(logits, dim=1).squeeze()
|
| 17 |
+
is_ai = bool(torch.argmax(probs).item())
|
| 18 |
+
confidence = float(probs[1].item()) if is_ai else float(probs[0].item())
|
| 19 |
+
message = "AI-generated image detected." if is_ai else "Image appears original/authentic."
|
| 20 |
+
return {"is_ai_generated": is_ai, "confidence": confidence, "message": message}
|
| 21 |
+
|
| 22 |
+
iface = gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs="json", allow_flagging="never")
|
| 23 |
+
iface.launch()
|