Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,32 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
-
#
|
| 8 |
# ----------------------------
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
# ----------------------------
|
| 15 |
-
#
|
| 16 |
# ----------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def has_camera_exif(image):
|
| 18 |
try:
|
| 19 |
exif = image._getexif()
|
|
@@ -27,25 +40,58 @@ def has_camera_exif(image):
|
|
| 27 |
return False
|
| 28 |
|
| 29 |
# ----------------------------
|
| 30 |
-
#
|
| 31 |
# ----------------------------
|
| 32 |
def detect_image(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
blur_score = calculate_blur(image)
|
|
|
|
| 34 |
exif_present = has_camera_exif(image)
|
| 35 |
-
|
| 36 |
-
#
|
| 37 |
-
# blur
|
| 38 |
-
# blur
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
else:
|
| 42 |
-
final_result =
|
| 43 |
-
|
| 44 |
output = f"""
|
| 45 |
### Hasil Deteksi:
|
| 46 |
{final_result}
|
| 47 |
|
|
|
|
| 48 |
**Blur Score:** {blur_score:.2f}
|
|
|
|
| 49 |
**Metadata Kamera:** {'Ada' if exif_present else 'Tidak Ada'}
|
| 50 |
"""
|
| 51 |
return output
|
|
@@ -57,8 +103,8 @@ iface = gr.Interface(
|
|
| 57 |
fn=detect_image,
|
| 58 |
inputs=gr.Image(type="pil"),
|
| 59 |
outputs="markdown",
|
| 60 |
-
title="AI vs Foto Asli Detector (Gratis)",
|
| 61 |
-
description="Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
|
|
|
| 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 Hugging Face AI detector (public)
|
| 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("Hugging Face model gagal dimuat, hanya analisis lokal yang dipakai:", e)
|
| 15 |
|
| 16 |
# ----------------------------
|
| 17 |
+
# Analisis gambar lokal
|
| 18 |
# ----------------------------
|
| 19 |
+
def calculate_blur(image):
|
| 20 |
+
gray = np.array(image.convert("L"))
|
| 21 |
+
return cv2.Laplacian(gray, cv2.CV_64F).var()
|
| 22 |
+
|
| 23 |
+
def calculate_noise(image):
|
| 24 |
+
img_gray = np.array(image.convert("L"), dtype=np.float32)
|
| 25 |
+
h, w = img_gray.shape
|
| 26 |
+
mean = np.mean(img_gray)
|
| 27 |
+
noise_std = np.std(img_gray - mean)
|
| 28 |
+
return noise_std
|
| 29 |
+
|
| 30 |
def has_camera_exif(image):
|
| 31 |
try:
|
| 32 |
exif = image._getexif()
|
|
|
|
| 40 |
return False
|
| 41 |
|
| 42 |
# ----------------------------
|
| 43 |
+
# Deteksi hybrid AI vs Foto Asli
|
| 44 |
# ----------------------------
|
| 45 |
def detect_image(image):
|
| 46 |
+
scores = []
|
| 47 |
+
|
| 48 |
+
# ---- HUGGING FACE ----
|
| 49 |
+
if hf_detector:
|
| 50 |
+
try:
|
| 51 |
+
result = hf_detector(image)
|
| 52 |
+
label = result[0]['label'].lower()
|
| 53 |
+
conf = result[0]['score'] * 100
|
| 54 |
+
if "fake" in label or "ai" in label or "artificial" in label:
|
| 55 |
+
scores.append(conf) # AI score
|
| 56 |
+
else:
|
| 57 |
+
scores.append(0) # Foto asli = 0 AI score
|
| 58 |
+
except:
|
| 59 |
+
scores.append(0)
|
| 60 |
+
else:
|
| 61 |
+
scores.append(0)
|
| 62 |
+
|
| 63 |
+
# ---- ANALISIS LOKAL ----
|
| 64 |
blur_score = calculate_blur(image)
|
| 65 |
+
noise_score = calculate_noise(image)
|
| 66 |
exif_present = has_camera_exif(image)
|
| 67 |
+
|
| 68 |
+
# Blur & noise heuristics
|
| 69 |
+
# Foto asli: blur tinggi (>100) atau noise > threshold
|
| 70 |
+
# AI: blur rendah atau noise sangat rendah
|
| 71 |
+
local_ai_score = 0
|
| 72 |
+
if blur_score < 100 or noise_score < 10:
|
| 73 |
+
local_ai_score += 50 # menambah skor AI
|
| 74 |
+
if not exif_present:
|
| 75 |
+
local_ai_score += 10 # sedikit penalti jika metadata hilang
|
| 76 |
+
|
| 77 |
+
scores.append(local_ai_score)
|
| 78 |
+
|
| 79 |
+
# ---- Gabungkan skor ----
|
| 80 |
+
avg_score = sum(scores) / len(scores)
|
| 81 |
+
|
| 82 |
+
# ---- Tentukan hasil final ----
|
| 83 |
+
if avg_score > 50:
|
| 84 |
+
final_result = "π€ AI Detected"
|
| 85 |
else:
|
| 86 |
+
final_result = "β
Foto Asli"
|
| 87 |
+
|
| 88 |
output = f"""
|
| 89 |
### Hasil Deteksi:
|
| 90 |
{final_result}
|
| 91 |
|
| 92 |
+
**Skor rata-rata AI:** {avg_score:.2f}
|
| 93 |
**Blur Score:** {blur_score:.2f}
|
| 94 |
+
**Noise Score:** {noise_score:.2f}
|
| 95 |
**Metadata Kamera:** {'Ada' if exif_present else 'Tidak Ada'}
|
| 96 |
"""
|
| 97 |
return output
|
|
|
|
| 103 |
fn=detect_image,
|
| 104 |
inputs=gr.Image(type="pil"),
|
| 105 |
outputs="markdown",
|
| 106 |
+
title="AI vs Foto Asli Detector (Hybrid, Gratis)",
|
| 107 |
+
description="Unggah gambar, sistem hybrid akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI."
|
| 108 |
)
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|