Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,105 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import os
|
| 3 |
-
import tempfile
|
| 4 |
-
import time
|
| 5 |
-
from facecomparison_multi_resume import DeepfakeDetector
|
| 6 |
-
from PIL import Image
|
| 7 |
-
|
| 8 |
-
# ===================================================================
|
| 9 |
-
# 1. MENGAMBIL KUNCI API DARI ENVIRONMENT VARIABLES (SECRETS)
|
| 10 |
-
# Nama-nama ini harus sama persis dengan yang Anda set di Hugging Face Secrets!
|
| 11 |
-
# ===================================================================
|
| 12 |
-
|
| 13 |
-
API_KEYS = {
|
| 14 |
-
"qwen": os.environ.get("OPENROUTER_API_KEY_QWEN"),
|
| 15 |
-
"gpt": os.environ.get("OPENROUTER_API_KEY_GPT"),
|
| 16 |
-
"gemini": os.environ.get("OPENROUTER_API_KEY_GEMINI"),
|
| 17 |
-
"llama": os.environ.get("OPENROUTER_API_KEY_LLAMA"),
|
| 18 |
-
"cohere": os.environ.get("OPENROUTER_API_KEY_COHERE"),
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
MODEL_NAMES = ["qwen", "gpt", "gemini", "llama", "cohere"]
|
| 22 |
-
|
| 23 |
-
# ===================================================================
|
| 24 |
-
# 2. FUNGSI UTAMA UNTUK ANALISIS SATU GAMBAR
|
| 25 |
-
# ===================================================================
|
| 26 |
-
|
| 27 |
-
def analyze_image_with_llms(image_pil):
|
| 28 |
-
"""
|
| 29 |
-
Menerima gambar PIL, memanggil 5 LLM secara berurutan, dan mengembalikan hasilnya.
|
| 30 |
-
"""
|
| 31 |
-
if image_pil is None:
|
| 32 |
-
return "N/A", "N/A", "N/A", "N/A", "N/A"
|
| 33 |
-
|
| 34 |
-
# Simpan gambar yang diunggah sementara
|
| 35 |
-
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp_file:
|
| 36 |
-
# Gunakan mode RGB untuk kompatibilitas yang lebih baik
|
| 37 |
-
image_pil.convert("RGB").save(tmp_file.name, "JPEG", quality=90)
|
| 38 |
-
temp_path = tmp_file.name
|
| 39 |
-
|
| 40 |
-
all_results = {}
|
| 41 |
-
|
| 42 |
-
for model_name in MODEL_NAMES:
|
| 43 |
-
api_key = API_KEYS.get(model_name)
|
| 44 |
-
|
| 45 |
-
if not api_key:
|
| 46 |
-
all_results[model_name] = f"❌ Key Missing"
|
| 47 |
-
continue
|
| 48 |
-
|
| 49 |
-
try:
|
| 50 |
-
# Inisialisasi Detektor
|
| 51 |
-
detector = DeepfakeDetector(
|
| 52 |
-
api_key=api_key,
|
| 53 |
-
model_name=model_name,
|
| 54 |
-
use_face_detector=True # Tetap gunakan cropping RetinaFace
|
| 55 |
-
)
|
| 56 |
-
|
| 57 |
-
# Panggil fungsi deteksi inti
|
| 58 |
-
result, _, _ = detector.detect_deepfake_llm(temp_path)
|
| 59 |
-
|
| 60 |
-
# Ubah output yang ambigu menjadi 'ERROR' untuk tampilan UI yang bersih
|
| 61 |
-
if result == "UNKNOWN" or result == "ERROR":
|
| 62 |
-
all_results[model_name] = f"⚠️ LLM Gagal Tebak"
|
| 63 |
-
else:
|
| 64 |
-
all_results[model_name] = result
|
| 65 |
-
|
| 66 |
-
except Exception as e:
|
| 67 |
-
all_results[model_name] = f"❌ API Error: {str(e)[:50]}"
|
| 68 |
-
|
| 69 |
-
# Tambahkan delay untuk menghindari Rate Limit OpenRouter
|
| 70 |
-
time.sleep(1.5)
|
| 71 |
-
|
| 72 |
-
# Bersihkan file sementara
|
| 73 |
-
os.unlink(temp_path)
|
| 74 |
-
|
| 75 |
-
# Kembalikan hasil dalam urutan yang benar
|
| 76 |
-
return (
|
| 77 |
-
all_results.get("qwen", "Error"),
|
| 78 |
-
all_results.get("gpt", "Error"),
|
| 79 |
-
all_results.get("gemini", "Error"),
|
| 80 |
-
all_results.get("llama", "Error"),
|
| 81 |
-
all_results.get("
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
# ===================================================================
|
| 85 |
-
# 3. INTERFACE GRADIOL
|
| 86 |
-
# ===================================================================
|
| 87 |
-
|
| 88 |
-
iface = gr.Interface(
|
| 89 |
-
fn=analyze_image_with_llms,
|
| 90 |
-
inputs=gr.Image(type="pil", label="🖼️ Upload Wajah untuk Analisis Deepfake"),
|
| 91 |
-
outputs=[
|
| 92 |
-
gr.Textbox(label="1. Qwen Prediction", type="text"),
|
| 93 |
-
gr.Textbox(label="2. GPT-4o Prediction", type="text"),
|
| 94 |
-
gr.Textbox(label="3. Gemini 2.5 Flash Prediction", type="text"),
|
| 95 |
-
gr.Textbox(label="4. Llama 3.2 Vision Prediction", type="text"),
|
| 96 |
-
gr.Textbox(label="5.
|
| 97 |
-
],
|
| 98 |
-
title="🔬 Perbandingan LLM Multimodal untuk Deteksi Deepfake Wajah",
|
| 99 |
-
description="Unggah gambar wajah. 5 LLM Multimodal (via OpenRouter) akan menganalisis dan menebak: **REAL** atau **FAKE**.",
|
| 100 |
-
allow_flagging="never",
|
| 101 |
-
theme=gr.themes.Soft()
|
| 102 |
-
)
|
| 103 |
-
|
| 104 |
-
if __name__ == "__main__":
|
| 105 |
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import tempfile
|
| 4 |
+
import time
|
| 5 |
+
from facecomparison_multi_resume import DeepfakeDetector
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
# ===================================================================
|
| 9 |
+
# 1. MENGAMBIL KUNCI API DARI ENVIRONMENT VARIABLES (SECRETS)
|
| 10 |
+
# Nama-nama ini harus sama persis dengan yang Anda set di Hugging Face Secrets!
|
| 11 |
+
# ===================================================================
|
| 12 |
+
|
| 13 |
+
API_KEYS = {
|
| 14 |
+
"qwen": os.environ.get("OPENROUTER_API_KEY_QWEN"),
|
| 15 |
+
"gpt": os.environ.get("OPENROUTER_API_KEY_GPT"),
|
| 16 |
+
"gemini": os.environ.get("OPENROUTER_API_KEY_GEMINI"),
|
| 17 |
+
"llama": os.environ.get("OPENROUTER_API_KEY_LLAMA"),
|
| 18 |
+
"cohere": os.environ.get("OPENROUTER_API_KEY_COHERE"),
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
MODEL_NAMES = ["qwen", "gpt", "gemini", "llama", "cohere"]
|
| 22 |
+
|
| 23 |
+
# ===================================================================
|
| 24 |
+
# 2. FUNGSI UTAMA UNTUK ANALISIS SATU GAMBAR
|
| 25 |
+
# ===================================================================
|
| 26 |
+
|
| 27 |
+
def analyze_image_with_llms(image_pil):
|
| 28 |
+
"""
|
| 29 |
+
Menerima gambar PIL, memanggil 5 LLM secara berurutan, dan mengembalikan hasilnya.
|
| 30 |
+
"""
|
| 31 |
+
if image_pil is None:
|
| 32 |
+
return "N/A", "N/A", "N/A", "N/A", "N/A"
|
| 33 |
+
|
| 34 |
+
# Simpan gambar yang diunggah sementara
|
| 35 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp_file:
|
| 36 |
+
# Gunakan mode RGB untuk kompatibilitas yang lebih baik
|
| 37 |
+
image_pil.convert("RGB").save(tmp_file.name, "JPEG", quality=90)
|
| 38 |
+
temp_path = tmp_file.name
|
| 39 |
+
|
| 40 |
+
all_results = {}
|
| 41 |
+
|
| 42 |
+
for model_name in MODEL_NAMES:
|
| 43 |
+
api_key = API_KEYS.get(model_name)
|
| 44 |
+
|
| 45 |
+
if not api_key:
|
| 46 |
+
all_results[model_name] = f"❌ Key Missing"
|
| 47 |
+
continue
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
# Inisialisasi Detektor
|
| 51 |
+
detector = DeepfakeDetector(
|
| 52 |
+
api_key=api_key,
|
| 53 |
+
model_name=model_name,
|
| 54 |
+
use_face_detector=True # Tetap gunakan cropping RetinaFace
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
# Panggil fungsi deteksi inti
|
| 58 |
+
result, _, _ = detector.detect_deepfake_llm(temp_path)
|
| 59 |
+
|
| 60 |
+
# Ubah output yang ambigu menjadi 'ERROR' untuk tampilan UI yang bersih
|
| 61 |
+
if result == "UNKNOWN" or result == "ERROR":
|
| 62 |
+
all_results[model_name] = f"⚠️ LLM Gagal Tebak"
|
| 63 |
+
else:
|
| 64 |
+
all_results[model_name] = result
|
| 65 |
+
|
| 66 |
+
except Exception as e:
|
| 67 |
+
all_results[model_name] = f"❌ API Error: {str(e)[:50]}"
|
| 68 |
+
|
| 69 |
+
# Tambahkan delay untuk menghindari Rate Limit OpenRouter
|
| 70 |
+
time.sleep(1.5)
|
| 71 |
+
|
| 72 |
+
# Bersihkan file sementara
|
| 73 |
+
os.unlink(temp_path)
|
| 74 |
+
|
| 75 |
+
# Kembalikan hasil dalam urutan yang benar
|
| 76 |
+
return (
|
| 77 |
+
all_results.get("qwen", "Error"),
|
| 78 |
+
all_results.get("gpt", "Error"),
|
| 79 |
+
all_results.get("gemini", "Error"),
|
| 80 |
+
all_results.get("llama", "Error"),
|
| 81 |
+
all_results.get("Deepseek", "Error"),
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# ===================================================================
|
| 85 |
+
# 3. INTERFACE GRADIOL
|
| 86 |
+
# ===================================================================
|
| 87 |
+
|
| 88 |
+
iface = gr.Interface(
|
| 89 |
+
fn=analyze_image_with_llms,
|
| 90 |
+
inputs=gr.Image(type="pil", label="🖼️ Upload Wajah untuk Analisis Deepfake"),
|
| 91 |
+
outputs=[
|
| 92 |
+
gr.Textbox(label="1. Qwen Prediction", type="text"),
|
| 93 |
+
gr.Textbox(label="2. GPT-4o Prediction", type="text"),
|
| 94 |
+
gr.Textbox(label="3. Gemini 2.5 Flash Prediction", type="text"),
|
| 95 |
+
gr.Textbox(label="4. Llama 3.2 Vision Prediction", type="text"),
|
| 96 |
+
gr.Textbox(label="5. deepseek-r1-0528-qwen3-8b:free", type="text")
|
| 97 |
+
],
|
| 98 |
+
title="🔬 Perbandingan LLM Multimodal untuk Deteksi Deepfake Wajah",
|
| 99 |
+
description="Unggah gambar wajah. 5 LLM Multimodal (via OpenRouter) akan menganalisis dan menebak: **REAL** atau **FAKE**.",
|
| 100 |
+
allow_flagging="never",
|
| 101 |
+
theme=gr.themes.Soft()
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
iface.launch()
|