### Install Dependencies and Clone Repository (Alternative) Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Installs PyTorch and other dependencies, then clones the silero-models repository. This is an alternative setup method. ```python #@title Install Dependencies # this assumes that you have a relevant version of PyTorch installed !pip install -q torchaudio omegaconf import os from os.path import exists if not exists('silero-models'): !git clone -q --depth 1 https://github.com/snakers4/silero-models %cd silero-models import torch import random from glob import glob from omegaconf import OmegaConf from src.silero.utils import ( init_jit_model, split_into_batches, read_batch, prepare_model_input) from IPython.display import display, Audio ``` -------------------------------- ### Install PyTorch and Torchaudio Locally Source: https://github.com/snakers4/silero-models/blob/master/examples_denoise.ipynb Installs specific versions of PyTorch and Torchaudio, required for running the local denoising example. ```python #@title Install dependencies !pip install -q torch==2.0.0 torchaudio ``` -------------------------------- ### Install Dependencies and Load Model Configuration Source: https://github.com/snakers4/silero-models/blob/master/examples_te.ipynb Installs necessary libraries and downloads the latest model configuration from a remote URL. This is the initial setup step for using the Silero text enhancement models. ```python #@title Install dependencies import os import yaml import torch from torch import package torch.hub.download_url_to_file('https://raw.githubusercontent.com/snakers4/silero-models/master/models.yml', 'latest_silero_models.yml', progress=False) with open('latest_silero_models.yml', 'r') as yaml_file: models = yaml.load(yaml_file, Loader=yaml.SafeLoader) model_conf = models.get('te_models').get('latest') ``` -------------------------------- ### Install and Import Dependencies Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Installs necessary libraries and imports PyTorch, ONNX, and OmegaConf. Assumes a relevant PyTorch version is already installed. ```python #@title Install and Import Dependencies # this assumes that you have a relevant version of PyTorch installed !pip install -q torchaudio omegaconf onnx onnxruntime ``` -------------------------------- ### Install Dependencies and Load Models Source: https://github.com/snakers4/silero-models/blob/master/examples_denoise.ipynb Installs necessary libraries and loads the latest Silero models configuration from a URL. ```python #@title Install dependencies !pip install -q torchaudio omegaconf import torch from omegaconf import OmegaConf from IPython.display import Audio, display torch.hub.download_url_to_file('https://raw.githubusercontent.com/snakers4/silero-models/master/models.yml', 'latest_silero_models.yml', progress=False) models = OmegaConf.load('latest_silero_models.yml') ``` -------------------------------- ### Install Dependencies Source: https://github.com/snakers4/silero-models/blob/master/examples_tts.ipynb Installs the necessary PyTorch version for local execution. Ensure you have a compatible environment. ```python #@title Install dependencies !pip install -q torch==1.12 ``` -------------------------------- ### Install silero-stress Package Source: https://github.com/snakers4/silero-models/blob/master/examples_tts_cis.ipynb Installs the silero-stress package quietly using pip. ```bash !pip install -q silero-stress ``` -------------------------------- ### Install Silero TTS Source: https://github.com/snakers4/silero-models/blob/master/examples_tts_cis.ipynb Installs the silero library using pip. This is the first step before importing and using the TTS functionalities. ```python #@title Install dependencies !pip install -q silero import torch from IPython.display import Audio, display ``` -------------------------------- ### Install Dependencies and Load Models Source: https://github.com/snakers4/silero-models/blob/master/examples_tts.ipynb Installs the 'omegaconf' library and downloads the latest Silero models configuration file. This is a prerequisite for using the Silero models. ```python #@title Install dependencies !pip install -q omegaconf import torch from pprint import pprint from omegaconf import OmegaConf from IPython.display import Audio, display torch.hub.download_url_to_file('https://raw.githubusercontent.com/snakers4/silero-models/master/models.yml', 'latest_silero_models.yml', progress=False) models = OmegaConf.load('latest_silero_models.yml') ``` -------------------------------- ### Install Dependencies and Clone Repository Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Installs necessary Python packages and clones the silero-models repository. This is a prerequisite for using the provided models. ```python #@title Install dependencies !pip install -q omegaconf torchaudio pydub import os from os.path import exists if not exists('silero-models'): !git clone -q --depth 1 https://github.com/snakers4/silero-models %cd silero-models # silero imports import torch import random from glob import glob from omegaconf import OmegaConf from src.silero.utils import ( init_jit_model, split_into_batches, read_audio, read_batch, prepare_model_input) from colab_utils import ( record_audio, audio_bytes_to_np, upload_audio) device = torch.device('cpu') # you can use any pytorch device models = OmegaConf.load('models.yml') # imports for uploading/recording import numpy as np import ipywidgets as widgets from scipy.io import wavfile from IPython.display import Audio, display, clear_output from torchaudio.functional import vad # wav to text method def wav_to_text(f='test.wav'): batch = read_batch([f]) input = prepare_model_input(batch, device=device) output = model(input) return decoder(output[0].cpu()) ``` -------------------------------- ### Local Denoising Example with JIT Model Source: https://github.com/snakers4/silero-models/blob/master/examples_denoise.ipynb Loads a JIT-compiled denoising model locally, processes a sample audio file, and displays the original and denoised output. Requires downloading the model file if it doesn't exist. ```python import os import torch import torchaudio def read_audio( path: str, sampling_rate: int = 24000 ): wav, sr = torchaudio.load(path) if wav.size(0) > 1: wav = wav.mean(dim=0, keepdim=True) if sr != sampling_rate: transform = torchaudio.transforms.Resample( orig_freq=sr, new_freq=sampling_rate ) wav = transform(wav) sr = sampling_rate assert sr == sampling_rate return wav * 0.95 device = torch.device('cpu') torch.set_num_threads(4) local_file = 'model.pt' if not os.path.isfile(local_file): torch.hub.download_url_to_file('https://models.silero.ai/denoise_models/sns_latest.jit', local_file) model = torch.jit.load(local_file) torch._C._jit_set_profiling_mode(False) torch.set_grad_enabled(False) model.to(device) torch.hub.download_url_to_file('https://models.silero.ai/denoise_models/sample1.wav', dst=f'sample1.wav', progress=True) audio_path = 'sample1.wav' a = read_audio(audio_path) a = a.to(device) out = model(a) print('Original: ') display(Audio(a.cpu(), rate=24000)) print('Output: ') display(Audio(out.cpu().squeeze(1).detach().numpy(), rate=48000)) ``` -------------------------------- ### Install and Use Silero TTS via pip Source: https://github.com/snakers4/silero-models/blob/master/README.md Use this snippet to integrate Silero TTS models into your project using the pip package. It demonstrates loading a Russian model and applying it to example text. ```python from silero import silero_tts model, example_text = silero_tts(language='ru', speaker='v5_ru') audio = model.apply_tts(text=example_text) ``` -------------------------------- ### Install FastText Language Detection Libraries Source: https://github.com/snakers4/silero-models/blob/master/examples_te.ipynb Installs the `fasttext-langdetect` and `wget` Python packages, which are required for automatic language detection before text enhancement. ```python ! pip install fasttext-langdetect ! pip install wget ``` -------------------------------- ### Download and Process Single Audio File (Torch Hub) Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Downloads a single audio file using torch.hub.download_url_to_file and prepares it for model input. This is a basic setup for testing. ```python torch.hub.download_url_to_file('https://opus-codec.org/static/examples/samples/speech_orig.wav', dst ='speech_orig.wav', progress=True) test_files = glob('speech_orig.wav') batches = split_into_batches(test_files, batch_size=10) input = prepare_model_input(read_batch(batches[0]), device=device) output = model(input) for example in output: print(decoder(example.cpu())) ``` -------------------------------- ### Example Texts for Various Languages Source: https://github.com/snakers4/silero-models/blob/master/examples_tts_cis.ipynb A dictionary containing example sentences for different languages, used for testing TTS models. These texts can be used to verify model output and pronunciation. ```python val_texts = { 'aze': 'Mən hər səhər erkən qalxıb təzə hava ilə məşq edirəm.', 'bak': 'Күп балалыларға былайҙа сертификат бирелә бит.', 'bel': 'В+ечарам +я любл+ю чыт+аць цік+авыя кн+ігі пры святл+е начнік+а.', 'chv': 'Эпĕ ача чухнех пиччĕшсемпе юнашар кĕтӳльех вăйă вылянă.', 'erz': 'Монь веленек шачемсёномань панжовксонть кис эрьва кизонь туема.', 'hye': 'Ես շաբաթ օրերին սիրում եմ երկար զբոսնել անտառով:', 'kat': 'მე ძალიან მიყვარს ჩემი ოჯახის წევრებთან ერთად დროის გატარება.', 'kaz': 'Мен балалық шақта жаңа досдармен танысуды әбден ұнататынмын.', 'kbd': 'Сэ уиӀуанэ уашъхъэри унагъуэхэри сэбэп хъущтыр сыту щӀэлъэӀу.', 'kir': 'Мен мектепте окуп жүргөндө эң жакшы досум менен тааныштым.', 'kjh': 'Мин аал чоньчарға пастабахсынар хайдиғырам хынаңның хоный.', 'mdf': 'Монь тяштеть эзда кизонь карьхть сельметь кштинь аф лац.', 'sah': 'Мин бүгүн оройунан саһарҕа оонньуу сылдьан сымнаҕыстык утуйбутум.', 'tat': 'Мин ерак түгел урман эчендә чиста һавада йөргәне яратам.', 'tgk': 'Ман дар бораи хонаи нави худ дар канори дарё хондем.', 'udm': 'Мон ашалэ тӥлед нуналлы огы быдэсэ кошко учке.', 'ukr': '+Я з р+аннього дит+инства д+уже любл+ю сл+ухати цік+аві к+азки.', 'uzb': "Men bolaligimda ko'pincha do'stlarim bilan hovlida futbol o'ynardim.", 'xal': 'Би эцкд сарин җилин дуулҗана хойр седклтә күрәм.' } ``` -------------------------------- ### Load and Use V5 TTS Model via Torch Hub Source: https://github.com/snakers4/silero-models/blob/master/README.md Demonstrates loading a V5 Russian TTS model from Torch Hub and applying it to generate audio. Ensure PyTorch 1.10+ is installed. ```python # V5 import torch language = 'ru' model_id = 'v5_ru' sample_rate = 48000 speaker = 'xenia' device = torch.device('cpu') model, example_text = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_tts', language=language, speaker=model_id) model.to(device) # gpu or cpu audio = model.apply_tts(text=example_text, speaker=speaker, sample_rate=sample_rate) ``` -------------------------------- ### Break Tag Example Source: https://github.com/snakers4/silero-models/wiki/SSML Use the `` tag to introduce pauses. You can specify duration in milliseconds or seconds, or use predefined strengths. ```xml Пауза длиной в три секунды После этого речь продолжается. ``` -------------------------------- ### Synthesize Speech (Uzbek Cyrillic) Source: https://github.com/snakers4/silero-models/blob/master/examples_tts_cis.ipynb Generates audio from Uzbek text written in Cyrillic script using the 'uzb_saida' speaker. The example text demonstrates sentence structure and common words in Uzbek. The resulting audio is then displayed. ```python # uzbek cyrillic sample_rate = 48000 speaker = 'uzb_saida' example_text = 'Бунинг устида ишлаш керак. бир йил эмас. кўп йил ишлаш керак.' audio = model.apply_tts(text=example_text, speaker=speaker, sample_rate=sample_rate) print(example_text) display(Audio(audio, rate=sample_rate)) ``` -------------------------------- ### Enhance Example Text Source: https://github.com/snakers4/silero-models/blob/master/examples_te.ipynb Applies the text enhancement function to the first example text provided by the model and prints both the input and output. This demonstrates basic usage of the `apply_te` function. ```python input_text = model.examples[0] output_text = apply_te(input_text, lan='en') print(f"Input: \n{input_text}\nOutput:\n{output_text}") ``` -------------------------------- ### Load and Use TTS Models with pip Package Source: https://context7.com/snakers4/silero-models/llms.txt Demonstrates loading TTS models using the silero pip package, offering a cleaner API without PyTorch Hub dependency management. Includes examples for both Russian and English TTS. ```python from silero import silero_tts # Load model model, example_text = silero_tts( language='ru', speaker='v5_ru' ) # Generate audio tensor audio = model.apply_tts( text=example_text, speaker='xenia', sample_rate=48000 ) # For English TTS model_en, _ = silero_tts(language='en', speaker='v3_en') audio_en = model_en.apply_tts( text='Can you can a canned can into an un-canned can?', speaker='en_0', sample_rate=48000 ) ``` -------------------------------- ### Synthesize Speech with Stress Markers (Russian) Source: https://github.com/snakers4/silero-models/blob/master/examples_tts_cis.ipynb Generates audio from Russian text using the 'ru_zhadyra' speaker. The example text includes stress markers (e.g., '+окнами') which are crucial for correct pronunciation. The synthesized audio is then displayed. ```python # v5_cis_base_nostress sample_rate = 48000 speaker = 'ru_zhadyra' example_text = 'брод+ить с дожд+ём п+од +окнами тво+ими.' audio = model.apply_tts(text=example_text, speaker=speaker, sample_rate=sample_rate) print(example_text) display(Audio(audio, rate=sample_rate)) ``` -------------------------------- ### Synthesize Speech with Stress Markers (Ukrainian) Source: https://github.com/snakers4/silero-models/blob/master/examples_tts_cis.ipynb Generates audio from Ukrainian text using the 'ukr_igor' speaker. The example text includes stress markers (e.g., '+Я') which are important for accurate pronunciation with this model. The audio is then displayed. ```python # v5_cis_base_nostress sample_rate = 48000 speaker = 'ukr_igor' example_text = '+Я з р+аннього дит+инства д+уже любл+ю сл+ухати цік+аві к+азки.' audio = model.apply_tts(text=example_text, speaker=speaker, sample_rate=sample_rate) print(example_text) display(Audio(audio, rate=sample_rate)) ``` -------------------------------- ### Transcribe a Set of Files Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Transcribes speech from a randomly selected batch of audio files. It prepares the input, runs the model, and prints the decoded text for each example. ```python # transcribe a set of files input = prepare_model_input(read_batch(random.sample(batches, k=1)[0]), device=device) output = model(input) for example in output: print(decoder(example.cpu())) ``` -------------------------------- ### Load TTS Model with torch.hub Source: https://github.com/snakers4/silero-models/blob/master/examples_tts_cis.ipynb Loads a Silero TTS model directly from GitHub using torch.hub without needing to install the library via pip. This method requires specifying the repository and model name, along with language and speaker details. The model is then moved to the appropriate device. ```python device = torch.device('cpu') model, _ = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_tts', language='ru', speaker='v5_cis_base_nostress', force_reload=True) model.to(device) # gpu or cpu ``` -------------------------------- ### Load Silero Text Enhancer Model via Torch Hub Source: https://github.com/snakers4/silero-models/blob/master/examples_te.ipynb Loads the Silero text enhancement model directly from the 'snakers4/silero-models' repository using `torch.hub.load`. This method provides access to the model, example texts, supported languages, punctuation, and the enhancement function. ```python import torch model, example_texts, languages, punct, apply_te = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_te') ``` -------------------------------- ### Download Audio File and Prepare Input Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Downloads a sample audio file and prepares it as input for the model. This involves reading the audio and formatting it. ```python # download a single file, any format compatible with TorchAudio torch.hub.download_url_to_file('https://opus-codec.org/static/examples/samples/speech_orig.wav', dst ='speech_orig.wav', progress=True) test_files = ['speech_orig.wav'] batches = split_into_batches(test_files, batch_size=10) input = prepare_model_input(read_batch(batches[0])) ``` -------------------------------- ### Prepare Model Input from Files Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Prepares model input by reading a batch of audio files and converting them to the required format. Assumes 'glob' has found relevant WAV files. ```python test_files = glob('*.wav') # replace with your data batches = split_into_batches(test_files, batch_size=10) ``` -------------------------------- ### Initialize SimpleAccentor for Various Languages Source: https://github.com/snakers4/silero-models/blob/master/examples_tts_cis.ipynb Initializes SimpleAccentor for multiple languages, demonstrating its use with provided sample texts. ```python from silero_stress.simple_accentor import SimpleAccentor ``` ```python sample_texts = { # if you need "aze" language, you need to specify which layout do you use - latin or cyrillic 'aze_lat': 'Mən hər səhər erkən qalxıb təzə hava ilə məşq edirəm.', 'aze_cyr': 'Мән һәр сәһәр еркән галхыб тәзә һава ылә мәшг едырәм.', 'bak': 'Күп балалыларға былайҙа сертификат бирелә бит.', 'bel': 'Вечарам я люблю чытаць цікавыя кнігі пры святле начніка.', 'chv': 'Эпĕ ача чухнех пиччĕшсемпе юнашар кĕтӳльех вăйă вылянă.', 'erz': 'Монь веленек шачемсёномань панжовксонть кис эрьва кизонь туема.', 'hye': 'Ես շաբաթ օրերին սիրում եմ երկար զբոսնել անտառով:', 'kat': 'მე ძალიან მიყვარს ჩემი ოჯახის წევრებთან ერთად დროის გატარება.', 'kaz': 'Мен балалық шақта жаңа досдармен танысуды әбден ұнататынмын.', 'kbd': 'Сэ уиӀуанэ уашъхъэри унагъуэхэри сэбэп хъущтыр сыту щӀэлъэӀу.', 'kir': 'Мен мектепте окуп жүргөндө эң жакшы досум менен тааныштым.', 'kjh': 'Мин аал чоньчарға пастабахсынар хайдиғырам хынаңның хоный.', 'mdf': 'Монь тяштеть эзда кизонь карьхть сельметь кштинь аф лац.', 'sah': 'Мин бүгүн оройунан саһарҕа оонньуу сылдьан сымнаҕыстык утуйбутум.', 'tat': 'Мин ерак түгел урман эчендә чиста һавада йөргәне яратам.', 'tgk': 'Ман дар бораи хонаи нави худ дар канори дарё хондем.', 'udm': 'Мон ашалэ тӥлед нуналлы огы быдэсэ кошко учке.', # if you need "uzb" language, you need to specify which layout do you use - latin or cyrillic 'uzb_lat': "Men bolaligimda ko'pincha do'stlarim bilan hovlida futbol o'ynardim.", 'uzb_cyr': "Мен болалигимда кўпинча дўстларим билан ҳовлида футбол ўйнардим.", 'xal': 'Би эцкд сарин җилин дуулҗана хойр седклтә күрәм.' } for lang in sample_texts: accentor = SimpleAccentor(lang=lang) print(sample_texts[lang]) print(accentor(sample_texts[lang])) print() ``` -------------------------------- ### Denoise Audio using Loaded Model (Method 1) Source: https://github.com/snakers4/silero-models/blob/master/examples_denoise.ipynb Downloads a sample audio file, reads it, applies the loaded denoising model, and saves the output. Displays original and denoised audio. ```python i = 0 torch.hub.download_url_to_file( samples[i], dst=f'sample{i}.wav', progress=True ) audio_path = f'sample{i}.wav' audio = read_audio(audio_path).to(device) print('Original: ') display(Audio(audio.cpu(), rate=24000)) output = model(audio) save_audio(f'result{i}.wav', output.squeeze(1).cpu()) print('Saved: ') display(Audio(f'result{i}.wav')) print('Output: ') display(Audio(output.squeeze(1).cpu(), rate=48000)) ``` -------------------------------- ### Denoise Audio using Loaded Model (Method 2) Source: https://github.com/snakers4/silero-models/blob/master/examples_denoise.ipynb Downloads a sample audio file, then uses the 'denoise' utility function to process it and save the result. Displays original and denoised audio. ```python i = 1 torch.hub.download_url_to_file( samples[i], dst=f'sample{i}.wav', progress=True ) print('Original: ') display(Audio(f'sample{i}.wav')) output, sr = denoise(model, f'sample{i}.wav', f'result{i}.wav', device='cpu') print('Saved: ') display(Audio(f'result{i}.wav')) print('Output: ') display(Audio(output.cpu(), rate=sr)) ``` -------------------------------- ### Save Audio with Silero Model Source: https://context7.com/snakers4/silero-models/llms.txt Demonstrates how to use a Silero model to generate and save audio from text. Requires the model to be loaded first. ```python audio_paths = model.save_wav( text='Standalone model usage without internet.', speaker='baya', sample_rate=48000 ) # Output: Creates 'test.wav' in current directory ``` -------------------------------- ### Speak Tag Example Source: https://github.com/snakers4/silero-models/wiki/SSML The `` tag is the root element for all SSML content. ```xml В недрах тундры выдры в г+етрах т+ырят в вёдра ядра к+едров. ``` -------------------------------- ### Sentence Tag Example Source: https://github.com/snakers4/silero-models/wiki/SSML The `` tag represents a sentence, equivalent to a `strong` pause. ```xml Первое предложение.Второе предложение. ``` -------------------------------- ### Paragraph Tag Example Source: https://github.com/snakers4/silero-models/wiki/SSML The `

` tag denotes a paragraph, equivalent to an `x-strong` pause. ```xml

Первый параграф.

Второй параграф.

``` -------------------------------- ### Load ONNX Model and Initialize Session Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Downloads the ONNX model file, checks its validity, and initializes an ONNX Runtime inference session for efficient execution. ```python # load the actual ONNX model torch.hub.download_url_to_file(models.stt_models.en.latest.onnx, 'model.onnx', progress=True) onnx_model = onnx.load('model.onnx') onnx.checker.check_model(onnx_model) ort_session = onnxruntime.InferenceSession('model.onnx') ``` -------------------------------- ### Initialize JIT Model Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Initializes a JIT-compiled model and its corresponding decoder for speech-to-text tasks. Specify the device (e.g., 'cpu') for model execution. ```python device = torch.device('cpu') # you can use any pytorch device model, decoder = init_jit_model(models.stt_models.en.latest.jit, device=device) ``` -------------------------------- ### Get Available Speakers Source: https://github.com/snakers4/silero-models/blob/master/examples_tts_cis.ipynb Prints a sorted list of available speaker IDs for the loaded TTS model. This helps in selecting the correct speaker for a desired language or voice. ```python sorted(model.speakers) ``` -------------------------------- ### List Available Denoising Models Source: https://github.com/snakers4/silero-models/blob/master/examples_denoise.ipynb Prints the available denoising models and their specific variants from the loaded configuration. ```python # see latest avaiable models available_models = models.denoise_models.models print(f'Available models {available_models}') for am in available_models: _models = list(models.denoise_models.get(am).keys()) print(f'Available models for {am}: {_models}') ``` -------------------------------- ### Initialize Speech-to-Text Model Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Initializes the speech-to-text model based on the selected language. Ensure the language parameter matches available models. ```python #@markdown { run: "auto" } language = "English" #@param ["English", "German", "Spanish"] print(language) if language == 'German': model, decoder = init_jit_model(models.stt_models.de.latest.jit, device=device) elif language == "Spanish": model, decoder = init_jit_model(models.stt_models.es.latest.jit, device=device) else: model, decoder = init_jit_model(models.stt_models.en.latest.jit, device=device) ``` -------------------------------- ### Speech-to-Text with Word Alignment Source: https://context7.com/snakers4/silero-models/llms.txt Obtain word-level timestamps for transcribed text using the Silero STT model. Requires audio file 'speech.wav' and PyTorch Hub setup. ```python import torch device = torch.device('cpu') model, decoder, utils = torch.hub.load( repo_or_dir='snakers4/silero-models', model='silero_stt', language='en', device=device ) (read_batch, split_into_batches, read_audio, prepare_model_input) = utils # Read audio file audio = read_audio('speech.wav') batch = [audio] input_tensor = prepare_model_input(batch, device=device) # Calculate audio length in seconds wav_len = input_tensor.shape[1] / 16000 # Get transcription with word alignment output = model(input_tensor) transcription, alignments = decoder(output[0].cpu(), wav_len, word_align=True) print(transcription) for word_info in alignments: print(f"{word_info['word']}: {word_info['start_ts']}s - {word_info['end_ts']}s") # Output: # hello world # {'word': 'hello', 'start_ts': 0.12, 'end_ts': 0.45} # {'word': 'world', 'start_ts': 0.52, 'end_ts': 0.89} ``` -------------------------------- ### ONNX Inference and Decoding Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Performs inference using the ONNX session and decodes the output to get the transcribed text. Converts PyTorch tensor to NumPy array for ONNX. ```python # actual onnx inference and decoding onnx_input = input.detach().cpu().numpy() ort_inputs = {'input': onnx_input} ort_outs = ort_session.run(None, ort_inputs) decoded = decoder(torch.Tensor(ort_outs[0])[0]) print(decoded) ``` -------------------------------- ### Audio Denoising Standalone Source: https://context7.com/snakers4/silero-models/llms.txt Runs audio denoising locally without PyTorch Hub, loading a pre-downloaded JIT model. Ensure 'noisy_audio.wav' exists. ```python import os import torch import torchaudio def read_audio(path: str, sampling_rate: int = 24000): wav, sr = torchaudio.load(path) if wav.size(0) > 1: wav = wav.mean(dim=0, keepdim=True) if sr != sampling_rate: transform = torchaudio.transforms.Resample(orig_freq=sr, new_freq=sampling_rate) wav = transform(wav) return wav * 0.95 device = torch.device('cpu') torch.set_num_threads(4) local_file = 'denoise_model.pt' # Download model if not os.path.isfile(local_file): torch.hub.download_url_to_file( 'https://models.silero.ai/denoise_models/sns_latest.jit', local_file ) # Load and configure model model = torch.jit.load(local_file) torch._C._jit_set_profiling_mode(False) torch.set_grad_enabled(False) model.to(device) # Process audio audio = read_audio('noisy_audio.wav').to(device) denois = model(audio) # Save result (output is 48kHz) torchaudio.save('clean_audio.wav', denoised.squeeze(1).cpu(), 48000) ``` -------------------------------- ### Load TTS Model with pip Source: https://github.com/snakers4/silero-models/blob/master/examples_tts_cis.ipynb Loads a Silero TTS model using the silero_tts function after installation. Specify the language and speaker model ID. The model is then moved to the appropriate device (CPU or GPU). ```python from silero import silero_tts model_id = 'v5_cis_base_nostress' device = torch.device('cpu') model, example_text = silero_tts(language='ru', speaker=model_id) model.to(device) # gpu or cpu ``` -------------------------------- ### Listen to One File and Display Audio Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Processes a single audio file, transcribes it, and plays the audio using the `display` function. Assumes audio was resampled to 16kHz. ```python # listen to one file batch = read_batch(random.sample(batches, k=1)[0]) input = prepare_model_input(batch, device=device) output = model(input) for i, example in enumerate(output): print(decoder(example.cpu())) display(Audio(batch[i], rate=16000)) # audio was resampled to 16kHz break ``` -------------------------------- ### Download and Use V5 TTS Model Standalone Source: https://github.com/snakers4/silero-models/blob/master/README.md Shows how to download a V5 Russian TTS model file and use it for standalone audio generation. Requires PyTorch 1.12+ and Python Standard Library. ```python # V5 import os import torch device = torch.device('cpu') torch.set_num_threads(4) local_file = 'model.pt' if not os.path.isfile(local_file): torch.hub.download_url_to_file('https://models.silero.ai/models/tts/ru/v5_ru.pt', local_file) model = torch.package.PackageImporter(local_file).load_pickle("tts_models", "model") model.to(device) example_text = 'Меня зовут Лева Королев. Я из готов. И я уже готов открыть все ваши замки любой сложности!' sample_rate = 48000 speaker='baya' audio_paths = model.save_wav(text=example_text, speaker=speaker, sample_rate=sample_rate) ``` -------------------------------- ### Load and Use Russian TTS Model with PyTorch Hub Source: https://context7.com/snakers4/silero-models/llms.txt Loads a Russian TTS model (v5) using PyTorch Hub. Demonstrates how to set the device, list available speakers, generate speech with specific parameters, and save the output to a WAV file. Ensure the correct language and speaker ID are used. ```python import torch # Load Russian TTS model (v5) language = 'ru' model_id = 'v5_5_ru' device = torch.device('cpu') model, example_text = torch.hub.load( repo_or_dir='snakers4/silero-models', model='silero_tts', language=language, speaker=model_id ) model.to(device) # List available speakers print(model.speakers) # ['aidar', 'baya', 'kseniya', 'xenia', 'eugene'] # Generate speech sample_rate = 48000 speaker = 'xenia' text = 'Hello, this is a test of Silero text to speech synthesis.' audio = model.apply_tts( text=text, speaker=speaker, sample_rate=sample_rate, put_accent=True, # Auto stress for Russian put_yo=True # Auto yo letters for Russian ) # Save to file model.save_wav( text=text, speaker=speaker, sample_rate=sample_rate ) # Output: saves 'test.wav' file ``` -------------------------------- ### Prosody Tag Example Source: https://github.com/snakers4/silero-models/wiki/SSML The `` tag allows modification of speech rate and pitch. Use predefined values like `x-slow`, `slow`, `medium`, `fast`, `x-fast` for rate, and `x-low`, `low`, `medium`, `high`, `x-high` for pitch. ```xml Когда я просыпаюсь, я говорю довольно медленно. Потом я начинаю говорить своим обычным голосом, а могу говорить тоном выше. ``` -------------------------------- ### Load TTS Model from Local File for Offline Use Source: https://context7.com/snakers4/silero-models/llms.txt Shows how to download and load a TTS model from a local file, enabling offline or air-gapped usage without PyTorch Hub. This requires pre-downloading the model file. ```python import os import torch device = torch.device('cpu') torch.set_num_threads(4) local_file = 'v5_ru.pt' # Download model once if not os.path.isfile(local_file): torch.hub.download_url_to_file( 'https://models.silero.ai/models/tts/ru/v5_ru.pt', local_file ) # Load from local file model = torch.package.PackageImporter(local_file).load_pickle("tts_models", "model") model.to(device) ``` -------------------------------- ### Download Model and Initialize Enhancer Source: https://github.com/snakers4/silero-models/blob/master/examples_te.ipynb Downloads the text enhancement model package if it doesn't exist locally, then imports and loads the model using `PackageImporter`. It also defines a helper function `apply_te` for enhancing text. ```python model_url = model_conf.get('package') model_dir = "downloaded_model" os.makedirs(model_dir, exist_ok=True) model_path = os.path.join(model_dir, os.path.basename(model_url)) if not os.path.isfile(model_path): torch.hub.download_url_to_file(model_url, model_path, progress=True) imp = package.PackageImporter(model_path) model = imp.load_pickle("te_model", "model") example_texts = model.examples def apply_te(text, lan='en'): return model.enhance_text(text, lan) ``` -------------------------------- ### Load STT Model and Utilities Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Loads the Silero STT model and associated utilities for a specified language. It also downloads the models.yml file to list available models. ```python import onnx import torch import onnxruntime from omegaconf import OmegaConf language = 'en' # also available 'de', 'es' # load provided utils _, decoder, utils = torch.hub.load(repo_or_dir='snakers4/silero-models', model='silero_stt', language=language) (read_batch, split_into_batches, read_audio, prepare_model_input) = utils # see available models torch.hub.download_url_to_file('https://raw.githubusercontent.com/snakers4/silero-models/master/models.yml', 'models.yml') models = OmegaConf.load('models.yml') available_languages = list(models.stt_models.keys()) assert language in available_languages ``` -------------------------------- ### Load Silero Denoising Model via PyTorch Hub Source: https://github.com/snakers4/silero-models/blob/master/examples_denoise.ipynb Loads a specified Silero denoising model ('silero_denoise') using PyTorch Hub. Ensure the 'name' parameter matches an available model. ```python import torch name = 'small_slow' # 'large_fast', 'small_fast' device = torch.device('cpu') model, samples, utils = torch.hub.load( repo_or_dir='snakers4/silero-models', model='silero_denoise', name=name, device=device) (read_audio, save_audio, denoise) = utils model.to(device) # gpu or cpu ``` -------------------------------- ### Download Silero Text Enhancement Model for Offline Use Source: https://context7.com/snakers4/silero-models/llms.txt Download the Silero text enhancement model directly for offline use. Ensures the model file is present in the specified directory. ```python import os import torch from torch import package # Download model model_url = 'https://models.silero.ai/te_models/v2_4lang_q.pt' model_dir = "downloaded_model" os.makedirs(model_dir, exist_ok=True) model_path = os.path.join(model_dir, 'v2_4lang_q.pt') if not os.path.isfile(model_path): torch.hub.download_url_to_file(model_url, model_path, progress=True) ``` -------------------------------- ### Load Model and Save WAV (v5_v4_ru) Source: https://github.com/snakers4/silero-models/blob/master/examples_tts.ipynb Loads a Russian TTS model (v5_v4_ru) from a local file or downloads it if not found. It then generates and saves speech for a given text and speaker. ```python import os import torch device = torch.device('cpu') torch.set_num_threads(4) local_file = 'model.pt' if not os.path.isfile(local_file): torch.hub.download_url_to_file('https://models.silero.ai/models/tts/ru/v5_v4_ru.pt', local_file) model = torch.package.PackageImporter(local_file).load_pickle("tts_models", "model") model.to(device) example_text = 'Меня зовут Лева Королев. Я из готов. И я уже готов открыть все ваши замки любой сложности!' sample_rate = 48000 speaker='baya' audio_paths = model.save_wav(text=example_text, speaker=speaker, sample_rate=sample_rate) ``` -------------------------------- ### Load Models Configuration Source: https://github.com/snakers4/silero-models/blob/master/examples.ipynb Loads the 'models.yml' configuration file to access information about available STT models, including language-specific and latest versions. ```python models = OmegaConf.load('models.yml') # all available models are listed in the yml file print(list(models.stt_models.keys()), list(models.stt_models.en.keys()), list(models.stt_models.en.latest.keys()), models.stt_models.en.latest.jit) ``` -------------------------------- ### Load and Use Text Enhancement Model Source: https://context7.com/snakers4/silero-models/llms.txt Loads a pre-trained text enhancement model and applies it to input text. Ensure the model path is correctly set. ```python imp = package.PackageImporter(model_path) model = imp.load_pickle("te_model", "model") def enhance_text(text, lan='en'): return model.enhance_text(text, lan) result = enhance_text("this is a test of text enhancement", lan='en') print(result) ``` -------------------------------- ### Load Model and Save WAV (v4_ru) Source: https://github.com/snakers4/silero-models/blob/master/examples_tts.ipynb Loads a Russian TTS model (v4_ru) from a local file or downloads it if not found. It then generates and saves speech for a given text and speaker. This is an alternative model version to v5_v4_ru. ```python import os import torch device = torch.device('cpu') torch.set_num_threads(4) local_file = 'model.pt' if not os.path.isfile(local_file): torch.hub.download_url_to_file('https://models.silero.ai/models/tts/ru/v4_ru.pt', local_file) model = torch.package.PackageImporter(local_file).load_pickle("tts_models", "model") model.to(device) example_text = 'В недрах тундры выдры в г+етрах т+ырят в вёдра ядра кедров.' sample_rate = 48000 speaker='baya' audio_paths = model.save_wav(text=example_text, speaker=speaker, sample_rate=sample_rate) ``` -------------------------------- ### Audio Denoising with PyTorch Hub Source: https://context7.com/snakers4/silero-models/llms.txt Removes background noise from audio recordings using Silero's neural denoising models. Supports direct inference and a utility function for denoising. ```python import torch # Load denoise model # Available models: 'small_slow' (best quality), 'large_fast', 'small_fast' name = 'small_slow' device = torch.device('cpu') model, samples, utils = torch.hub.load( repo_or_dir='snakers4/silero-models', model='silero_denoise', name=name, device=device ) (read_audio, save_audio, denoise) = utils model.to(device) # Download and process sample torch.hub.download_url_to_file(samples[0], dst='noisy_sample.wav', progress=True) # Method 1: Direct model inference audio = read_audio('noisy_sample.wav').to(device) output = model(audio) save_audio('cleaned_output.wav', output.squeeze(1).cpu()) # Input at 24kHz, output at 48kHz # Method 2: Using denoise utility function output, sr = denoise(model, 'noisy_sample.wav', 'result.wav', device='cpu') print(f"Output sample rate: {sr}") # Output: Output sample rate: 48000 ```