From 13aff24e0b3a7fe71593aab453df69e15e6496f3 Mon Sep 17 00:00:00 2001 From: ekoc Date: Sun, 17 Nov 2024 16:00:54 +0100 Subject: [PATCH] Use config classes for firstrun and welcome --- config.py | 4 ++ system/config/firstrun.py | 84 ++++++++++---------------- system/config/firstrun_tgwui.py | 78 ++++++++++-------------- system/gradio_pages/alltalk_welcome.py | 15 ++--- test/test_config.py | 1 + 5 files changed, 75 insertions(+), 107 deletions(-) diff --git a/config.py b/config.py index 5eae634..60080c2 100644 --- a/config.py +++ b/config.py @@ -282,6 +282,10 @@ class AlltalkConfig(AbstractJsonConfig): self.gradio_pages = AlltalkConfigGradioPages() self._load_config() + @staticmethod + def default_config_path(): + return AlltalkConfig.__this_dir / "confignew.json" + @staticmethod def get_instance(force_reload = False): if AlltalkConfig.__instance is None: diff --git a/system/config/firstrun.py b/system/config/firstrun.py index 59c18a4..1917a09 100644 --- a/system/config/firstrun.py +++ b/system/config/firstrun.py @@ -1,18 +1,19 @@ import json import os -import time import shutil +import sys import zipfile import requests from tqdm import tqdm from inputimeout import inputimeout, TimeoutOccurred from pathlib import Path +# Setting the module search path: +sys.path.insert(0, str(Path(__file__).parent.parent.parent.resolve())) + +from config import AlltalkTTSEnginesConfig, AlltalkConfig + this_dir = Path(__file__).parent.resolve().parent.resolve().parent.resolve() -# File paths -config_path = this_dir / 'confignew.json' -tts_engines_path = this_dir / 'system/tts_engines/tts_engines.json' -new_config_template_path = this_dir / 'system/config/confignew.json' def download_file(url, dest_path): response = requests.get(url, stream=True) @@ -60,31 +61,14 @@ def setup_xtts(): -def update_tts_engines(engine, model): - with open(tts_engines_path, 'r') as file: - tts_engines = json.load(file) - - tts_engines['engine_loaded'] = engine - tts_engines['selected_model'] = model - - with open(tts_engines_path, 'w') as file: - json.dump(tts_engines, file, indent=4) +def update_tts_engines(engine): + AlltalkTTSEnginesConfig.get_instance().change_engine(engine).save() def set_firstrun_model_false(): - with open(config_path, 'r') as file: - config = json.load(file) - - config['firstrun_model'] = False - - with open(config_path, 'w') as file: - json.dump(config, file, indent=4) + config.firstrun_model = False + config.save() -def load_config(file_path): - with open(file_path, "r") as config_file: - config = json.load(config_file) - return config - -def warning_mesasge(): +def warning_message(): print(f"[{branding}TTS]") print(f"[{branding}TTS] \033[93mIf you have you have UPGRADED from v1 ensure you have re-installed\033[0m") print(f"[{branding}TTS] \033[93mthe requirements. Otherwise you will get failures and errors!\033[0m") @@ -92,34 +76,34 @@ def warning_mesasge(): print(f"[{branding}TTS] \033[0m'using untested triton version' \033[93mmessages.\033[0m") print(f"[{branding}TTS]") -# Load confignew.json -with open(config_path, 'r') as file: - config = json.load(file) +# Load confignew.json (might be version 1 or 2): +with open(AlltalkConfig.default_config_path(), 'r') as file: + config_json = json.load(file) -branding = config['branding'] +branding = config_json['branding'] # Check if the config file is version 1 -if "ip_address" in config: +if "ip_address" in config_json: print(f"[{branding}TTS] Detected version 1 configuration file.") print(f"[{branding}TTS] Upgrading to version 2 configuration file...") # Backup the old configuration file - backup_path = config_path + ".backup" - shutil.copyfile(config_path, backup_path) + backup_path = AlltalkConfig.default_config_path() + ".backup" + shutil.copyfile(AlltalkConfig.default_config_path(), backup_path) # Copy the new configuration template to replace the old config file - shutil.copyfile(new_config_template_path, config_path) + new_config_template_path = this_dir / 'system/config/confignew.json' + shutil.copyfile(new_config_template_path, AlltalkConfig.default_config_path()) print(f"[{branding}TTS] Configuration file upgraded successfully.") - - # Reload the new config file - with open(config_path, 'r') as file: - config = json.load(file) + +config = AlltalkConfig.get_instance() +branding = config.branding # Check if firstrun_model is true -if config.get('firstrun_model', False): +if config.firstrun_model: print(f"[{branding}TTS]") - print(f"[{branding}TTS] \033[92mThis is the first time startup. Please download a start TTS model. Other TTS engines\033[0m") + print(f"[{branding}TTS] \033[92mThis is the first time startup.. Please download a start TTS model. Other TTS engines\033[0m") print(f"[{branding}TTS] \033[92mand TTS models can be downloaded/managed in the Gradio Interface `TTS Engines Settings`\033[0m") print(f"[{branding}TTS] \033[92mtab after inital setup.\033[0m") print(f"[{branding}TTS]") @@ -159,32 +143,30 @@ if config.get('firstrun_model', False): os.makedirs('models/piper', exist_ok=True) os.makedirs('models/vits', exist_ok=True) os.makedirs('models/xtts/', exist_ok=True) - update_tts_engines('piper', 'piper') + update_tts_engines('piper') set_firstrun_model_false() - warning_mesasge() + warning_message() exit() if user_choice is not None and user_choice.isdigit() and 1 <= int(user_choice) <= len(models): selected_model = models[int(user_choice) - 1] - warning_mesasge() + warning_message() if user_choice is None or user_choice.lower() != 'own': if selected_model['name'] == 'piper': setup_piper() - update_tts_engines('piper', 'piper') - set_firstrun_model_false() + update_tts_engines('piper') elif selected_model['name'] == 'vits': setup_vits() - update_tts_engines('vits', 'vits - tts_models--en--vctk--vits') - set_firstrun_model_false() + update_tts_engines('vits') elif selected_model['name'] == 'xtts': setup_xtts() - update_tts_engines('xtts', 'xtts - xttsv2_2.0.3') - set_firstrun_model_false() + update_tts_engines('xtts') + print(f"[{branding}TTS] {selected_model['name']} model downloaded and configuration updated successfully.") # Set firstrun_model to false set_firstrun_model_false() - warning_mesasge() + warning_message() exit() else: exit() diff --git a/system/config/firstrun_tgwui.py b/system/config/firstrun_tgwui.py index ef19f30..7e628be 100644 --- a/system/config/firstrun_tgwui.py +++ b/system/config/firstrun_tgwui.py @@ -1,18 +1,19 @@ import json import os -import time import shutil +import sys import zipfile import requests from tqdm import tqdm from inputimeout import inputimeout, TimeoutOccurred from pathlib import Path +# Setting the module search path: +sys.path.insert(0, str(Path(__file__).parent.parent.parent.resolve())) + +from config import AlltalkTTSEnginesConfig, AlltalkConfig + this_dir = Path(__file__).parent.resolve().parent.resolve().parent.resolve() -# File paths -config_path = this_dir / 'confignew.json' -tts_engines_path = this_dir / 'system/tts_engines/tts_engines.json' -new_config_template_path = this_dir / 'system/config/confignew.json' def download_file(url, dest_path): response = requests.get(url, stream=True) @@ -60,31 +61,14 @@ def setup_xtts(): -def update_tts_engines(engine, model): - with open(tts_engines_path, 'r') as file: - tts_engines = json.load(file) - - tts_engines['engine_loaded'] = engine - tts_engines['selected_model'] = model - - with open(tts_engines_path, 'w') as file: - json.dump(tts_engines, file, indent=4) +def update_tts_engines(engine): + AlltalkTTSEnginesConfig.get_instance().change_engine(engine).save() def set_firstrun_model_false(): - with open(config_path, 'r') as file: - config = json.load(file) - - config['firstrun_model'] = False - - with open(config_path, 'w') as file: - json.dump(config, file, indent=4) + config.firstrun_model = False + config.save() -def load_config(file_path): - with open(file_path, "r") as config_file: - config = json.load(config_file) - return config - -def warning_mesasge(): +def warning_message(): print(f"[{branding}TTS]") print(f"[{branding}TTS] \033[93mIf you have you have UPGRADED from v1 ensure you have re-installed\033[0m") print(f"[{branding}TTS] \033[93mthe requirements. Otherwise you will get failures and errors!\033[0m") @@ -92,32 +76,32 @@ def warning_mesasge(): print(f"[{branding}TTS] \033[0m'using untested triton version' \033[93mmessages.\033[0m") print(f"[{branding}TTS]") -# Load confignew.json -with open(config_path, 'r') as file: - config = json.load(file) +# Load confignew.json (might be version 1 or 2): +with open(AlltalkConfig.default_config_path(), 'r') as file: + config_json = json.load(file) -branding = config['branding'] +branding = config_json['branding'] # Check if the config file is version 1 -if "ip_address" in config: +if "ip_address" in config_json: print(f"[{branding}TTS] Detected version 1 configuration file.") print(f"[{branding}TTS] Upgrading to version 2 configuration file...") # Backup the old configuration file - backup_path = config_path + ".backup" - shutil.copyfile(config_path, backup_path) + backup_path = AlltalkConfig.default_config_path() + ".backup" + shutil.copyfile(AlltalkConfig.default_config_path(), backup_path) # Copy the new configuration template to replace the old config file - shutil.copyfile(new_config_template_path, config_path) + new_config_template_path = this_dir / 'system/config/confignew.json' + shutil.copyfile(new_config_template_path, AlltalkConfig.default_config_path()) print(f"[{branding}TTS] Configuration file upgraded successfully.") - - # Reload the new config file - with open(config_path, 'r') as file: - config = json.load(file) + +config = AlltalkConfig.get_instance() +branding = config.branding # Check if firstrun_model is true -if config.get('firstrun_model', False): +if config.firstrun_model: print(f"[{branding}TTS]") print(f"[{branding}TTS] \033[92mThis is the first time startup. Please download a start TTS model.\033[0m") print(f"[{branding}TTS]") @@ -157,30 +141,30 @@ if config.get('firstrun_model', False): os.makedirs('models/piper', exist_ok=True) os.makedirs('models/vits', exist_ok=True) os.makedirs('models/xtts/', exist_ok=True) - update_tts_engines('piper', 'piper') + update_tts_engines('piper') set_firstrun_model_false() - warning_mesasge() + warning_message() exit() if user_choice is not None and user_choice.isdigit() and 1 <= int(user_choice) <= len(models): selected_model = models[int(user_choice) - 1] - warning_mesasge() + warning_message() if user_choice is None or user_choice.lower() != 'own': if selected_model['name'] == 'piper': setup_piper() - update_tts_engines('piper', 'piper') + update_tts_engines('piper') elif selected_model['name'] == 'vits': setup_vits() - update_tts_engines('vits', 'vits - tts_models--en--vctk--vits') + update_tts_engines('vits') elif selected_model['name'] == 'xtts': setup_xtts() - update_tts_engines('xtts', 'xtts - xttsv2_2.0.3') + update_tts_engines('xtts') print(f"[{branding}TTS] {selected_model['name']} model downloaded and configuration updated successfully.") # Set firstrun_model to false set_firstrun_model_false() - warning_mesasge() + warning_message() exit() else: exit() diff --git a/system/gradio_pages/alltalk_welcome.py b/system/gradio_pages/alltalk_welcome.py index 2fa5db5..7023917 100644 --- a/system/gradio_pages/alltalk_welcome.py +++ b/system/gradio_pages/alltalk_welcome.py @@ -1,15 +1,12 @@ -import os -import json -import pathlib as Path import gradio as gr +from config import AlltalkConfig + + def modify_config(): - config_file = os.path.join("confignew.json") - with open(config_file, "r") as f: - config = json.load(f) - config["firstrun_splash"] = False - with open(config_file, "w") as f: - json.dump(config, f, indent=4) + config = AlltalkConfig.get_instance() + config.firstrun_splash = False + config.save() return f"Welcome screen is disabled." def alltalk_welcome(analytics_enabled=False): diff --git a/test/test_config.py b/test/test_config.py index 895ffd8..2845414 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -15,6 +15,7 @@ class TestAlltalkConfig(unittest.TestCase): def test_default_config_path(self): expected_config_path = Path(__file__).parent.parent.resolve() / "confignew.json" self.assertEqual(self.config.get_config_path(), expected_config_path) + self.assertEqual(AlltalkConfig.default_config_path(), expected_config_path) def test_branding(self): self.assertEqual(self.config.branding, "AllTalk ")