Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| from PIL import Image | |
| from PIL.ExifTags import TAGS | |
| import numpy as np | |
| # Model utama deteksi AI | |
| detector = pipeline("image-classification", model="umm-maybe/AI-image-detector") | |
| # Model general classifier (backup) | |
| general = pipeline("image-classification", model="google/vit-base-patch16-224") | |
| def get_exif_metadata(img): | |
| """Cek metadata kamera untuk membantu deteksi foto asli""" | |
| try: | |
| exif_data = img._getexif() | |
| if exif_data is None: | |
| return False | |
| for tag_id, value in exif_data.items(): | |
| tag = TAGS.get(tag_id, tag_id) | |
| if tag in ["Make", "Model", "Software"]: | |
| return True | |
| return False | |
| except: | |
| return False | |
| def detect_image(img): | |
| try: | |
| # Prediksi dengan AI-detector | |
| result1 = detector(img) | |
| label1 = result1[0]['label'] | |
| conf1 = result1[0]['score'] * 100 | |
| # Prediksi dengan model general | |
| result2 = general(img) | |
| label2 = result2[0]['label'] | |
| conf2 = result2[0]['score'] * 100 | |
| # Cek metadata kamera | |
| has_metadata = get_exif_metadata(img) | |
| # Weighted hybrid scoring | |
| # Skema: AI-detector 50%, Metadata 30%, general model 20% | |
| ai_score = conf1 if "artificial" in label1.lower() or "fake" in label1.lower() else 0 | |
| human_score = conf1 if "human" in label1.lower() or "real" in label1.lower() else 0 | |
| if has_metadata: | |
| human_score += 30 # boost jika metadata ada | |
| ai_score = min(ai_score, 100) | |
| human_score = min(human_score, 100) | |
| # Normalisasi agar total = 100% | |
| total = ai_score + human_score | |
| if total == 0: | |
| ai_percent = 50 | |
| human_percent = 50 | |
| else: | |
| ai_percent = round((ai_score / total) * 100, 2) | |
| human_percent = round((human_score / total) * 100, 2) | |
| # Tentukan hasil akhir | |
| if ai_percent == 100: | |
| final_text = "🖼️ Gambar ini 100% AI" | |
| elif human_percent == 100: | |
| final_text = "🖼️ Gambar ini asli 100%" | |
| else: | |
| final_text = f"🖼️ Gambar ini {ai_percent}% AI / {human_percent}% Asli" | |
| # Tambahkan info model | |
| output = f""" | |
| ### Hasil Deteksi: | |
| {final_text} | |
| **Model AI-detector:** {label1} ({conf1:.2f}%) | |
| **Model General (ViT):** {label2} ({conf2:.2f}%) | |
| **Metadata Kamera Ada:** {"Ya" if has_metadata else "Tidak"} | |
| """ | |
| return output | |
| except Exception as e: | |
| return f"Terjadi error: {str(e)}" | |
| # UI Gradio | |
| iface = gr.Interface( | |
| fn=detect_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs="markdown", | |
| title="Hybrid AI vs Real Image Detector", | |
| description="Upload gambar, sistem akan mendeteksi persentase AI vs foto asli, sensitif untuk AI photorealistic tapi tetap akurat untuk foto asli." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |