Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
from PIL import Image
|
| 4 |
|
| 5 |
-
#
|
| 6 |
model_ids = [
|
| 7 |
"umm-maybe/AI-image-detector",
|
| 8 |
-
"
|
|
|
|
| 9 |
]
|
| 10 |
|
|
|
|
| 11 |
detectors = [pipeline("image-classification", model=m) for m in model_ids]
|
| 12 |
|
| 13 |
def detect_image(image):
|
| 14 |
-
|
| 15 |
-
|
| 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="
|
| 29 |
)
|
| 30 |
|
| 31 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
+
# Daftar model publik yang relatif stabil
|
| 5 |
model_ids = [
|
| 6 |
"umm-maybe/AI-image-detector",
|
| 7 |
+
"falconsai/nsfw_image_detection", # meski nsfw, output-nya bermanfaat untuk fitur visual
|
| 8 |
+
"microsoft/resnet-50" # model general untuk referensi real-world image
|
| 9 |
]
|
| 10 |
|
| 11 |
+
# Load semua model
|
| 12 |
detectors = [pipeline("image-classification", model=m) for m in model_ids]
|
| 13 |
|
| 14 |
def detect_image(image):
|
| 15 |
+
combined_scores = {}
|
| 16 |
+
results_text = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Jalankan semua model
|
| 19 |
+
for i, detector in enumerate(detectors):
|
| 20 |
+
preds = detector(image)
|
| 21 |
+
results_text.append(f"Model {i+1} ({model_ids[i]}):")
|
| 22 |
+
for p in preds:
|
| 23 |
+
label = p["label"]
|
| 24 |
+
score = float(p["score"])
|
| 25 |
+
results_text.append(f" {label}: {round(score*100, 2)}%")
|
| 26 |
+
|
| 27 |
+
# Gabungkan skor
|
| 28 |
+
if label not in combined_scores:
|
| 29 |
+
combined_scores[label] = []
|
| 30 |
+
combined_scores[label].append(score)
|
| 31 |
+
|
| 32 |
+
# Hitung rata-rata dari semua model
|
| 33 |
+
final_scores = {label: sum(scores)/len(scores) for label, scores in combined_scores.items()}
|
| 34 |
+
|
| 35 |
+
# Cari label dengan skor tertinggi
|
| 36 |
+
best_label = max(final_scores, key=final_scores.get)
|
| 37 |
+
best_score = round(final_scores[best_label]*100, 2)
|
| 38 |
+
|
| 39 |
+
results_text.append("\n=== ENSEMBLE HASIL AKHIR ===")
|
| 40 |
+
results_text.append(f"{best_label} ({best_score}%)")
|
| 41 |
+
|
| 42 |
+
return "\n".join(results_text)
|
| 43 |
+
|
| 44 |
+
# Gradio interface
|
| 45 |
iface = gr.Interface(
|
| 46 |
fn=detect_image,
|
| 47 |
inputs=gr.Image(type="pil"),
|
| 48 |
outputs="text",
|
| 49 |
+
title="AI vs Real Image Detector (3-Model Ensemble)",
|
| 50 |
+
description="Menggabungkan 3 model publik untuk meningkatkan akurasi deteksi gambar AI vs asli."
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|