Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
from PIL import Image, ExifTags
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
| 6 |
|
| 7 |
-
# ----------------------------
|
| 8 |
-
# MODEL DETEKSI AI REALISTIS
|
| 9 |
-
# ----------------------------
|
| 10 |
-
try:
|
| 11 |
-
hf_detector = pipeline("image-classification", model="danielgatis/rembg-image-ai-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 |
# ----------------------------
|
|
@@ -43,86 +27,68 @@ def has_camera_exif(image):
|
|
| 43 |
return False
|
| 44 |
return False
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# ----------------------------
|
| 47 |
-
# DETEKSI HYBRID
|
| 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
|
| 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 ["ai", "synthetic", "generated"]):
|
| 62 |
-
hf_score = hf_conf
|
| 63 |
-
except:
|
| 64 |
-
hf_score = 0
|
| 65 |
-
|
| 66 |
-
if hf_score > 40: # threshold lebih sensitif
|
| 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 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
if not exif_present:
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
weighted_score = general_score*0.7 + local_score*0.3
|
| 99 |
|
| 100 |
-
if
|
| 101 |
final_result = "🤖 AI Detected"
|
| 102 |
else:
|
| 103 |
final_result = "✅ Foto Asli"
|
| 104 |
|
| 105 |
-
# -------- Output --------
|
| 106 |
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
|
| 107 |
-
output_lines.append(f"Weighted Skor: {
|
| 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 |
-
#
|
| 118 |
# ----------------------------
|
| 119 |
-
with gr.Blocks(title="AI Realistic Detector (Gratis
|
| 120 |
gr.Markdown("Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI.")
|
| 121 |
-
|
| 122 |
with gr.Row():
|
| 123 |
img_input = gr.Image(type="pil", label="Unggah Gambar")
|
| 124 |
output_md = gr.Markdown(label="Hasil Deteksi")
|
| 125 |
-
|
| 126 |
detect_btn = gr.Button("Deteksi")
|
| 127 |
detect_btn.click(fn=detect_image, inputs=img_input, outputs=output_md)
|
| 128 |
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image, ExifTags
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# ----------------------------
|
| 7 |
# ANALISIS LOKAL
|
| 8 |
# ----------------------------
|
|
|
|
| 27 |
return False
|
| 28 |
return False
|
| 29 |
|
| 30 |
+
# -------- FFT ANALYSIS --------
|
| 31 |
+
def high_freq_artifacts(image):
|
| 32 |
+
gray = np.array(image.convert("L"), dtype=np.float32)
|
| 33 |
+
f = np.fft.fft2(gray)
|
| 34 |
+
fshift = np.fft.fftshift(f)
|
| 35 |
+
magnitude_spectrum = np.abs(fshift)
|
| 36 |
+
# rasio energi high frequency vs total
|
| 37 |
+
rows, cols = gray.shape
|
| 38 |
+
crow, ccol = rows // 2 , cols // 2
|
| 39 |
+
# buat mask low freq
|
| 40 |
+
r = min(crow, ccol) // 4
|
| 41 |
+
mask = np.zeros((rows, cols))
|
| 42 |
+
mask[crow-r:crow+r, ccol-r:ccol+r] = 1
|
| 43 |
+
low_energy = np.sum(magnitude_spectrum * mask)
|
| 44 |
+
total_energy = np.sum(magnitude_spectrum)
|
| 45 |
+
high_ratio = (total_energy - low_energy) / total_energy * 100
|
| 46 |
+
return high_ratio
|
| 47 |
+
|
| 48 |
# ----------------------------
|
| 49 |
+
# DETEKSI AI HYBRID
|
| 50 |
# ----------------------------
|
| 51 |
def detect_image(image: Image.Image):
|
| 52 |
output_lines = []
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
blur_score = calculate_blur(image)
|
| 55 |
noise_score = calculate_noise(image)
|
| 56 |
exif_present = has_camera_exif(image)
|
| 57 |
+
high_freq_score = high_freq_artifacts(image)
|
| 58 |
+
|
| 59 |
+
# Weighted scoring
|
| 60 |
+
score = 0
|
| 61 |
+
if blur_score < 80:
|
| 62 |
+
score += 20
|
| 63 |
+
if noise_score < 8:
|
| 64 |
+
score += 20
|
| 65 |
if not exif_present:
|
| 66 |
+
score += 10
|
| 67 |
+
if high_freq_score > 25: # banyak high freq artifacts = AI
|
| 68 |
+
score += 50
|
|
|
|
| 69 |
|
| 70 |
+
if score > 40:
|
| 71 |
final_result = "🤖 AI Detected"
|
| 72 |
else:
|
| 73 |
final_result = "✅ Foto Asli"
|
| 74 |
|
|
|
|
| 75 |
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
|
| 76 |
+
output_lines.append(f"Weighted Skor: {score:.2f}")
|
|
|
|
|
|
|
| 77 |
output_lines.append(f"Blur Score: {blur_score:.2f}")
|
| 78 |
output_lines.append(f"Noise Score: {noise_score:.2f}")
|
| 79 |
+
output_lines.append(f"High-Freq Artifacts Score: {high_freq_score:.2f}")
|
| 80 |
output_lines.append(f"Metadata Kamera: {'Ada' if exif_present else 'Tidak Ada'}")
|
| 81 |
|
| 82 |
return "\n".join(output_lines)
|
| 83 |
|
| 84 |
# ----------------------------
|
| 85 |
+
# GRADIO INTERFACE
|
| 86 |
# ----------------------------
|
| 87 |
+
with gr.Blocks(title="Hybrid AI Realistic Detector (Gratis)") as demo:
|
| 88 |
gr.Markdown("Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI.")
|
|
|
|
| 89 |
with gr.Row():
|
| 90 |
img_input = gr.Image(type="pil", label="Unggah Gambar")
|
| 91 |
output_md = gr.Markdown(label="Hasil Deteksi")
|
|
|
|
| 92 |
detect_btn = gr.Button("Deteksi")
|
| 93 |
detect_btn.click(fn=detect_image, inputs=img_input, outputs=output_md)
|
| 94 |
|