NickVerri commited on
Commit
d0f59fe
·
verified ·
1 Parent(s): 923938f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -13
app.py CHANGED
@@ -6,6 +6,7 @@ import requests
6
  import torch
7
  import whisperx
8
  import gc
 
9
  from datetime import timedelta
10
 
11
  # --- Configuration & Tokens ---
@@ -90,8 +91,8 @@ with st.sidebar:
90
  fps = st.number_input("Timeline FPS", value=25)
91
 
92
  st.header("Model Settings")
93
- model_size = st.selectbox("Whisper Model Size", ["large-v3", "large-v2", "medium", "base"], index=0)
94
- num_speakers = st.number_input("Number of Speakers (Optional)", min_value=0, value=0, help="Set to 0 for auto-detect")
95
 
96
  st.divider()
97
  st.info("API Keys are managed via Environment Secrets.")
@@ -109,10 +110,7 @@ if uploaded_file:
109
  if not ACTIVE_HF_TOKEN or "PASTE_YOUR_HF_TOKEN" in ACTIVE_HF_TOKEN:
110
  st.error("Please provide a valid Hugging Face Token.")
111
  else:
112
- status_container = st.empty()
113
- with status_container.container():
114
- st.write("🔄 **Processing Started...**")
115
-
116
  # Save local temp file
117
  with open("temp_input", "wb") as f:
118
  f.write(uploaded_file.getbuffer())
@@ -129,8 +127,7 @@ if uploaded_file:
129
  device = "cuda" if torch.cuda.is_available() else "cpu"
130
  st.write(f"🚀 **Loading WhisperX on {device}...**")
131
 
132
- # 1. Transcribe with WhisperX (Faster-Whisper)
133
- batch_size = 16 # Reduce if low VRAM
134
  # Use float16 for GPU, int8 for CPU
135
  compute_type = "float16" if device == "cuda" else "int8"
136
 
@@ -138,28 +135,27 @@ if uploaded_file:
138
 
139
  st.write("📝 **Transcribing...**")
140
  audio = whisperx.load_audio("temp_audio.wav")
141
- result = model.transcribe(audio, batch_size=batch_size)
142
 
143
  # Cleanup VRAM
144
- model_a = None
145
  gc.collect()
146
  torch.cuda.empty_cache()
 
147
 
148
- # 2. Align (Improves timestamp accuracy)
149
  st.write("⏱️ **Aligning Audio...**")
150
  model_a, metadata = whisperx.load_align_model(language_code=result["language"], device=device)
151
  result = whisperx.align(result["segments"], model_a, metadata, audio, device, return_char_alignments=False)
152
 
153
  # Cleanup VRAM
154
- model_a = None
155
  gc.collect()
156
  torch.cuda.empty_cache()
 
157
 
158
  # 3. Diarize
159
  st.write("🗣️ **Diarizing Speakers...**")
160
  diarize_model = whisperx.DiarizationPipeline(use_auth_token=ACTIVE_HF_TOKEN, device=device)
161
 
162
- # Optional: Enforce speaker count
163
  diarize_kwargs = {}
164
  if num_speakers > 0:
165
  diarize_kwargs = {"min_speakers": num_speakers, "max_speakers": num_speakers}
 
6
  import torch
7
  import whisperx
8
  import gc
9
+ import pandas as pd
10
  from datetime import timedelta
11
 
12
  # --- Configuration & Tokens ---
 
91
  fps = st.number_input("Timeline FPS", value=25)
92
 
93
  st.header("Model Settings")
94
+ model_size = st.selectbox("Whisper Model", ["large-v2", "medium", "base"], index=0)
95
+ num_speakers = st.number_input("Speakers (0=Auto)", min_value=0, value=0)
96
 
97
  st.divider()
98
  st.info("API Keys are managed via Environment Secrets.")
 
110
  if not ACTIVE_HF_TOKEN or "PASTE_YOUR_HF_TOKEN" in ACTIVE_HF_TOKEN:
111
  st.error("Please provide a valid Hugging Face Token.")
112
  else:
113
+ with st.spinner("Processing... This may take a moment."):
 
 
 
114
  # Save local temp file
115
  with open("temp_input", "wb") as f:
116
  f.write(uploaded_file.getbuffer())
 
127
  device = "cuda" if torch.cuda.is_available() else "cpu"
128
  st.write(f"🚀 **Loading WhisperX on {device}...**")
129
 
130
+ # 1. Transcribe
 
131
  # Use float16 for GPU, int8 for CPU
132
  compute_type = "float16" if device == "cuda" else "int8"
133
 
 
135
 
136
  st.write("📝 **Transcribing...**")
137
  audio = whisperx.load_audio("temp_audio.wav")
138
+ result = model.transcribe(audio, batch_size=16)
139
 
140
  # Cleanup VRAM
 
141
  gc.collect()
142
  torch.cuda.empty_cache()
143
+ del model
144
 
145
+ # 2. Align
146
  st.write("⏱️ **Aligning Audio...**")
147
  model_a, metadata = whisperx.load_align_model(language_code=result["language"], device=device)
148
  result = whisperx.align(result["segments"], model_a, metadata, audio, device, return_char_alignments=False)
149
 
150
  # Cleanup VRAM
 
151
  gc.collect()
152
  torch.cuda.empty_cache()
153
+ del model_a
154
 
155
  # 3. Diarize
156
  st.write("🗣️ **Diarizing Speakers...**")
157
  diarize_model = whisperx.DiarizationPipeline(use_auth_token=ACTIVE_HF_TOKEN, device=device)
158
 
 
159
  diarize_kwargs = {}
160
  if num_speakers > 0:
161
  diarize_kwargs = {"min_speakers": num_speakers, "max_speakers": num_speakers}