File size: 10,192 Bytes
cb39c05
 
 
 
 
 
 
95e1515
cb39c05
 
95e1515
cb39c05
 
 
 
 
 
 
 
 
 
 
 
95e1515
cb39c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95e1515
cb39c05
 
 
95e1515
cb39c05
 
95e1515
cb39c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95e1515
cb39c05
 
95e1515
cb39c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95e1515
cb39c05
 
 
95e1515
cb39c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95e1515
 
cb39c05
95e1515
cb39c05
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
ModelManager: HuggingFace model download, caching, and lazy loading.

Handles automatic model downloads on first run, local caching, and singleton
pattern to ensure models are loaded only once per process.
"""

import logging
import os
from pathlib import Path
from typing import Any, Dict, Optional

logger = logging.getLogger(__name__)


class ModelManager:
    """
    Manages HuggingFace model downloads and caching.

    Implements singleton pattern to ensure models are loaded once and reused
    across all processing operations.
    """

    _instance: Optional["ModelManager"] = None
    _models: Dict[str, Any] = {}
    _models_loaded: bool = False

    def __new__(cls):
        """Singleton pattern - one instance per process."""
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self, cache_dir: str = "./models"):
        """
        Initialize ModelManager.

        Args:
            cache_dir: Directory for caching downloaded models (default: ./models)
        """
        self.cache_dir = Path(cache_dir)
        self.cache_dir.mkdir(parents=True, exist_ok=True)

        # Set HuggingFace cache environment variable
        os.environ["HF_HOME"] = str(self.cache_dir)
        os.environ["TRANSFORMERS_CACHE"] = str(self.cache_dir)

    def get_hf_token(self) -> Optional[str]:
        """
        Get HuggingFace authentication token.

        Checks multiple sources in order:
        1. HF_TOKEN environment variable
        2. ~/.cache/huggingface/token file (from huggingface-cli login)

        Returns:
            HuggingFace token or None if not found
        """
        # Check environment variable
        token = os.environ.get("HF_TOKEN")
        if token:
            return token

        # Check huggingface-cli token file
        token_file = Path.home() / ".cache" / "huggingface" / "token"
        if token_file.exists():
            return token_file.read_text().strip()

        return None

    def load_speaker_diarization(self, progress_callback=None) -> Any:
        """
        Load pyannote speaker diarization model.

        Downloads on first run (~150MB), then loads from cache.
        Requires HuggingFace authentication and license acceptance at:
        https://huggingface.co/pyannote/speaker-diarization-3.1

        Args:
            progress_callback: Optional callback(progress: float, message: str)

        Returns:
            Loaded pyannote Pipeline object

        Raises:
            RuntimeError: If authentication fails or model cannot be loaded
        """
        if "diarization" in self._models:
            return self._models["diarization"]

        if progress_callback:
            progress_callback(0.0, "Loading speaker diarization model...")

        logger.info("Loading speaker diarization model (first run downloads ~150MB)")

        try:
            import torch
            from pyannote.audio import Pipeline

            token = self.get_hf_token()
            if not token:
                raise RuntimeError("HuggingFace token not found. Please run: huggingface-cli login")

            pipeline = Pipeline.from_pretrained(
                "pyannote/speaker-diarization-3.1", token=token, cache_dir=str(self.cache_dir)
            )

            # Force CPU execution
            pipeline.to(torch.device("cpu"))

            self._models["diarization"] = pipeline

            if progress_callback:
                progress_callback(1.0, "Speaker diarization model loaded")

            logger.info("✓ Speaker diarization model loaded successfully")
            return pipeline

        except Exception as e:
            error_msg = str(e)
            if "401" in error_msg or "authentication" in error_msg.lower():
                raise RuntimeError(
                    "Authentication failed for pyannote/speaker-diarization-3.1. "
                    "Please:\n"
                    "1. Run: huggingface-cli login\n"
                    "2. Accept license at: https://huggingface.co/pyannote/speaker-diarization-3.1"
                )
            elif "disk" in error_msg.lower() or "space" in error_msg.lower():
                raise RuntimeError(
                    f"Insufficient disk space to download model. "
                    f"Need ~600MB free in {self.cache_dir}"
                )
            else:
                raise RuntimeError(f"Failed to load speaker diarization model: {error_msg}")

    def load_embedding_model(self, progress_callback=None) -> Any:
        """
        Load pyannote embedding model for voice matching.

        Downloads on first run (~17MB), then loads from cache.

        Args:
            progress_callback: Optional callback(progress: float, message: str)

        Returns:
            Loaded pyannote Model object
        """
        if "embedding" in self._models:
            return self._models["embedding"]

        if progress_callback:
            progress_callback(0.0, "Loading voice embedding model...")

        logger.info("Loading voice embedding model (first run downloads ~17MB)")

        try:
            from pyannote.audio import Model

            token = self.get_hf_token()
            if not token:
                raise RuntimeError("HuggingFace token not found. Please run: huggingface-cli login")

            model = Model.from_pretrained(
                "pyannote/embedding", token=token, cache_dir=str(self.cache_dir)
            )

            self._models["embedding"] = model

            if progress_callback:
                progress_callback(1.0, "Voice embedding model loaded")

            logger.info("✓ Voice embedding model loaded successfully")
            return model

        except Exception as e:
            raise RuntimeError(f"Failed to load embedding model: {str(e)}")

    def load_ast_classifier(self, progress_callback=None) -> Any:
        """
        Load Audio Spectrogram Transformer for speech classification.

        Downloads on first run (~340MB), then loads from cache.

        Args:
            progress_callback: Optional callback(progress: float, message: str)

        Returns:
            Tuple of (feature_extractor, classifier_model)
        """
        if "ast" in self._models:
            return self._models["ast"]

        if progress_callback:
            progress_callback(0.0, "Loading audio classifier model...")

        logger.info("Loading audio classifier model (first run downloads ~340MB)")

        try:
            from transformers import ASTFeatureExtractor, ASTForAudioClassification

            feature_extractor = ASTFeatureExtractor.from_pretrained(
                "MIT/ast-finetuned-audioset-10-10-0.4593", cache_dir=str(self.cache_dir)
            )

            classifier = ASTForAudioClassification.from_pretrained(
                "MIT/ast-finetuned-audioset-10-10-0.4593", cache_dir=str(self.cache_dir)
            )

            classifier.eval()  # Set to inference mode

            self._models["ast"] = (feature_extractor, classifier)

            if progress_callback:
                progress_callback(1.0, "Audio classifier model loaded")

            logger.info("✓ Audio classifier model loaded successfully")
            return self._models["ast"]

        except Exception as e:
            raise RuntimeError(f"Failed to load AST classifier: {str(e)}")

    def load_vad_model(self, progress_callback=None) -> Any:
        """
        Load Silero VAD model for voice activity detection.

        Downloads on first run (~1.5MB), then loads from cache.

        Args:
            progress_callback: Optional callback(progress: float, message: str)

        Returns:
            Loaded Silero VAD model
        """
        if "vad" in self._models:
            return self._models["vad"]

        if progress_callback:
            progress_callback(0.0, "Loading voice activity detection model...")

        logger.info("Loading VAD model (first run downloads ~1.5MB)")

        try:
            import torch

            # Silero VAD uses torch.hub
            model, utils = torch.hub.load(
                repo_or_dir="snakers4/silero-vad",
                model="silero_vad",
                force_reload=False,
                onnx=False,
            )

            model.eval()

            self._models["vad"] = (model, utils)

            if progress_callback:
                progress_callback(1.0, "VAD model loaded")

            logger.info("✓ VAD model loaded successfully")
            return self._models["vad"]

        except Exception as e:
            raise RuntimeError(f"Failed to load VAD model: {str(e)}")

    def models_are_cached(self) -> bool:
        """
        Check if all required models are already downloaded.

        Returns:
            True if all models are cached locally, False otherwise
        """
        required_models = [
            "pyannote--speaker-diarization-3.1",
            "pyannote--embedding",
            "MIT--ast-finetuned-audioset-10-10-0.4593",
        ]

        hub_cache = self.cache_dir / "hub"
        if not hub_cache.exists():
            return False

        for model_name in required_models:
            model_path = hub_cache / f"models--{model_name}"
            if not model_path.exists():
                return False

        return True

    def get_cache_size(self) -> int:
        """
        Get total size of cached models in bytes.

        Returns:
            Total cache size in bytes
        """
        total_size = 0
        for path in self.cache_dir.rglob("*"):
            if path.is_file():
                total_size += path.stat().st_size
        return total_size

    def clear_cache(self):
        """
        Clear all cached models.

        WARNING: This will force re-download on next use (~600MB).
        """
        import shutil

        if self.cache_dir.exists():
            shutil.rmtree(self.cache_dir)
            self.cache_dir.mkdir(parents=True, exist_ok=True)
            logger.info(f"Cleared model cache at {self.cache_dir}")

        # Reset loaded models
        self._models.clear()
        self._models_loaded = False