File size: 1,642 Bytes
64a94a9
 
6394c51
f0ff41c
 
6394c51
64a94a9
6394c51
 
 
64a94a9
ed4c5f6
6394c51
 
958932b
6394c51
 
64a94a9
6394c51
64a94a9
6394c51
 
 
 
 
 
 
 
 
 
 
3561a8c
6394c51
 
 
 
 
923a637
6394c51
64a94a9
6394c51
64a94a9
6394c51
 
 
64a94a9
 
19e6f0c
64a94a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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()