Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 28, 2023 11:43
Show Gist options
  • Save code-boxx/3faa92449a5aef823f9c368add649879 to your computer and use it in GitHub Desktop.
Save code-boxx/3faa92449a5aef823f9c368add649879 to your computer and use it in GitHub Desktop.
Python Text To Speech (With Coqui TTS)

PYTHON TEXT TO SPEECH WITH COQUI TTS

REQUIREMENTS

  1. Python 3.8 - 3.10 should work properly.
  2. Microsoft C++ Build Tools - https://visualstudio.microsoft.com/visual-cpp-build-tools/
  3. espeak - https://github.com/espeak-ng/espeak-ng/releases

NOTES

Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will:

  • Create and activate a virtual environment.
  • Install Coqui.
  • Run 2-convery.py

CUDA NOT AVAILABLE ERROR

  • Visit https://pytorch.org/. Scroll down to "install Pytorch", and select the respective version for your platform.
  • Copy-paste the command in your terminal, append --force-reinstall to the end.
  • E.G. pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118 --force-reins tall

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from TTS.api import TTS
tts = TTS(model_name="tts_models/en/ljspeech/vits", progress_bar=True, gpu=True)
tts.tts_to_file("YES! Text to speech works like magic.", file_path="OUTPUT.wav")
"""
(X) NOTES
(X1) to get list of models
- run "tts --list_models" in command line
- or open "venv\lib\site-packages\tts\.models.json"
(X2) if model has multiple speakers, check tts\model\speaker_ids.json
(X3) edit "tts\model\config.json" to control voices
- length_scale = speed (more is slower)
- noise_scale = speech variation (more is dynamic)
"""
# (A) LOAD MODULES
from TTS.utils.manage import ModelManager
from TTS.utils.synthesizer import Synthesizer
import os
# (B) SETTINGS
PATH_BASE = os.path.dirname(__file__)
SET_TXT = os.path.join(PATH_BASE, "narrate.txt")
SET_SAVE = os.path.join(PATH_BASE, "output.wav")
SET_MODEL = "tts_models/en/vctk/vits"
SET_SPEAKER = "p274"
# (C) MODEL MANAGER
manager = ModelManager(
models_file = PATH_BASE + "/venv/Lib/site-packages/TTS/.models.json",
output_prefix = PATH_BASE,
progress_bar = True
)
model_path, config_path, model_item = manager.download_model(SET_MODEL)
if model_item["default_vocoder"] is None:
voc_path = None
voc_config_path = None
else:
voc_path, voc_config_path, _ = manager.download_model(model_item["default_vocoder"])
# (D) SYNTHESIZER
syn = Synthesizer(
tts_checkpoint = model_path,
tts_config_path = config_path,
vocoder_checkpoint = voc_path,
vocoder_config = voc_config_path,
use_cuda = True
)
# (E) OUTPUT
output = syn.tts(
text = open(SET_TXT, "r").read(),
speaker_name = SET_SPEAKER
)
syn.save_wav(output, SET_SAVE)
Hello world! This is a random paragraph and TTS works!
virtualenv venv
call venv\Scripts\activate
pip install tts
python 2-convert.py
virtualenv venv
source "venv/bin/activate"
pip install tts
python 2-convert.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment