Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,20 +5,14 @@ import numpy as np
|
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
# ----------------------------
|
| 8 |
-
# MODEL
|
| 9 |
# ----------------------------
|
| 10 |
try:
|
| 11 |
-
hf_detector = pipeline("image-classification", model="
|
| 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 |
# ----------------------------
|
|
@@ -44,12 +38,11 @@ def has_camera_exif(image):
|
|
| 44 |
return False
|
| 45 |
|
| 46 |
# ----------------------------
|
| 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
|
|
@@ -58,47 +51,32 @@ def detect_image(image):
|
|
| 58 |
result = hf_detector(image)
|
| 59 |
hf_label = result[0]['label']
|
| 60 |
hf_conf = result[0]['score'] * 100
|
| 61 |
-
if
|
| 62 |
hf_score = hf_conf
|
| 63 |
except:
|
| 64 |
hf_score = 0
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
if hf_score >
|
| 68 |
final_result = "π€ AI Detected"
|
| 69 |
weighted_score = hf_score
|
| 70 |
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
|
| 71 |
output_lines.append(f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)")
|
| 72 |
return "\n".join(output_lines)
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
general_score = 0
|
| 76 |
-
general_label = "N/A"
|
| 77 |
-
general_conf = 0
|
| 78 |
-
if general_model:
|
| 79 |
-
try:
|
| 80 |
-
result2 = general_model(image)
|
| 81 |
-
general_label = result2[0]['label']
|
| 82 |
-
general_conf = result2[0]['score'] * 100
|
| 83 |
-
if any(x in general_label.lower() for x in ["anime","cartoon","illustration","maya","3d"]):
|
| 84 |
-
general_score = general_conf
|
| 85 |
-
except:
|
| 86 |
-
general_score = 0
|
| 87 |
-
|
| 88 |
-
# -------- Analisis lokal --------
|
| 89 |
blur_score = calculate_blur(image)
|
| 90 |
noise_score = calculate_noise(image)
|
| 91 |
exif_present = has_camera_exif(image)
|
| 92 |
local_score = 0
|
| 93 |
if blur_score < 100 or noise_score < 10:
|
| 94 |
-
local_score +=
|
| 95 |
if not exif_present:
|
| 96 |
-
local_score +=
|
| 97 |
|
| 98 |
-
|
| 99 |
-
weighted_score = general_score*0.7 + local_score*0.3
|
| 100 |
|
| 101 |
-
if weighted_score >
|
| 102 |
final_result = "π€ AI Detected"
|
| 103 |
else:
|
| 104 |
final_result = "β
Foto Asli"
|
|
@@ -107,7 +85,6 @@ def detect_image(image):
|
|
| 107 |
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
|
| 108 |
output_lines.append(f"Weighted Skor: {weighted_score:.2f}")
|
| 109 |
output_lines.append(f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)")
|
| 110 |
-
output_lines.append(f"General Model: {general_label} ({general_conf:.2f}%)")
|
| 111 |
output_lines.append(f"Blur Score: {blur_score:.2f}")
|
| 112 |
output_lines.append(f"Noise Score: {noise_score:.2f}")
|
| 113 |
output_lines.append(f"Metadata Kamera: {'Ada' if exif_present else 'Tidak Ada'}")
|
|
|
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
# ----------------------------
|
| 8 |
+
# MODEL DETEKSI AI
|
| 9 |
# ----------------------------
|
| 10 |
try:
|
| 11 |
+
hf_detector = pipeline("image-classification", model="ckpt/real-or-ai") # model gratis HF khusus AI detection
|
| 12 |
except Exception as e:
|
| 13 |
hf_detector = None
|
| 14 |
print("HF AI-detector gagal dimuat:", e)
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# ----------------------------
|
| 17 |
# ANALISIS LOKAL
|
| 18 |
# ----------------------------
|
|
|
|
| 38 |
return False
|
| 39 |
|
| 40 |
# ----------------------------
|
| 41 |
+
# DETEKSI HYBRID
|
| 42 |
# ----------------------------
|
| 43 |
def detect_image(image):
|
| 44 |
output_lines = []
|
| 45 |
|
|
|
|
| 46 |
hf_score = 0
|
| 47 |
hf_label = "N/A"
|
| 48 |
hf_conf = 0
|
|
|
|
| 51 |
result = hf_detector(image)
|
| 52 |
hf_label = result[0]['label']
|
| 53 |
hf_conf = result[0]['score'] * 100
|
| 54 |
+
if "ai" in hf_label.lower() or "synthetic" in hf_label.lower():
|
| 55 |
hf_score = hf_conf
|
| 56 |
except:
|
| 57 |
hf_score = 0
|
| 58 |
|
| 59 |
+
# Threshold HF lebih rendah agar sensitif
|
| 60 |
+
if hf_score > 30:
|
| 61 |
final_result = "π€ AI Detected"
|
| 62 |
weighted_score = hf_score
|
| 63 |
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
|
| 64 |
output_lines.append(f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)")
|
| 65 |
return "\n".join(output_lines)
|
| 66 |
|
| 67 |
+
# Analisis lokal tambahan
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
blur_score = calculate_blur(image)
|
| 69 |
noise_score = calculate_noise(image)
|
| 70 |
exif_present = has_camera_exif(image)
|
| 71 |
local_score = 0
|
| 72 |
if blur_score < 100 or noise_score < 10:
|
| 73 |
+
local_score += 30
|
| 74 |
if not exif_present:
|
| 75 |
+
local_score += 20
|
| 76 |
|
| 77 |
+
weighted_score = hf_score * 0.6 + local_score * 0.4
|
|
|
|
| 78 |
|
| 79 |
+
if weighted_score > 40:
|
| 80 |
final_result = "π€ AI Detected"
|
| 81 |
else:
|
| 82 |
final_result = "β
Foto Asli"
|
|
|
|
| 85 |
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
|
| 86 |
output_lines.append(f"Weighted Skor: {weighted_score:.2f}")
|
| 87 |
output_lines.append(f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)")
|
|
|
|
| 88 |
output_lines.append(f"Blur Score: {blur_score:.2f}")
|
| 89 |
output_lines.append(f"Noise Score: {noise_score:.2f}")
|
| 90 |
output_lines.append(f"Metadata Kamera: {'Ada' if exif_present else 'Tidak Ada'}")
|