NickVerri commited on
Commit
5528036
·
verified ·
1 Parent(s): d4d58e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -19
app.py CHANGED
@@ -2,10 +2,26 @@ import os
2
  import numpy as np
3
  import torch
4
 
5
- # --- PYTORCH 2.6+ SECURITY & COMPATIBILITY PATCHES ---
6
- # 1. Allow WhisperX/Pyannote globals for model loading
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  try:
8
- # We import these specifically to add them to the safe list
9
  from omegaconf.listconfig import ListConfig
10
  from omegaconf.dictconfig import DictConfig
11
  try:
@@ -14,7 +30,6 @@ try:
14
  ContainerMetadata = None
15
  Metadata = None
16
 
17
- # Exhaustive list of classes required by Pyannote/WhisperX checkpoints
18
  safe_list = [
19
  ListConfig,
20
  DictConfig,
@@ -24,7 +39,6 @@ try:
24
  if ContainerMetadata: safe_list.append(ContainerMetadata)
25
  if Metadata: safe_list.append(Metadata)
26
 
27
- # Handle numpy scalar differences across versions
28
  if hasattr(np, '_core') and hasattr(np._core, 'multiarray'):
29
  safe_list.append(np._core.multiarray.scalar)
30
  elif hasattr(np, 'core') and hasattr(np.core, 'multiarray'):
@@ -34,7 +48,7 @@ try:
34
  except Exception as e:
35
  print(f"Safe Globals Warning: {e}")
36
 
37
- # 2. Fix NumPy 2.0+ attribute removal (required for older pyannote internals)
38
  if not hasattr(np, 'NaN'):
39
  np.NaN = np.nan
40
 
@@ -189,19 +203,7 @@ if uploaded_file:
189
 
190
  # 3. Diarize
191
  st.write("🗣️ **Diarizing Speakers...**")
192
- # Pass token for gated diarization models
193
- # Try to bypass the torch security default if necessary
194
- try:
195
- # We wrap this in a safe_globals context to be absolutely sure
196
- diarize_model = whisperx.DiarizationPipeline(use_auth_token=ACTIVE_HF_TOKEN, device=device)
197
- except Exception as e:
198
- if "Weights only load failed" in str(e) or "Unsupported global" in str(e):
199
- st.warning("⚠️ Security restriction encountered. Re-attempting load with legacy weights_only=False.")
200
- # Note: WhisperX doesn't expose the weights_only flag, so we rely on
201
- # the Safe Globals established at the top of the file.
202
- diarize_model = whisperx.DiarizationPipeline(use_auth_token=ACTIVE_HF_TOKEN, device=device)
203
- else:
204
- raise e
205
 
206
  diarize_kwargs = {}
207
  if num_speakers > 0:
 
2
  import numpy as np
3
  import torch
4
 
5
+ # --- CRITICAL ENVIRONMENT FIXES ---
6
+ # 1. Fix for Hugging Face millicore OMP_NUM_THREADS error (e.g., "7500m")
7
+ # This prevents the underlying math libraries from crashing on startup.
8
+ if os.environ.get("OMP_NUM_THREADS", "").endswith("m"):
9
+ os.environ["OMP_NUM_THREADS"] = "1"
10
+
11
+ # 2. Monkeypatch torch.load to default weights_only=False
12
+ # PyTorch 2.6 changed the default to True for security, which breaks
13
+ # WhisperX/Pyannote checkpoints. Monkeypatching is the only way to fix
14
+ # internal library calls that we don't control.
15
+ import torch.serialization
16
+ original_load = torch.load
17
+ def patched_load(*args, **kwargs):
18
+ if 'weights_only' not in kwargs:
19
+ kwargs['weights_only'] = False
20
+ return original_load(*args, **kwargs)
21
+ torch.load = patched_load
22
+
23
+ # --- PYTORCH 2.6+ COMPATIBILITY PATCHES ---
24
  try:
 
25
  from omegaconf.listconfig import ListConfig
26
  from omegaconf.dictconfig import DictConfig
27
  try:
 
30
  ContainerMetadata = None
31
  Metadata = None
32
 
 
33
  safe_list = [
34
  ListConfig,
35
  DictConfig,
 
39
  if ContainerMetadata: safe_list.append(ContainerMetadata)
40
  if Metadata: safe_list.append(Metadata)
41
 
 
42
  if hasattr(np, '_core') and hasattr(np._core, 'multiarray'):
43
  safe_list.append(np._core.multiarray.scalar)
44
  elif hasattr(np, 'core') and hasattr(np.core, 'multiarray'):
 
48
  except Exception as e:
49
  print(f"Safe Globals Warning: {e}")
50
 
51
+ # Fix NumPy 2.0+ attribute removal (required for older pyannote internals)
52
  if not hasattr(np, 'NaN'):
53
  np.NaN = np.nan
54
 
 
203
 
204
  # 3. Diarize
205
  st.write("🗣️ **Diarizing Speakers...**")
206
+ diarize_model = whisperx.DiarizationPipeline(use_auth_token=ACTIVE_HF_TOKEN, device=device)
 
 
 
 
 
 
 
 
 
 
 
 
207
 
208
  diarize_kwargs = {}
209
  if num_speakers > 0: