Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,25 +5,22 @@ import numpy as np
|
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
# ----------------------------
|
| 8 |
-
#
|
| 9 |
# ----------------------------
|
| 10 |
try:
|
| 11 |
-
|
| 12 |
-
except:
|
| 13 |
-
|
| 14 |
-
print("HF AI-detector gagal dimuat")
|
| 15 |
|
| 16 |
-
# ----------------------------
|
| 17 |
-
# Model General (backup)
|
| 18 |
-
# ----------------------------
|
| 19 |
try:
|
| 20 |
general_model = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 21 |
-
except:
|
| 22 |
general_model = None
|
| 23 |
-
print("General classifier gagal dimuat")
|
| 24 |
|
| 25 |
# ----------------------------
|
| 26 |
-
#
|
| 27 |
# ----------------------------
|
| 28 |
def calculate_blur(image):
|
| 29 |
gray = np.array(image.convert("L"))
|
|
@@ -47,26 +44,26 @@ def has_camera_exif(image):
|
|
| 47 |
return False
|
| 48 |
|
| 49 |
# ----------------------------
|
| 50 |
-
#
|
| 51 |
# ----------------------------
|
| 52 |
def detect_image(image):
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
ai_conf = 0
|
| 59 |
-
if ai_detector:
|
| 60 |
try:
|
| 61 |
-
result =
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
if any(x in
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
except:
|
| 67 |
-
|
| 68 |
|
| 69 |
-
#
|
| 70 |
general_score = 0
|
| 71 |
general_label = "N/A"
|
| 72 |
general_conf = 0
|
|
@@ -80,41 +77,41 @@ def detect_image(image):
|
|
| 80 |
except:
|
| 81 |
general_score = 0
|
| 82 |
|
| 83 |
-
#
|
| 84 |
blur_score = calculate_blur(image)
|
| 85 |
noise_score = calculate_noise(image)
|
| 86 |
exif_present = has_camera_exif(image)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
if
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
weighted_camera = camera_score
|
| 98 |
-
final_score = weighted_camera - weighted_ai # Positif = asli, Negatif = AI
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
|
|
|
| 104 |
else:
|
| 105 |
-
|
| 106 |
-
if final_score > 0:
|
| 107 |
-
final_result = f"✅ Gambar ini {min(round(final_score,2),100)}% asli"
|
| 108 |
-
else:
|
| 109 |
-
final_result = f"🤖 Gambar ini {min(round(-final_score,2),100)}% hasil AI"
|
| 110 |
|
| 111 |
-
#
|
| 112 |
-
output_lines
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
| 118 |
|
| 119 |
return "\n".join(output_lines)
|
| 120 |
|
|
@@ -126,7 +123,7 @@ iface = gr.Interface(
|
|
| 126 |
inputs=gr.Image(type="pil"),
|
| 127 |
outputs="markdown",
|
| 128 |
title="AI vs Foto Asli Detector (Hybrid)",
|
| 129 |
-
description="Unggah gambar, sistem akan mendeteksi
|
| 130 |
)
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|
|
|
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
# ----------------------------
|
| 8 |
+
# MODEL
|
| 9 |
# ----------------------------
|
| 10 |
try:
|
| 11 |
+
hf_detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
|
| 12 |
+
except Exception as e:
|
| 13 |
+
hf_detector = None
|
| 14 |
+
print("HF AI-detector gagal dimuat:", e)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
general_model = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 18 |
+
except Exception as e:
|
| 19 |
general_model = None
|
| 20 |
+
print("General classifier gagal dimuat:", e)
|
| 21 |
|
| 22 |
# ----------------------------
|
| 23 |
+
# ANALISIS LOKAL
|
| 24 |
# ----------------------------
|
| 25 |
def calculate_blur(image):
|
| 26 |
gray = np.array(image.convert("L"))
|
|
|
|
| 44 |
return False
|
| 45 |
|
| 46 |
# ----------------------------
|
| 47 |
+
# DETEKSI HYBRID
|
| 48 |
# ----------------------------
|
| 49 |
def detect_image(image):
|
| 50 |
+
# Prediksi HF AI-detector
|
| 51 |
+
hf_score = 0
|
| 52 |
+
hf_label = "N/A"
|
| 53 |
+
hf_conf = 0
|
| 54 |
+
if hf_detector:
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
+
result = hf_detector(image)
|
| 57 |
+
hf_label = result[0]['label']
|
| 58 |
+
hf_conf = result[0]['score'] * 100
|
| 59 |
+
if any(x in hf_label.lower() for x in ["fake", "ai", "artificial"]):
|
| 60 |
+
hf_score = hf_conf
|
| 61 |
+
else:
|
| 62 |
+
hf_score = 100 - hf_conf # jika human, kurangi
|
| 63 |
except:
|
| 64 |
+
hf_score = 0
|
| 65 |
|
| 66 |
+
# Prediksi general model
|
| 67 |
general_score = 0
|
| 68 |
general_label = "N/A"
|
| 69 |
general_conf = 0
|
|
|
|
| 77 |
except:
|
| 78 |
general_score = 0
|
| 79 |
|
| 80 |
+
# Analisis lokal
|
| 81 |
blur_score = calculate_blur(image)
|
| 82 |
noise_score = calculate_noise(image)
|
| 83 |
exif_present = has_camera_exif(image)
|
| 84 |
+
local_score = 0
|
| 85 |
+
|
| 86 |
+
# Blur rendah atau noise rendah → kemungkinan AI
|
| 87 |
+
if blur_score < 100 or noise_score < 10:
|
| 88 |
+
local_score += 50
|
| 89 |
+
# Tidak ada metadata kamera → kemungkinan AI
|
| 90 |
+
if not exif_present:
|
| 91 |
+
local_score += 40
|
| 92 |
+
else:
|
| 93 |
+
local_score -= 20 # ada metadata kamera → lebih asli
|
| 94 |
|
| 95 |
+
# Weighted hybrid
|
| 96 |
+
weighted_score = hf_score*0.4 + general_score*0.2 + local_score*0.4
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# Konversi ke persen AI / Asli
|
| 99 |
+
if weighted_score >= 95:
|
| 100 |
+
final_result = "🖼️ Gambar ini hasil AI (100%)"
|
| 101 |
+
elif weighted_score <= 5:
|
| 102 |
+
final_result = "🖼️ Gambar ini asli (100%)"
|
| 103 |
else:
|
| 104 |
+
final_result = f"🖼️ Gambar ini {round(weighted_score,2)}% AI / {round(100-weighted_score,2)}% Asli"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
# Output
|
| 107 |
+
output_lines = [
|
| 108 |
+
f"### Hasil Deteksi:\n{final_result}",
|
| 109 |
+
f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)",
|
| 110 |
+
f"General Model: {general_label} ({general_conf:.2f}%)",
|
| 111 |
+
f"Blur Score: {blur_score:.2f}",
|
| 112 |
+
f"Noise Score: {noise_score:.2f}",
|
| 113 |
+
f"Metadata Kamera: {'Ada' if exif_present else 'Tidak Ada'}"
|
| 114 |
+
]
|
| 115 |
|
| 116 |
return "\n".join(output_lines)
|
| 117 |
|
|
|
|
| 123 |
inputs=gr.Image(type="pil"),
|
| 124 |
outputs="markdown",
|
| 125 |
title="AI vs Foto Asli Detector (Hybrid)",
|
| 126 |
+
description="Unggah gambar, sistem akan mendeteksi persentase AI vs Asli."
|
| 127 |
)
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|