Update app.py
Browse files
app.py
CHANGED
|
@@ -130,6 +130,7 @@ if uploaded_file:
|
|
| 130 |
])
|
| 131 |
|
| 132 |
# 1. Diarization with Manual Config Loading
|
|
|
|
| 133 |
try:
|
| 134 |
# Fix for PyTorch 2.6+ security - whitelist ALL necessary classes
|
| 135 |
try:
|
|
@@ -161,7 +162,6 @@ if uploaded_file:
|
|
| 161 |
diarization_output = pipeline("audio_optimized.m4a")
|
| 162 |
|
| 163 |
# Fix for 'DiarizeOutput' object has no attribute 'itertracks'
|
| 164 |
-
# Handle potential tuple output or direct object
|
| 165 |
if isinstance(diarization_output, tuple):
|
| 166 |
diarization = diarization_output[0]
|
| 167 |
else:
|
|
@@ -169,11 +169,9 @@ if uploaded_file:
|
|
| 169 |
|
| 170 |
# Verify we have the annotation object by checking for itertracks
|
| 171 |
if not hasattr(diarization, "itertracks"):
|
| 172 |
-
# If it's a Hook/Wrapper object (common in some versions), try getting the annotation
|
| 173 |
if hasattr(diarization_output, "annotation"):
|
| 174 |
diarization = diarization_output.annotation
|
| 175 |
else:
|
| 176 |
-
# Last resort: if it behaves like a dict, try getting 'annotation' key
|
| 177 |
try:
|
| 178 |
diarization = diarization_output['annotation']
|
| 179 |
except:
|
|
@@ -181,7 +179,8 @@ if uploaded_file:
|
|
| 181 |
|
| 182 |
except Exception as e:
|
| 183 |
st.error(f"Diarization Error: {e}")
|
| 184 |
-
|
|
|
|
| 185 |
|
| 186 |
# 2. Whisper Transcription
|
| 187 |
model = whisper.load_model("base")
|
|
@@ -189,19 +188,36 @@ if uploaded_file:
|
|
| 189 |
|
| 190 |
# 3. Alignment
|
| 191 |
final_segments = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
for segment in result['segments']:
|
| 193 |
mid_time = (segment['start'] + segment['end']) / 2
|
| 194 |
speaker = "Unknown"
|
| 195 |
|
| 196 |
-
#
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
| 201 |
break
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
final_segments.append({
|
| 207 |
"speaker": speaker,
|
|
|
|
| 130 |
])
|
| 131 |
|
| 132 |
# 1. Diarization with Manual Config Loading
|
| 133 |
+
diarization = None
|
| 134 |
try:
|
| 135 |
# Fix for PyTorch 2.6+ security - whitelist ALL necessary classes
|
| 136 |
try:
|
|
|
|
| 162 |
diarization_output = pipeline("audio_optimized.m4a")
|
| 163 |
|
| 164 |
# Fix for 'DiarizeOutput' object has no attribute 'itertracks'
|
|
|
|
| 165 |
if isinstance(diarization_output, tuple):
|
| 166 |
diarization = diarization_output[0]
|
| 167 |
else:
|
|
|
|
| 169 |
|
| 170 |
# Verify we have the annotation object by checking for itertracks
|
| 171 |
if not hasattr(diarization, "itertracks"):
|
|
|
|
| 172 |
if hasattr(diarization_output, "annotation"):
|
| 173 |
diarization = diarization_output.annotation
|
| 174 |
else:
|
|
|
|
| 175 |
try:
|
| 176 |
diarization = diarization_output['annotation']
|
| 177 |
except:
|
|
|
|
| 179 |
|
| 180 |
except Exception as e:
|
| 181 |
st.error(f"Diarization Error: {e}")
|
| 182 |
+
# Don't stop here, allow transcription to proceed even if diarization fails
|
| 183 |
+
diarization = None
|
| 184 |
|
| 185 |
# 2. Whisper Transcription
|
| 186 |
model = whisper.load_model("base")
|
|
|
|
| 188 |
|
| 189 |
# 3. Alignment
|
| 190 |
final_segments = []
|
| 191 |
+
|
| 192 |
+
# Pre-calculate speaker turns for faster lookup
|
| 193 |
+
speaker_turns = []
|
| 194 |
+
if diarization:
|
| 195 |
+
try:
|
| 196 |
+
for turn, _, speaker_id in diarization.itertracks(yield_label=True):
|
| 197 |
+
speaker_turns.append({"start": turn.start, "end": turn.end, "speaker": speaker_id})
|
| 198 |
+
except Exception as e:
|
| 199 |
+
print(f"Error iterating tracks: {e}")
|
| 200 |
+
|
| 201 |
for segment in result['segments']:
|
| 202 |
mid_time = (segment['start'] + segment['end']) / 2
|
| 203 |
speaker = "Unknown"
|
| 204 |
|
| 205 |
+
# Improved matching logic
|
| 206 |
+
if speaker_turns:
|
| 207 |
+
# 1. Try strict containment
|
| 208 |
+
for turn in speaker_turns:
|
| 209 |
+
if turn["start"] <= mid_time <= turn["end"]:
|
| 210 |
+
speaker = turn["speaker"]
|
| 211 |
break
|
| 212 |
+
|
| 213 |
+
# 2. If no strict match, find closest turn (within 1 second)
|
| 214 |
+
if speaker == "Unknown":
|
| 215 |
+
best_dist = 1.0 # Max distance 1 second
|
| 216 |
+
for turn in speaker_turns:
|
| 217 |
+
dist = min(abs(turn["start"] - mid_time), abs(turn["end"] - mid_time))
|
| 218 |
+
if dist < best_dist:
|
| 219 |
+
best_dist = dist
|
| 220 |
+
speaker = turn["speaker"]
|
| 221 |
|
| 222 |
final_segments.append({
|
| 223 |
"speaker": speaker,
|