File size: 3,663 Bytes
4e7f850
 
 
 
 
 
 
 
842df4f
4e7f850
 
 
 
 
 
d73cdb3
4e7f850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
acb43e1
4e7f850
 
 
 
 
 
 
 
 
 
 
 
 
 
d73cdb3
4e7f850
79c039a
4e7f850
 
 
 
 
 
c47d277
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import gradio as gr
import os
import tempfile
import time
from facecomparison_multi_resume import DeepfakeDetector
from PIL import Image

# 1. MENGAMBIL KUNCI API DARI ENVIRONMENT VARIABLES (SECRETS)


API_KEYS = {
    "qwen": os.environ.get("OPENROUTER_API_KEY_QWEN"),
    "gpt": os.environ.get("OPENROUTER_API_KEY_GPT"),
    "gemini": os.environ.get("OPENROUTER_API_KEY_GEMINI"),
    "llama": os.environ.get("OPENROUTER_API_KEY_LLAMA"),
    # "cohere": os.environ.get("OPENROUTER_API_KEY_COHERE"),
}

MODEL_NAMES = ["qwen", "gpt", "gemini", "llama", "cohere"]

# ===================================================================
# 2. FUNGSI UTAMA UNTUK ANALISIS SATU GAMBAR
# ===================================================================

def analyze_image_with_llms(image_pil):
    """
    Menerima gambar PIL, memanggil 5 LLM secara berurutan, dan mengembalikan hasilnya.
    """
    if image_pil is None:
        return "N/A", "N/A", "N/A", "N/A", "N/A"

    # Simpan gambar yang diunggah sementara
    with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp_file:
        # Gunakan mode RGB untuk kompatibilitas yang lebih baik
        image_pil.convert("RGB").save(tmp_file.name, "JPEG", quality=90)
        temp_path = tmp_file.name

    all_results = {}
    
    for model_name in MODEL_NAMES:
        api_key = API_KEYS.get(model_name)
        
        if not api_key:
            all_results[model_name] = f"❌ Key Missing"
            continue

        try:
            # Inisialisasi Detektor
            detector = DeepfakeDetector(
                api_key=api_key,
                model_name=model_name,
                use_face_detector=True # Tetap gunakan cropping RetinaFace
            )
            
            # Panggil fungsi deteksi inti
            result, _, _ = detector.detect_deepfake_llm(temp_path)
            
            # Ubah output yang ambigu menjadi 'ERROR' untuk tampilan UI yang bersih
            if result == "UNKNOWN" or result == "ERROR":
                 all_results[model_name] = f"⚠️ LLM Gagal Tebak"
            else:
                 all_results[model_name] = result
            
        except Exception as e:
            all_results[model_name] = f"❌ API Error: {str(e)[:50]}"

        # Tambahkan delay untuk menghindari Rate Limit OpenRouter
        time.sleep(1.5) 

    # Bersihkan file sementara
    os.unlink(temp_path)

    # Kembalikan hasil dalam urutan yang benar
    return (
        all_results.get("qwen", "Error"),
        all_results.get("gpt", "Error"),
        all_results.get("gemini", "Error"),
        all_results.get("llama", "Error"),
        # all_results.get("cohere", "Error"),
    )

# ===================================================================
# 3. INTERFACE GRADIOL
# ===================================================================

iface = gr.Interface(
    fn=analyze_image_with_llms,
    inputs=gr.Image(type="pil", label="🖼️ Upload Wajah untuk Analisis Deepfake"),
    outputs=[
        gr.Textbox(label="1. Qwen Prediction", type="text"),
        gr.Textbox(label="2. GPT-4o Prediction", type="text"),
        gr.Textbox(label="3. Gemini 2.5 Flash Prediction", type="text"),
        gr.Textbox(label="4. Llama 3.2 Vision Prediction", type="text"),
        # gr.Textbox(label="5. cohere/command-r-plus-08-2024", type="text")
    ],
    title="🔬 Vision-Language Models for Deepfake Face Detection",
    description="Unggah gambar wajah. 5 LLM Multimodal (via OpenRouter) akan menganalisis dan menebak: **REAL** atau **FAKE**.",
    allow_flagging="never",
    theme=gr.themes.Soft()
)

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