Spaces:
Sleeping
Sleeping
File size: 3,235 Bytes
8c30b41 f034887 f658766 f23fa83 923a637 f658766 94e9dd7 f658766 f034887 94e9dd7 f034887 505b320 f658766 505b320 f658766 f034887 505b320 f034887 f23fa83 f658766 f23fa83 94e9dd7 f23fa83 b735378 f034887 b735378 f034887 b735378 94e9dd7 b735378 505b320 31daa87 505b320 94e9dd7 b735378 94e9dd7 f23fa83 f034887 f23fa83 31daa87 f034887 94e9dd7 f034887 94e9dd7 f034887 94e9dd7 f034887 94e9dd7 f034887 f658766 f034887 b735378 923a637 b735378 923a637 f658766 c03cdfb 8c30b41 923a637 2433dff b735378 8c30b41 923a637 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import gradio as gr
from transformers import pipeline
from PIL import Image, ExifTags
import numpy as np
import cv2
# ----------------------------
# MODEL DETEKSI AI
# ----------------------------
try:
hf_detector = pipeline("image-classification", model="ckpt/real-or-ai") # model gratis HF khusus AI detection
except Exception as e:
hf_detector = None
print("HF AI-detector gagal dimuat:", e)
# ----------------------------
# ANALISIS LOKAL
# ----------------------------
def calculate_blur(image):
gray = np.array(image.convert("L"))
return cv2.Laplacian(gray, cv2.CV_64F).var()
def calculate_noise(image):
gray = np.array(image.convert("L"), dtype=np.float32)
noise_std = np.std(gray - np.mean(gray))
return noise_std
def has_camera_exif(image):
try:
exif = image._getexif()
if exif:
for tag, value in exif.items():
decoded = ExifTags.TAGS.get(tag, tag)
if decoded in ["Make", "Model"]:
return True
except:
return False
return False
# ----------------------------
# DETEKSI HYBRID
# ----------------------------
def detect_image(image):
output_lines = []
hf_score = 0
hf_label = "N/A"
hf_conf = 0
if hf_detector:
try:
result = hf_detector(image)
hf_label = result[0]['label']
hf_conf = result[0]['score'] * 100
if "ai" in hf_label.lower() or "synthetic" in hf_label.lower():
hf_score = hf_conf
except:
hf_score = 0
# Threshold HF lebih rendah agar sensitif
if hf_score > 30:
final_result = "🤖 AI Detected"
weighted_score = hf_score
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
output_lines.append(f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)")
return "\n".join(output_lines)
# Analisis lokal tambahan
blur_score = calculate_blur(image)
noise_score = calculate_noise(image)
exif_present = has_camera_exif(image)
local_score = 0
if blur_score < 100 or noise_score < 10:
local_score += 30
if not exif_present:
local_score += 20
weighted_score = hf_score * 0.6 + local_score * 0.4
if weighted_score > 40:
final_result = "🤖 AI Detected"
else:
final_result = "✅ Foto Asli"
# -------- Output --------
output_lines.append(f"### Hasil Deteksi:\n{final_result}")
output_lines.append(f"Weighted Skor: {weighted_score:.2f}")
output_lines.append(f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)")
output_lines.append(f"Blur Score: {blur_score:.2f}")
output_lines.append(f"Noise Score: {noise_score:.2f}")
output_lines.append(f"Metadata Kamera: {'Ada' if exif_present else 'Tidak Ada'}")
return "\n".join(output_lines)
# ----------------------------
# Gradio Interface
# ----------------------------
iface = gr.Interface(
fn=detect_image,
inputs=gr.Image(type="pil"),
outputs="markdown",
title="AI vs Foto Asli Detector (Versi Akurat)",
description="Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI."
)
if __name__ == "__main__":
iface.launch()
|