Spaces:
Sleeping
Sleeping
Update diarization.py
Browse files- diarization.py +24 -7
diarization.py
CHANGED
|
@@ -51,14 +51,14 @@ def get_diarization_pipeline(hf_token: Optional[str] = None):
|
|
| 51 |
return None
|
| 52 |
|
| 53 |
|
| 54 |
-
def diarize_audio(audio_path: str, pipeline, num_speakers: int =
|
| 55 |
"""
|
| 56 |
Perform speaker diarization on audio file.
|
| 57 |
|
| 58 |
Args:
|
| 59 |
audio_path: Path to audio file
|
| 60 |
pipeline: Pyannote diarization pipeline
|
| 61 |
-
num_speakers: Expected number of speakers (
|
| 62 |
|
| 63 |
Returns:
|
| 64 |
List of (start_time, end_time, speaker_label) tuples
|
|
@@ -67,13 +67,30 @@ def diarize_audio(audio_path: str, pipeline, num_speakers: int = 2) -> List[Tupl
|
|
| 67 |
return []
|
| 68 |
|
| 69 |
try:
|
| 70 |
-
# Run diarization
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
# Extract segments
|
| 74 |
segments = []
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
print(f"✅ Diarization tamamlandı: {len(segments)} segment bulundu")
|
| 79 |
return segments
|
|
|
|
| 51 |
return None
|
| 52 |
|
| 53 |
|
| 54 |
+
def diarize_audio(audio_path: str, pipeline, num_speakers: int = None) -> List[Tuple[float, float, str]]:
|
| 55 |
"""
|
| 56 |
Perform speaker diarization on audio file.
|
| 57 |
|
| 58 |
Args:
|
| 59 |
audio_path: Path to audio file
|
| 60 |
pipeline: Pyannote diarization pipeline
|
| 61 |
+
num_speakers: Expected number of speakers (None for auto-detect)
|
| 62 |
|
| 63 |
Returns:
|
| 64 |
List of (start_time, end_time, speaker_label) tuples
|
|
|
|
| 67 |
return []
|
| 68 |
|
| 69 |
try:
|
| 70 |
+
# Run diarization (auto-detect speakers or use specified count)
|
| 71 |
+
if num_speakers:
|
| 72 |
+
diarization = pipeline(audio_path, min_speakers=1, max_speakers=num_speakers)
|
| 73 |
+
else:
|
| 74 |
+
diarization = pipeline(audio_path)
|
| 75 |
|
| 76 |
+
# Extract segments - handle both old and new pyannote API
|
| 77 |
segments = []
|
| 78 |
+
|
| 79 |
+
# Try new API first (pyannote 3.x)
|
| 80 |
+
if hasattr(diarization, 'itertracks'):
|
| 81 |
+
for turn, _, speaker in diarization.itertracks(yield_label=True):
|
| 82 |
+
segments.append((turn.start, turn.end, speaker))
|
| 83 |
+
# Fallback for different output format
|
| 84 |
+
elif hasattr(diarization, 'get_timeline'):
|
| 85 |
+
for segment, _, speaker in diarization.get_timeline().itertracks(yield_label=True):
|
| 86 |
+
segments.append((segment.start, segment.end, speaker))
|
| 87 |
+
# Direct iteration if it's a list-like object
|
| 88 |
+
else:
|
| 89 |
+
# Try to iterate over the diarization result
|
| 90 |
+
for item in diarization:
|
| 91 |
+
if hasattr(item, 'start') and hasattr(item, 'end'):
|
| 92 |
+
speaker = getattr(item, 'speaker', 'SPEAKER_00')
|
| 93 |
+
segments.append((item.start, item.end, speaker))
|
| 94 |
|
| 95 |
print(f"✅ Diarization tamamlandı: {len(segments)} segment bulundu")
|
| 96 |
return segments
|