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 tanpa "Tidak Pasti" | |
| # ---------------------------- | |
| 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 atau ambil confidence tertinggi | |
| # ---------------------------- | |
| votes = [r[0] for r in results] | |
| if votes[0] == votes[1]: | |
| final_label = votes[0] # mayoritas sama | |
| final_conf = np.mean([r[1] for r in results if r[0]==final_label])*100 | |
| else: | |
| # ambil model dengan confidence tertinggi | |
| if results[0][1] > results[1][1]: | |
| final_label = results[0][0] | |
| final_conf = results[0][1]*100 | |
| else: | |
| final_label = results[1][0] | |
| final_conf = results[1][1]*100 | |
| # ---------------------------- | |
| # Hasil akhir | |
| # ---------------------------- | |
| if final_label == 'human': | |
| final_result = f"β Foto Asli ({final_conf:.2f}%)" | |
| else: | |
| final_result = f"π€ AI Detected ({final_conf:.2f}%)" | |
| 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 AI atau Foto Asli berdasarkan 2 model publik." | |
| ) | |
| if __name__ == "__main__": | |
| iface.launch() | |