Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,92 +2,99 @@ import gradio as gr
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
-
import cv2
|
| 6 |
|
| 7 |
-
# Model utama
|
| 8 |
detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
|
| 9 |
-
# Model tambahan general classifier
|
| 10 |
general = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
return noise
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
def detect_image(img):
|
| 24 |
try:
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
# Prediksi general
|
| 41 |
-
result_gen = general(img)
|
| 42 |
-
label_gen = result_gen[0]['label']
|
| 43 |
-
conf_gen = result_gen[0]['score'] * 100
|
| 44 |
-
|
| 45 |
-
# Hybrid scoring
|
| 46 |
-
if "artificial" in label_ai or "fake" in label_ai:
|
| 47 |
-
ai_score = max(conf_ai, 70)
|
| 48 |
-
elif "human" in label_ai:
|
| 49 |
-
ai_score = 100 - conf_ai * 0.7
|
| 50 |
else:
|
| 51 |
-
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
if noise_score < 30:
|
| 62 |
-
ai_score += 5
|
| 63 |
|
| 64 |
-
# Clamp nilai
|
| 65 |
-
ai_score = max(0, min(100, ai_score))
|
| 66 |
-
real_score = 100 - ai_score
|
| 67 |
-
|
| 68 |
-
# Output
|
| 69 |
output = f"""
|
| 70 |
### Hasil Deteksi:
|
| 71 |
-
🖼️ Gambar ini {
|
| 72 |
|
| 73 |
-
**Model AI-detector:** {
|
| 74 |
-
**Model General (ViT):** {
|
| 75 |
-
Blur Score
|
| 76 |
-
Noise Score
|
| 77 |
-
Metadata Kamera
|
| 78 |
"""
|
| 79 |
return output
|
| 80 |
-
|
| 81 |
except Exception as e:
|
| 82 |
return f"Terjadi error: {str(e)}"
|
| 83 |
|
| 84 |
-
# Gradio
|
| 85 |
iface = gr.Interface(
|
| 86 |
fn=detect_image,
|
| 87 |
inputs=gr.Image(type="pil"),
|
| 88 |
outputs="markdown",
|
| 89 |
-
title="AI vs Real Image Detector (
|
| 90 |
-
description="
|
| 91 |
)
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
|
|
|
| 2 |
from transformers import pipeline
|
| 3 |
from PIL import Image
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
| 6 |
+
# Model utama
|
| 7 |
detector = pipeline("image-classification", model="umm-maybe/AI-image-detector")
|
|
|
|
| 8 |
general = pipeline("image-classification", model="google/vit-base-patch16-224")
|
| 9 |
|
| 10 |
+
# Estimasi blur & noise tanpa OpenCV
|
| 11 |
+
def estimate_blur(img):
|
| 12 |
+
gray = img.convert("L")
|
| 13 |
+
arr = np.array(gray)
|
| 14 |
+
gx, gy = np.gradient(arr)
|
| 15 |
+
laplacian_var = np.var(gx + gy)
|
| 16 |
+
return laplacian_var
|
| 17 |
+
|
| 18 |
+
def estimate_noise(img):
|
| 19 |
+
gray = img.convert("L")
|
| 20 |
+
arr = np.array(gray)
|
| 21 |
+
m = np.mean(arr)
|
| 22 |
+
noise = np.mean((arr - m) ** 2)
|
| 23 |
return noise
|
| 24 |
|
| 25 |
+
# Pengecekan pola photorealistic AI
|
| 26 |
+
def photorealistic_ai_adjustment(img, ai_score):
|
| 27 |
+
# Cek metadata, blur, dan noise
|
| 28 |
+
has_metadata = bool(img.info)
|
| 29 |
+
blur = estimate_blur(img)
|
| 30 |
+
noise = estimate_noise(img)
|
| 31 |
+
|
| 32 |
+
# Logika sensitif:
|
| 33 |
+
# Foto asli biasanya ada metadata kamera, blur & noise tertentu
|
| 34 |
+
# Jika metadata kosong, blur sangat rendah, dan noise aneh => kemungkinan AI lebih tinggi
|
| 35 |
+
adjustment = 0
|
| 36 |
+
if not has_metadata:
|
| 37 |
+
adjustment += 10
|
| 38 |
+
if blur < 100: # sangat tajam
|
| 39 |
+
adjustment += 10
|
| 40 |
+
if noise < 50: # terlalu bersih
|
| 41 |
+
adjustment += 10
|
| 42 |
+
|
| 43 |
+
ai_score += adjustment
|
| 44 |
+
ai_score = min(ai_score, 100) # maksimal 100%
|
| 45 |
+
return ai_score
|
| 46 |
+
|
| 47 |
def detect_image(img):
|
| 48 |
try:
|
| 49 |
+
# Prediksi AI detector
|
| 50 |
+
result1 = detector(img)
|
| 51 |
+
label1 = result1[0]['label'].lower()
|
| 52 |
+
conf1 = round(result1[0]['score'] * 100, 2)
|
| 53 |
+
|
| 54 |
+
# Prediksi model general
|
| 55 |
+
result2 = general(img)
|
| 56 |
+
label2 = result2[0]['label']
|
| 57 |
+
conf2 = round(result2[0]['score'] * 100, 2)
|
| 58 |
+
|
| 59 |
+
# Hitung AI % awal
|
| 60 |
+
if "artificial" in label1 or "fake" in label1:
|
| 61 |
+
ai_percent = conf1
|
| 62 |
+
elif "human" in label1:
|
| 63 |
+
ai_percent = 100 - conf1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
else:
|
| 65 |
+
ai_percent = conf1
|
| 66 |
|
| 67 |
+
# Adjust untuk photorealistic AI
|
| 68 |
+
ai_percent = photorealistic_ai_adjustment(img, ai_percent)
|
| 69 |
+
ai_percent = round(ai_percent, 2)
|
| 70 |
+
real_percent = round(100 - ai_percent, 2)
|
| 71 |
|
| 72 |
+
# Blur & Noise untuk info
|
| 73 |
+
blur_score = round(estimate_blur(img), 2)
|
| 74 |
+
noise_score = round(estimate_noise(img), 2)
|
| 75 |
+
has_metadata = "Ada" if img.info else "Tidak"
|
|
|
|
|
|
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
output = f"""
|
| 78 |
### Hasil Deteksi:
|
| 79 |
+
🖼️ Gambar ini {ai_percent}% AI / {real_percent}% Asli
|
| 80 |
|
| 81 |
+
**Model AI-detector:** {label1} ({conf1}%)
|
| 82 |
+
**Model General (ViT):** {label2} ({conf2}%)
|
| 83 |
+
**Blur Score:** {blur_score}
|
| 84 |
+
**Noise Score:** {noise_score}
|
| 85 |
+
**Metadata Kamera:** {has_metadata}
|
| 86 |
"""
|
| 87 |
return output
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
return f"Terjadi error: {str(e)}"
|
| 90 |
|
| 91 |
+
# UI Gradio
|
| 92 |
iface = gr.Interface(
|
| 93 |
fn=detect_image,
|
| 94 |
inputs=gr.Image(type="pil"),
|
| 95 |
outputs="markdown",
|
| 96 |
+
title="Hybrid AI vs Real Image Detector (Sensitif Photorealistic)",
|
| 97 |
+
description="Upload foto untuk mendeteksi kemungkinan besar gambar asli atau hasil AI. Lebih sensitif terhadap AI photorealistic. 100% gratis."
|
| 98 |
)
|
| 99 |
|
| 100 |
if __name__ == "__main__":
|