Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| from PIL import Image | |
| import numpy as np | |
| # ====== MODEL LIST ====== | |
| model_ids = [ | |
| "vikhyatk/moondream2", # butuh trust_remote_code=True | |
| "umm-maybe/synthetic-image-detector", | |
| "falconsai/nsfw_image_detection" | |
| ] | |
| # Load semua model dengan trust_remote_code | |
| detectors = [pipeline("image-classification", model=m, trust_remote_code=True) for m in model_ids] | |
| def detect_image(img: Image.Image): | |
| results = [] | |
| scores = [] | |
| for det in detectors: | |
| try: | |
| out = det(img) | |
| # Ambil label & skor tertinggi | |
| top = max(out, key=lambda x: x["score"]) | |
| results.append(f"{det.model.config.name_or_path}: {top['label']} ({top['score']:.2f})") | |
| scores.append(top["score"] if "ai" in top["label"].lower() or "fake" in top["label"].lower() or "artificial" in top["label"].lower() else 1 - top["score"]) | |
| except Exception as e: | |
| results.append(f"Error {det.model.config.name_or_path}: {str(e)}") | |
| if scores: | |
| avg_score = np.mean(scores) * 100 | |
| else: | |
| avg_score = 0 | |
| verdict = "AI" if avg_score > 50 else "Asli" | |
| return f"π Hasil Deteksi: {verdict}\nPersentase AI: {avg_score:.2f}%\n\nDetail:\n" + "\n".join(results) | |
| # ====== GRADIO UI ====== | |
| demo = gr.Interface( | |
| fn=detect_image, | |
| inputs=gr.Image(type="pil"), | |
| outputs="text", | |
| title="AI vs Real Image Detector", | |
| description="Upload foto untuk mendeteksi apakah itu AI-generated atau asli. Menggunakan 3 model publik dengan trust_remote_code=True." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |