Created
July 19, 2023 14:00
-
-
Save eschmidbauer/2500364e48bf0b812c749ab765681a44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing | |
from concurrent.futures import ProcessPoolExecutor, as_completed | |
# !pip install -q torchaudio | |
SAMPLING_RATE = 16000 | |
import torch | |
from pprint import pprint | |
torch.set_num_threads(1) | |
NUM_PROCESS=4 # set to the number of CPU cores in the machine | |
model, utils = torch.hub.load(repo_or_dir='snakers4/silero-vad', | |
model='silero_vad', | |
force_reload=True, | |
onnx=False) | |
(get_speech_timestamps, | |
save_audio, | |
read_audio, | |
VADIterator, | |
collect_chunks) = utils | |
vad_models = dict() | |
def init_model(model): | |
pid = multiprocessing.current_process().pid | |
model, _ = torch.hub.load(repo_or_dir='snakers4/silero-vad', | |
model='silero_vad', | |
force_reload=False, | |
onnx=False) | |
vad_models[pid] = model | |
def vad_process(audio_file: str): | |
pid = multiprocessing.current_process().pid | |
with torch.no_grad(): | |
wav = read_audio(audio_file, sampling_rate=SAMPLING_RATE) | |
return get_speech_timestamps( | |
wav, | |
vad_models[pid], | |
0.46, # speech prob threshold | |
16000, # sample rate | |
300, # min speech duration in ms | |
20, # max speech duration in seconds | |
600, # min silence duration | |
512, # window size | |
200, # spech pad ms | |
) | |
futures = [] | |
with ProcessPoolExecutor(max_workers=NUM_PROCESS, initializer=init_model, initargs=(model,)) as ex: | |
for i in ["file1.wav","file2.wav"]: | |
futures.append(ex.submit(vad_process, i)) | |
for finished in as_completed(futures): | |
pprint(finished.result()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment