Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| from PIL import Image | |
| import numpy as np | |
| # ---------------------------- | |
| # Inisialisasi model publik | |
| # ---------------------------- | |
| model1 = pipeline("image-classification", model="microsoft/resnet-50") | |
| model2 = pipeline("image-classification", model="google/vit-base-patch16-224") | |
| # ---------------------------- | |
| # Fungsi deteksi dengan ensemble | |
| # ---------------------------- | |
| def detect_image(image): | |
| results = [] | |
| # Model 1 | |
| res1 = model1(image)[0] | |
| label1, score1 = res1['label'].lower(), res1['score'] | |
| label1_final = 'human' if 'person' in label1 or 'human' in label1 else 'ai' | |
| results.append((label1_final, score1)) | |
| # Model 2 | |
| res2 = model2(image)[0] | |
| label2, score2 = res2['label'].lower(), res2['score'] | |
| label2_final = 'human' if 'person' in label2 or 'human' in label2 else 'ai' | |
| results.append((label2_final, score2)) | |
| # ---------------------------- | |
| # Voting + threshold | |
| # ---------------------------- | |
| votes = [r[0] for r in results] | |
| final_label = max(set(votes), key=votes.count) | |
| # Rata-rata confidence untuk label final | |
| relevant_scores = [s for (l, s) in results if l == final_label] | |
| avg_confidence = np.mean(relevant_scores) * 100 | |
| # Tentukan hasil akhir | |
| if avg_confidence < 80: | |
| final_result = "⚠️ Tidak Pasti (cek manual)" | |
| elif final_label == 'human': | |
| final_result = f"✅ Foto Asli ({avg_confidence:.2f}%)" | |
| else: | |
| final_result = f"🤖 AI Detected ({avg_confidence:.2f}%)" | |
| # Detail per model | |
| output = f""" | |
| ### Hasil Deteksi: | |
| {final_result} | |
| **Model 1 (ResNet-50):** {label1} ({score1*100:.2f}%) | |
| **Model 2 (ViT):** {label2} ({score2*100:.2f}%) | |
| """ | |
| return output | |
| # ---------------------------- | |
| # Gradio Interface | |
| # ---------------------------- | |
| iface = gr.Interface( | |
| fn=detect_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs="markdown", | |
| title="AI vs Foto Asli Detector", | |
| description="Unggah gambar, sistem akan mendeteksi apakah gambar kemungkinan besar asli atau dihasilkan AI menggunakan ensemble 2 model publik + voting." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |