whisper-diarization

repository·main·Indexed 26 days ago

https://github.com/mahmoudashraf97/whisper-diarization

A speaker diarization pipeline that integrates OpenAI Whisper for ASR with Nvidia NeMo for speaker identification and MarbleNet for VAD. The pipeline provides word-level speaker timestamps through a workflow involving vocal extraction via Demucs, transcription, ctc-forced-aligner for timestamp correction, TitaNet for embedding extraction, and punctuation-based realignment.

Tokens
2K
Snippets
6
Records
11
Agent score
41%

What's inside whisper-diarization

  1. Overview of Whisper Diarization Pipeline

    main

    This project is a speaker diarization pipeline that combines OpenAI Whisper ASR with Voice Activity Detection (VAD) and Speaker Embedding.

    The pipeline workflow is as follows:

    1. Vocal Extraction: Vocals are extracted from the audio to improve speaker embedding accuracy.
    2. Transcription: Whisper generates the initial transcription.
    3. Alignment: Timestamps are corrected and aligned using ctc-forced-aligner to minimize diarization errors caused by time shifts.
    4. VAD & Segmentation: Audio is passed through MarbleNet for VAD and segmentation to exclude silences.
    5. Embedding Extraction: TitaNet extracts speaker embeddings to identify speakers for each segment.
    6. Association & Realignment: Results are associated with ctc-forced-aligner timestamps to detect the speaker for each word, then realigned using punctuation models to compensate for minor time shifts.
  2. Install Whisper Diarization

    main

    Prerequisites

    • Python: Version 3.10 or higher is recommended. (Version 3.9 is supported but requires manual requirement installation).
    • Cython: Must be installed via pip install cython or sudo apt install cython3.
    • FFMPEG: Required for audio processing.

    Install FFMPEG by OS

    • Ubuntu/Debian: sudo apt update && sudo apt install ffmpeg
    • Arch Linux: sudo pacman -S ffmpeg
    • MacOS (Homebrew): brew install ffmpeg
    • Windows (Chocolatey): choco install ffmpeg
    • Windows (Scoop): scoop install ffmpeg
    • Windows (WinGet): winget install ffmpeg

    Install Python Requirements

    After installing prerequisites, install the project dependencies using the provided constraints file:

    pip install -c constraints.txt -r requirements.txt
    pip install cython
    # or
    sudo apt update && sudo apt install cython3
    
    # After ffmpeg installation:
    pip install -c constraints.txt -r requirements.txt
  3. Separate music from speech using Demucs

    main

    To improve diarization quality, you can isolate vocals from background music using Demucs. This helps the model identify speakers based on speech signals rather than musical characteristics. If the separation fails, the pipeline falls back to the original audio.

    if enable_stemming:
        # Isolate vocals from the rest of the audio
        return_code = os.system(
            f'python -m demucs.separate -n htdemucs --two-stems=vocals "{audio_path}" -o "temp_outputs" --device "{device}"'
        )
    
        if return_code != 0:
            logging.warning("Source splitting failed, using original audio file.")
            vocal_target = audio_path
        else:
            vocal_target = os.path.join(
                "temp_outputs",
                "htdemucs",
                os.path.splitext(os.path.basename(audio_path))[0],
                "vocals.wav",
            )
    else:
        vocal_target = audio_path
  4. Install dependencies for Whisper and NeMo Diarization

    main

    To use this pipeline, you must install faster-whisper, nemo-toolkit, demucs, deepmultilingualpunctuation, and ctc-forced-aligner. Note that nvidia-cudnn-cu12 should be uninstalled to avoid conflicts.

    !pip install "faster-whisper>=1.1.0"
    !pip install "nemo-toolkit[asr]>=2.dev"
    !pip install git+https://github.com/MahmoudAshraf97/demucs.git
    !pip install git+https://github.com/oliverguhr/deepmultilingualpunctuation.git
    !pip install git+https://github.com/MahmoudAshraf97/ctc-forced-aligner.git
    !pip uninstall -y nvidia-cudnn-cu12
  5. Realign speaker labels using punctuation

    main

    To fix cases where a sentence is split between two speakers (e.g., Speaker A starts a sentence and Speaker B finishes it with a short interjection), use the get_realigned_ws_mapping_with_punctuation function. It uses a punctuation model to determine the dominant speaker for a sentence based on punctuation marks like ., ?, or !.

    if info.language in punct_model_langs:
        # restoring punctuation in the transcript to help realign the sentences
        punct_model = PunctuationModel(model="kredor/punctuate-all")
    
    # ... (punctuation restoration logic)
    
    wsm = get_realigned_ws_mapping_with_punctuation(wsm)
    ssm = get_sentences_speaker_mapping(wsm, speaker_ts)
  6. Export transcriptions to TXT and SRT formats

    main

    Once processing is complete, you can export the speaker-aware transcript to a plain text file or a SubRip (.srt) subtitle file.

    # Export to TXT
    with open(f"{os.path.splitext(audio_path)[0]}.txt", "w", encoding="utf-8-sig") as f:
        get_speaker_aware_transcript(ssm, f)
    
    # Export to SRT
    with open(f"{os.path.splitext(audio_path)[0]}.srt", "w", encoding="utf-8-sig") as srt:
        write_srt(ssm, srt)
  7. Known Limitations of Whisper Diarization

    main

    Users should be aware of the following current limitations:

    • Overlapping Speakers: The pipeline does not yet address overlapping speakers. A potential future approach involves isolating single speakers via audio separation, though this increases computational cost.
    • General Errors: Users are encouraged to raise issues if errors are encountered.
  8. Command Line Options for diarize.py

    main

    The following flags are available for the diarization CLI:

    FlagDescription
    -a AUDIO_FILE_NAMEThe name of the audio file to be processed
    --no-stemDisables source separation
    --whisper-modelThe model to be used for ASR (default: medium.en)
    --suppress_numeralsTranscribes numbers in their pronounced letters instead of digits to improve alignment accuracy
    --deviceChoose which device to use (defaults to cuda if available)
    --languageManually select language (useful if detection fails)
    --batch-sizeBatch size for batched inference. Reduce if running out of memory; set to 0 for non-batched inference