Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,21 @@
|
|
| 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 (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.
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
import torch.serialization
|
| 16 |
original_load = torch.load
|
| 17 |
def patched_load(*args, **kwargs):
|
|
@@ -48,7 +52,7 @@ try:
|
|
| 48 |
except Exception as e:
|
| 49 |
print(f"Safe Globals Warning: {e}")
|
| 50 |
|
| 51 |
-
# Fix NumPy 2.0+ attribute removal
|
| 52 |
if not hasattr(np, 'NaN'):
|
| 53 |
np.NaN = np.nan
|
| 54 |
|
|
@@ -144,14 +148,24 @@ with st.sidebar:
|
|
| 144 |
|
| 145 |
st.header("Model Settings")
|
| 146 |
model_size = st.selectbox("Whisper Model", ["large-v2", "medium", "base"], index=0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
num_speakers = st.number_input("Speakers (0=Auto)", min_value=0, value=0)
|
| 148 |
|
| 149 |
st.divider()
|
| 150 |
st.info("API Keys are managed via Environment Secrets.")
|
| 151 |
-
if not ACTIVE_GEMINI_KEY:
|
| 152 |
-
st.error("⚠️ Gemini API Key not found!")
|
| 153 |
-
if not ACTIVE_HF_TOKEN:
|
| 154 |
-
st.error("⚠️ HF Token not found!")
|
| 155 |
|
| 156 |
uploaded_file = st.file_uploader("Upload Video/Audio Clip", type=["mp4", "m4a", "wav", "mp3", "mov"])
|
| 157 |
|
|
@@ -168,7 +182,7 @@ if uploaded_file:
|
|
| 168 |
with open("temp_input", "wb") as f:
|
| 169 |
f.write(uploaded_file.getbuffer())
|
| 170 |
|
| 171 |
-
st.write("🎵 **Extracting Audio...**")
|
| 172 |
subprocess.run([
|
| 173 |
"ffmpeg", "-i", "temp_input",
|
| 174 |
"-vn", "-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1",
|
|
@@ -177,7 +191,10 @@ if uploaded_file:
|
|
| 177 |
|
| 178 |
try:
|
| 179 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 180 |
-
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
# 1. Transcribe
|
| 183 |
compute_type = "float16" if device == "cuda" else "int8"
|
|
@@ -185,9 +202,11 @@ if uploaded_file:
|
|
| 185 |
|
| 186 |
st.write("📝 **Transcribing...**")
|
| 187 |
audio = whisperx.load_audio("temp_audio.wav")
|
| 188 |
-
result = model.transcribe(audio, batch_size=16)
|
| 189 |
|
| 190 |
-
#
|
|
|
|
|
|
|
|
|
|
| 191 |
del model
|
| 192 |
gc.collect()
|
| 193 |
torch.cuda.empty_cache()
|
|
@@ -208,7 +227,7 @@ if uploaded_file:
|
|
| 208 |
diarize_kwargs = {}
|
| 209 |
if num_speakers > 0:
|
| 210 |
diarize_kwargs = {"min_speakers": num_speakers, "max_speakers": num_speakers}
|
| 211 |
-
|
| 212 |
diarize_segments = diarize_model(audio, **diarize_kwargs)
|
| 213 |
|
| 214 |
# 4. Final Merge
|
|
|
|
| 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):
|
|
|
|
| 52 |
except Exception as e:
|
| 53 |
print(f"Safe Globals Warning: {e}")
|
| 54 |
|
| 55 |
+
# Fix NumPy 2.0+ attribute removal
|
| 56 |
if not hasattr(np, 'NaN'):
|
| 57 |
np.NaN = np.nan
|
| 58 |
|
|
|
|
| 148 |
|
| 149 |
st.header("Model Settings")
|
| 150 |
model_size = st.selectbox("Whisper Model", ["large-v2", "medium", "base"], index=0)
|
| 151 |
+
|
| 152 |
+
# --- New Language Option ---
|
| 153 |
+
language_map = {
|
| 154 |
+
"Auto-Detect": None,
|
| 155 |
+
"English": "en",
|
| 156 |
+
"Spanish": "es",
|
| 157 |
+
"French": "fr",
|
| 158 |
+
"German": "de",
|
| 159 |
+
"Italian": "it",
|
| 160 |
+
"Portuguese": "pt"
|
| 161 |
+
}
|
| 162 |
+
selected_lang_label = st.selectbox("Audio Language (Speeds up processing)", list(language_map.keys()), index=1)
|
| 163 |
+
target_language = language_map[selected_lang_label]
|
| 164 |
+
|
| 165 |
num_speakers = st.number_input("Speakers (0=Auto)", min_value=0, value=0)
|
| 166 |
|
| 167 |
st.divider()
|
| 168 |
st.info("API Keys are managed via Environment Secrets.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
uploaded_file = st.file_uploader("Upload Video/Audio Clip", type=["mp4", "m4a", "wav", "mp3", "mov"])
|
| 171 |
|
|
|
|
| 182 |
with open("temp_input", "wb") as f:
|
| 183 |
f.write(uploaded_file.getbuffer())
|
| 184 |
|
| 185 |
+
st.write("🎵 **Extracting Audio (WAV 16k Mono)...**")
|
| 186 |
subprocess.run([
|
| 187 |
"ffmpeg", "-i", "temp_input",
|
| 188 |
"-vn", "-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1",
|
|
|
|
| 191 |
|
| 192 |
try:
|
| 193 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 194 |
+
if device == "cpu":
|
| 195 |
+
st.warning("⚠️ No GPU detected. WhisperX will be very slow.")
|
| 196 |
+
|
| 197 |
+
st.write(f"🚀 **Loading WhisperX on {device}...**")
|
| 198 |
|
| 199 |
# 1. Transcribe
|
| 200 |
compute_type = "float16" if device == "cuda" else "int8"
|
|
|
|
| 202 |
|
| 203 |
st.write("📝 **Transcribing...**")
|
| 204 |
audio = whisperx.load_audio("temp_audio.wav")
|
|
|
|
| 205 |
|
| 206 |
+
# Pass the language to speed up processing
|
| 207 |
+
result = model.transcribe(audio, batch_size=16, language=target_language)
|
| 208 |
+
|
| 209 |
+
# Memory cleanup
|
| 210 |
del model
|
| 211 |
gc.collect()
|
| 212 |
torch.cuda.empty_cache()
|
|
|
|
| 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
|