Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,11 +8,17 @@ import cv2
|
|
| 8 |
# MODEL DETEKSI AI
|
| 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 |
# ----------------------------
|
| 17 |
# ANALISIS LOKAL
|
| 18 |
# ----------------------------
|
|
@@ -38,11 +44,12 @@ def has_camera_exif(image):
|
|
| 38 |
return False
|
| 39 |
|
| 40 |
# ----------------------------
|
| 41 |
-
# DETEKSI HYBRID
|
| 42 |
# ----------------------------
|
| 43 |
def detect_image(image: Image.Image):
|
| 44 |
output_lines = []
|
| 45 |
|
|
|
|
| 46 |
hf_score = 0
|
| 47 |
hf_label = "N/A"
|
| 48 |
hf_conf = 0
|
|
@@ -51,32 +58,46 @@ def detect_image(image: Image.Image):
|
|
| 51 |
result = hf_detector(image)
|
| 52 |
hf_label = result[0]['label']
|
| 53 |
hf_conf = result[0]['score'] * 100
|
| 54 |
-
if
|
| 55 |
hf_score = hf_conf
|
| 56 |
except:
|
| 57 |
hf_score = 0
|
| 58 |
|
| 59 |
-
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 +=
|
| 74 |
if not exif_present:
|
| 75 |
-
local_score +=
|
| 76 |
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
if weighted_score >
|
| 80 |
final_result = "π€ AI Detected"
|
| 81 |
else:
|
| 82 |
final_result = "β
Foto Asli"
|
|
@@ -85,6 +106,7 @@ def detect_image(image: Image.Image):
|
|
| 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'}")
|
|
|
|
| 8 |
# MODEL DETEKSI AI
|
| 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 |
# ----------------------------
|
|
|
|
| 44 |
return False
|
| 45 |
|
| 46 |
# ----------------------------
|
| 47 |
+
# DETEKSI HYBRID DENGAN THRESHOLD LAMA
|
| 48 |
# ----------------------------
|
| 49 |
def detect_image(image: Image.Image):
|
| 50 |
output_lines = []
|
| 51 |
|
| 52 |
+
# -------- HF AI-detector --------
|
| 53 |
hf_score = 0
|
| 54 |
hf_label = "N/A"
|
| 55 |
hf_conf = 0
|
|
|
|
| 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 |
except:
|
| 64 |
hf_score = 0
|
| 65 |
|
| 66 |
+
if hf_score > 50: # threshold HF lama
|
|
|
|
| 67 |
final_result = "π€ AI Detected"
|
| 68 |
weighted_score = hf_score
|
| 69 |
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
|
| 70 |
output_lines.append(f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)")
|
| 71 |
return "\n".join(output_lines)
|
| 72 |
|
| 73 |
+
# -------- General model --------
|
| 74 |
+
general_score = 0
|
| 75 |
+
general_label = "N/A"
|
| 76 |
+
general_conf = 0
|
| 77 |
+
if general_model:
|
| 78 |
+
try:
|
| 79 |
+
result2 = general_model(image)
|
| 80 |
+
general_label = result2[0]['label']
|
| 81 |
+
general_conf = result2[0]['score'] * 100
|
| 82 |
+
if any(x in general_label.lower() for x in ["anime","cartoon","illustration","maya","3d"]):
|
| 83 |
+
general_score = general_conf
|
| 84 |
+
except:
|
| 85 |
+
general_score = 0
|
| 86 |
+
|
| 87 |
+
# -------- Analisis lokal --------
|
| 88 |
blur_score = calculate_blur(image)
|
| 89 |
noise_score = calculate_noise(image)
|
| 90 |
exif_present = has_camera_exif(image)
|
| 91 |
local_score = 0
|
| 92 |
if blur_score < 100 or noise_score < 10:
|
| 93 |
+
local_score += 50
|
| 94 |
if not exif_present:
|
| 95 |
+
local_score += 10
|
| 96 |
|
| 97 |
+
# -------- Weighted Score --------
|
| 98 |
+
weighted_score = general_score*0.7 + local_score*0.3
|
| 99 |
|
| 100 |
+
if weighted_score > 50:
|
| 101 |
final_result = "π€ AI Detected"
|
| 102 |
else:
|
| 103 |
final_result = "β
Foto Asli"
|
|
|
|
| 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'}")
|