File size: 2,214 Bytes
555fed5
 
 
 
 
 
 
 
 
3898bcc
 
034fc15
7908448
3898bcc
c87cacc
7908448
 
 
c87cacc
7908448
 
 
3898bcc
3022c96
555fed5
3898bcc
7908448
555fed5
 
 
 
 
 
 
 
 
3898bcc
 
 
cfb7b57
555fed5
 
3898bcc
555fed5
3898bcc
555fed5
 
3022c96
3898bcc
7908448
3898bcc
 
034fc15
3898bcc
034fc15
3898bcc
555fed5
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import gradio as gr
from transformers import pipeline
from PIL import Image

# === Load 3 model publik khusus AI vs Real ===
model1 = pipeline("image-classification", model="prithivMLmods/AI-vs-Deepfake-vs-Real")
model2 = pipeline("image-classification", model="dima806/ai_vs_real_image_detection")
model3 = pipeline("image-classification", model="Hemg/AI-VS-REAL-IMAGE-DETECTION")

def detect_image(img: Image.Image):
    results = {}

    # Model 1
    res1 = model1(img)
    results["Model 1 (prithivMLmods)"] = res1

    # Model 2
    res2 = model2(img)
    results["Model 2 (dima806)"] = res2

    # Model 3
    res3 = model3(img)
    results["Model 3 (Hemg)"] = res3

    # --- Ensemble dengan threshold agar lebih stabil ---
    ai_score, real_score = 0, 0
    for res in [res1, res2, res3]:
        for item in res:
            label = item["label"].lower()
            score = item["score"]
            if "real" in label:
                real_score += score
            elif "fake" in label or "deepfake" in label or "artificial" in label:
                ai_score += score

    # Normalisasi
    total = ai_score + real_score + 1e-9
    ai_percent = (ai_score / total) * 100
    real_percent = (real_score / total) * 100

    # Keputusan akhir dengan threshold
    if real_percent > ai_percent and real_percent > 55:  
        verdict = f"✅ Foto Asli ({real_percent:.2f}%)"
    elif ai_percent > real_percent and ai_percent > 55:
        verdict = f"⚠️ AI Generated ({ai_percent:.2f}%)"
    else:
        verdict = f"⚠️ Tidak Pasti (Real {real_percent:.2f}%, AI {ai_percent:.2f}%)"

    # Format output
    output = f"## 📊 Ringkasan Deteksi\n"
    for name, res in results.items():
        output += f"\n🔹 **{name}**: {res}\n"

    output += f"\n=== 🧠 ENSEMBLE HASIL AKHIR ===\n{verdict}\n"

    return output

iface = gr.Interface(
    fn=detect_image,
    inputs=gr.Image(type="pil"),
    outputs="markdown",
    title="Deteksi AI vs Foto Asli (Ensemble 3 Model Publik)",
    description="Menggunakan 3 model publik Hugging Face (prithivMLmods, dima806, Hemg) dengan threshold untuk mendeteksi apakah gambar AI-generated atau asli."
)

if __name__ == "__main__":
    iface.launch()