Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -47,31 +47,41 @@ def has_camera_exif(image):
|
|
| 47 |
# DETEKSI HYBRID
|
| 48 |
# ----------------------------
|
| 49 |
def detect_image(image):
|
|
|
|
|
|
|
|
|
|
| 50 |
hf_score = 0
|
|
|
|
|
|
|
| 51 |
if hf_detector:
|
| 52 |
try:
|
| 53 |
result = hf_detector(image)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
if
|
| 57 |
-
hf_score =
|
| 58 |
-
elif "human" in
|
| 59 |
-
hf_score = 100 -
|
| 60 |
except:
|
| 61 |
hf_score = 0
|
| 62 |
|
|
|
|
| 63 |
general_score = 0
|
|
|
|
|
|
|
| 64 |
if general_model:
|
| 65 |
try:
|
| 66 |
result2 = general_model(image)
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
if any(x in
|
| 70 |
-
general_score =
|
|
|
|
|
|
|
| 71 |
except:
|
| 72 |
general_score = 0
|
| 73 |
|
| 74 |
-
# Analisis lokal
|
| 75 |
blur_score = calculate_blur(image)
|
| 76 |
noise_score = calculate_noise(image)
|
| 77 |
exif_present = has_camera_exif(image)
|
|
@@ -81,29 +91,27 @@ def detect_image(image):
|
|
| 81 |
if not exif_present:
|
| 82 |
local_score += 10
|
| 83 |
|
| 84 |
-
# Weighted
|
| 85 |
weighted_score = hf_score*0.6 + general_score*0.2 + local_score*0.2
|
| 86 |
-
weighted_score = min(max(weighted_score, 0), 100) #
|
| 87 |
|
| 88 |
-
# Output
|
| 89 |
if weighted_score >= 95:
|
| 90 |
-
final_result =
|
| 91 |
elif weighted_score <= 5:
|
| 92 |
-
final_result =
|
| 93 |
else:
|
| 94 |
-
final_result = f"
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
General Model: {label2 if general_model else 'N/A'} ({conf2 if general_model else 0:.2f}%)
|
| 102 |
-
Blur Score: {blur_score:.2f}
|
| 103 |
-
Noise Score: {noise_score:.2f}
|
| 104 |
-
Metadata Kamera: {'Ada' if exif_present else 'Tidak Ada'}
|
| 105 |
-
"""
|
| 106 |
-
return output
|
| 107 |
|
| 108 |
# ----------------------------
|
| 109 |
# Gradio Interface
|
|
@@ -113,7 +121,7 @@ iface = gr.Interface(
|
|
| 113 |
inputs=gr.Image(type="pil"),
|
| 114 |
outputs="markdown",
|
| 115 |
title="AI vs Foto Asli Detector (Hybrid)",
|
| 116 |
-
description="Unggah gambar, sistem akan mendeteksi
|
| 117 |
)
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
|
|
|
| 47 |
# DETEKSI HYBRID
|
| 48 |
# ----------------------------
|
| 49 |
def detect_image(image):
|
| 50 |
+
output_lines = []
|
| 51 |
+
|
| 52 |
+
# -------- HF AI-detector --------
|
| 53 |
hf_score = 0
|
| 54 |
+
hf_label = "N/A"
|
| 55 |
+
hf_conf = 0
|
| 56 |
if hf_detector:
|
| 57 |
try:
|
| 58 |
result = hf_detector(image)
|
| 59 |
+
hf_label = result[0]['label']
|
| 60 |
+
hf_conf = result[0]['score'] * 100
|
| 61 |
+
if any(x in hf_label.lower() for x in ["fake", "ai", "artificial"]):
|
| 62 |
+
hf_score = hf_conf
|
| 63 |
+
elif "human" in hf_label.lower():
|
| 64 |
+
hf_score = 100 - hf_conf # semakin besar human β kecil skor AI
|
| 65 |
except:
|
| 66 |
hf_score = 0
|
| 67 |
|
| 68 |
+
# -------- General model --------
|
| 69 |
general_score = 0
|
| 70 |
+
general_label = "N/A"
|
| 71 |
+
general_conf = 0
|
| 72 |
if general_model:
|
| 73 |
try:
|
| 74 |
result2 = general_model(image)
|
| 75 |
+
general_label = result2[0]['label']
|
| 76 |
+
general_conf = result2[0]['score'] * 100
|
| 77 |
+
if any(x in general_label.lower() for x in ["anime","cartoon","illustration","3d","maya","render"]):
|
| 78 |
+
general_score = general_conf
|
| 79 |
+
else:
|
| 80 |
+
general_score = 100 - general_conf
|
| 81 |
except:
|
| 82 |
general_score = 0
|
| 83 |
|
| 84 |
+
# -------- Analisis lokal --------
|
| 85 |
blur_score = calculate_blur(image)
|
| 86 |
noise_score = calculate_noise(image)
|
| 87 |
exif_present = has_camera_exif(image)
|
|
|
|
| 91 |
if not exif_present:
|
| 92 |
local_score += 10
|
| 93 |
|
| 94 |
+
# -------- Weighted Score --------
|
| 95 |
weighted_score = hf_score*0.6 + general_score*0.2 + local_score*0.2
|
| 96 |
+
weighted_score = min(max(weighted_score, 0), 100) # pastikan 0β100
|
| 97 |
|
| 98 |
+
# -------- Output Final --------
|
| 99 |
if weighted_score >= 95:
|
| 100 |
+
final_result = "π€ Gambar ini hasil AI"
|
| 101 |
elif weighted_score <= 5:
|
| 102 |
+
final_result = "β
Gambar ini asli"
|
| 103 |
else:
|
| 104 |
+
final_result = f"βοΈ Gambar ini {weighted_score:.2f}% AI, {100-weighted_score:.2f}% asli"
|
| 105 |
|
| 106 |
+
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
|
| 107 |
+
output_lines.append(f"Weighted Skor: {weighted_score:.2f}")
|
| 108 |
+
output_lines.append(f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)")
|
| 109 |
+
output_lines.append(f"General Model: {general_label} ({general_conf:.2f}%)")
|
| 110 |
+
output_lines.append(f"Blur Score: {blur_score:.2f}")
|
| 111 |
+
output_lines.append(f"Noise Score: {noise_score:.2f}")
|
| 112 |
+
output_lines.append(f"Metadata Kamera: {'Ada' if exif_present else 'Tidak Ada'}")
|
| 113 |
|
| 114 |
+
return "\n".join(output_lines)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
# ----------------------------
|
| 117 |
# Gradio Interface
|
|
|
|
| 121 |
inputs=gr.Image(type="pil"),
|
| 122 |
outputs="markdown",
|
| 123 |
title="AI vs Foto Asli Detector (Hybrid)",
|
| 124 |
+
description="Unggah gambar, sistem akan mendeteksi persentase AI vs asli. 100% AI β hasil AI, 100% asli β asli."
|
| 125 |
)
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|