NickVerri commited on
Commit
98ae544
·
verified ·
1 Parent(s): 6fe6f80

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -41
app.py CHANGED
@@ -6,21 +6,28 @@ import os
6
  import requests
7
  import torch
8
  import numpy
 
 
9
  from datetime import timedelta
10
  from pyannote.audio import Pipeline
11
- from huggingface_hub import hf_hub_download
12
 
13
- # --- Safe Globals ---
14
  try:
15
  from pyannote.audio.core.task import Specifications, Problem, Resolution
 
 
16
 
17
  torch.serialization.add_safe_globals([
18
  torch.torch_version.TorchVersion,
19
  Specifications,
20
  Problem,
21
  Resolution,
 
 
22
  numpy.dtype,
23
  torch.nn.modules.container.ModuleList,
 
24
  ])
25
  except Exception as e:
26
  print(f"Safe Globals Warning: {e}")
@@ -36,7 +43,7 @@ ACTIVE_HF_TOKEN = ENV_HF_TOKEN if ENV_HF_TOKEN else HARDCODED_HF_TOKEN
36
  ACTIVE_GEMINI_KEY = ENV_GEMINI_KEY if ENV_GEMINI_KEY else HARDCODED_GEMINI_KEY
37
 
38
  def format_timecode(seconds, fps=25):
39
- """Converts seconds to HH:MM:SS:FF."""
40
  td = timedelta(seconds=seconds)
41
  total_seconds = int(td.total_seconds())
42
  hours = total_seconds // 3600
@@ -122,79 +129,106 @@ if uploaded_file:
122
  if not ACTIVE_HF_TOKEN or "PASTE_YOUR_HF_TOKEN" in ACTIVE_HF_TOKEN:
123
  st.error("Please provide a valid Hugging Face Token.")
124
  else:
125
- with st.spinner("Processing... This may take a few minutes."):
 
 
 
126
  with open("temp_input", "wb") as f:
127
  f.write(uploaded_file.getbuffer())
128
 
129
- # Convert to WAV
130
  subprocess.run([
131
  "ffmpeg", "-i", "temp_input",
132
  "-vn", "-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1",
133
  "temp_audio.wav", "-y"
134
- ], capture_output=True)
135
 
136
- # 1. Diarization - FIXED VERSION
 
137
  diarization = None
138
- speaker_turns = []
139
-
140
  try:
141
- st.write("Loading diarization pipeline...")
142
-
143
- # THE KEY FIX: Use direct from_pretrained with token
144
- pipeline = Pipeline.from_pretrained(
145
- "pyannote/speaker-diarization-3.1",
146
- use_auth_token=ACTIVE_HF_TOKEN
147
  )
 
148
 
 
 
 
 
 
 
 
 
 
 
149
  if torch.cuda.is_available():
 
150
  pipeline.to(torch.device("cuda"))
 
 
 
 
151
 
152
- st.write("Running speaker diarization...")
153
- diarization = pipeline("temp_audio.wav")
154
-
155
- # Extract speaker turns
156
- if diarization and hasattr(diarization, 'itertracks'):
157
- for turn, _, speaker_id in diarization.itertracks(yield_label=True):
158
- speaker_turns.append({
159
- "start": turn.start,
160
- "end": turn.end,
161
- "speaker": speaker_id
162
- })
163
- st.write(f"✓ Found {len(speaker_turns)} speaker segments")
164
  else:
165
- st.warning("Diarization completed but no speaker turns detected")
166
 
 
 
 
 
 
167
  except Exception as e:
168
  st.error(f"Diarization Error: {e}")
169
- st.write("Continuing without speaker identification...")
170
 
171
  # 2. Whisper Transcription
172
- st.write("Transcribing audio...")
173
- model = whisper.load_model("base")
 
174
  result = model.transcribe("temp_audio.wav", word_timestamps=True)
175
 
176
  # 3. Alignment
 
177
  final_segments = []
 
178
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  for segment in result['segments']:
180
  mid_time = (segment['start'] + segment['end']) / 2
181
  speaker = "Unknown"
