NickVerri commited on
Commit
27a167d
·
verified ·
1 Parent(s): da08a71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -8
app.py CHANGED
@@ -1,7 +1,15 @@
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  import subprocess
3
  import json
4
- import os
5
  import requests
6
  import torch
7
  import whisperx
@@ -92,7 +100,8 @@ with st.sidebar:
92
  fps = st.number_input("Timeline FPS", value=25)
93
 
94
  st.header("Model Settings")
95
- model_size = st.selectbox("Whisper Model", ["large-v2", "medium", "base"], index=0)
 
96
  num_speakers = st.number_input("Speakers (0=Auto)", min_value=0, value=0)
97
 
98
  st.divider()
@@ -111,7 +120,10 @@ if uploaded_file:
111
  if not ACTIVE_HF_TOKEN or "PASTE_YOUR_HF_TOKEN" in ACTIVE_HF_TOKEN:
112
  st.error("Please provide a valid Hugging Face Token.")
113
  else:
114
- with st.spinner("Processing... This may take a moment."):
 
 
 
115
  # Save local temp file
116
  with open("temp_input", "wb") as f:
117
  f.write(uploaded_file.getbuffer())
@@ -126,10 +138,12 @@ if uploaded_file:
126
 
127
  try:
128
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
129
  st.write(f"🚀 **Loading WhisperX on {device}...**")
130
 
131
- # 1. Transcribe with WhisperX (Faster-Whisper)
132
- batch_size = 16 # Reduce if low VRAM
133
  # Use float16 for GPU, int8 for CPU
134
  compute_type = "float16" if device == "cuda" else "int8"
135
 
@@ -137,14 +151,14 @@ if uploaded_file:
137
 
138
  st.write("📝 **Transcribing...**")
139
  audio = whisperx.load_audio("temp_audio.wav")
140
- result = model.transcribe(audio, batch_size=batch_size)
141
 
142
  # Cleanup VRAM
143
  gc.collect()
144
  torch.cuda.empty_cache()
145
  del model
146
 
147
- # 2. Align (Improves timestamp accuracy)
148
  st.write("⏱️ **Aligning Audio...**")
149
  model_a, metadata = whisperx.load_align_model(language_code=result["language"], device=device)
150
  result = whisperx.align(result["segments"], model_a, metadata, audio, device, return_char_alignments=False)
@@ -170,10 +184,11 @@ if uploaded_file:
170
 
171
  # Format for Gemini
172
  processed_segments = []
 
173
  for segment in final_result["segments"]:
174
  processed_segments.append({
175
  "speaker": segment.get("speaker", "Unknown"),
176
- "text": segment["text"],
177
  "start": segment["start"],
178
  "end": segment["end"]
179
  })
 
1
+ import os
2
+ import numpy as np
3
+
4
+ # --- NUMPY 2.0 PATCH ---
5
+ # This must run before any other library imports to prevent crashes
6
+ # with pyannote/whisperx which might expect the old 'np.NaN' attribute.
7
+ if not hasattr(np, 'NaN'):
8
+ np.NaN = np.nan
9
+
10
  import streamlit as st
11
  import subprocess
12
  import json
 
13
  import requests
14
  import torch
15
  import whisperx
 
100
  fps = st.number_input("Timeline FPS", value=25)
101
 
102
  st.header("Model Settings")
103
+ # T4 has 16GB VRAM, large-v2 works well
104
+ model_size = st.selectbox("Whisper Model", ["large-v2", "medium"], index=0)
105
  num_speakers = st.number_input("Speakers (0=Auto)", min_value=0, value=0)
106
 
107
  st.divider()
 
120
  if not ACTIVE_HF_TOKEN or "PASTE_YOUR_HF_TOKEN" in ACTIVE_HF_TOKEN:
121
  st.error("Please provide a valid Hugging Face Token.")
122
  else:
123
+ status_container = st.empty()
124
+ with status_container.container():
125
+ st.write("🔄 **Processing Started...**")
126
+
127
  # Save local temp file
128
  with open("temp_input", "wb") as f:
129
  f.write(uploaded_file.getbuffer())
 
138
 
139
  try:
140
  device = "cuda" if torch.cuda.is_available() else "cpu"
141
+ if device == "cpu":
142
+ st.warning("⚠️ No GPU detected. WhisperX will be very slow.")
143
+
144
  st.write(f"🚀 **Loading WhisperX on {device}...**")
145
 
146
+ # 1. Transcribe
 
147
  # Use float16 for GPU, int8 for CPU
148
  compute_type = "float16" if device == "cuda" else "int8"
149
 
 
151
 
152
  st.write("📝 **Transcribing...**")
153
  audio = whisperx.load_audio("temp_audio.wav")
154
+ result = model.transcribe(audio, batch_size=16)
155
 
156
  # Cleanup VRAM
157
  gc.collect()
158
  torch.cuda.empty_cache()
159
  del model
160
 
161
+ # 2. Align (Improves timestamp accuracy for diarization)
162
  st.write("⏱️ **Aligning Audio...**")
163
  model_a, metadata = whisperx.load_align_model(language_code=result["language"], device=device)
164
  result = whisperx.align(result["segments"], model_a, metadata, audio, device, return_char_alignments=False)
 
184
 
185
  # Format for Gemini
186
  processed_segments = []
187
+ # WhisperX structure is slightly different, it returns 'segments' list
188
  for segment in final_result["segments"]:
189
  processed_segments.append({
190
  "speaker": segment.get("speaker", "Unknown"),
191
+ "text": segment["text"].strip(),
192
  "start": segment["start"],
193
  "end": segment["end"]
194
  })