From 28e76e2cd11fb03189d73713bb7be4f352b7d588 Mon Sep 17 00:00:00 2001 From: mrfakename Date: Tue, 27 Feb 2024 01:00:46 +0000 Subject: [PATCH] Allow loading from file --- README.md | 9 ++++++++- melo/app.py | 11 ++++++++++- melo/main.py | 12 +++++++++++- setup.py | 1 + 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index af62279..ed658ba 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,8 @@ An unofficial [live demo](https://huggingface.co/spaces/mrfakename/MeloTTS) is h The WebUI supports muliple languages and voices. First, follow the installation steps. Then, simply run: ```bash -python app.py +melo-ui +# Or: python melo/app.py ``` ### CLI @@ -84,6 +85,12 @@ melo "Text to read" output.wav --speed 1.5 melo "语音合成领域近年来发展迅速" zh.wav -l ZH ``` +**Load from a file:** + +```bash +melo file.txt out.wav --file +``` + The full API documentation may be found using: ```bash diff --git a/melo/app.py b/melo/app.py index 093738e..2fe949b 100644 --- a/melo/app.py +++ b/melo/app.py @@ -7,6 +7,7 @@ print("Make sure you've downloaded unidic (python -m unidic download) for this W from melo.api import TTS speed = 1.0 import tempfile +import click device = 'auto' models = { 'EN': TTS(language='EN', device=device), @@ -35,4 +36,12 @@ with gr.Blocks() as demo: aud = gr.Audio(interactive=False) btn.click(synthesize, inputs=[speaker, text, speed, language], outputs=[aud]) gr.Markdown('WebUI by [mrfakename](https://twitter.com/realmrfakename).') -demo.queue(api_open=False, default_concurrency_limit=10).launch(show_api=False) +@click.command() +@click.option('--share', '-s', is_flag=True, show_default=True, default=False, help="Expose a publicly-accessible shared Gradio link usable by anyone with the link. Only share the link with people you trust.") +@click.option('--host', '-h', default=None) +@click.option('--port', '-p', default=None) +def main(share, host, port): + demo.queue(api_open=False, default_concurrency_limit=10).launch(show_api=False, share=share, server_name=host, server_port=port) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/melo/main.py b/melo/main.py index 265e088..58064f1 100644 --- a/melo/main.py +++ b/melo/main.py @@ -1,14 +1,24 @@ import click import warnings +import os @click.command @click.argument('text') @click.argument('output_path') +@click.option("--file", '-f', is_flag=True, show_default=True, default=False, help="Text is a file") @click.option('--language', '-l', default='EN', help='Language, defaults to English', type=click.Choice(['EN', 'ES', 'FR', 'ZH', 'JP', 'KR'], case_sensitive=False)) @click.option('--speaker', '-spk', default='EN-Default', help='Speaker ID, only for English, leave empty for default, ignored if not English. If English, defaults to "EN-Default"', type=click.Choice(['EN-Default', 'EN-US', 'EN-BR', 'EN-INDIA', 'EN-AU'])) @click.option('--speed', '-s', default=1.0, help='Speed, defaults to 1.0', type=float) @click.option('--device', '-d', default='auto', help='Device, defaults to auto') -def main(text, output_path, language, speaker, speed, device): +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 diff --git a/setup.py b/setup.py index ad67a65..5f0990e 100644 --- a/setup.py +++ b/setup.py @@ -32,6 +32,7 @@ setup( "console_scripts": [ "melotts = melo.main:main", "melo = melo.main:main", + "melo-ui = melo.app:main", ], }, )