182
 
183
  if speaker_turns:
184
- # Match speaker by midpoint
185
  for turn in speaker_turns:
186
  if turn["start"] <= mid_time <= turn["end"]:
187
  speaker = turn["speaker"]
188
  break
189
 
190
- # Fallback: find closest speaker within 1 second
191
  if speaker == "Unknown":
192
- best_dist = 1.0
193
  for turn in speaker_turns:
194
- dist = min(
195
- abs(turn["start"] - mid_time),
196
- abs(turn["end"] - mid_time)
197
- )
198
  if dist < best_dist:
199
  best_dist = dist
200
  speaker = turn["speaker"]
@@ -208,16 +242,15 @@ if uploaded_file:
208
  })
209
 
210
  st.session_state.transcript = final_segments
211
- st.success(f"✓ Processing complete! Transcribed {len(final_segments)} segments")
212
 
213
  if "transcript" in st.session_state:
214
  st.divider()
215
-
216
  with st.expander("Transcript Preview (Diarized)"):
217
  for seg in st.session_state.transcript[:20]:
218
  st.markdown(f"**{seg['speaker']}:** {seg['text']}")
219
 
220
- brief = st.text_area("Creative Brief", placeholder="e.g. Focus on the yeast story, remove the interviewer.")
221
 
222
  if st.button("Step 2: Create EDL"):
223
  if not ACTIVE_GEMINI_KEY:
 
6
  import requests
7
  import torch
8
  import numpy
9
+ import torchaudio
10
+ import torchaudio.transforms as T
11
  from datetime import timedelta
12
  from pyannote.audio import Pipeline
13
+ from huggingface_hub import login, hf_hub_download
14
 
15
+ # --- Safe Globals for PyTorch 2.6+ ---
16
  try:
17
  from pyannote.audio.core.task import Specifications, Problem, Resolution
18
+ from pyannote.audio.core.model import Model
19
+ from pyannote.audio.pipelines.speaker_diarization import SpeakerDiarization
20
 
21
  torch.serialization.add_safe_globals([
22
  torch.torch_version.TorchVersion,
23
  Specifications,
24
  Problem,
25
  Resolution,
26
+ Model,
27
+ SpeakerDiarization,
28
  numpy.dtype,
29
  torch.nn.modules.container.ModuleList,
30
+ numpy.core.multiarray.scalar
31
  ])
32
  except Exception as e:
33
  print(f"Safe Globals Warning: {e}")
 
43
  ACTIVE_GEMINI_KEY = ENV_GEMINI_KEY if ENV_GEMINI_KEY else HARDCODED_GEMINI_KEY
44
 
45
  def format_timecode(seconds, fps=25):
46
+ """Converts seconds to HH:MM:SS:FF for Resolve/Premiere."""
47
  td = timedelta(seconds=seconds)
48
  total_seconds = int(td.total_seconds())
49
  hours = total_seconds // 3600
 
129
  if not ACTIVE_HF_TOKEN or "PASTE_YOUR_HF_TOKEN" in ACTIVE_HF_TOKEN:
130
  st.error("Please provide a valid Hugging Face Token.")
131
  else:
132
+ with st.spinner("Processing... This may take a moment."):
133
+ # Authenticate Globally
134
+ login(token=ACTIVE_HF_TOKEN)
135
+
136
  with open("temp_input", "wb") as f:
137
  f.write(uploaded_file.getbuffer())
138
 
139
+ # Convert to strict WAV using FFmpeg
140
  subprocess.run([
141
  "ffmpeg", "-i", "temp_input",
142
  "-vn", "-acodec", "pcm_s16le", "-ar", "16000", "-ac", "1",
143
  "temp_audio.wav", "-y"
144
+ ])
145
 
146
+ # 1. Diarization
147
+ st.write("🗣️ **Running Speaker Diarization...**")
148
  diarization = None
 
 
149
  try:
150
+ # Download config manually
151
+ config_path = hf_hub_download(
152
+ repo_id="pyannote/speaker-diarization-3.1",
153
+ filename="config.yaml",
154
+ token=ACTIVE_HF_TOKEN
 
155
  )
