Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silero VAD support #888

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ Bug finding and pull requests are also highly appreciated to keep this project g

* [ ] Add benchmarking code (TEDLIUM for spd/WER & word segmentation)

* [ ] Allow silero-vad as alternative VAD option
* [x] Allow silero-vad as alternative VAD option

* [ ] Improve diarization (word level). *Harder than first thought...*

Expand All @@ -281,7 +281,9 @@ Borrows important alignment code from [PyTorch tutorial on forced alignment](htt
And uses the wonderful pyannote VAD / Diarization https://github.com/pyannote/pyannote-audio


Valuable VAD & Diarization Models from [pyannote audio][https://github.com/pyannote/pyannote-audio]
Valuable VAD & Diarization Models from:
- [pyannote audio][https://github.com/pyannote/pyannote-audio]
- [silero vad][https://github.com/snakers4/silero-vad]

Great backend from [faster-whisper](https://github.com/guillaumekln/faster-whisper) and [CTranslate2](https://github.com/OpenNMT/CTranslate2)

Expand Down
4 changes: 2 additions & 2 deletions whisperx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .transcribe import load_model
from .alignment import load_align_model, align
from .audio import load_audio
from .diarize import assign_word_speakers, DiarizationPipeline
from .diarize import assign_word_speakers, DiarizationPipeline
from .asr import load_model
25 changes: 21 additions & 4 deletions whisperx/asr.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import os
import warnings
from typing import List, Union, Optional, NamedTuple

import ctranslate2
Expand All @@ -10,7 +9,7 @@
from transformers.pipelines.pt_utils import PipelineIterator

from .audio import N_SAMPLES, SAMPLE_RATE, load_audio, log_mel_spectrogram
from .vad import load_vad_model, merge_chunks
import whisperx.vads
from .types import TranscriptionResult, SingleSegment

def find_numeral_symbol_tokens(tokenizer):
Expand Down Expand Up @@ -183,7 +182,16 @@ def data(audio, segments):
# print(f2-f1)
yield {'inputs': audio[f1:f2]}

vad_segments = self.vad_model({"waveform": torch.from_numpy(audio).unsqueeze(0), "sample_rate": SAMPLE_RATE})
# Pre-process audio and merge chunks as defined by the respective VAD child class
# In case vad_model is manually assigned (see 'load_model') follow the functionality of pyannote toolkit
if issubclass(type(self.vad_model), whisperx.vads.Vad):
waveform = self.vad_model.preprocess_audio(audio)
merge_chunks = self.vad_model.merge_chunks
else:
waveform = whisperx.vads.Pyannote.preprocess_audio(audio)
merge_chunks = whisperx.vads.Pyannote.merge_chunks

vad_segments = self.vad_model({"waveform": waveform, "sample_rate": SAMPLE_RATE})
vad_segments = merge_chunks(
vad_segments,
chunk_size,
Expand Down Expand Up @@ -263,6 +271,7 @@ def load_model(whisper_arch,
asr_options=None,
language : Optional[str] = None,
vad_model=None,
vad_method=None,
vad_options=None,
model : Optional[WhisperModel] = None,
task="transcribe",
Expand All @@ -273,6 +282,7 @@ def load_model(whisper_arch,
whisper_arch: str - The name of the Whisper model to load.
device: str - The device to load the model on.
compute_type: str - The compute type to use for the model.
vad_method: str - The vad method to use. vad_model has higher priority if is not None.
options: dict - A dictionary of options to use for the model.
language: str - The language of the model. (use English for now)
model: Optional[WhisperModel] - The WhisperModel instance to use.
Expand Down Expand Up @@ -334,17 +344,24 @@ def load_model(whisper_arch,
default_asr_options = faster_whisper.transcribe.TranscriptionOptions(**default_asr_options)

default_vad_options = {
"chunk_size": 30, # needed by silero since binarization happens before merge_chunks
"vad_onset": 0.500,
"vad_offset": 0.363
}

if vad_options is not None:
default_vad_options.update(vad_options)

# Note: manually assigned vad_model has higher priority than vad_method!
if vad_model is not None:
print("Use manually assigned vad_model. vad_method is ignored.")
vad_model = vad_model
else:
vad_model = load_vad_model(torch.device(device), use_auth_token=None, **default_vad_options)
match vad_method:
case "silero":
vad_model = whisperx.vads.Silero(**default_vad_options)
case "pyannote" | _:
vad_model = whisperx.vads.Pyannote(torch.device(device), use_auth_token=None, **default_vad_options)

return FasterWhisperPipeline(
model=model,
Expand Down
4 changes: 3 additions & 1 deletion whisperx/transcribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def cli():
parser.add_argument("--return_char_alignments", action='store_true', help="Return character-level alignments in the output json file")

# vad params
parser.add_argument("--vad_method", type=str, default="pyannote", choices=["pyannote", "silero"], help="VAD method to be used")
parser.add_argument("--vad_onset", type=float, default=0.500, help="Onset threshold for VAD (see pyannote.audio), reduce this if speech is not being detected")
parser.add_argument("--vad_offset", type=float, default=0.363, help="Offset threshold for VAD (see pyannote.audio), reduce this if speech is not being detected.")
parser.add_argument("--chunk_size", type=int, default=30, help="Chunk size for merging VAD segments. Default is 30, reduce this if the chunk is too long.")
Expand Down Expand Up @@ -102,6 +103,7 @@ def cli():
return_char_alignments: bool = args.pop("return_char_alignments")

hf_token: str = args.pop("hf_token")
vad_method: str = args.pop("vad_method")
vad_onset: float = args.pop("vad_onset")
vad_offset: float = args.pop("vad_offset")

Expand Down Expand Up @@ -167,7 +169,7 @@ def cli():
results = []
tmp_results = []
# model = load_model(model_name, device=device, download_root=model_dir)
model = load_model(model_name, device=device, device_index=device_index, download_root=model_dir, compute_type=compute_type, language=args['language'], asr_options=asr_options, vad_options={"vad_onset": vad_onset, "vad_offset": vad_offset}, task=task, threads=faster_whisper_threads)
model = load_model(model_name, device=device, device_index=device_index, download_root=model_dir, compute_type=compute_type, language=args['language'], asr_options=asr_options, vad_method=vad_method, vad_options={"chunk_size":chunk_size, "vad_onset": vad_onset, "vad_offset": vad_offset}, task=task, threads=faster_whisper_threads)

for audio_path in args.pop("audio"):
audio = load_audio(audio_path)
Expand Down
3 changes: 3 additions & 0 deletions whisperx/vads/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from whisperx.vads.pyannote import Pyannote
from whisperx.vads.silero import Silero
from whisperx.vads.vad import Vad
Loading