Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,52 @@
|
|
| 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 |
-
# Bobot tiap model
|
| 11 |
-
weights = {
|
| 12 |
-
"prithivMLmods": 1.2,
|
| 13 |
-
"dima806": 1.0,
|
| 14 |
-
"Hemg": 1.5
|
| 15 |
-
}
|
| 16 |
-
|
| 17 |
def detect_image(img: Image.Image):
|
| 18 |
results = {}
|
| 19 |
|
| 20 |
-
#
|
| 21 |
res1 = model1(img)
|
| 22 |
-
res2 = model2(img)
|
| 23 |
-
res3 = model3(img)
|
| 24 |
results["Model 1 (prithivMLmods)"] = res1
|
|
|
|
|
|
|
|
|
|
| 25 |
results["Model 2 (dima806)"] = res2
|
|
|
|
|
|
|
|
|
|
| 26 |
results["Model 3 (Hemg)"] = res3
|
| 27 |
|
| 28 |
-
# Ensemble dengan
|
| 29 |
ai_score, real_score = 0, 0
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
top = res[0]
|
| 32 |
label = top["label"].lower()
|
| 33 |
-
score = top["score"]
|
| 34 |
|
| 35 |
if "real" in label:
|
| 36 |
real_score += score
|
|
|
|
|
|
|
| 37 |
else:
|
| 38 |
ai_score += score
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
# Normalisasi
|
| 41 |
total = ai_score + real_score + 1e-9
|
| 42 |
ai_percent = (ai_score / total) * 100
|
| 43 |
real_percent = (real_score / total) * 100
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
any("real" in r["label"].lower() and r["score"] > 0.6 for r in res)
|
| 48 |
-
for res in [res1, res2, res3]
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
if override_real or real_percent > ai_percent:
|
| 52 |
verdict = f"✅ Foto Asli ({real_percent:.2f}%)"
|
| 53 |
else:
|
| 54 |
verdict = f"⚠️ AI Generated ({ai_percent:.2f}%)"
|
| 55 |
|
| 56 |
# Format output
|
| 57 |
-
output = "## 📊 Ringkasan Deteksi\n"
|
| 58 |
for name, res in results.items():
|
| 59 |
output += f"\n🔹 **{name}**: {res}\n"
|
| 60 |
|
| 61 |
output += f"\n=== 🧠 ENSEMBLE HASIL AKHIR ===\n{verdict}\n"
|
| 62 |
|
| 63 |
return output
|
| 64 |
-
|
| 65 |
-
iface = gr.Interface(
|
| 66 |
-
fn=detect_image,
|
| 67 |
-
inputs=gr.Image(type="pil"),
|
| 68 |
-
outputs="markdown",
|
| 69 |
-
title="Deteksi AI vs Foto Asli (Ensemble 3 Model Publik)",
|
| 70 |
-
description="Menggunakan 3 model publik Hugging Face (prithivMLmods, dima806, Hemg) dengan voting berbobot & threshold untuk deteksi AI vs real."
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
if __name__ == "__main__":
|
| 74 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def detect_image(img: Image.Image):
|
| 2 |
results = {}
|
| 3 |
|
| 4 |
+
# Model 1
|
| 5 |
res1 = model1(img)
|
|
|
|
|
|
|
| 6 |
results["Model 1 (prithivMLmods)"] = res1
|
| 7 |
+
|
| 8 |
+
# Model 2
|
| 9 |
+
res2 = model2(img)
|
| 10 |
results["Model 2 (dima806)"] = res2
|
| 11 |
+
|
| 12 |
+
# Model 3
|
| 13 |
+
res3 = model3(img)
|
| 14 |
results["Model 3 (Hemg)"] = res3
|
| 15 |
|
| 16 |
+
# --- Ensemble dengan aturan baru ---
|
| 17 |
ai_score, real_score = 0, 0
|
| 18 |
+
real_votes, ai_votes = 0, 0
|
| 19 |
+
|
| 20 |
+
for res in [res1, res2, res3]:
|
| 21 |
top = res[0]
|
| 22 |
label = top["label"].lower()
|
| 23 |
+
score = top["score"]
|
| 24 |
|
| 25 |
if "real" in label:
|
| 26 |
real_score += score
|
| 27 |
+
if score >= 0.5: # ambang confidence
|
| 28 |
+
real_votes += 1
|
| 29 |
else:
|
| 30 |
ai_score += score
|
| 31 |
+
if score >= 0.5:
|
| 32 |
+
ai_votes += 1
|
| 33 |
|
| 34 |
+
# Normalisasi skor
|
| 35 |
total = ai_score + real_score + 1e-9
|
| 36 |
ai_percent = (ai_score / total) * 100
|
| 37 |
real_percent = (real_score / total) * 100
|
| 38 |
|
| 39 |
+
# Keputusan akhir (butuh ≥2 real votes)
|
| 40 |
+
if real_votes >= 2:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
verdict = f"✅ Foto Asli ({real_percent:.2f}%)"
|
| 42 |
else:
|
| 43 |
verdict = f"⚠️ AI Generated ({ai_percent:.2f}%)"
|
| 44 |
|
| 45 |
# Format output
|
| 46 |
+
output = f"## 📊 Ringkasan Deteksi\n"
|
| 47 |
for name, res in results.items():
|
| 48 |
output += f"\n🔹 **{name}**: {res}\n"
|
| 49 |
|
| 50 |
output += f"\n=== 🧠 ENSEMBLE HASIL AKHIR ===\n{verdict}\n"
|
| 51 |
|
| 52 |
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|