Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,81 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
# Model
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Prediksi dengan AI detector
|
| 14 |
-
result1 = detector(img)
|
| 15 |
-
label1 = result1[0]['label']
|
| 16 |
-
conf1 = round(result1[0]['score'] * 100, 2)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
label2 = result2[0]['label']
|
| 21 |
-
conf2 = round(result2[0]['score'] * 100, 2)
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
**Model AI-detector:** {label1} ({conf1}%)
|
| 36 |
-
**Model General (ViT):** {label2} ({conf2}%)
|
| 37 |
-
"""
|
| 38 |
-
return output
|
| 39 |
-
except Exception as e:
|
| 40 |
-
return f"Terjadi error: {str(e)}"
|
| 41 |
-
|
| 42 |
-
# UI Gradio
|
| 43 |
iface = gr.Interface(
|
| 44 |
fn=detect_image,
|
| 45 |
inputs=gr.Image(type="pil"),
|
| 46 |
-
outputs="
|
| 47 |
-
title="AI vs
|
| 48 |
-
description="
|
| 49 |
)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
|
| 6 |
+
# ----------------------------
|
| 7 |
+
# Inisialisasi model
|
| 8 |
+
# ----------------------------
|
| 9 |
|
| 10 |
+
# Model A: AI detector wajah
|
| 11 |
+
model_a = pipeline("image-classification", model="rodrigoberriel/real-or-fake-face")
|
| 12 |
|
| 13 |
+
# Model B: AI detector general (misal: resnet-50)
|
| 14 |
+
model_b = pipeline("image-classification", model="microsoft/resnet-50")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# Model C: General image classifier
|
| 17 |
+
model_c = pipeline("image-classification", model="google/vit-base-patch16-224")
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# ----------------------------
|
| 20 |
+
# Fungsi prediksi dengan ensemble
|
| 21 |
+
# ----------------------------
|
| 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 |
+
results.append((label_a, score_a))
|
| 29 |
+
|
| 30 |
+
# Model B
|
| 31 |
+
res_b = model_b(image)[0]
|
| 32 |
+
label_b, score_b = res_b['label'].lower(), res_b['score']
|
| 33 |
+
# Simplifikasi: jika label mirip 'human' atau 'person' = human, lainnya = AI
|
| 34 |
+
if 'human' in label_b or 'person' in label_b:
|
| 35 |
+
label_b_final = 'human'
|
| 36 |
+
else:
|
| 37 |
+
label_b_final = 'ai'
|
| 38 |
+
results.append((label_b_final, score_b))
|
| 39 |
+
|
| 40 |
+
# Model C
|
| 41 |
+
res_c = model_c(image)[0]
|
| 42 |
+
label_c, score_c = res_c['label'].lower(), res_c['score']
|
| 43 |
+
if 'human' in label_c or 'person' in label_c:
|
| 44 |
+
label_c_final = 'human'
|
| 45 |
+
else:
|
| 46 |
+
label_c_final = 'ai'
|
| 47 |
+
results.append((label_c_final, score_c))
|
| 48 |
+
|
| 49 |
+
# ----------------------------
|
| 50 |
+
# Voting + Threshold
|
| 51 |
+
# ----------------------------
|
| 52 |
+
votes = [r[0] for r in results]
|
| 53 |
+
scores = [r[1] for r in results]
|
| 54 |
+
|
| 55 |
+
# Mayoritas voting
|
| 56 |
+
final_label = max(set(votes), key=votes.count)
|
| 57 |
+
|
| 58 |
+
# Rata-rata confidence untuk final label
|
| 59 |
+
relevant_scores = [s for (l, s) in results if l == final_label]
|
| 60 |
+
avg_confidence = np.mean(relevant_scores) * 100 # ke persen
|
| 61 |
+
|
| 62 |
+
# Tentukan hasil akhir
|
| 63 |
+
if avg_confidence < 80:
|
| 64 |
+
return "⚠️ Tidak Pasti (cek manual)", round(avg_confidence, 2)
|
| 65 |
+
elif final_label == 'human':
|
| 66 |
+
return f"Foto Asli ✅", round(avg_confidence, 2)
|
| 67 |
+
else:
|
| 68 |
+
return f"AI Detected 🤖", round(avg_confidence, 2)
|
| 69 |
|
| 70 |
+
# ----------------------------
|
| 71 |
+
# Gradio Interface
|
| 72 |
+
# ----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
iface = gr.Interface(
|
| 74 |
fn=detect_image,
|
| 75 |
inputs=gr.Image(type="pil"),
|
| 76 |
+
outputs=[gr.Textbox(label="Hasil Deteksi"), gr.Number(label="Confidence (%)")],
|
| 77 |
+
title="AI vs Foto Asli Detector",
|
| 78 |
+
description="Unggah gambar, sistem akan mendeteksi apakah gambar asli atau dihasilkan AI menggunakan ensemble 3 model + voting."
|
| 79 |
)
|
| 80 |
|
| 81 |
+
iface.launch()
|
|
|