File size: 24,523 Bytes
3f792e8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
"""
Speaker Diarization Module for Multilingual Audio Intelligence System
This module implements state-of-the-art speaker diarization using pyannote.audio.
It segments audio to identify "who spoke when" with high accuracy and language-agnostic
speaker separation capabilities as required by PS-6.
Key Features:
- SOTA speaker diarization using pyannote.audio
- Language-agnostic voice characteristic analysis
- Integrated Voice Activity Detection (VAD)
- Automatic speaker count detection
- CPU and GPU optimization support
- Robust error handling and logging
Model: pyannote/speaker-diarization-3.1
Dependencies: pyannote.audio, torch, transformers
"""
import os
import logging
import warnings
import numpy as np
import torch
from typing import List, Tuple, Dict, Optional, Union
import tempfile
from dataclasses import dataclass
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
try:
from pyannote.audio import Pipeline
from pyannote.core import Annotation, Segment
PYANNOTE_AVAILABLE = True
except ImportError:
PYANNOTE_AVAILABLE = False
logging.warning("pyannote.audio not available. Install with: pip install pyannote.audio")
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Suppress various warnings for cleaner output
warnings.filterwarnings("ignore", category=UserWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
@dataclass
class SpeakerSegment:
"""
Data class representing a single speaker segment.
Attributes:
start_time (float): Segment start time in seconds
end_time (float): Segment end time in seconds
speaker_id (str): Unique speaker identifier (e.g., "SPEAKER_00")
confidence (float): Confidence score of the diarization (if available)
"""
start_time: float
end_time: float
speaker_id: str
confidence: float = 1.0
@property
def duration(self) -> float:
"""Duration of the segment in seconds."""
return self.end_time - self.start_time
def to_dict(self) -> dict:
"""Convert to dictionary for JSON serialization."""
return {
'start_time': self.start_time,
'end_time': self.end_time,
'speaker_id': self.speaker_id,
'duration': self.duration,
'confidence': self.confidence
}
class SpeakerDiarizer:
"""
State-of-the-art speaker diarization using pyannote.audio.
This class provides language-agnostic speaker diarization capabilities,
focusing on acoustic voice characteristics rather than linguistic content.
"""
def __init__(self,
model_name: str = "pyannote/speaker-diarization-3.1",
hf_token: Optional[str] = None,
device: Optional[str] = None,
min_speakers: Optional[int] = None,
max_speakers: Optional[int] = None):
"""
Initialize the Speaker Diarizer.
Args:
model_name (str): Hugging Face model name for diarization
hf_token (str, optional): Hugging Face token for gated models
device (str, optional): Device to run on ('cpu', 'cuda', 'auto')
min_speakers (int, optional): Minimum number of speakers to detect
max_speakers (int, optional): Maximum number of speakers to detect
"""
self.model_name = model_name
self.hf_token = hf_token or os.getenv('HUGGINGFACE_TOKEN')
self.min_speakers = min_speakers
self.max_speakers = max_speakers
# Device selection
if device == 'auto' or device is None:
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
else:
self.device = torch.device(device)
logger.info(f"Initializing SpeakerDiarizer on {self.device}")
# Initialize pipeline
self.pipeline = None
self._load_pipeline()
def _load_pipeline(self):
"""Load the pyannote.audio diarization pipeline."""
if not PYANNOTE_AVAILABLE:
raise ImportError(
"pyannote.audio is required for speaker diarization. "
"Install with: pip install pyannote.audio"
)
try:
# Load the pre-trained pipeline
logger.info(f"Loading {self.model_name}...")
if self.hf_token:
self.pipeline = Pipeline.from_pretrained(
self.model_name,
use_auth_token=self.hf_token
)
else:
# Try without token first (for public models)
try:
self.pipeline = Pipeline.from_pretrained(self.model_name)
except Exception as e:
logger.error(
f"Failed to load {self.model_name}. "
"This model may be gated and require a Hugging Face token. "
f"Set HUGGINGFACE_TOKEN environment variable. Error: {e}"
)
raise
# Move pipeline to appropriate device
self.pipeline = self.pipeline.to(self.device)
# Configure speaker count constraints
if self.min_speakers is not None or self.max_speakers is not None:
self.pipeline.instantiate({
"clustering": {
"min_cluster_size": self.min_speakers or 1,
"max_num_speakers": self.max_speakers or 20
}
})
logger.info(f"Successfully loaded {self.model_name} on {self.device}")
except Exception as e:
logger.error(f"Failed to load diarization pipeline: {e}")
raise
def diarize(self,
audio_input: Union[str, np.ndarray],
sample_rate: int = 16000) -> List[SpeakerSegment]:
"""
Perform speaker diarization on audio input.
Args:
audio_input: Audio file path or numpy array
sample_rate: Sample rate if audio_input is numpy array
Returns:
List[SpeakerSegment]: List of speaker segments with timestamps
Raises:
ValueError: If input is invalid
Exception: For diarization errors
"""
if self.pipeline is None:
raise RuntimeError("Pipeline not loaded. Call _load_pipeline() first.")
try:
# Prepare audio input for pyannote
audio_file = self._prepare_audio_input(audio_input, sample_rate)
logger.info("Starting speaker diarization...")
start_time = torch.cuda.Event(enable_timing=True) if torch.cuda.is_available() else None
end_time = torch.cuda.Event(enable_timing=True) if torch.cuda.is_available() else None
if start_time:
start_time.record()
# Run diarization
diarization_result = self.pipeline(audio_file)
if end_time and start_time:
end_time.record()
torch.cuda.synchronize()
processing_time = start_time.elapsed_time(end_time) / 1000.0
logger.info(f"Diarization completed in {processing_time:.2f}s")
# Convert results to structured format
segments = self._parse_diarization_result(diarization_result)
# Log summary
num_speakers = len(set(seg.speaker_id for seg in segments))
total_speech_time = sum(seg.duration for seg in segments)
logger.info(f"Detected {num_speakers} speakers, {len(segments)} segments, "
f"{total_speech_time:.1f}s total speech")
return segments
except Exception as e:
logger.error(f"Diarization failed: {str(e)}")
raise
finally:
# Clean up temporary files if created
if isinstance(audio_input, np.ndarray):
try:
if hasattr(audio_file, 'name') and os.path.exists(audio_file.name):
os.unlink(audio_file.name)
except Exception:
pass
def _prepare_audio_input(self,
audio_input: Union[str, np.ndarray],
sample_rate: int) -> str:
"""
Prepare audio input for pyannote.audio pipeline.
Args:
audio_input: Audio file path or numpy array
sample_rate: Sample rate for numpy array input
Returns:
str: Path to audio file ready for pyannote
"""
if isinstance(audio_input, str):
# File path - validate existence
if not os.path.exists(audio_input):
raise FileNotFoundError(f"Audio file not found: {audio_input}")
return audio_input
elif isinstance(audio_input, np.ndarray):
# Numpy array - save to temporary file
return self._save_array_to_tempfile(audio_input, sample_rate)
else:
raise ValueError(f"Unsupported audio input type: {type(audio_input)}")
def _save_array_to_tempfile(self, audio_array: np.ndarray, sample_rate: int) -> str:
"""
Save numpy array to temporary WAV file for pyannote processing.
Args:
audio_array: Audio data as numpy array
sample_rate: Sample rate of the audio
Returns:
str: Path to temporary WAV file
"""
try:
import soundfile as sf
# Create temporary file
temp_file = tempfile.NamedTemporaryFile(
delete=False,
suffix='.wav',
prefix='diarization_'
)
temp_path = temp_file.name
temp_file.close()
# Ensure audio is in correct format
if len(audio_array.shape) > 1:
audio_array = audio_array.flatten()
# Normalize to prevent clipping
if np.max(np.abs(audio_array)) > 1.0:
audio_array = audio_array / np.max(np.abs(audio_array))
# Save using soundfile
sf.write(temp_path, audio_array, sample_rate)
logger.debug(f"Saved audio array to temporary file: {temp_path}")
return temp_path
except ImportError:
# Fallback to scipy if soundfile not available
try:
from scipy.io import wavfile
temp_file = tempfile.NamedTemporaryFile(
delete=False,
suffix='.wav',
prefix='diarization_'
)
temp_path = temp_file.name
temp_file.close()
# Convert to 16-bit int for scipy
if audio_array.dtype != np.int16:
audio_array_int = (audio_array * 32767).astype(np.int16)
else:
audio_array_int = audio_array
wavfile.write(temp_path, sample_rate, audio_array_int)
logger.debug(f"Saved audio array using scipy: {temp_path}")
return temp_path
except ImportError:
raise ImportError(
"Neither soundfile nor scipy available for audio saving. "
"Install with: pip install soundfile"
)
def _parse_diarization_result(self, diarization: Annotation) -> List[SpeakerSegment]:
"""
Parse pyannote diarization result into structured segments.
Args:
diarization: pyannote Annotation object
Returns:
List[SpeakerSegment]: Parsed speaker segments
"""
segments = []
for segment, _, speaker_label in diarization.itertracks(yield_label=True):
# Convert pyannote segment to our format
speaker_segment = SpeakerSegment(
start_time=float(segment.start),
end_time=float(segment.end),
speaker_id=str(speaker_label),
confidence=1.0 # pyannote doesn't provide segment-level confidence
)
segments.append(speaker_segment)
# Sort segments by start time
segments.sort(key=lambda x: x.start_time)
return segments
def get_speaker_statistics(self, segments: List[SpeakerSegment]) -> Dict[str, dict]:
"""
Generate speaker statistics from diarization results.
Args:
segments: List of speaker segments
Returns:
Dict: Speaker statistics including speaking time, turn counts, etc.
"""
stats = {}
for segment in segments:
speaker_id = segment.speaker_id
if speaker_id not in stats:
stats[speaker_id] = {
'total_speaking_time': 0.0,
'number_of_turns': 0,
'average_turn_duration': 0.0,
'longest_turn': 0.0,
'shortest_turn': float('inf')
}
# Update statistics
stats[speaker_id]['total_speaking_time'] += segment.duration
stats[speaker_id]['number_of_turns'] += 1
stats[speaker_id]['longest_turn'] = max(
stats[speaker_id]['longest_turn'],
segment.duration
)
stats[speaker_id]['shortest_turn'] = min(
stats[speaker_id]['shortest_turn'],
segment.duration
)
# Calculate averages
for speaker_id, speaker_stats in stats.items():
if speaker_stats['number_of_turns'] > 0:
speaker_stats['average_turn_duration'] = (
speaker_stats['total_speaking_time'] /
speaker_stats['number_of_turns']
)
# Handle edge case for shortest turn
if speaker_stats['shortest_turn'] == float('inf'):
speaker_stats['shortest_turn'] = 0.0
return stats
def merge_short_segments(self,
segments: List[SpeakerSegment],
min_duration: float = 1.0) -> List[SpeakerSegment]:
"""
Merge segments that are too short with adjacent segments from same speaker.
Args:
segments: List of speaker segments
min_duration: Minimum duration for segments in seconds
Returns:
List[SpeakerSegment]: Processed segments with short ones merged
"""
if not segments:
return segments
merged_segments = []
current_segment = segments[0]
for next_segment in segments[1:]:
# If current segment is too short and next is same speaker, merge
if (current_segment.duration < min_duration and
current_segment.speaker_id == next_segment.speaker_id):
# Extend current segment to include next segment
current_segment.end_time = next_segment.end_time
else:
# Add current segment and move to next
merged_segments.append(current_segment)
current_segment = next_segment
# Add the last segment
merged_segments.append(current_segment)
logger.debug(f"Merged {len(segments)} segments into {len(merged_segments)}")
return merged_segments
def export_to_rttm(self,
segments: List[SpeakerSegment],
audio_filename: str = "audio") -> str:
"""
Export diarization results to RTTM format.
RTTM (Rich Transcription Time Marked) is a standard format
for speaker diarization results.
Args:
segments: List of speaker segments
audio_filename: Name of the audio file for RTTM output
Returns:
str: RTTM formatted string
"""
rttm_lines = []
for segment in segments:
# RTTM format: SPEAKER <file> <chnl> <tbeg> <tdur> <ortho> <stype> <name> <conf>
rttm_line = (
f"SPEAKER {audio_filename} 1 "
f"{segment.start_time:.3f} {segment.duration:.3f} "
f"<NA> <NA> {segment.speaker_id} {segment.confidence:.3f}"
)
rttm_lines.append(rttm_line)
return "\n".join(rttm_lines)
def __del__(self):
"""Cleanup resources when the object is destroyed."""
# Clear GPU cache if using CUDA
if hasattr(self, 'device') and self.device.type == 'cuda':
try:
torch.cuda.empty_cache()
except Exception:
pass
# Convenience function for easy usage
def diarize_audio(audio_input: Union[str, np.ndarray],
sample_rate: int = 16000,
hf_token: Optional[str] = None,
min_speakers: Optional[int] = None,
max_speakers: Optional[int] = None,
merge_short: bool = True,
min_duration: float = 1.0) -> List[SpeakerSegment]:
"""
Convenience function to perform speaker diarization with default settings.
Args:
audio_input: Audio file path or numpy array
sample_rate: Sample rate for numpy array input
hf_token: Hugging Face token for gated models
min_speakers: Minimum number of speakers to detect
max_speakers: Maximum number of speakers to detect
merge_short: Whether to merge short segments
min_duration: Minimum duration for segments (if merge_short=True)
Returns:
List[SpeakerSegment]: Speaker diarization results
Example:
>>> # From file
>>> segments = diarize_audio("meeting.wav")
>>>
>>> # From numpy array
>>> import numpy as np
>>> audio_data = np.random.randn(16000 * 60) # 1 minute of audio
>>> segments = diarize_audio(audio_data, sample_rate=16000)
>>>
>>> # Print results
>>> for seg in segments:
>>> print(f"{seg.speaker_id}: {seg.start_time:.1f}s - {seg.end_time:.1f}s")
"""
# Initialize diarizer
diarizer = SpeakerDiarizer(
hf_token=hf_token,
min_speakers=min_speakers,
max_speakers=max_speakers
)
# Perform diarization
segments = diarizer.diarize(audio_input, sample_rate)
# Merge short segments if requested
if merge_short and segments:
segments = diarizer.merge_short_segments(segments, min_duration)
return segments
# Example usage and testing
if __name__ == "__main__":
import sys
import argparse
import json
def main():
"""Command line interface for testing speaker diarization."""
parser = argparse.ArgumentParser(description="Speaker Diarization Tool")
parser.add_argument("audio_file", help="Path to audio file")
parser.add_argument("--token", help="Hugging Face token")
parser.add_argument("--min-speakers", type=int, help="Minimum number of speakers")
parser.add_argument("--max-speakers", type=int, help="Maximum number of speakers")
parser.add_argument("--output-format", choices=["json", "rttm", "text"],
default="text", help="Output format")
parser.add_argument("--merge-short", action="store_true",
help="Merge short segments")
parser.add_argument("--min-duration", type=float, default=1.0,
help="Minimum segment duration for merging")
parser.add_argument("--verbose", "-v", action="store_true",
help="Enable verbose logging")
args = parser.parse_args()
if args.verbose:
logging.getLogger().setLevel(logging.DEBUG)
try:
# Perform diarization
print(f"Processing audio file: {args.audio_file}")
segments = diarize_audio(
audio_input=args.audio_file,
hf_token=args.token,
min_speakers=args.min_speakers,
max_speakers=args.max_speakers,
merge_short=args.merge_short,
min_duration=args.min_duration
)
# Output results in requested format
if args.output_format == "json":
# JSON output
result = {
"audio_file": args.audio_file,
"num_speakers": len(set(seg.speaker_id for seg in segments)),
"num_segments": len(segments),
"total_speech_time": sum(seg.duration for seg in segments),
"segments": [seg.to_dict() for seg in segments]
}
print(json.dumps(result, indent=2))
elif args.output_format == "rttm":
# RTTM output
diarizer = SpeakerDiarizer()
rttm_content = diarizer.export_to_rttm(segments, args.audio_file)
print(rttm_content)
else: # text format
# Human-readable text output
print(f"\n=== SPEAKER DIARIZATION RESULTS ===")
print(f"Audio file: {args.audio_file}")
print(f"Number of speakers: {len(set(seg.speaker_id for seg in segments))}")
print(f"Number of segments: {len(segments)}")
print(f"Total speech time: {sum(seg.duration for seg in segments):.1f}s")
print("\n--- Segment Details ---")
for i, segment in enumerate(segments, 1):
print(f"#{i:2d} | {segment.speaker_id:10s} | "
f"{segment.start_time:7.1f}s - {segment.end_time:7.1f}s | "
f"{segment.duration:5.1f}s")
# Speaker statistics
diarizer = SpeakerDiarizer()
stats = diarizer.get_speaker_statistics(segments)
print("\n--- Speaker Statistics ---")
for speaker_id, speaker_stats in stats.items():
print(f"{speaker_id}:")
print(f" Speaking time: {speaker_stats['total_speaking_time']:.1f}s")
print(f" Number of turns: {speaker_stats['number_of_turns']}")
print(f" Average turn: {speaker_stats['average_turn_duration']:.1f}s")
print(f" Longest turn: {speaker_stats['longest_turn']:.1f}s")
print(f" Shortest turn: {speaker_stats['shortest_turn']:.1f}s")
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
# Run CLI if script is executed directly
if not PYANNOTE_AVAILABLE:
print("Warning: pyannote.audio not available. Install with: pip install pyannote.audio")
print("Running in demo mode...")
# Create dummy segments for testing
dummy_segments = [
SpeakerSegment(0.0, 5.2, "SPEAKER_00", 0.95),
SpeakerSegment(5.5, 8.3, "SPEAKER_01", 0.87),
SpeakerSegment(8.8, 12.1, "SPEAKER_00", 0.92),
SpeakerSegment(12.5, 15.7, "SPEAKER_01", 0.89),
]
print("\n=== DEMO OUTPUT (pyannote.audio not available) ===")
for segment in dummy_segments:
print(f"{segment.speaker_id}: {segment.start_time:.1f}s - {segment.end_time:.1f}s")
else:
main() |