156
+ pipeline = Pipeline.from_pretrained(config_path)
157
 
158
+ # Load Audio into Memory (Fix for file read issues)
159
+ waveform, sample_rate = torchaudio.load("temp_audio.wav")
160
+
161
+ # Resample if needed (Paranoia check)
162
+ if sample_rate != 16000:
163
+ resampler = T.Resample(sample_rate, 16000)
164
+ waveform = resampler(waveform)
165
+ sample_rate = 16000
166
+
167
+ # Move to GPU if available
168
  if torch.cuda.is_available():
169
+ st.write("🚀 Using GPU for Diarization")
170
  pipeline.to(torch.device("cuda"))
171
+ waveform = waveform.to(torch.device("cuda"))
172
+
173
+ # Run Pipeline on Tensor directly
174
+ diarization_output = pipeline({"waveform": waveform, "sample_rate": sample_rate})
175
 
176
+ # Handle Output Wrapper
177
+ if isinstance(diarization_output, tuple):
178
+ diarization = diarization_output[0]
 
 
 
 
 
 
 
 
 
179
  else:
180
+ diarization = diarization_output
181
 
182
+ # CRITICAL FIX: Extract annotation from wrapper
183
+ if not hasattr(diarization, "itertracks"):
184
+ if hasattr(diarization_output, "annotation"):
185
+ diarization = diarization_output.annotation
186
+
187
  except Exception as e:
188
  st.error(f"Diarization Error: {e}")
189
+ diarization = None
190
 
191
  # 2. Whisper Transcription
192
+ st.write("📝 **Transcribing with Whisper...**")
193
+ device = "cuda" if torch.cuda.is_available() else "cpu"
194
+ model = whisper.load_model("medium", device=device)
195
  result = model.transcribe("temp_audio.wav", word_timestamps=True)
196
 
197
  # 3. Alignment
198
+ st.write("🔗 **Aligning Speakers...**")
199
  final_segments = []
200
+ speaker_turns = []
201
 
202
+ if diarization:
203
+ try:
204
+ # Iterate safely
205
+ iterator = None
206
+ if hasattr(diarization, 'itertracks'):
207
+ iterator = diarization.itertracks(yield_label=True)
208
+
209
+ if iterator:
210
+ for turn, _, speaker_id in iterator:
211
+ speaker_turns.append({"start": turn.start, "end": turn.end, "speaker": speaker_id})
212
+ st.write(f"✅ Found {len(speaker_turns)} speaker turns.")
213
+ else:
214
+ st.warning("⚠️ Pipeline ran but returned no tracks.")
215
+ except Exception as e:
216
+ st.error(f"Error iterating tracks: {e}")
217
+
218
  for segment in result['segments']:
219
  mid_time = (segment['start'] + segment['end']) / 2
220
  speaker = "Unknown"
221
 
222
  if speaker_turns:
 
223
  for turn in speaker_turns:
224
  if turn["start"] <= mid_time <= turn["end"]:
225
  speaker = turn["speaker"]
226
  break
227
 
 
228
  if speaker == "Unknown":
229
+ best_dist = 1.0
230
  for turn in speaker_turns:
231
+ dist = min(abs(turn["start"] - mid_time), abs(turn["end"] - mid_time))
 
 
 
232
  if dist < best_dist:
233
  best_dist = dist
234
  speaker = turn["speaker"]
 
242
  })
243
 
244
  st.session_state.transcript = final_segments
245
+ st.success("Complete!")
246
 
247
  if "transcript" in st.session_state:
248
  st.divider()
 
249
  with st.expander("Transcript Preview (Diarized)"):
250
  for seg in st.session_state.transcript[:20]:
251
  st.markdown(f"**{seg['speaker']}:** {seg['text']}")
252
 
253
+ brief = st.text_area("Creative Brief", placeholder="e.g. Focus on the yeast story.")
254
 
255
  if st.button("Step 2: Create EDL"):
256
  if not ACTIVE_GEMINI_KEY: