Spaces:
Sleeping
Sleeping
| 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 WEIGHTED | |
| # ---------------------------- | |
| def detect_image(image): | |
| hf_score = 0 | |
| general_score = 0 | |
| local_score = 0 | |
| # -------- HF AI-detector -------- | |
| if hf_detector: | |
| try: | |
| result = hf_detector(image) | |
| label = result[0]['label'].lower() | |
| conf = result[0]['score'] * 100 | |
| hf_score = conf if any(x in label for x in ["fake", "ai", "artificial"]) else 0 | |
| except: | |
| hf_score = 0 | |
| # -------- General model -------- | |
| if general_model: | |
| try: | |
| result2 = general_model(image) | |
| label2 = result2[0]['label'].lower() | |
| conf2 = result2[0]['score'] * 100 | |
| general_score = conf2 if any(x in label2 for x in ["anime","cartoon","illustration","maya"]) else 0 | |
| except: | |
| general_score = 0 | |
| # -------- Analisis lokal -------- | |
| 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 += 50 | |
| if not exif_present: | |
| local_score += 10 | |
| # -------- Weighted Score -------- | |
| final_score = hf_score*0.6 + general_score*0.25 + local_score*0.15 | |
| if final_score > 50: | |
| final_result = "🤖 AI Detected" | |
| else: | |
| final_result = "✅ Foto Asli" | |
| output = f""" | |
| ### Hasil Deteksi: | |
| {final_result} | |
| **Weighted Skor:** {final_score:.2f} | |
| **HF AI-detector:** {result[0]['label']} ({result[0]['score']*100:.2f}%) | |
| **General Model:** {result2[0]['label']} ({result2[0]['score']*100:.2f}%) | |
| **Blur Score:** {blur_score:.2f} | |
| **Noise Score:** {noise_score:.2f} | |
| **Metadata Kamera:** {'Ada' if exif_present else 'Tidak Ada'} | |
| """ | |
| return output | |
| # ---------------------------- | |
| # Gradio Interface | |
| # ---------------------------- | |
| iface = gr.Interface( | |
| fn=detect_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs="markdown", | |
| title="AI vs Foto Asli Detector (Weighted Hybrid)", | |
| description="Unggah gambar, sistem hybrid akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |