Silero Models

repository·master·Indexed 27 days ago

https://github.com/snakers4/silero-models

High-quality, lightweight pre-trained models for Text-To-Speech (TTS), Speech-To-Text (STT), Denoising, and Text Enhancement. Supports multiple languages including English, Russian, and various CIS and Indic languages. Models can be deployed via PyTorch Hub, pip installation of the silero package, or manual JIT/package loading for offline use. Features include SSML support, automatic accentuation via silero-stress, and specialized tools for audio denoising and text punctuation.

Tokens
11.8K
Snippets
31
Records
51
Agent score
91%

What's inside silero-models

  1. Use Silero TTS via PyTorch Hub

    master

    You can load Silero Text-To-Speech models directly using torch.hub.load. This method automatically handles model downloading and setup. You need to specify the repo_or_dir, the model type (silero_tts), the language, and the speaker (model ID).

    # 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)
  2. Install and use Silero Models

    master

    Silero Models provide end-to-end, natural-sounding Text-To-Speech (TTS) with a large library of voices. They are designed to be fast on both CPU and GPU and offer one-line usage.

    You can use the models in three ways:

    1. Via PyTorch Hub: Using torch.hub.load().
    2. Via pip: Install the package and import the TTS module.
    3. Manual Caching: Manually cache the required models and utilities.

    Models are downloaded on demand. If you need to cache them for offline use, invoke a model once to trigger the download to your cache folder.

    pip install silero
  3. Use Silero TTS in Standalone Mode

    master

    For environments without direct Torch Hub access, you can download the .pt model file manually and use torch.package.PackageImporter to load the model. This requires PyTorch 1.12+ and the Python Standard Library. Use model.save_wav to generate audio files.

    # 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)
  4. Load Silero TTS models

    master

    You can load Silero TTS models using the silero_tts function or directly via torch.hub.

    Available model IDs:

    • v5_cis_base: Requires manual stress marks for all languages (e.g., к+ошка).
    • v5_cis_ext: Requires manual stress marks for all languages.
    • v5_cis_base_nostress: Requires manual stress marks ONLY for Slavic languages (ru, bel, ukr).

    Note: It is recommended to select a speaker that matches the target language (e.g., kaz_zhadyra for Kazakh or ru_zhadyra for Russian).

    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)
  5. Generate and manage random speakers

    master

    Silero allows generating speech with a random speaker and saving that voice for later use.

    1. Generate with random speaker: Set speaker='random' in apply_tts.
    2. Save a random voice: Use model.save_random_voice(voice_path) to save the generated voice to a .pt file.
    3. Load saved voice: Use the voice_path parameter in apply_tts to use a previously saved voice.
  6. List available Silero TTS models and languages

    master

    You can iterate through the loaded models object to see which languages are supported and which specific model IDs are available for each language.

    available_languages = list(models.tts_models.keys())
    print(f'Available languages {available_languages}')
    
    for lang in available_languages:
        _models = list(models.tts_models.get(lang).keys())
        print(f'Available models for {lang}: {_models}')
  7. Load Silero STT via PyTorch Hub

    master

    The easiest way to use Silero Speech-to-Text (STT) is via torch.hub. This method returns the model, a decoder function, and a set of utility functions for processing audio.

    import torch
    
    device = torch.device('cpu')  # gpu also works
    model, decoder, utils = torch.hub.load(repo_or_dir='snakers4/silero-models',
                                           model='silero_stt',
                                           jit_model='jit_xlarge',
                                           language='en', # also available 'de', 'es'
                                           device=device)
    
    # Unpack utilities
    (read_batch, split_into_batches,
     read_audio, prepare_model_input) = utils
  8. Install and load the Silero TTS v5 CIS base model

    master

    To use the Silero Text-to-Speech model, download the JIT model file and load it using torch.jit.load. It is recommended to set the number of threads for CPU execution.

    import torch
    from IPython.display import Audio
    
    device = torch.device('cpu')
    torch.set_num_threads(4)
    local_file = 'v5_cis_base_nostress.jit'
    torch.hub.download_url_to_file('https://models.silero.ai/models/tts/ru/v5_cis_base_nostress.jit',
                                   local_file)
    model = torch.jit.load(local_file)
  9. Install dependencies for Silero TTS

    master

    To use Silero models in a Colab or notebook environment, you need omegaconf and torch. You can also download the models.yml file to inspect available models and languages.

    !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')
  10. Load Silero Denoise model locally (JIT)

    master

    For environments without internet access or for optimized local deployment, you can download the JIT model file directly and load it using torch.jit.load. The model file for the latest version is located at https://models.silero.ai/denoise_models/sns_latest.jit.

    import os
    import torch
    
    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)
    model.to(torch.device('cpu'))
    
    # Recommended settings for local JIT execution
    torch._C._jit_set_profiling_mode(False)
    torch.set_grad_enabled(False)