File size: 7,329 Bytes
91491b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dab037b
 
91491b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dab037b
 
 
91491b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99e8b57
91491b1
99e8b57
91491b1
99e8b57
91491b1
 
af86297
 
 
 
 
 
 
99e8b57
af86297
91491b1
99e8b57
91491b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99e8b57
 
91491b1
 
99e8b57
 
 
dab037b
 
99e8b57
 
dab037b
91491b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
model_loader.py
===============
Singleton pattern untuk memuat semua model AI sekali saat startup.
Mencegah OOM (Out of Memory) pada VRAM 4GB RTX 3050 Ti dengan
manajemen memori yang ketat dan Mixed Precision (FP16).
"""

# ============================================================
# PENTING: Set cache directory SEBELUM import timm/huggingface
# Ini menghindari PermissionError di Windows saat timm mencoba
# menulis ke C:\Users\..\.cache\huggingface yang diblokir ACL.
# ============================================================
import os
from pathlib import Path

# Gunakan folder 'models/hf_cache' di dalam project sebagai cache
_PROJECT_ROOT = Path(__file__).parent
_HF_CACHE_DIR = str(_PROJECT_ROOT / "models" / "hf_cache")

# Set env var SEBELUM huggingface_hub / timm diinisialisasi
os.environ["HF_HOME"]               = _HF_CACHE_DIR
os.environ["HUGGINGFACE_HUB_CACHE"] = _HF_CACHE_DIR
os.environ["TORCH_HOME"]            = str(_PROJECT_ROOT / "models" / "torch_cache")

# Pastikan direktori cache ada
Path(_HF_CACHE_DIR).mkdir(parents=True, exist_ok=True)
Path(os.environ["TORCH_HOME"]).mkdir(parents=True, exist_ok=True)
# ============================================================

import torch
import timm
from transformers import pipeline
import logging

# Konfigurasi logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# --- Konfigurasi Global ---
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
MODEL_DIR = Path(__file__).parent / "models"
MODEL_DIR.mkdir(exist_ok=True)

# Nama model EfficientNet yang digunakan (bisa diganti ke efficientnet_b3 jika VRAM kurang)
EFFICIENTNET_VARIANT = "efficientnet_b4"
NUM_CLASSES = 2  # [REAL, FAKE]


class ModelLoader:
    """
    Singleton class untuk mengelola semua model AI.
    Memastikan model hanya dimuat satu kali ke VRAM.
    """
    _instance = None          # Referensi singleton
    _face_deepfake_model = None  # Model klasifikasi wajah deepfake utama (Wvolf)
    _face_deepfake_model_2 = None   # Model klasifikasi wajah deepfake kedua (prithiv)
    _audio_model = None       # Model klasifikasi suara deepfake
    _is_initialized = False   # Flag apakah sudah diinisialisasi

    def __new__(cls):
        """Implementasi singleton: hanya buat satu instance."""
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

    def initialize(self):
        """
        Muat semua model ke device (GPU/CPU).
        Dipanggil sekali saat startup FastAPI.
        """
        if self._is_initialized:
            logger.info("Models sudah diinisialisasi, skip loading.")
            return

        logger.info(f"๐Ÿš€ Menginisialisasi ModelLoader pada device: {DEVICE}")
        self._log_device_info()

        # Muat 2 model klasifikasi wajah khusus Deepfake
        self._face_deepfake_model = self._load_model_pipeline("Wvolf/ViT_Deepfake_Detection", "Deepfake 1 (Wvolf)")
        self._face_deepfake_model_2 = self._load_model_pipeline("prithivMLmods/deepfake-detector-model-v1", "Deepfake 2 (Prithiv)")

        # Muat model klasifikasi audio (CNN sederhana)
        self._audio_model = self._load_audio_model()

        self._is_initialized = True
        logger.info("โœ… Semua model berhasil dimuat!")

    def _log_device_info(self):
        """Tampilkan informasi GPU/CUDA untuk debugging."""
        if torch.cuda.is_available():
            gpu_name = torch.cuda.get_device_name(0)
            vram_total = torch.cuda.get_device_properties(0).total_memory / 1024**3
            logger.info(f"๐ŸŽฎ GPU Terdeteksi: {gpu_name}")
            logger.info(f"๐Ÿ’พ VRAM Total: {vram_total:.1f} GB")
        else:
            logger.warning("โš ๏ธ CUDA tidak tersedia. Menggunakan CPU (performa lebih lambat).")

    def _load_model_pipeline(self, model_name: str, desc: str):
        """
        Muat model pre-trained pipeline dari HuggingFace.
        """
        logger.info(f"๐Ÿ“ฆ Memuat model {desc} dari HuggingFace: {model_name}...")

        try:
            device_id = 0 if DEVICE.type == "cuda" else -1
            model_pipeline = pipeline(
                "image-classification",
                model=model_name,
                device=device_id,
                top_k=2  # Return semua kelas (Real dan Fake)
            )
            logger.info(f"โœ… Model {desc} ({model_name}) berhasil dimuat!")
            return model_pipeline
        except Exception as e:
            logger.error(f"โŒ Gagal memuat model {desc}: {e}")
            raise e

    def _load_audio_model(self) -> torch.nn.Module:
        """
        Muat model CNN sederhana untuk analisis spektrogram audio.
        Menggunakan MobileNetV3-Small untuk efisiensi VRAM.
        """
        logger.info("๐Ÿ“ฆ Memuat model audio: mobilenetv3_small_100...")

        # MobileNetV3 lebih ringan dari EfficientNet, cocok untuk audio spectrogram
        model = timm.create_model(
            "mobilenetv3_small_100",
            pretrained=True,
            num_classes=NUM_CLASSES,
            in_chans=1  # Spectrogram adalah grayscale (1 channel)
        )

        model.eval()
        model = model.to(DEVICE)

        if DEVICE.type == "cuda":
            model = model.half()
            logger.info("โœ… Model audio: FP16 (half-precision) diaktifkan")

        logger.info("โœ… Model audio 'MobileNetV3-Small' siap digunakan")
        return model

    @property
    def face_deepfake_model(self):
        """Akses model deepfake utama yang sudah dimuat."""
        if not self._is_initialized:
            raise RuntimeError("ModelLoader belum diinisialisasi! Panggil .initialize() dulu.")
        return self._face_deepfake_model

    @property
    def face_deepfake_model_2(self):
        """Akses model deepfake kedua yang sudah dimuat."""
        if not self._is_initialized:
            raise RuntimeError("ModelLoader belum diinisialisasi! Panggil .initialize() dulu.")
        return self._face_deepfake_model_2

    @property
    def audio_model(self) -> torch.nn.Module:
        """Akses model audio yang sudah dimuat."""
        if not self._is_initialized:
            raise RuntimeError("ModelLoader belum diinisialisasi! Panggil .initialize() dulu.")
        return self._audio_model

    @property
    def device(self) -> torch.device:
        """Kembalikan device yang sedang digunakan (cuda/cpu)."""
        return DEVICE

    def get_vram_usage(self) -> dict:
        """
        Kembalikan informasi penggunaan VRAM saat ini.
        Berguna untuk monitoring di endpoint /api/health.
        """
        if DEVICE.type != "cuda":
            return {"available": False, "reason": "CUDA not available"}

        allocated = torch.cuda.memory_allocated(0) / 1024**3   # GB
        reserved  = torch.cuda.memory_reserved(0) / 1024**3    # GB
        total     = torch.cuda.get_device_properties(0).total_memory / 1024**3

        return {
            "available": True,
            "gpu_name": torch.cuda.get_device_name(0),
            "vram_total_gb": round(total, 2),
            "vram_allocated_gb": round(allocated, 3),
            "vram_reserved_gb": round(reserved, 3),
            "vram_free_gb": round(total - reserved, 3),
        }


# Instance singleton global - diimport oleh modul lain
model_loader = ModelLoader()