import gradio as gr from transformers import pipeline from PIL import Image, ExifTags import numpy as np import cv2 # ---------------------------- # MODEL # ---------------------------- try: hf_detector = pipeline("image-classification", model="umm-maybe/AI-image-detector") except Exception as e: hf_detector = None print("HF AI-detector gagal dimuat:", e) try: general_model = pipeline("image-classification", model="google/vit-base-patch16-224") except Exception as e: general_model = None print("General classifier 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 DENGAN PERSENTASE # ---------------------------- def detect_image(image): hf_score = 0 general_score = 0 local_score = 0 # -------- HF AI-detector -------- hf_label, hf_conf = "N/A", 0 if hf_detector: try: result = hf_detector(image) hf_label = result[0]['label'] hf_conf = result[0]['score'] * 100 # Jika label mengandung tanda AI → skor AI = confidence if any(x in hf_label.lower() for x in ["fake", "ai", "artificial"]): hf_score = hf_conf except: hf_score = 0 # -------- General model -------- general_label, general_conf = "N/A", 0 if general_model: try: result2 = general_model(image) general_label = result2[0]['label'] general_conf = result2[0]['score'] * 100 if any(x in general_label.lower() for x in ["anime","cartoon","illustration","maya","3d"]): general_score = general_conf * 0.7 # dikurangi bobot karena cadangan except: general_score = 0 # -------- Analisis lokal -------- blur_score = calculate_blur(image) noise_score = calculate_noise(image) exif_present = has_camera_exif(image) if blur_score < 100 or noise_score < 10: local_score += 50 if not exif_present: local_score += 10 # -------- Weighted Score -------- total_ai_score = hf_score*0.7 + general_score*0.2 + local_score*0.1 total_ai_score = min(max(total_ai_score, 0), 100) # clamp 0–100 total_real_score = 100 - total_ai_score # -------- Output -------- if total_ai_score == 100: final_text = "🤖 Gambar ini hasil AI" elif total_real_score == 100: final_text = "✅ Gambar ini asli" else: final_text = f"🖼️ {total_ai_score:.2f}% AI / {total_real_score:.2f}% Asli" output_lines = [ f"### Hasil Deteksi:\n{final_text}", f"HF AI-detector: {hf_label} ({hf_conf:.2f}%)", f"General Model: {general_label} ({general_conf:.2f}%)", f"Blur Score: {blur_score:.2f}", f"Noise Score: {noise_score:.2f}", 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="Hybrid AI vs Foto Asli Detector (Gratis)", description="Unggah gambar, sistem akan mendeteksi persentase AI dan persentase asli. Tidak ada kategori tidak pasti." ) if __name__ == "__main__": iface.launch()