Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
-
import numpy as np
|
| 5 |
|
| 6 |
-
#
|
| 7 |
model_ids = [
|
| 8 |
-
"
|
| 9 |
-
"
|
| 10 |
-
"falconsai/nsfw_image_detection"
|
| 11 |
]
|
| 12 |
|
| 13 |
-
|
| 14 |
-
detectors = [pipeline("image-classification", model=m, trust_remote_code=True) for m in model_ids]
|
| 15 |
|
| 16 |
-
def detect_image(
|
| 17 |
results = []
|
| 18 |
-
scores = []
|
| 19 |
-
|
| 20 |
for det in detectors:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
results.append(f"{
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
if scores:
|
| 31 |
-
avg_score = np.mean(scores) * 100
|
| 32 |
-
else:
|
| 33 |
-
avg_score = 0
|
| 34 |
-
|
| 35 |
-
verdict = "AI" if avg_score > 50 else "Asli"
|
| 36 |
-
|
| 37 |
-
return f"🔎 Hasil Deteksi: {verdict}\nPersentase AI: {avg_score:.2f}%\n\nDetail:\n" + "\n".join(results)
|
| 38 |
-
|
| 39 |
-
# ====== GRADIO UI ======
|
| 40 |
-
demo = gr.Interface(
|
| 41 |
fn=detect_image,
|
| 42 |
inputs=gr.Image(type="pil"),
|
| 43 |
outputs="text",
|
| 44 |
title="AI vs Real Image Detector",
|
| 45 |
-
description="Upload
|
| 46 |
)
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
+
# Model deteksi AI vs Asli (tanpa moondream2, biar tidak error trust_remote_code)
|
| 6 |
model_ids = [
|
| 7 |
+
"umm-maybe/AI-image-detector",
|
| 8 |
+
"roberta-base-openai-detector"
|
|
|
|
| 9 |
]
|
| 10 |
|
| 11 |
+
detectors = [pipeline("image-classification", model=m) for m in model_ids]
|
|
|
|
| 12 |
|
| 13 |
+
def detect_image(image):
|
| 14 |
results = []
|
|
|
|
|
|
|
| 15 |
for det in detectors:
|
| 16 |
+
preds = det(image)
|
| 17 |
+
if isinstance(preds, list) and len(preds) > 0:
|
| 18 |
+
label = preds[0]["label"]
|
| 19 |
+
score = preds[0]["score"]
|
| 20 |
+
results.append(f"{label}: {score:.2f}")
|
| 21 |
+
return "\n".join(results)
|
| 22 |
+
|
| 23 |
+
iface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
fn=detect_image,
|
| 25 |
inputs=gr.Image(type="pil"),
|
| 26 |
outputs="text",
|
| 27 |
title="AI vs Real Image Detector",
|
| 28 |
+
description="Upload a photo to check if it's AI-generated or a real one using multiple models."
|
| 29 |
)
|
| 30 |
|
| 31 |
if __name__ == "__main__":
|
| 32 |
+
iface.launch()
|