Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,14 +6,7 @@ import numpy as np
|
|
| 6 |
# ----------------------------
|
| 7 |
# Inisialisasi model publik
|
| 8 |
# ----------------------------
|
| 9 |
-
|
| 10 |
-
# Model A: Deteksi wajah
|
| 11 |
-
model_a = pipeline("image-classification", model="qualcomm/MediaPipe-Face-Detection")
|
| 12 |
-
|
| 13 |
-
# Model B: Klasifikasi gambar umum
|
| 14 |
model_b = pipeline("image-classification", model="microsoft/resnet-50")
|
| 15 |
-
|
| 16 |
-
# Model C: Klasifikasi gambar umum
|
| 17 |
model_c = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 18 |
|
| 19 |
# ----------------------------
|
|
@@ -22,13 +15,6 @@ model_c = pipeline("image-classification", model="google/vit-base-patch16-224")
|
|
| 22 |
def detect_image(image):
|
| 23 |
results = []
|
| 24 |
|
| 25 |
-
# Model A
|
| 26 |
-
res_a = model_a(image)[0]
|
| 27 |
-
label_a, score_a = res_a['label'].lower(), res_a['score']
|
| 28 |
-
# Sederhanakan: ada wajah = human, tidak = AI
|
| 29 |
-
label_a_final = 'human' if 'face' in label_a else 'ai'
|
| 30 |
-
results.append((label_a_final, score_a))
|
| 31 |
-
|
| 32 |
# Model B
|
| 33 |
res_b = model_b(image)[0]
|
| 34 |
label_b, score_b = res_b['label'].lower(), res_b['score']
|
|
@@ -41,20 +27,12 @@ def detect_image(image):
|
|
| 41 |
label_c_final = 'human' if 'person' in label_c or 'human' in label_c else 'ai'
|
| 42 |
results.append((label_c_final, score_c))
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
# Voting + Threshold
|
| 46 |
-
# ----------------------------
|
| 47 |
votes = [r[0] for r in results]
|
| 48 |
-
scores = [r[1] for r in results]
|
| 49 |
-
|
| 50 |
-
# Mayoritas voting
|
| 51 |
final_label = max(set(votes), key=votes.count)
|
| 52 |
-
|
| 53 |
-
# Rata-rata confidence untuk final label
|
| 54 |
relevant_scores = [s for (l, s) in results if l == final_label]
|
| 55 |
-
avg_confidence = np.mean(relevant_scores) * 100
|
| 56 |
|
| 57 |
-
# Tentukan hasil akhir
|
| 58 |
if avg_confidence < 80:
|
| 59 |
return "⚠️ Tidak Pasti (cek manual)", round(avg_confidence, 2)
|
| 60 |
elif final_label == 'human':
|
|
@@ -70,7 +48,7 @@ iface = gr.Interface(
|
|
| 70 |
inputs=gr.Image(type="pil"),
|
| 71 |
outputs=[gr.Textbox(label="Hasil Deteksi"), gr.Number(label="Confidence (%)")],
|
| 72 |
title="AI vs Foto Asli Detector",
|
| 73 |
-
description="Unggah gambar, sistem akan mendeteksi apakah gambar asli atau dihasilkan AI menggunakan ensemble
|
| 74 |
)
|
| 75 |
|
| 76 |
iface.launch()
|
|
|
|
| 6 |
# ----------------------------
|
| 7 |
# Inisialisasi model publik
|
| 8 |
# ----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
model_b = pipeline("image-classification", model="microsoft/resnet-50")
|
|
|
|
|
|
|
| 10 |
model_c = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 11 |
|
| 12 |
# ----------------------------
|
|
|
|
| 15 |
def detect_image(image):
|
| 16 |
results = []
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
# Model B
|
| 19 |
res_b = model_b(image)[0]
|
| 20 |
label_b, score_b = res_b['label'].lower(), res_b['score']
|
|
|
|
| 27 |
label_c_final = 'human' if 'person' in label_c or 'human' in label_c else 'ai'
|
| 28 |
results.append((label_c_final, score_c))
|
| 29 |
|
| 30 |
+
# Voting + threshold
|
|
|
|
|
|
|
| 31 |
votes = [r[0] for r in results]
|
|
|
|
|
|
|
|
|
|
| 32 |
final_label = max(set(votes), key=votes.count)
|
|
|
|
|
|
|
| 33 |
relevant_scores = [s for (l, s) in results if l == final_label]
|
| 34 |
+
avg_confidence = np.mean(relevant_scores) * 100
|
| 35 |
|
|
|
|
| 36 |
if avg_confidence < 80:
|
| 37 |
return "⚠️ Tidak Pasti (cek manual)", round(avg_confidence, 2)
|
| 38 |
elif final_label == 'human':
|
|
|
|
| 48 |
inputs=gr.Image(type="pil"),
|
| 49 |
outputs=[gr.Textbox(label="Hasil Deteksi"), gr.Number(label="Confidence (%)")],
|
| 50 |
title="AI vs Foto Asli Detector",
|
| 51 |
+
description="Unggah gambar, sistem akan mendeteksi apakah gambar asli atau dihasilkan AI menggunakan ensemble 2 model publik + voting."
|
| 52 |
)
|
| 53 |
|
| 54 |
iface.launch()
|