Spaces:
Running
Running
import click | |
import warnings | |
import os | |
def main(text, file, output_path, language, speaker, speed, device): | |
if file: | |
if not os.path.exists(text): | |
raise FileNotFoundError(f'Trying to load text from file due to --file/-f flag, but file not found. Remove the --file/-f flag to pass a string.') | |
else: | |
with open(text) as f: | |
text = f.read().strip() | |
if text == '': | |
raise ValueError('You entered empty text or the file you passed was empty.') | |
language = language.upper() | |
if language == '': language = 'EN' | |
if speaker == '': speaker = None | |
if (not language == 'EN') and speaker: | |
warnings.warn('You specified a speaker but the language is English.') | |
from melo.api import TTS | |
model = TTS(language=language, device=device) | |
speaker_ids = model.hps.data.spk2id | |
if language == 'EN': | |
if not speaker: speaker = 'EN-Default' | |
spkr = speaker_ids[speaker] | |
else: | |
spkr = speaker_ids[list(speaker_ids.keys())[0]] | |
model.tts_to_file(text, spkr, output_path, speed=speed) | |