Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
-
import cv2
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
img = cv2.imread(img_path)
|
| 12 |
-
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 13 |
-
img = cv2.resize(img, (512, 512))
|
| 14 |
-
img = cv2.GaussianBlur(img, (3, 3), 0) # Kurangi noise
|
| 15 |
-
return Image.fromarray(img)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
# =========================
|
| 20 |
-
detector_ai = pipeline("image-classification", model="umm-maybe/AI-image-detector")
|
| 21 |
-
detector_resnet = pipeline("image-classification", model="microsoft/resnet-50")
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def detect_image(img):
|
| 27 |
-
# Preprocess
|
| 28 |
-
img = preprocess_image(img)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
verdict = "๐ฃ AI-generated"
|
| 46 |
-
elif human_score > 0.65:
|
| 47 |
-
verdict = "๐ข Foto Asli"
|
| 48 |
-
else:
|
| 49 |
-
verdict = "โ ๏ธ Tidak Pasti"
|
| 50 |
-
|
| 51 |
-
# Extra check: kalau AI-detector bilang AI tapi ResNet yakin objek nyata
|
| 52 |
-
if verdict == "๐ฃ AI-generated" and top_resnet['score'] > 0.70:
|
| 53 |
-
verdict = "โ ๏ธ Tidak Pasti (deteksi objek nyata tinggi)"
|
| 54 |
|
| 55 |
-
#
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
- Top Label: {top_resnet['label']} ({top_resnet['score']:.2%})
|
| 67 |
|
| 68 |
-
|
| 69 |
-
{verdict}
|
| 70 |
-
"""
|
| 71 |
-
return summary
|
| 72 |
|
| 73 |
-
|
| 74 |
-
# Gradio App
|
| 75 |
-
# =========================
|
| 76 |
-
demo = gr.Interface(
|
| 77 |
fn=detect_image,
|
| 78 |
-
inputs=gr.Image(type="
|
| 79 |
-
outputs="
|
| 80 |
-
title="Deteksi AI vs Foto Asli",
|
| 81 |
-
description="
|
| 82 |
)
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# === Load 3 model publik khusus AI vs Real ===
|
| 6 |
+
model1 = pipeline("image-classification", model="prithivMLmods/AI-vs-Deepfake-vs-Real")
|
| 7 |
+
model2 = pipeline("image-classification", model="dima806/ai_vs_real_image_detection")
|
| 8 |
+
model3 = pipeline("image-classification", model="Hemg/AI-VS-REAL-IMAGE-DETECTION")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def detect_image(img: Image.Image):
|
| 11 |
+
results = {}
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Model 1
|
| 14 |
+
res1 = model1(img)
|
| 15 |
+
results["Model 1 (prithivMLmods)"] = res1
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Model 2
|
| 18 |
+
res2 = model2(img)
|
| 19 |
+
results["Model 2 (dima806)"] = res2
|
| 20 |
|
| 21 |
+
# Model 3
|
| 22 |
+
res3 = model3(img)
|
| 23 |
+
results["Model 3 (Hemg)"] = res3
|
| 24 |
|
| 25 |
+
# --- Ensemble sederhana ---
|
| 26 |
+
ai_score, real_score = 0, 0
|
| 27 |
+
for res in [res1, res2, res3]:
|
| 28 |
+
top = res[0]
|
| 29 |
+
label = top["label"].lower()
|
| 30 |
+
score = top["score"]
|
| 31 |
+
if "real" in label:
|
| 32 |
+
real_score += score
|
| 33 |
+
else:
|
| 34 |
+
ai_score += score
|
| 35 |
|
| 36 |
+
# Normalisasi
|
| 37 |
+
total = ai_score + real_score + 1e-9
|
| 38 |
+
ai_percent = (ai_score / total) * 100
|
| 39 |
+
real_percent = (real_score / total) * 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
# Keputusan akhir
|
| 42 |
+
if real_percent > ai_percent:
|
| 43 |
+
verdict = f"โ
Foto Asli ({real_percent:.2f}%)"
|
| 44 |
+
else:
|
| 45 |
+
verdict = f"โ ๏ธ AI Generated ({ai_percent:.2f}%)"
|
| 46 |
|
| 47 |
+
# Format output
|
| 48 |
+
output = f"## ๐ Ringkasan Deteksi\n"
|
| 49 |
+
for name, res in results.items():
|
| 50 |
+
output += f"\n๐น **{name}**: {res}\n"
|
| 51 |
|
| 52 |
+
output += f"\n=== ๐ง ENSEMBLE HASIL AKHIR ===\n{verdict}\n"
|
|
|
|
| 53 |
|
| 54 |
+
return output
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
iface = gr.Interface(
|
|
|
|
|
|
|
|
|
|
| 57 |
fn=detect_image,
|
| 58 |
+
inputs=gr.Image(type="pil"),
|
| 59 |
+
outputs="markdown",
|
| 60 |
+
title="Deteksi AI vs Foto Asli (Ensemble 3 Model Publik)",
|
| 61 |
+
description="Menggunakan 3 model publik Hugging Face (prithivMLmods, dima806, Hemg) untuk mendeteksi apakah gambar AI-generated atau asli."
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
+
iface.launch()
|