Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,52 @@
|
|
| 1 |
import os
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
# --- CRITICAL
|
| 6 |
-
#
|
| 7 |
-
|
| 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 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
return _original_torch_load(*args, **kwargs)
|
| 18 |
-
torch.load = patched_torch_load
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
if "ffmpeg" in torchaudio.list_audio_backends():
|
| 24 |
-
torchaudio.set_audio_backend("ffmpeg")
|
| 25 |
-
except Exception:
|
| 26 |
-
pass
|
| 27 |
|
| 28 |
-
# ---
|
| 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
|
| 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 |
-
|
|
|
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
print(f"Safe Globals Warning: {e}")
|
| 62 |
|
|
@@ -83,6 +73,10 @@ ENV_GEMINI_KEY = os.environ.get("GEMINI_API_KEY", "")
|
|
| 83 |
ACTIVE_HF_TOKEN = ENV_HF_TOKEN if ENV_HF_TOKEN else HARDCODED_HF_TOKEN
|
| 84 |
ACTIVE_GEMINI_KEY = ENV_GEMINI_KEY if ENV_GEMINI_KEY else HARDCODED_GEMINI_KEY
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
def format_timecode(seconds, fps=25):
|
| 87 |
"""Converts seconds to HH:MM:SS:FF."""
|
| 88 |
td = timedelta(seconds=seconds)
|
|
@@ -156,20 +150,6 @@ with st.sidebar:
|
|
| 156 |
|
| 157 |
st.header("Model Settings")
|
| 158 |
model_size = st.selectbox("Whisper Model", ["large-v2", "medium", "base"], index=0)
|
| 159 |
-
|
| 160 |
-
# --- New Language Option ---
|
| 161 |
-
language_map = {
|
| 162 |
-
"Auto-Detect": None,
|
| 163 |
-
"English": "en",
|
| 164 |
-
"Spanish": "es",
|
| 165 |
-
"French": "fr",
|
| 166 |
-
"German": "de",
|
| 167 |
-
"Italian": "it",
|
| 168 |
-
"Portuguese": "pt"
|
| 169 |
-
}
|
| 170 |
-
selected_lang_label = st.selectbox("Audio Language (Speeds up processing)", list(language_map.keys()), index=1)
|
| 171 |
-
target_language = language_map[selected_lang_label]
|
| 172 |
-
|
| 173 |
num_speakers = st.number_input("Speakers (0=Auto)", min_value=0, value=0)
|
| 174 |
|
| 175 |
st.divider()
|
|
@@ -200,9 +180,9 @@ if uploaded_file:
|
|
| 200 |
try:
|
| 201 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 202 |
if device == "cpu":
|
| 203 |
-
st.warning("โ ๏ธ No GPU detected. WhisperX will be
|
| 204 |
-
|
| 205 |
-
|
| 206 |
|
| 207 |
# 1. Transcribe
|
| 208 |
compute_type = "float16" if device == "cuda" else "int8"
|
|
@@ -210,11 +190,8 @@ if uploaded_file:
|
|
| 210 |
|
| 211 |
st.write("๐ **Transcribing...**")
|
| 212 |
audio = whisperx.load_audio("temp_audio.wav")
|
|
|
|
| 213 |
|
| 214 |
-
# Pass the language to speed up processing
|
| 215 |
-
result = model.transcribe(audio, batch_size=16, language=target_language)
|
| 216 |
-
|
| 217 |
-
# Memory cleanup
|
| 218 |
del model
|
| 219 |
gc.collect()
|
| 220 |
torch.cuda.empty_cache()
|
|
@@ -230,6 +207,7 @@ if uploaded_file:
|
|
| 230 |
|
| 231 |
# 3. Diarize
|
| 232 |
st.write("๐ฃ๏ธ **Diarizing Speakers...**")
|
|
|
|
| 233 |
diarize_model = whisperx.DiarizationPipeline(use_auth_token=ACTIVE_HF_TOKEN, device=device)
|
| 234 |
|
| 235 |
diarize_kwargs = {}
|
|
|
|
| 1 |
import os
|
| 2 |
import numpy as np
|
| 3 |
import torch
|
| 4 |
+
import typing
|
| 5 |
|
| 6 |
+
# --- CRITICAL: AGGRESSIVE PYTORCH SECURITY OVERRIDE ---
|
| 7 |
+
# The error "Weights only load failed" means weights_only=True is active.
|
| 8 |
+
# We must intercept torch.load and FORCE it to False for pyannote/whisperx to work.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
_original_torch_load = torch.load
|
| 10 |
+
|
| 11 |
def patched_torch_load(*args, **kwargs):
|
| 12 |
+
# Debug print to verify interception (Check Logs tab in HF)
|
| 13 |
+
print(f"DEBUG: Intercepted torch.load call. Target: {args[0] if args else 'Unknown'}")
|
| 14 |
+
|
| 15 |
+
# FORCE Disable security check
|
| 16 |
+
kwargs['weights_only'] = False
|
| 17 |
+
|
| 18 |
return _original_torch_load(*args, **kwargs)
|
|
|
|
| 19 |
|
| 20 |
+
# Apply the patch
|
| 21 |
+
torch.load = patched_torch_load
|
| 22 |
+
print("DEBUG: torch.load has been monkeypatched to allow all globals.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# --- Secondary Backup: Safe Globals Whitelist ---
|
|
|
|
|
|
|
| 25 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
from omegaconf.listconfig import ListConfig
|
| 27 |
from omegaconf.dictconfig import DictConfig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
+
from omegaconf.base import ContainerMetadata, Metadata
|
|
|
|
|
|
|
|
|
|
| 30 |
except ImportError:
|
| 31 |
+
ContainerMetadata = None
|
| 32 |
+
Metadata = None
|
| 33 |
|
| 34 |
+
safe_list = [
|
| 35 |
+
typing.Any, # Specific fix for your error
|
| 36 |
+
ListConfig,
|
| 37 |
+
DictConfig,
|
| 38 |
+
torch.nn.modules.container.ModuleList,
|
| 39 |
+
np.dtype,
|
| 40 |
+
]
|
| 41 |
+
if ContainerMetadata: safe_list.append(ContainerMetadata)
|
| 42 |
+
if Metadata: safe_list.append(Metadata)
|
| 43 |
+
|
| 44 |
+
if hasattr(np, '_core') and hasattr(np._core, 'multiarray'):
|
| 45 |
+
safe_list.append(np._core.multiarray.scalar)
|
| 46 |
+
elif hasattr(np, 'core') and hasattr(np.core, 'multiarray'):
|
| 47 |
+
safe_list.append(np.core.multiarray.scalar)
|
| 48 |
+
|
| 49 |
+
torch.serialization.add_safe_globals(safe_list)
|
| 50 |
except Exception as e:
|
| 51 |
print(f"Safe Globals Warning: {e}")
|
| 52 |
|
|
|
|
| 73 |
ACTIVE_HF_TOKEN = ENV_HF_TOKEN if ENV_HF_TOKEN else HARDCODED_HF_TOKEN
|
| 74 |
ACTIVE_GEMINI_KEY = ENV_GEMINI_KEY if ENV_GEMINI_KEY else HARDCODED_GEMINI_KEY
|
| 75 |
|
| 76 |
+
# Fix OMP Threads
|
| 77 |
+
if os.environ.get("OMP_NUM_THREADS", "").endswith("m"):
|
| 78 |
+
os.environ["OMP_NUM_THREADS"] = "1"
|
| 79 |
+
|
| 80 |
def format_timecode(seconds, fps=25):
|
| 81 |
"""Converts seconds to HH:MM:SS:FF."""
|
| 82 |
td = timedelta(seconds=seconds)
|
|
|
|
| 150 |
|
| 151 |
st.header("Model Settings")
|
| 152 |
model_size = st.selectbox("Whisper Model", ["large-v2", "medium", "base"], index=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
num_speakers = st.number_input("Speakers (0=Auto)", min_value=0, value=0)
|
| 154 |
|
| 155 |
st.divider()
|
|
|
|
| 180 |
try:
|
| 181 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 182 |
if device == "cpu":
|
| 183 |
+
st.warning("โ ๏ธ No GPU detected. WhisperX will be slow.")
|
| 184 |
+
else:
|
| 185 |
+
st.write(f"๐ **Loading WhisperX on {device}...**")
|
| 186 |
|
| 187 |
# 1. Transcribe
|
| 188 |
compute_type = "float16" if device == "cuda" else "int8"
|
|
|
|
| 190 |
|
| 191 |
st.write("๐ **Transcribing...**")
|
| 192 |
audio = whisperx.load_audio("temp_audio.wav")
|
| 193 |
+
result = model.transcribe(audio, batch_size=16)
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
del model
|
| 196 |
gc.collect()
|
| 197 |
torch.cuda.empty_cache()
|
|
|
|
| 207 |
|
| 208 |
# 3. Diarize
|
| 209 |
st.write("๐ฃ๏ธ **Diarizing Speakers...**")
|
| 210 |
+
# The monkeypatch above should protect this call
|
| 211 |
diarize_model = whisperx.DiarizationPipeline(use_auth_token=ACTIVE_HF_TOKEN, device=device)
|
| 212 |
|
| 213 |
diarize_kwargs = {}
|