From 6ea56eddfa62d3685a92a5a8c9954988045236d3 Mon Sep 17 00:00:00 2001 From: erew123 <35898566+erew123@users.noreply.github.com> Date: Sun, 10 Dec 2023 20:02:36 +0000 Subject: [PATCH] Allow standalone running Added logic to allow script to be run as a standalone if required (outside of Text-generation-webui) Corrected spelling mistake in documentation. --- script.py | 45 ++++++++++++++++++++++++++++++--------------- tts_server.py | 2 +- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/script.py b/script.py index f139cee..0408145 100644 --- a/script.py +++ b/script.py @@ -12,37 +12,26 @@ import atexit from pathlib import Path from datetime import datetime, timedelta import re - -import gradio as gr - -from modules import chat, shared, ui_chat -from modules.logging_colors import logger -from modules.ui import create_refresh_button -from modules.utils import gradio - import numpy as np import soundfile as sf import uuid - ############################################################## #### LOAD PARAMS FROM CONFIG.JSON - REQUIRED FOR BRANDING #### ############################################################## # STARTUP VARIABLE - Create "this_dir" variable as the current script directory this_dir = Path(__file__).parent.resolve() - # load config file in and get settings def load_config(file_path): with open(file_path, "r") as config_file: config = json.load(config_file) return config - config_file_path = this_dir / "config.json" +# Load the params dictionary from the config.json file params = load_config(config_file_path) - # Required for sentence splitting try: from TTS.api import TTS @@ -56,7 +45,7 @@ except ModuleNotFoundError: ) raise -# IMPORT - Attempt Importing DeepSpeed +# IMPORT - Attempt Importing DeepSpeed (required for displaying Deepspeed checkbox in gradio) try: import deepspeed @@ -64,6 +53,21 @@ try: except ImportError: deepspeed_installed = False +# Import gradio if being used within text generation webUI +try: + import gradio as gr + + from modules import chat, shared, ui_chat + from modules.logging_colors import logger + from modules.ui import create_refresh_button + from modules.utils import gradio + # This is set to check if the script is being run within text generation webui or as a standalone script. False is running as part of text gen web ui or a gradio interface + running_in_standalone = False +except ModuleNotFoundError: + print(f"[{params['branding']}Startup] Running script.py in standalone mode") + # This is set to check if the script is being run within text generation webui or as a standalone script. true means standalone + running_in_standalone = True + ########################### #### STARTUP VARIABLES #### ########################### @@ -73,11 +77,10 @@ with open(this_dir / "languages.json", encoding="utf8") as f: # Create a global lock process_lock = threading.Lock() - +# Gather the voice files def get_available_voices(): return sorted([voice.name for voice in Path(f"{this_dir}/voices").glob("*.wav")]) - ######################### #### LICENSE DISPLAY #### ######################### @@ -811,3 +814,15 @@ def ui(): # Play preview preview_text.submit(voice_preview, preview_text, preview_audio) preview_play.click(voice_preview, preview_text, preview_audio) + + +########################################## +#### STANDALONE MODE LOOP TERMINATION #### +########################################## +## Loop to keep the script from exiting out if its being run as a standalone script and not part of text-generation-webui +if running_in_standalone: + while True: + try: + time.sleep(1) # Add a small delay to avoid high CPU usage + except KeyboardInterrupt: + break # Allow graceful exit on Ctrl+C \ No newline at end of file diff --git a/tts_server.py b/tts_server.py index b1b23df..4b10699 100644 --- a/tts_server.py +++ b/tts_server.py @@ -933,7 +933,7 @@ simple_webpage = """
Generating TTS - Linux
curl -X POST -H "Content-Type: application/json" -d '{"text": "This is text to generate as TTS","voice": "female_01.wav", "language": "en", "output_file": "outputfile.wav"}' "http://127.0.0.1:7851/api/generate"
Generating TTS - Windows
-curl -X POST -H "Content-Type: application/json" -d="{\"text\": \"This is text to generate as TTS\",\"voice\": \"female_01.wav\", \"language\": \"en\", \"output_file\": \"outputfile.wav\"}" 'http://127.0.0.1:7851/api/generate'
+ curl -X POST -H "Content-Type: application/json" -d "{\"text\": \"This is text to generate as TTS\", \"voice\": \"female_01.wav\", \"language\": \"en\", \"output_file\": \"outputfile.wav\"}" http://127.0.0.1:7851/api/generate
Generating TTS - Notes
Replace This is text to generate as TTS with whatever you want it to say. female_01.wav with the voice sample you want to use. output_file.wav with the file name you want it to create.
JSON return {"status":"generate-success","data":{"audio_path":"outputfile.wav"}}