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.
This commit is contained in:
erew123 authored and GitHub committed 2023-12-10 20:02:36 +00:00
1 parent 963f06cc7f
commit 6ea56eddfa
2 files changed
+31 -16

No files matched your search

+30 -15
View File
@@ -12,37 +12,26 @@ import atexit
from pathlib import Path from pathlib import Path
from datetime import datetime, timedelta from datetime import datetime, timedelta
import re 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 numpy as np
import soundfile as sf import soundfile as sf
import uuid import uuid
############################################################## ##############################################################
#### LOAD PARAMS FROM CONFIG.JSON - REQUIRED FOR BRANDING #### #### LOAD PARAMS FROM CONFIG.JSON - REQUIRED FOR BRANDING ####
############################################################## ##############################################################
# STARTUP VARIABLE - Create "this_dir" variable as the current script directory # STARTUP VARIABLE - Create "this_dir" variable as the current script directory
this_dir = Path(__file__).parent.resolve() this_dir = Path(__file__).parent.resolve()
# load config file in and get settings # load config file in and get settings
def load_config(file_path): def load_config(file_path):
with open(file_path, "r") as config_file: with open(file_path, "r") as config_file:
config = json.load(config_file) config = json.load(config_file)
return config return config
config_file_path = this_dir / "config.json" config_file_path = this_dir / "config.json"
# Load the params dictionary from the config.json file
params = load_config(config_file_path) params = load_config(config_file_path)
# Required for sentence splitting # Required for sentence splitting
try: try:
from TTS.api import TTS from TTS.api import TTS
@@ -56,7 +45,7 @@ except ModuleNotFoundError:
) )
raise raise
# IMPORT - Attempt Importing DeepSpeed # IMPORT - Attempt Importing DeepSpeed (required for displaying Deepspeed checkbox in gradio)
try: try:
import deepspeed import deepspeed
@@ -64,6 +53,21 @@ try:
except ImportError: except ImportError:
deepspeed_installed = False 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 #### #### STARTUP VARIABLES ####
########################### ###########################
@@ -73,11 +77,10 @@ with open(this_dir / "languages.json", encoding="utf8") as f:
# Create a global lock # Create a global lock
process_lock = threading.Lock() process_lock = threading.Lock()
# Gather the voice files
def get_available_voices(): def get_available_voices():
return sorted([voice.name for voice in Path(f"{this_dir}/voices").glob("*.wav")]) return sorted([voice.name for voice in Path(f"{this_dir}/voices").glob("*.wav")])
######################### #########################
#### LICENSE DISPLAY #### #### LICENSE DISPLAY ####
######################### #########################
@@ -811,3 +814,15 @@ def ui():
# Play preview # Play preview
preview_text.submit(voice_preview, preview_text, preview_audio) preview_text.submit(voice_preview, preview_text, preview_audio)
preview_play.click(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
+1 -1
View File
@@ -933,7 +933,7 @@ simple_webpage = """
<p><b>Generating TTS - Linux</b></p> <p><b>Generating TTS - Linux</b></p>
<code><span class="key">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"</code></span> <code><span class="key">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"</code></span>
<p><b>Generating TTS - Windows</b></p> <p><b>Generating TTS - Windows</b></p>
<code><span class="key">curl -X POST -H "Content-Type: application/json" -d="{&#92;&quot;text&#92;&quot;: &#92;&quot;This is text to generate as TTS&#92;&quot;,&#92;&quot;voice&#92;&quot;: &#92;&quot;female_01.wav&#92;&quot;, &#92;&quot;language&#92;&quot;: &#92;&quot;en&#92;&quot;, &#92;&quot;output_file&#92;&quot;: &#92;&quot;outputfile.wav&#92;&quot;}" 'http://127.0.0.1:7851/api/generate'</span></code> <code><span class="key">curl -X POST -H "Content-Type: application/json" -d "{&#92;&quot;text&#92;&quot;: &#92;&quot;This is text to generate as TTS&#92;&quot;, &#92;&quot;voice&#92;&quot;: &#92;&quot;female_01.wav&#92;&quot;, &#92;&quot;language&#92;&quot;: &#92;&quot;en&#92;&quot;, &#92;&quot;output_file&#92;&quot;: &#92;&quot;outputfile.wav&#92;&quot;}" http://127.0.0.1:7851/api/generate</span></code>
<p><b>Generating TTS - Notes</b></p> <p><b>Generating TTS - Notes</b></p>
<p>Replace <code>This is text to generate as TTS</code> with whatever you want it to say. <code>female_01.wav</code> with the voice sample you want to use. <code>output_file.wav</code> with the file name you want it to create.</p> <p>Replace <code>This is text to generate as TTS</code> with whatever you want it to say. <code>female_01.wav</code> with the voice sample you want to use. <code>output_file.wav</code> with the file name you want it to create.</p>
<p>JSON return <code>{"status":"generate-success","data":{"audio_path":"outputfile.wav"}}</code></p> <p>JSON return <code>{"status":"generate-success","data":{"audio_path":"outputfile.wav"}}</code></p>