NickVerri commited on
Commit
d929156
·
verified ·
1 Parent(s): 3d2a98b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -19
app.py CHANGED
@@ -5,12 +5,32 @@ import json
5
  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 login, hf_hub_download
12
  from pydub import AudioSegment
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  # --- Configuration & Tokens ---
15
  HARDCODED_HF_TOKEN = "PASTE_YOUR_HF_TOKEN_HERE"
16
  HARDCODED_GEMINI_KEY = ""
@@ -127,13 +147,12 @@ if uploaded_file:
127
  st.write("🗣️ **Running Speaker Diarization...**")
128
  diarization = None
129
  try:
130
- # SIMPLEST LOADING METHOD (v2.1.1 compatible)
131
- # We use hf_hub_download to get the config.yaml path manually
132
- # This avoids "use_auth_token" ambiguity in From_Pretrained
133
  config_path = hf_hub_download(
134
- repo_id="pyannote/speaker-diarization@2.1",
 
135
  filename="config.yaml",
136
- token=ACTIVE_HF_TOKEN # FIXED ARGUMENT NAME
137
  )
138
 
139
  pipeline = Pipeline.from_pretrained(config_path)
@@ -162,31 +181,26 @@ if uploaded_file:
162
 
163
  if diarization:
164
  # 2.1.1 returns a proper Annotation object directly
165
- try:
166
- for turn, _, speaker_id in diarization.itertracks(yield_label=True):
167
- speaker_turns.append({"start": turn.start, "end": turn.end, "speaker": speaker_id})
168
-
169
- if len(speaker_turns) > 0:
170
- st.write(f"✅ Found {len(speaker_turns)} speaker turns.")
171
- else:
172
- st.warning("⚠️ Pipeline ran but returned no tracks.")
173
- except AttributeError:
174
- st.error("Could not iterate tracks. Output object format mismatch.")
175
 
176
  for segment in result['segments']:
177
  mid_time = (segment['start'] + segment['end']) / 2
178
  speaker = "Unknown"
179
 
180
  if speaker_turns:
181
- # 1. Strict overlap
182
  for turn in speaker_turns:
183
  if turn["start"] <= mid_time <= turn["end"]:
184
  speaker = turn["speaker"]
185
  break
186
 
187
- # 2. Relaxed distance fallback (1 sec)
188
  if speaker == "Unknown":
189
- best_dist = 1.0
190
  for turn in speaker_turns:
191
  dist = min(abs(turn["start"] - mid_time), abs(turn["end"] - mid_time))
192
  if dist < best_dist:
 
5
  import os
6
  import requests
7
  import torch
8
+ import numpy as np
9
  from datetime import timedelta
10
  from pyannote.audio import Pipeline
11
  from huggingface_hub import login, hf_hub_download
12
  from pydub import AudioSegment
13
 
14
+ # --- Safe Globals for PyTorch 2.6+ ---
15
+ try:
16
+ from pyannote.audio.core.task import Specifications, Problem, Resolution
17
+ from pyannote.audio.core.model import Model
18
+ from pyannote.audio.pipelines.speaker_diarization import SpeakerDiarization
19
+
20
+ torch.serialization.add_safe_globals([
21
+ torch.torch_version.TorchVersion,
22
+ Specifications,
23
+ Problem,
24
+ Resolution,
25
+ Model,
26
+ SpeakerDiarization,
27
+ np.dtype,
28
+ torch.nn.modules.container.ModuleList,
29
+ np.core.multiarray.scalar
30
+ ])
31
+ except Exception as e:
32
+ print(f"Safe Globals Warning: {e}")
33
+
34
  # --- Configuration & Tokens ---
35
  HARDCODED_HF_TOKEN = "PASTE_YOUR_HF_TOKEN_HERE"
36
  HARDCODED_GEMINI_KEY = ""
 
147
  st.write("🗣️ **Running Speaker Diarization...**")
148
  diarization = None
149
  try:
150
+ # FIX: Pass revision separately, remove @ from repo_id
 
 
151
  config_path = hf_hub_download(
152
+ repo_id="pyannote/speaker-diarization",
153
+ revision="2.1", # Explicit revision
154
  filename="config.yaml",
155
+ token=ACTIVE_HF_TOKEN
156
  )
157
 
158
  pipeline = Pipeline.from_pretrained(config_path)
 
181
 
182
  if diarization:
183
  # 2.1.1 returns a proper Annotation object directly
184
+ for turn, _, speaker_id in diarization.itertracks(yield_label=True):
185
+ speaker_turns.append({"start": turn.start, "end": turn.end, "speaker": speaker_id})
186
+
187
+ if len(speaker_turns) > 0:
188
+ st.write(f"✅ Found {len(speaker_turns)} speaker turns.")
189
+ else:
190
+ st.warning("⚠️ Pipeline ran but returned no tracks.")
 
 
 
191
 
192
  for segment in result['segments']:
193
  mid_time = (segment['start'] + segment['end']) / 2
194
  speaker = "Unknown"
195
 
196
  if speaker_turns:
 
197
  for turn in speaker_turns:
198
  if turn["start"] <= mid_time <= turn["end"]:
199
  speaker = turn["speaker"]
200
  break
201
 
 
202
  if speaker == "Unknown":
203
+ best_dist = 1.0
204
  for turn in speaker_turns:
205
  dist = min(abs(turn["start"] - mid_time), abs(turn["end"] - mid_time))
206
  if dist < best_dist: