NickVerri commited on
Commit
ff99e39
·
verified ·
1 Parent(s): 8765cc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -32
app.py CHANGED
@@ -1,54 +1,62 @@
1
  import os
2
  import numpy as np
3
  import torch
4
- import torchaudio
5
 
6
  # --- CRITICAL ENVIRONMENT FIXES ---
7
- # 1. Fix for Hugging Face millicore OMP_NUM_THREADS error (e.g., "7500m")
8
  if os.environ.get("OMP_NUM_THREADS", "").endswith("m"):
9
  os.environ["OMP_NUM_THREADS"] = "1"
10
 
11
- # 2. Force Torchaudio Backend to avoid VAD hang
 
 
 
 
 
 
 
 
 
 
 
12
  try:
13
  if "ffmpeg" in torchaudio.list_audio_backends():
14
  torchaudio.set_audio_backend("ffmpeg")
15
  except Exception:
16
  pass
17
 
18
- # 3. Monkeypatch torch.load to default weights_only=False for PyTorch 2.6+
19
- import torch.serialization
20
- original_load = torch.load
21
- def patched_load(*args, **kwargs):
22
- if 'weights_only' not in kwargs:
23
- kwargs['weights_only'] = False
24
- return original_load(*args, **kwargs)
25
- torch.load = patched_load
26
-
27
- # --- PYTORCH 2.6+ COMPATIBILITY PATCHES ---
28
  try:
 
 
 
 
 
 
 
 
 
 
 
 
29
  from omegaconf.listconfig import ListConfig
30
  from omegaconf.dictconfig import DictConfig
 
 
 
 
 
31
  try:
32
- from omegaconf.base import ContainerMetadata, Metadata
 
 
 
33
  except ImportError:
34
- ContainerMetadata = None
35
- Metadata = None
36
 
37
- safe_list = [
38
- ListConfig,
39
- DictConfig,
40
- torch.nn.modules.container.ModuleList,
41
- np.dtype,
42
- ]
43
- if ContainerMetadata: safe_list.append(ContainerMetadata)
44
- if Metadata: safe_list.append(Metadata)
45
-
46
- if hasattr(np, '_core') and hasattr(np._core, 'multiarray'):
47
- safe_list.append(np._core.multiarray.scalar)
48
- elif hasattr(np, 'core') and hasattr(np.core, 'multiarray'):
49
- safe_list.append(np.core.multiarray.scalar)
50
-
51
- torch.serialization.add_safe_globals(safe_list)
52
  except Exception as e:
53
  print(f"Safe Globals Warning: {e}")
54
 
@@ -227,7 +235,7 @@ if uploaded_file:
227
  diarize_kwargs = {}
228
  if num_speakers > 0:
229
  diarize_kwargs = {"min_speakers": num_speakers, "max_speakers": num_speakers}
230
-
231
  diarize_segments = diarize_model(audio, **diarize_kwargs)
232
 
233
  # 4. Final Merge
 
1
  import os
2
  import numpy as np
3
  import torch
 
4
 
5
  # --- CRITICAL ENVIRONMENT FIXES ---
6
+ # 1. Fix for Hugging Face millicore OMP_NUM_THREADS error
7
  if os.environ.get("OMP_NUM_THREADS", "").endswith("m"):
8
  os.environ["OMP_NUM_THREADS"] = "1"
9
 
10
+ # 2. GLOBAL PYTORCH 2.6+ SECURITY BYPASS (MONKEYPATCH)
11
+ # This forces torch.load to behave like older versions (trusting the file).
12
+ # Essential for loading 3rd party checkpoints (WhisperX/Pyannote) that aren't updated yet.
13
+ _original_torch_load = torch.load
14
+ def patched_torch_load(*args, **kwargs):
15
+ if 'weights_only' not in kwargs:
16
+ kwargs['weights_only'] = False
17
+ return _original_torch_load(*args, **kwargs)
18
+ torch.load = patched_torch_load
19
+
20
+ # 3. Force Torchaudio Backend
21
+ import torchaudio
22
  try:
23
  if "ffmpeg" in torchaudio.list_audio_backends():
24
  torchaudio.set_audio_backend("ffmpeg")
25
  except Exception:
26
  pass
27
 
28
+ # --- SAFE GLOBALS WHITELIST (Backup Strategy) ---
29
+ # Even with the monkeypatch, we add these to be double-safe.
30
+ safe_globals = []
 
 
 
 
 
 
 
31
  try:
32
+ # PyTorch internals
33
+ safe_globals.append(torch.nn.modules.container.ModuleList)
34
+
35
+ # NumPy internals
36
+ safe_globals.append(np.dtype)
37
+ if hasattr(np, '_core') and hasattr(np._core, 'multiarray'):
38
+ safe_globals.append(np._core.multiarray.scalar)
39
+ elif hasattr(np, 'core') and hasattr(np.core, 'multiarray'):
40
+ safe_globals.append(np.core.multiarray.scalar)
41
+
42
+ # OmegaConf (Used by Pyannote config)
43
+ import omegaconf
44
  from omegaconf.listconfig import ListConfig
45
  from omegaconf.dictconfig import DictConfig
46
+ from omegaconf.base import ContainerMetadata, Metadata, Node
47
+ safe_globals.extend([ListConfig, DictConfig, ContainerMetadata, Metadata, Node])
48
+
49
+ # Pyannote internals (if available at this stage)
50
+ # We wrap these in try/except in case pyannote isn't fully loaded yet
51
  try:
52
+ from pyannote.audio.core.task import Specifications, Problem, Resolution
53
+ from pyannote.audio.core.model import Model
54
+ from pyannote.audio.pipelines.speaker_diarization import SpeakerDiarization
55
+ safe_globals.extend([Specifications, Problem, Resolution, Model, SpeakerDiarization])
56
  except ImportError:
57
+ pass
 
58
 
59
+ torch.serialization.add_safe_globals(safe_globals)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  except Exception as e:
61
  print(f"Safe Globals Warning: {e}")
62
 
 
235
  diarize_kwargs = {}
236
  if num_speakers > 0:
237
  diarize_kwargs = {"min_speakers": num_speakers, "max_speakers": num_speakers}
238
+
239
  diarize_segments = diarize_model(audio, **diarize_kwargs)
240
 
241
  # 4. Final Merge