Prep for Google Colab

This commit is contained in:
erew123 authored and GitHub committed 2024-06-11 10:19:00 +01:00
1 parent bc0ebc03a6
commit 411b93f55c
1 file changed
+85 -34
+85 -34
View File
@@ -63,16 +63,30 @@ print(f"[{branding}TTS]\033[94m / ___ \| | |\033[1;35m | | (_| | | < \033[0m
print(f"[{branding}TTS]\033[94m /_/ \_\_|_|\033[1;35m |_|\__,_|_|_|\_\ \033[0m |_| |_| |____/ ") print(f"[{branding}TTS]\033[94m /_/ \_\_|_|\033[1;35m |_|\__,_|_|_|\_\ \033[0m |_| |_| |____/ ")
print(f"[{branding}TTS]") print(f"[{branding}TTS]")
##############################################
# START-UP # Check if we are on Google Colab #
##############################################
def check_google_colab():
try:
import google.colab
return True
except ImportError:
return False
running_on_google_colab = check_google_colab()
###################################################### ######################################################
# START-UP # Check if this is a first time start-up # # START-UP # Check if this is a first time start-up #
###################################################### ######################################################
def run_firsttime_script(): def run_firsttime_script():
script_path = '/content/alltalk_tts/system/config/firstrun.py' if running_on_google_colab else 'system/config/firstrun.py'
try: try:
subprocess.run(['python', 'system/config/firstrun.py'], check=True) subprocess.run(['python', script_path], check=True)
except subprocess.CalledProcessError as e:
print(f"Error occurred while running the script: {e}")
except Exception as e: except Exception as e:
print(f"An unexpected error occurred: {e}") print(f"An unexpected error occurred: {e}")
# Call the function to run the startup script
run_firsttime_script() run_firsttime_script()
########################################################### ###########################################################
@@ -301,16 +315,14 @@ def signal_handler(sig, frame):
try: try:
import google.colab import google.colab
running_on_google_colab = True running_on_google_colab = True
#if deepspeed_installed:
#params["deepspeed_activate"] = True
with open('/content/alltalk_tts/googlecolab.json', 'r') as f: with open('/content/alltalk_tts/googlecolab.json', 'r') as f:
data = json.load(f) data = json.load(f)
google_ip_address = data.get('google_ip_address', tunnel_url) tunnel_url_1, tunnel_url_2 = data.get('google_ip_address', [None, None])
except FileNotFoundError: except FileNotFoundError:
print("Could not find IP address") print("Could not find IP address")
google_ip_address = tunnel_url tunnel_url_1, tunnel_url_2 = None, None
except ImportError: except ImportError:
pass running_on_google_colab = False
# Attach the signal handler to the SIGINT signal (Ctrl+C) # Attach the signal handler to the SIGINT signal (Ctrl+C)
signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGINT, signal_handler)
@@ -326,7 +338,7 @@ else:
if process.poll() is None: if process.poll() is None:
if running_on_google_colab: if running_on_google_colab:
print(f"[{branding}TTS]") print(f"[{branding}TTS]")
print(f"[{branding}TTS] \033[94m{branding}Google Colab Address:\033[00m",f"\033[92m{google_ip_address}:443\033[00m") print(f"[{branding}TTS] \033[94mGoogle Colab Detected\033[00m")
print(f"[{branding}TTS]") print(f"[{branding}TTS]")
else: else:
print(f"[{branding}TTS] \033[91mWarning\033[0m TTS Subprocess Webserver failing to start process") print(f"[{branding}TTS] \033[91mWarning\033[0m TTS Subprocess Webserver failing to start process")
@@ -376,11 +388,18 @@ else:
# Cleanly kill off this script, but allow text-generation-webui to keep running, albeit without this alltalk_tts # Cleanly kill off this script, but allow text-generation-webui to keep running, albeit without this alltalk_tts
sys.exit(1) sys.exit(1)
print(f"[{branding}TTS]") if running_on_google_colab:
print(f"[{branding}TTS] \033[94mAPI Address :\033[00m",f"\033[92m127.0.0.1:{params['api_def']['api_port_number']}\033[00m") print(f"[{branding}TTS]")
print(f"[{branding}TTS] \033[94mGradio Light:\033[00m",f"\033[92mhttp://127.0.0.1:{params['gradio_port_number']}\033[00m") print(f"[{branding}TTS] \033[94mAPI Address :\033[00m",f"\033[92m{tunnel_url_1}\033[00m")
print(f"[{branding}TTS] \033[94mGradio Dark :\033[00m \033[92mhttp://127.0.0.1:{params['gradio_port_number']}?__theme=dark\033[00m") print(f"[{branding}TTS] \033[94mGradio Light:\033[00m",f"\033[92m{tunnel_url_2}\033[00m")
print(f"[{branding}TTS]") print(f"[{branding}TTS] \033[94mGradio Dark :\033[00m",f"\033[92m{tunnel_url_2}?__theme=dark\033[00m")
print(f"[{branding}TTS]")
else:
print(f"[{branding}TTS]")
print(f"[{branding}TTS] \033[94mAPI Address :\033[00m",f"\033[92m127.0.0.1:{params['api_def']['api_port_number']}\033[00m")
print(f"[{branding}TTS] \033[94mGradio Light:\033[00m",f"\033[92mhttp://127.0.0.1:{params['gradio_port_number']}\033[00m")
print(f"[{branding}TTS] \033[94mGradio Dark :\033[00m \033[92mhttp://127.0.0.1:{params['gradio_port_number']}?__theme=dark\033[00m")
print(f"[{branding}TTS]")
######################################### #########################################
@@ -497,9 +516,14 @@ tts_model_loaded = None
################################################# #################################################
def get_alltalk_settings(): def get_alltalk_settings():
global current_model_loaded, models_available, engines_available, current_engine_loaded global current_model_loaded, models_available, engines_available, current_engine_loaded
voices_url = f"{alltalk_protocol}{alltalk_ip_port}/api/voices" if running_on_google_colab:
rvcvoices_url = f"{alltalk_protocol}{alltalk_ip_port}/api/rvcvoices" voices_url = f"{tunnel_url_1}/api/voices"
settings_url = f"{alltalk_protocol}{alltalk_ip_port}/api/currentsettings" rvcvoices_url = f"{tunnel_url_1}/api/rvcvoices"
settings_url = f"{tunnel_url_1}/api/currentsettings"
else:
voices_url = f"{alltalk_protocol}{alltalk_ip_port}/api/voices"
rvcvoices_url = f"{alltalk_protocol}{alltalk_ip_port}/api/rvcvoices"
settings_url = f"{alltalk_protocol}{alltalk_ip_port}/api/currentsettings"
try: try:
voices_response = requests.get(voices_url, timeout=connection_timeout) voices_response = requests.get(voices_url, timeout=connection_timeout)
@@ -612,7 +636,10 @@ at_settings = get_alltalk_settings() # Pull all the current settings from the
#### TTS STOP GENERATION #### #### TTS STOP GENERATION ####
############################# #############################
def stop_generate_tts(): def stop_generate_tts():
api_url = f"{alltalk_protocol}{alltalk_ip_port}/api/stop-generation" if running_on_google_colab:
api_url = f"{tunnel_url_1}/api/stop-generation"
else:
api_url = f"{alltalk_protocol}{alltalk_ip_port}/api/stop-generation"
try: try:
response = requests.put(api_url, timeout=connection_timeout) response = requests.put(api_url, timeout=connection_timeout)
if response.status_code == 200: if response.status_code == 200:
@@ -630,7 +657,10 @@ def stop_generate_tts():
# MODEL - Swap model based on Gradio selection # MODEL - Swap model based on Gradio selection
def send_reload_request(value_sent): def send_reload_request(value_sent):
try: try:
url = f"{alltalk_protocol}{alltalk_ip_port}/api/reload" if running_on_google_colab:
url = f"{tunnel_url_1}/api/reload"
else:
url = f"{alltalk_protocol}{alltalk_ip_port}/api/reload"
payload = {"tts_method": value_sent} payload = {"tts_method": value_sent}
response = requests.post(url, params=payload) response = requests.post(url, params=payload)
response.raise_for_status() # Raises an HTTPError for bad responses response.raise_for_status() # Raises an HTTPError for bad responses
@@ -667,9 +697,9 @@ def tgwui_save_settings():
with open(config_file_path, "w") as file: with open(config_file_path, "w") as file:
json.dump(existing_settings, file, indent=4) json.dump(existing_settings, file, indent=4)
############################################# #####################################################
# Low VRAM change request to enable/disable # # TGWUI - Low VRAM change request to enable/disable #
############################################# #####################################################
# LOW VRAM - Gradio Checkbox handling # LOW VRAM - Gradio Checkbox handling
def send_lowvram_request(value_sent): def send_lowvram_request(value_sent):
try: try:
@@ -693,9 +723,9 @@ def send_lowvram_request(value_sent):
print(f"[{branding}Server] \033[91mWarning\033[0m Error during request to webserver process: Status code:\n{e}") print(f"[{branding}Server] \033[91mWarning\033[0m Error during request to webserver process: Status code:\n{e}")
return {"status": "error", "message": str(e)} return {"status": "error", "message": str(e)}
################### #########################
#### DeepSpeed #### # TGWUI - DeepSpeed ####
################### #########################
def send_deepspeed_request(value_sent): def send_deepspeed_request(value_sent):
try: try:
params["tgwui"]["tts_model_loaded"] = False params["tgwui"]["tts_model_loaded"] = False
@@ -718,9 +748,9 @@ def send_deepspeed_request(value_sent):
print(f"[{branding}Server] \033[91mWarning\033[0m Error during request to webserver process: Status code:\n{e}") print(f"[{branding}Server] \033[91mWarning\033[0m Error during request to webserver process: Status code:\n{e}")
return {"status": "error", "message": str(e)} return {"status": "error", "message": str(e)}
################################# ###################################
#### TTS STANDARD GENERATION #### # TGWUI - TTS STANDARD GENERATION #
################################# ###################################
def send_and_generate(gen_text, gen_character_voice, gen_narrartor_voice, gen_narrator_activated, gen_textnotinisde, gen_repetition, gen_language, gen_filter, gen_speed, gen_pitch, gen_autoplay, gen_autoplay_vol, gen_file_name, gen_temperature, gen_filetimestamp, gen_stream, gen_stopcurrentgen): def send_and_generate(gen_text, gen_character_voice, gen_narrartor_voice, gen_narrator_activated, gen_textnotinisde, gen_repetition, gen_language, gen_filter, gen_speed, gen_pitch, gen_autoplay, gen_autoplay_vol, gen_file_name, gen_temperature, gen_filetimestamp, gen_stream, gen_stopcurrentgen):
api_url = f"{alltalk_protocol}{alltalk_ip_port}/api/tts-generate" api_url = f"{alltalk_protocol}{alltalk_ip_port}/api/tts-generate"
if gen_text == "": if gen_text == "":
@@ -1173,7 +1203,10 @@ if gradio_enabled == True:
def reload_config(): def reload_config():
# Send a GET request to the /reload_config endpoint # Send a GET request to the /reload_config endpoint
reload_config_url = f"http://localhost:{params['api_def']['api_port_number']}/api/reload_config" if running_on_google_colab:
reload_config_url = f"{tunnel_url_1}/api/reload_config"
else:
reload_config_url = f"http://localhost:{params['api_def']['api_port_number']}/api/reload_config"
try: try:
response = requests.get(reload_config_url) response = requests.get(reload_config_url)
if response.status_code == 200: if response.status_code == 200:
@@ -1259,7 +1292,7 @@ if gradio_enabled == True:
# Restart the subprocess # Restart the subprocess
restart_subprocess() restart_subprocess()
# Wait for the engine to be ready with error handling and retries # Wait for the engine to be ready with error handling and retries
max_retries = 40 max_retries = 80
retry_delay = 1 # seconds retry_delay = 1 # seconds
retries = 0 retries = 0
while retries < max_retries: while retries < max_retries:
@@ -1306,7 +1339,10 @@ if gradio_enabled == True:
# Define the output path for the processed audio # Define the output path for the processed audio
output_rvc_path = this_dir / "outputs" / "voice2rvcOutput.wav" output_rvc_path = this_dir / "outputs" / "voice2rvcOutput.wav"
url = f"{alltalk_protocol}{alltalk_ip_port}/api/voice2rvc" if running_on_google_colab:
url = f"{tunnel_url_1}/api/voice2rvc"
else:
url = f"{alltalk_protocol}{alltalk_ip_port}/api/voice2rvc"
# Submit the paths to the API endpoint # Submit the paths to the API endpoint
response = requests.post(url, data={ response = requests.post(url, data={
@@ -1335,7 +1371,10 @@ if gradio_enabled == True:
print(f"[{branding}ENG]") print(f"[{branding}ENG]")
print(f"[{branding}ENG] \033[94mChanging model loaded. Please wait.\033[00m") print(f"[{branding}ENG] \033[94mChanging model loaded. Please wait.\033[00m")
print(f"[{branding}ENG]") print(f"[{branding}ENG]")
url = f"{alltalk_protocol}{alltalk_ip_port}/api/reload" if running_on_google_colab:
url = f"{tunnel_url_1}/api/reload"
else:
url = f"{alltalk_protocol}{alltalk_ip_port}/api/reload"
payload = {"tts_method": selected_model} payload = {"tts_method": selected_model}
response = requests.post(url, params=payload) response = requests.post(url, params=payload)
response.raise_for_status() # Raises an HTTPError for bad responses response.raise_for_status() # Raises an HTTPError for bad responses
@@ -1461,14 +1500,20 @@ if gradio_enabled == True:
return "RVC settings updated successfully!" return "RVC settings updated successfully!"
def generate_tts(gen_text, gen_char, rvcgen_char, gen_narr, rvcgen_narr, gen_narren, gen_textni, gen_repetition, gen_lang, gen_filter, gen_speed, gen_pitch, gen_autopl, gen_autoplvol, gen_filen, gen_temperature, gen_filetime, gen_stream, gen_stopcurrentgen): def generate_tts(gen_text, gen_char, rvcgen_char, gen_narr, rvcgen_narr, gen_narren, gen_textni, gen_repetition, gen_lang, gen_filter, gen_speed, gen_pitch, gen_autopl, gen_autoplvol, gen_filen, gen_temperature, gen_filetime, gen_stream, gen_stopcurrentgen):
api_url = f"http://{my_current_url}/api/tts-generate" if running_on_google_colab:
api_url = f"{tunnel_url_1}/api/tts-generate"
else:
api_url = f"http://{my_current_url}/api/tts-generate"
if gen_text == "": if gen_text == "":
print(f"[{branding}TTS] No Text was sent to generate as TTS") print(f"[{branding}TTS] No Text was sent to generate as TTS")
return None, str("No Text was sent to generate as TTS") return None, str("No Text was sent to generate as TTS")
if gen_stopcurrentgen: if gen_stopcurrentgen:
stop_generate_tts() stop_generate_tts()
if gen_stream == "true": if gen_stream == "true":
api_url = f"http://{my_current_url}/api/tts-generate-streaming" if running_on_google_colab:
api_url = f"{tunnel_url_1}/api/tts-generate-streaming"
else:
api_url = f"http://{my_current_url}/api/tts-generate-streaming"
encoded_text = requests.utils.quote(gen_text) encoded_text = requests.utils.quote(gen_text)
streaming_url = f"{api_url}?text={encoded_text}&voice={gen_char}&language={gen_lang}&output_file={gen_filen}" streaming_url = f"{api_url}?text={encoded_text}&voice={gen_char}&language={gen_lang}&output_file={gen_filen}"
return streaming_url, str("TTS Streaming Audio Generated") return streaming_url, str("TTS Streaming Audio Generated")
@@ -1506,7 +1551,10 @@ if gradio_enabled == True:
# Set the protocol type # Set the protocol type
protocol = "http://" # or "https://" if using HTTPS protocol = "http://" # or "https://" if using HTTPS
# Prepend the URL and PORT to the output_file_url # Prepend the URL and PORT to the output_file_url
output_file_url = f"{protocol}{my_current_url}{result['output_file_url']}" if running_on_google_colab:
output_file_url = f"{tunnel_url_1}{result['output_file_url']}"
else:
output_file_url = f"{protocol}{my_current_url}{result['output_file_url']}"
return output_file_url, str("TTS Audio Generated") return output_file_url, str("TTS Audio Generated")
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
error_message = "An error occurred. Please see console output." error_message = "An error occurred. Please see console output."
@@ -1520,6 +1568,9 @@ if gradio_enabled == True:
# Get the URL IP or domain name # Get the URL IP or domain name
def get_domain_name(request: gr.Request): def get_domain_name(request: gr.Request):
global my_current_url global my_current_url
if running_on_google_colab:
my_current_url = tunnel_url_1
return None
if request: if request:
host = request.headers.get("host", "Unknown") host = request.headers.get("host", "Unknown")
my_current_url = host.split(":")[0] # Split the host by ":" and take the first part my_current_url = host.split(":")[0] # Split the host by ":" and take the first part