Spaces:
Sleeping
Sleeping
| 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() |