Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| from PIL import Image, ExifTags, ImageStat, ImageFilter | |
| import numpy as np | |
| # Model utama untuk deteksi AI | |
| detector = pipeline("image-classification", model="umm-maybe/AI-image-detector") | |
| # Model tambahan general classifier (backup) | |
| general = pipeline("image-classification", model="google/vit-base-patch16-224") | |
| def analyze_noise(img): | |
| gray = img.convert("L") | |
| arr = np.array(gray) | |
| return np.std(arr) # Standar deviasi β noise | |
| def analyze_blur(img): | |
| gray = img.convert("L") | |
| arr = np.array(gray) | |
| lap = cv2.Laplacian(arr, cv2.CV_64F).var() | |
| return lap # Variansi Laplacian β blur | |
| def metadata_score(img): | |
| try: | |
| exif = img._getexif() | |
| if exif is None: | |
| return 1 # Tidak ada metadata β kemungkinan AI | |
| for tag, value in exif.items(): | |
| decoded = ExifTags.TAGS.get(tag, tag) | |
| if decoded == "Make" or decoded == "Model": | |
| return 0 # Ada kamera β kemungkinan asli | |
| return 1 | |
| except: | |
| return 1 | |
| def detect_image(img: Image.Image): | |
| try: | |
| # Prediksi AI-detector | |
| result1 = detector(img) | |
| label1 = result1[0]['label'] | |
| conf1 = result1[0]['score'] # 0-1 | |
| # Prediksi general model | |
| result2 = general(img) | |
| label2 = result2[0]['label'] | |
| conf2 = result2[0]['score'] | |
| # Analisis blur & noise | |
| noise = analyze_noise(img) | |
| try: | |
| import cv2 | |
| blur = analyze_blur(img) | |
| except: | |
| blur = 0 | |
| # Metadata | |
| meta = metadata_score(img) | |
| # Weighted skor akhir (lebih sensitif terhadap AI photorealistic) | |
| ai_score = conf1 * 0.6 + meta * 0.2 + (1 - min(noise/100,1)) * 0.1 + (1 - min(blur/1000,1)) * 0.1 | |
| ai_score = min(max(ai_score, 0), 1) # Clamp 0-1 | |
| human_score = 1 - ai_score | |
| ai_percent = round(ai_score * 100, 2) | |
| human_percent = round(human_score * 100, 2) | |
| output = f""" | |
| ### Hasil Deteksi: | |
| πΌοΈ Gambar ini {ai_percent}% AI / {human_percent}% Asli | |
| **Model AI-detector:** {label1} ({round(conf1*100,2)}%) | |
| **Model General (ViT):** {label2} ({round(conf2*100,2)}%) | |
| **Blur Score:** {round(blur,2)} | |
| **Noise Score:** {round(noise,2)} | |
| **Metadata Kamera:** {"Ada" if meta==0 else "Tidak Ada"} | |
| """ | |
| return output | |
| except Exception as e: | |
| return f"Terjadi error: {str(e)}" | |
| iface = gr.Interface( | |
| fn=detect_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs="markdown", | |
| title="Hybrid AI vs Foto Asli Detector", | |
| description="Upload foto untuk mendeteksi persentase AI vs foto asli. Lebih sensitif terhadap AI photorealistic." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |