Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
model1 = pipeline("image-classification", model="prithivMLmods/
|
| 7 |
-
model2 = pipeline("image-classification", model="dima806/
|
| 8 |
-
model3 = pipeline("image-classification", model="Hemg/
|
| 9 |
|
| 10 |
def detect_image(img: Image.Image):
|
| 11 |
results = {}
|
| 12 |
|
| 13 |
-
#
|
| 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 |
-
#
|
| 26 |
-
|
| 27 |
-
for
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
ai_score += score
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
ai_percent = (ai_score / total) * 100
|
| 39 |
-
real_percent = (real_score / total) * 100
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
verdict = f"✅ Foto Asli ({real_percent:.2f}%)"
|
| 44 |
-
elif ai_percent > real_percent and ai_percent > 55:
|
| 45 |
-
verdict = f"⚠️ AI Generated ({ai_percent:.2f}%)"
|
| 46 |
else:
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
return
|
| 57 |
|
| 58 |
-
|
|
|
|
| 59 |
fn=detect_image,
|
| 60 |
inputs=gr.Image(type="pil"),
|
| 61 |
-
outputs="
|
| 62 |
-
title="Deteksi
|
| 63 |
-
description="
|
| 64 |
)
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
+
# Load 3 model Hugging Face
|
| 6 |
+
model1 = pipeline("image-classification", model="prithivMLmods/deepfake-vs-real-image-detection")
|
| 7 |
+
model2 = pipeline("image-classification", model="dima806/ai-image-detector")
|
| 8 |
+
model3 = pipeline("image-classification", model="Hemg/A-real-and-fake-image-detection")
|
| 9 |
|
| 10 |
def detect_image(img: Image.Image):
|
| 11 |
results = {}
|
| 12 |
|
| 13 |
+
# Prediksi masing-masing model
|
| 14 |
res1 = model1(img)
|
|
|
|
|
|
|
|
|
|
| 15 |
res2 = model2(img)
|
|
|
|
|
|
|
|
|
|
| 16 |
res3 = model3(img)
|
|
|
|
| 17 |
|
| 18 |
+
# Ambil skor Real/Artificial sesuai output
|
| 19 |
+
score1_real = next((r["score"] for r in res1 if "real" in r["label"].lower()), 0)
|
| 20 |
+
score1_fake = next((r["score"] for r in res1 if "fake" in r["label"].lower() or "artificial" in r["label"].lower()), 0)
|
| 21 |
+
|
| 22 |
+
score2_real = next((r["score"] for r in res2 if "real" in r["label"].lower()), 0)
|
| 23 |
+
score2_fake = next((r["score"] for r in res2 if "fake" in r["label"].lower()), 0)
|
| 24 |
+
|
| 25 |
+
score3_real = next((r["score"] for r in res3 if "real" in r["label"].lower()), 0)
|
| 26 |
+
score3_fake = next((r["score"] for r in res3 if "fake" in r["label"].lower()), 0)
|
|
|
|
| 27 |
|
| 28 |
+
# Voting: jika 2 model yakin REAL (>0.5), maka final REAL
|
| 29 |
+
votes_real = sum([score1_real > 0.5, score2_real > 0.5, score3_real > 0.5])
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
if votes_real >= 2:
|
| 32 |
+
final_label = "✅ Foto Asli"
|
|
|
|
|
|
|
|
|
|
| 33 |
else:
|
| 34 |
+
# Weighted ensemble (Hemg lebih berat)
|
| 35 |
+
weighted_real = (0.25 * score1_real) + (0.25 * score2_real) + (0.5 * score3_real)
|
| 36 |
+
weighted_fake = (0.25 * score1_fake) + (0.25 * score2_fake) + (0.5 * score3_fake)
|
| 37 |
|
| 38 |
+
if weighted_real >= weighted_fake:
|
| 39 |
+
final_label = "✅ Foto Asli"
|
| 40 |
+
else:
|
| 41 |
+
final_label = "❌ Foto AI / Hasil Buatan"
|
| 42 |
|
| 43 |
+
# Tampilkan hasil detail
|
| 44 |
+
results["Model1"] = res1
|
| 45 |
+
results["Model2"] = res2
|
| 46 |
+
results["Model3"] = res3
|
| 47 |
+
results["Final"] = final_label
|
| 48 |
|
| 49 |
+
return results
|
| 50 |
|
| 51 |
+
# Interface Gradio
|
| 52 |
+
demo = gr.Interface(
|
| 53 |
fn=detect_image,
|
| 54 |
inputs=gr.Image(type="pil"),
|
| 55 |
+
outputs="json",
|
| 56 |
+
title="Deteksi Foto Asli vs AI",
|
| 57 |
+
description="Menggabungkan 3 model Hugging Face dengan hybrid voting + bobot."
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|