File size: 29,108 Bytes
b5df735 |
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 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 |
"""
Transcription Service
Handles audio transcription logic with support for parallel processing
"""
import whisper
import os
import json
import tempfile
import subprocess
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
from typing import Dict, Any, List
class TranscriptionService:
"""Service for handling audio transcription"""
def __init__(self, cache_dir: str = "/tmp"):
self.cache_dir = cache_dir
def _load_cached_model(self, model_size: str = "turbo"):
"""Load Whisper model from cache directory if available"""
try:
# Try to load from preloaded cache first
model_cache_dir = "/model"
if os.path.exists(model_cache_dir):
print(f"π¦ Loading {model_size} model from cache: {model_cache_dir}")
# Set download root to cache directory
model = whisper.load_model(model_size, download_root=model_cache_dir)
print(f"β
Successfully loaded {model_size} model from cache")
return model
else:
print(f"β οΈ Cache directory not found, downloading {model_size} model...")
return whisper.load_model(model_size)
except Exception as e:
print(f"β οΈ Failed to load cached model, downloading: {e}")
return whisper.load_model(model_size)
def _load_speaker_diarization_pipeline(self):
"""Load speaker diarization pipeline from cache if available"""
try:
speaker_cache_dir = "/model/speaker-diarization"
config_file = os.path.join(speaker_cache_dir, "download_complete.json")
# Set proper cache directory for pyannote
os.environ["PYANNOTE_CACHE"] = "/model/speaker-diarization"
# Check if cached speaker diarization models exist
if os.path.exists(config_file):
print(f"π¦ Loading speaker diarization from cache: {speaker_cache_dir}")
# Load from cache with proper cache_dir
from pyannote.audio import Pipeline
pipeline = Pipeline.from_pretrained(
"pyannote/speaker-diarization-3.1",
use_auth_token=os.environ.get("HF_TOKEN"),
cache_dir="/model/speaker-diarization"
)
print("β
Successfully loaded speaker diarization pipeline from cache")
return pipeline
else:
print("β οΈ Speaker diarization cache not found, downloading...")
# Download fresh if cache not available
from pyannote.audio import Pipeline
pipeline = Pipeline.from_pretrained(
"pyannote/speaker-diarization-3.1",
use_auth_token=os.environ.get("HF_TOKEN"),
cache_dir="/model/speaker-diarization"
)
# Create marker file to indicate successful download
import json
config = {
"model_name": "pyannote/speaker-diarization-3.1",
"cached_at": speaker_cache_dir,
"cache_complete": True,
"runtime_download": True
}
with open(config_file, "w") as f:
json.dump(config, f)
return pipeline
except Exception as e:
print(f"β οΈ Failed to load speaker diarization pipeline: {e}")
return None
def transcribe_audio(
self,
audio_file_path: str,
model_size: str = "turbo",
language: str = None,
output_format: str = "srt",
enable_speaker_diarization: bool = False
) -> Dict[str, Any]:
"""
Transcribe audio file using Whisper
Args:
audio_file_path: Path to audio file
model_size: Whisper model size
language: Language code (optional)
output_format: Output format
enable_speaker_diarization: Enable speaker identification
Returns:
Transcription result dictionary
"""
try:
print(f"π€ Starting transcription for: {audio_file_path}")
print(f"π Using model: {model_size}")
# Check if file exists
if not os.path.exists(audio_file_path):
raise FileNotFoundError(f"Audio file not found: {audio_file_path}")
# Load Whisper model from cache
model = self._load_cached_model(model_size)
# Load speaker diarization pipeline if enabled
speaker_pipeline = None
if enable_speaker_diarization:
speaker_pipeline = self._load_speaker_diarization_pipeline()
if speaker_pipeline is None:
print("β οΈ Speaker diarization disabled due to loading failure")
enable_speaker_diarization = False
# Transcribe audio
transcribe_options = {
"language": language if language and language != "auto" else None,
"task": "transcribe",
"verbose": True
}
print(f"π Transcribing with options: {transcribe_options}")
result = model.transcribe(audio_file_path, **transcribe_options)
# Extract information
text = result.get("text", "").strip()
segments = result.get("segments", [])
language_detected = result.get("language", "unknown")
# Apply speaker diarization if enabled
speaker_segments = []
global_speaker_count = 0
speaker_summary = {}
if enable_speaker_diarization and speaker_pipeline:
try:
print("π₯ Applying speaker diarization...")
diarization_result = speaker_pipeline(audio_file_path)
# Process diarization results
speakers = set()
for turn, _, speaker in diarization_result.itertracks(yield_label=True):
speakers.add(speaker)
speaker_segments.append({
"start": turn.start,
"end": turn.end,
"speaker": speaker
})
global_speaker_count = len(speakers)
speaker_summary = {f"SPEAKER_{i:02d}": speaker for i, speaker in enumerate(sorted(speakers))}
# Merge speaker information with transcription segments
segments = self._merge_speaker_segments(segments, speaker_segments)
print(f"β
Speaker diarization completed: {global_speaker_count} speakers detected")
except Exception as e:
print(f"β οΈ Speaker diarization failed: {e}")
enable_speaker_diarization = False
# Generate output files
output_files = self._generate_output_files(
audio_file_path, text, segments, enable_speaker_diarization
)
# Get audio duration
audio_duration = 0.0
if segments:
audio_duration = max(seg.get("end", 0) for seg in segments)
print(f"β
Transcription completed successfully")
print(f" Text length: {len(text)} characters")
print(f" Segments: {len(segments)}")
print(f" Duration: {audio_duration:.2f}s")
print(f" Language: {language_detected}")
return {
"txt_file_path": output_files.get("txt_file"),
"srt_file_path": output_files.get("srt_file"),
"audio_file": audio_file_path,
"model_used": model_size,
"segment_count": len(segments),
"audio_duration": audio_duration,
"processing_status": "success",
"saved_files": [f for f in output_files.values() if f],
"speaker_diarization_enabled": enable_speaker_diarization,
"global_speaker_count": global_speaker_count,
"speaker_summary": speaker_summary,
"language_detected": language_detected,
"text": text,
"segments": [
{
"start": seg.get("start", 0),
"end": seg.get("end", 0),
"text": seg.get("text", "").strip(),
"speaker": seg.get("speaker", None)
}
for seg in segments
]
}
except Exception as e:
print(f"β Transcription failed: {e}")
return self._create_error_result(audio_file_path, model_size, str(e))
def _merge_speaker_segments(self, transcription_segments: List[Dict], speaker_segments: List[Dict]) -> List[Dict]:
"""
Merge speaker information with transcription segments, splitting transcription segments
when multiple speakers are detected within a single segment
"""
merged_segments = []
for trans_seg in transcription_segments:
trans_start = trans_seg.get("start", 0)
trans_end = trans_seg.get("end", 0)
trans_text = trans_seg.get("text", "").strip()
# Find all overlapping speaker segments
overlapping_speakers = []
for speaker_seg in speaker_segments:
speaker_start = speaker_seg["start"]
speaker_end = speaker_seg["end"]
# Check if there's any overlap
overlap_start = max(trans_start, speaker_start)
overlap_end = min(trans_end, speaker_end)
overlap_duration = max(0, overlap_end - overlap_start)
if overlap_duration > 0:
overlapping_speakers.append({
"speaker": speaker_seg["speaker"],
"start": speaker_start,
"end": speaker_end,
"overlap_start": overlap_start,
"overlap_end": overlap_end,
"overlap_duration": overlap_duration
})
if not overlapping_speakers:
# No speaker detected, keep original segment
merged_seg = trans_seg.copy()
merged_seg["speaker"] = None
merged_segments.append(merged_seg)
continue
# Sort overlapping speakers by start time
overlapping_speakers.sort(key=lambda x: x["overlap_start"])
if len(overlapping_speakers) == 1:
# Single speaker for this transcription segment
merged_seg = trans_seg.copy()
merged_seg["speaker"] = overlapping_speakers[0]["speaker"]
merged_segments.append(merged_seg)
else:
# Multiple speakers detected - split the transcription segment
print(f"π Splitting segment ({trans_start:.2f}s-{trans_end:.2f}s) with {len(overlapping_speakers)} speakers")
split_segments = self._split_transcription_segment(
trans_seg, overlapping_speakers, trans_text
)
merged_segments.extend(split_segments)
return merged_segments
def _split_transcription_segment(self, trans_seg: Dict, overlapping_speakers: List[Dict], trans_text: str) -> List[Dict]:
"""
Split a transcription segment into multiple segments based on speaker changes
"""
split_segments = []
trans_start = trans_seg.get("start", 0)
trans_end = trans_seg.get("end", 0)
trans_duration = trans_end - trans_start
# Calculate the proportion of text for each speaker based on overlap duration
total_overlap_duration = sum(sp["overlap_duration"] for sp in overlapping_speakers)
if total_overlap_duration == 0:
# Fallback: equal distribution
text_per_speaker = len(trans_text) // len(overlapping_speakers)
current_text_pos = 0
for i, speaker_info in enumerate(overlapping_speakers):
# Calculate text portion for this speaker
if total_overlap_duration > 0:
text_proportion = speaker_info["overlap_duration"] / total_overlap_duration
else:
text_proportion = 1.0 / len(overlapping_speakers)
# Calculate text length for this speaker
if i == len(overlapping_speakers) - 1:
# Last speaker gets remaining text
speaker_text_length = len(trans_text) - current_text_pos
else:
speaker_text_length = int(len(trans_text) * text_proportion)
# Extract text for this speaker
speaker_text_end = min(current_text_pos + speaker_text_length, len(trans_text))
speaker_text = trans_text[current_text_pos:speaker_text_end].strip()
# Adjust word boundaries to avoid cutting words in half
if speaker_text_end < len(trans_text) and i < len(overlapping_speakers) - 1:
# Find the last complete word
last_space = speaker_text.rfind(' ')
if last_space > 0:
speaker_text = speaker_text[:last_space]
speaker_text_end = current_text_pos + last_space + 1 # +1 to skip the space
else:
# If no space found, keep original text but update position
speaker_text_end = current_text_pos + speaker_text_length
# Use actual speaker diarization timing directly
segment_start = speaker_info["overlap_start"]
segment_end = speaker_info["overlap_end"]
# Always create segment if we have valid timing, even with empty text
if segment_start < segment_end:
split_segment = {
"start": segment_start,
"end": segment_end,
"text": speaker_text,
"speaker": speaker_info["speaker"]
}
split_segments.append(split_segment)
print(f" β {speaker_info['speaker']}: {segment_start:.2f}s-{segment_end:.2f}s: \"{speaker_text[:50]}{'...' if len(speaker_text) > 50 else ''}\"")
current_text_pos = speaker_text_end
return split_segments
def transcribe_audio_parallel(
self,
audio_file_path: str,
model_size: str = "turbo",
language: str = None,
output_format: str = "srt",
enable_speaker_diarization: bool = False,
chunk_duration: int = 300
) -> Dict[str, Any]:
"""
Transcribe audio with parallel processing for long files
Args:
audio_file_path: Path to audio file
model_size: Whisper model size
language: Language code (optional)
output_format: Output format
enable_speaker_diarization: Enable speaker identification
chunk_duration: Duration of chunks in seconds
Returns:
Transcription result dictionary
"""
try:
print(f"π€ Starting parallel transcription for: {audio_file_path}")
print(f"π Using model: {model_size}")
print(f"β‘ Chunk duration: {chunk_duration}s")
# Check if file exists
if not os.path.exists(audio_file_path):
raise FileNotFoundError(f"Audio file not found: {audio_file_path}")
# Get audio duration
total_duration = self._get_audio_duration(audio_file_path)
print(f"π Total audio duration: {total_duration:.2f}s")
# If audio is shorter than chunk duration, use single processing
if total_duration <= chunk_duration:
print("π Audio is short, using single processing")
return self.transcribe_audio(
audio_file_path, model_size, language, output_format, enable_speaker_diarization
)
# Split audio into chunks
chunks = self._split_audio_into_chunks(audio_file_path, chunk_duration, total_duration)
print(f"π Created {len(chunks)} chunks for parallel processing")
# Load Whisper model once from cache
model = self._load_cached_model(model_size)
# Process chunks in parallel
chunk_results = self._process_chunks_parallel(chunks, model, language)
# Combine results
combined_result = self._combine_chunk_results(
chunk_results, audio_file_path, model_size,
enable_speaker_diarization, total_duration
)
# Cleanup chunk files
self._cleanup_chunks(chunks)
return combined_result
except Exception as e:
print(f"β Parallel transcription failed: {e}")
result = self._create_error_result(audio_file_path, model_size, str(e))
result["parallel_processing"] = True
return result
def normalize_audio_file(self, input_file: str, output_file: str = None) -> str:
"""
Normalize audio file for better Whisper compatibility
Args:
input_file: Input audio file path
output_file: Output file path (optional)
Returns:
Path to normalized audio file
"""
if output_file is None:
temp_dir = tempfile.mkdtemp()
output_file = os.path.join(temp_dir, "normalized_audio.wav")
# Convert to standardized format: 16kHz, mono, PCM
cmd = [
"ffmpeg", "-i", input_file,
"-ar", "16000", # 16kHz sample rate (Whisper's native)
"-ac", "1", # Mono channel
"-c:a", "pcm_s16le", # PCM 16-bit encoding
"-y", # Overwrite output file
output_file
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
print(f"β οΈ FFmpeg normalization failed: {result.stderr}")
return input_file # Return original file if normalization fails
else:
print("β
Audio normalized for Whisper")
return output_file
def _get_audio_duration(self, audio_file_path: str) -> float:
"""Get audio duration using ffprobe"""
cmd = ["ffprobe", "-v", "quiet", "-show_entries", "format=duration", "-of", "csv=p=0", audio_file_path]
duration_output = subprocess.run(cmd, capture_output=True, text=True)
return float(duration_output.stdout.strip())
def _split_audio_into_chunks(self, audio_file_path: str, chunk_duration: int, total_duration: float) -> List[Dict]:
"""Split audio file into chunks for parallel processing"""
chunks = []
temp_dir = tempfile.mkdtemp()
for i in range(0, int(total_duration), chunk_duration):
chunk_start = i
chunk_end = min(i + chunk_duration, total_duration)
chunk_file = os.path.join(temp_dir, f"chunk_{i//chunk_duration:03d}.wav")
# Extract chunk using ffmpeg
cmd = [
"ffmpeg", "-i", audio_file_path,
"-ss", str(chunk_start),
"-t", str(chunk_end - chunk_start),
"-c:a", "pcm_s16le", # Use PCM encoding for better quality
"-ar", "16000", # 16kHz sample rate for Whisper
chunk_file
]
subprocess.run(cmd, capture_output=True)
if os.path.exists(chunk_file):
chunks.append({
"file": chunk_file,
"start_time": chunk_start,
"end_time": chunk_end,
"index": len(chunks),
"temp_dir": temp_dir
})
print(f"π¦ Created chunk {len(chunks)}: {chunk_start:.1f}s-{chunk_end:.1f}s")
return chunks
def _process_chunks_parallel(self, chunks: List[Dict], model, language: str) -> List[Dict]:
"""Process audio chunks in parallel"""
def process_chunk(chunk_info):
try:
print(f"π Processing chunk {chunk_info['index']}: {chunk_info['start_time']:.1f}s-{chunk_info['end_time']:.1f}s")
transcribe_options = {
"language": language if language and language != "auto" else None,
"task": "transcribe",
"verbose": False # Reduce verbosity for parallel processing
}
result = model.transcribe(chunk_info["file"], **transcribe_options)
# Adjust segment timing to global timeline
segments = []
for seg in result.get("segments", []):
adjusted_seg = {
"start": seg["start"] + chunk_info["start_time"],
"end": seg["end"] + chunk_info["start_time"],
"text": seg["text"].strip(),
"speaker": None
}
segments.append(adjusted_seg)
print(f"β
Chunk {chunk_info['index']} completed: {len(segments)} segments")
return {
"text": result.get("text", "").strip(),
"segments": segments,
"language": result.get("language", "unknown"),
"chunk_index": chunk_info["index"]
}
except Exception as e:
print(f"β Chunk {chunk_info['index']} failed: {e}")
return {
"text": "",
"segments": [],
"language": "unknown",
"chunk_index": chunk_info["index"],
"error": str(e)
}
# Process chunks in parallel using ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=min(len(chunks), 8)) as executor:
chunk_results = list(executor.map(process_chunk, chunks))
# Sort results by chunk index
chunk_results.sort(key=lambda x: x["chunk_index"])
return chunk_results
def _combine_chunk_results(
self,
chunk_results: List[Dict],
audio_file_path: str,
model_size: str,
enable_speaker_diarization: bool,
total_duration: float
) -> Dict[str, Any]:
"""Combine results from multiple chunks"""
# Combine results
full_text = " ".join([chunk["text"] for chunk in chunk_results if chunk["text"]])
all_segments = []
for chunk in chunk_results:
all_segments.extend(chunk["segments"])
# Sort segments by start time
all_segments.sort(key=lambda x: x["start"])
# Get detected language (use most common one)
languages = [chunk["language"] for chunk in chunk_results if chunk["language"] != "unknown"]
language_detected = max(set(languages), key=languages.count) if languages else "unknown"
# Generate output files
output_files = self._generate_output_files(
audio_file_path, full_text, all_segments, enable_speaker_diarization
)
print(f"β
Parallel transcription completed successfully")
print(f" Text length: {len(full_text)} characters")
print(f" Total segments: {len(all_segments)}")
print(f" Duration: {total_duration:.2f}s")
print(f" Language: {language_detected}")
print(f" Chunks processed: {len(chunk_results)}")
return {
"txt_file_path": output_files.get("txt_file"),
"srt_file_path": output_files.get("srt_file"),
"audio_file": audio_file_path,
"model_used": model_size,
"segment_count": len(all_segments),
"audio_duration": total_duration,
"processing_status": "success",
"saved_files": [f for f in output_files.values() if f],
"speaker_diarization_enabled": enable_speaker_diarization,
"global_speaker_count": 0,
"speaker_summary": {},
"language_detected": language_detected,
"text": full_text,
"segments": all_segments,
"chunks_processed": len(chunk_results),
"parallel_processing": True
}
def _cleanup_chunks(self, chunks: List[Dict]):
"""Clean up temporary chunk files"""
temp_dirs = set()
for chunk in chunks:
try:
if os.path.exists(chunk["file"]):
os.remove(chunk["file"])
temp_dirs.add(chunk["temp_dir"])
except Exception as e:
print(f"β οΈ Failed to cleanup chunk file: {e}")
# Remove temp directories
for temp_dir in temp_dirs:
try:
os.rmdir(temp_dir)
except Exception as e:
print(f"β οΈ Failed to cleanup temp directory: {e}")
def _generate_output_files(
self,
audio_file_path: str,
text: str,
segments: List[Dict],
enable_speaker_diarization: bool
) -> Dict[str, str]:
"""Generate output files (TXT and SRT)"""
base_path = Path(audio_file_path).with_suffix("")
output_files = {}
# Generate TXT file
if text:
txt_file = f"{base_path}.txt"
with open(txt_file, 'w', encoding='utf-8') as f:
f.write(text)
output_files["txt_file"] = txt_file
# Generate SRT file
if segments:
srt_file = f"{base_path}.srt"
srt_content = self._generate_srt_content(segments, enable_speaker_diarization)
with open(srt_file, 'w', encoding='utf-8') as f:
f.write(srt_content)
output_files["srt_file"] = srt_file
return output_files
def _generate_srt_content(self, segments: List[Dict], include_speakers: bool = False) -> str:
"""Generate SRT format content from segments"""
srt_lines = []
for i, segment in enumerate(segments, 1):
start_time = self._format_timestamp(segment.get('start', 0))
end_time = self._format_timestamp(segment.get('end', 0))
text = segment.get('text', '').strip()
if include_speakers and segment.get('speaker'):
text = f"[{segment['speaker']}] {text}"
srt_lines.append(f"{i}")
srt_lines.append(f"{start_time} --> {end_time}")
srt_lines.append(text)
srt_lines.append("") # Empty line between segments
return "\n".join(srt_lines)
def _format_timestamp(self, seconds: float) -> str:
"""Format timestamp for SRT format"""
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
secs = int(seconds % 60)
milliseconds = int((seconds % 1) * 1000)
return f"{hours:02d}:{minutes:02d}:{secs:02d},{milliseconds:03d}"
def _create_error_result(self, audio_file_path: str, model_size: str, error_message: str) -> Dict[str, Any]:
"""Create error result dictionary"""
return {
"txt_file_path": None,
"srt_file_path": None,
"audio_file": audio_file_path,
"model_used": model_size,
"segment_count": 0,
"audio_duration": 0,
"processing_status": "failed",
"saved_files": [],
"speaker_diarization_enabled": False,
"global_speaker_count": 0,
"speaker_summary": {},
"error_message": error_message
} |