Use config classes for firstrun and welcome

This commit is contained in:
ekoc committed 2024-11-17 16:00:54 +01:00
1 parent 4d12520801
commit 13aff24e0b
5 files changed
+75 -107

No files matched your search

+4
View File
@@ -282,6 +282,10 @@ class AlltalkConfig(AbstractJsonConfig):
self.gradio_pages = AlltalkConfigGradioPages() self.gradio_pages = AlltalkConfigGradioPages()
self._load_config() self._load_config()
@staticmethod
def default_config_path():
return AlltalkConfig.__this_dir / "confignew.json"
@staticmethod @staticmethod
def get_instance(force_reload = False): def get_instance(force_reload = False):
if AlltalkConfig.__instance is None: if AlltalkConfig.__instance is None:
+33 -51
View File
@@ -1,18 +1,19 @@
import json import json
import os import os
import time
import shutil import shutil
import sys
import zipfile import zipfile
import requests import requests
from tqdm import tqdm from tqdm import tqdm
from inputimeout import inputimeout, TimeoutOccurred from inputimeout import inputimeout, TimeoutOccurred
from pathlib import Path 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() 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): def download_file(url, dest_path):
response = requests.get(url, stream=True) response = requests.get(url, stream=True)
@@ -60,31 +61,14 @@ def setup_xtts():
def update_tts_engines(engine, model): def update_tts_engines(engine):
with open(tts_engines_path, 'r') as file: AlltalkTTSEnginesConfig.get_instance().change_engine(engine).save()
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 set_firstrun_model_false(): def set_firstrun_model_false():
with open(config_path, 'r') as file: config.firstrun_model = False
config = json.load(file) config.save()
config['firstrun_model'] = False
with open(config_path, 'w') as file:
json.dump(config, file, indent=4)
def load_config(file_path): def warning_message():
with open(file_path, "r") as config_file:
config = json.load(config_file)
return config
def warning_mesasge():
print(f"[{branding}TTS]") 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[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") 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] \033[0m'using untested triton version' \033[93mmessages.\033[0m")
print(f"[{branding}TTS]") print(f"[{branding}TTS]")
# Load confignew.json # Load confignew.json (might be version 1 or 2):
with open(config_path, 'r') as file: with open(AlltalkConfig.default_config_path(), 'r') as file:
config = json.load(file) config_json = json.load(file)
branding = config['branding'] branding = config_json['branding']
# Check if the config file is version 1 # 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] Detected version 1 configuration file.")
print(f"[{branding}TTS] Upgrading to version 2 configuration file...") print(f"[{branding}TTS] Upgrading to version 2 configuration file...")
# Backup the old configuration file # Backup the old configuration file
backup_path = config_path + ".backup" backup_path = AlltalkConfig.default_config_path() + ".backup"
shutil.copyfile(config_path, backup_path) shutil.copyfile(AlltalkConfig.default_config_path(), backup_path)
# Copy the new configuration template to replace the old config file # 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.") print(f"[{branding}TTS] Configuration file upgraded successfully.")
# Reload the new config file config = AlltalkConfig.get_instance()
with open(config_path, 'r') as file: branding = config.branding
config = json.load(file)
# Check if firstrun_model is true # Check if firstrun_model is true
if config.get('firstrun_model', False): if config.firstrun_model:
print(f"[{branding}TTS]") 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[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] \033[92mtab after inital setup.\033[0m")
print(f"[{branding}TTS]") print(f"[{branding}TTS]")
@@ -159,32 +143,30 @@ if config.get('firstrun_model', False):
os.makedirs('models/piper', exist_ok=True) os.makedirs('models/piper', exist_ok=True)
os.makedirs('models/vits', exist_ok=True) os.makedirs('models/vits', exist_ok=True)
os.makedirs('models/xtts/', exist_ok=True) os.makedirs('models/xtts/', exist_ok=True)
update_tts_engines('piper', 'piper') update_tts_engines('piper')
set_firstrun_model_false() set_firstrun_model_false()
warning_mesasge() warning_message()
exit() exit()
if user_choice is not None and user_choice.isdigit() and 1 <= int(user_choice) <= len(models): if user_choice is not None and user_choice.isdigit() and 1 <= int(user_choice) <= len(models):
selected_model = models[int(user_choice) - 1] selected_model = models[int(user_choice) - 1]
warning_mesasge() warning_message()
if user_choice is None or user_choice.lower() != 'own': if user_choice is None or user_choice.lower() != 'own':
if selected_model['name'] == 'piper': if selected_model['name'] == 'piper':
setup_piper() setup_piper()
update_tts_engines('piper', 'piper') update_tts_engines('piper')
set_firstrun_model_false()
elif selected_model['name'] == 'vits': elif selected_model['name'] == 'vits':
setup_vits() setup_vits()
update_tts_engines('vits', 'vits - tts_models--en--vctk--vits') update_tts_engines('vits')
set_firstrun_model_false()
elif selected_model['name'] == 'xtts': elif selected_model['name'] == 'xtts':
setup_xtts() setup_xtts()
update_tts_engines('xtts', 'xtts - xttsv2_2.0.3') update_tts_engines('xtts')
set_firstrun_model_false()
print(f"[{branding}TTS] {selected_model['name']} model downloaded and configuration updated successfully.") print(f"[{branding}TTS] {selected_model['name']} model downloaded and configuration updated successfully.")
# Set firstrun_model to false # Set firstrun_model to false
set_firstrun_model_false() set_firstrun_model_false()
warning_mesasge() warning_message()
exit() exit()
else: else:
exit() exit()
+31 -47
View File
@@ -1,18 +1,19 @@
import json import json
import os import os
import time
import shutil import shutil
import sys
import zipfile import zipfile
import requests import requests
from tqdm import tqdm from tqdm import tqdm
from inputimeout import inputimeout, TimeoutOccurred from inputimeout import inputimeout, TimeoutOccurred
from pathlib import Path 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() 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): def download_file(url, dest_path):
response = requests.get(url, stream=True) response = requests.get(url, stream=True)
@@ -60,31 +61,14 @@ def setup_xtts():
def update_tts_engines(engine, model): def update_tts_engines(engine):
with open(tts_engines_path, 'r') as file: AlltalkTTSEnginesConfig.get_instance().change_engine(engine).save()
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 set_firstrun_model_false(): def set_firstrun_model_false():
with open(config_path, 'r') as file: config.firstrun_model = False
config = json.load(file) config.save()
config['firstrun_model'] = False
with open(config_path, 'w') as file:
json.dump(config, file, indent=4)
def load_config(file_path): def warning_message():
with open(file_path, "r") as config_file:
config = json.load(config_file)
return config
def warning_mesasge():
print(f"[{branding}TTS]") 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[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") 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] \033[0m'using untested triton version' \033[93mmessages.\033[0m")
print(f"[{branding}TTS]") print(f"[{branding}TTS]")
# Load confignew.json # Load confignew.json (might be version 1 or 2):
with open(config_path, 'r') as file: with open(AlltalkConfig.default_config_path(), 'r') as file:
config = json.load(file) config_json = json.load(file)
branding = config['branding'] branding = config_json['branding']
# Check if the config file is version 1 # 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] Detected version 1 configuration file.")
print(f"[{branding}TTS] Upgrading to version 2 configuration file...") print(f"[{branding}TTS] Upgrading to version 2 configuration file...")
# Backup the old configuration file # Backup the old configuration file
backup_path = config_path + ".backup" backup_path = AlltalkConfig.default_config_path() + ".backup"
shutil.copyfile(config_path, backup_path) shutil.copyfile(AlltalkConfig.default_config_path(), backup_path)
# Copy the new configuration template to replace the old config file # 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.") print(f"[{branding}TTS] Configuration file upgraded successfully.")
# Reload the new config file config = AlltalkConfig.get_instance()
with open(config_path, 'r') as file: branding = config.branding
config = json.load(file)
# Check if firstrun_model is true # Check if firstrun_model is true
if config.get('firstrun_model', False): if config.firstrun_model:
print(f"[{branding}TTS]") 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] \033[92mThis is the first time startup. Please download a start TTS model.\033[0m")
print(f"[{branding}TTS]") print(f"[{branding}TTS]")
@@ -157,30 +141,30 @@ if config.get('firstrun_model', False):
os.makedirs('models/piper', exist_ok=True) os.makedirs('models/piper', exist_ok=True)
os.makedirs('models/vits', exist_ok=True) os.makedirs('models/vits', exist_ok=True)
os.makedirs('models/xtts/', exist_ok=True) os.makedirs('models/xtts/', exist_ok=True)
update_tts_engines('piper', 'piper') update_tts_engines('piper')
set_firstrun_model_false() set_firstrun_model_false()
warning_mesasge() warning_message()
exit() exit()
if user_choice is not None and user_choice.isdigit() and 1 <= int(user_choice) <= len(models): if user_choice is not None and user_choice.isdigit() and 1 <= int(user_choice) <= len(models):
selected_model = models[int(user_choice) - 1] selected_model = models[int(user_choice) - 1]
warning_mesasge() warning_message()
if user_choice is None or user_choice.lower() != 'own': if user_choice is None or user_choice.lower() != 'own':
if selected_model['name'] == 'piper': if selected_model['name'] == 'piper':
setup_piper() setup_piper()
update_tts_engines('piper', 'piper') update_tts_engines('piper')
elif selected_model['name'] == 'vits': elif selected_model['name'] == 'vits':
setup_vits() setup_vits()
update_tts_engines('vits', 'vits - tts_models--en--vctk--vits') update_tts_engines('vits')
elif selected_model['name'] == 'xtts': elif selected_model['name'] == 'xtts':
setup_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.") print(f"[{branding}TTS] {selected_model['name']} model downloaded and configuration updated successfully.")
# Set firstrun_model to false # Set firstrun_model to false
set_firstrun_model_false() set_firstrun_model_false()
warning_mesasge() warning_message()
exit() exit()
else: else:
exit() exit()
+6 -9
View File
@@ -1,15 +1,12 @@
import os
import json
import pathlib as Path
import gradio as gr import gradio as gr
from config import AlltalkConfig
def modify_config(): def modify_config():
config_file = os.path.join("confignew.json") config = AlltalkConfig.get_instance()
with open(config_file, "r") as f: config.firstrun_splash = False
config = json.load(f) config.save()
config["firstrun_splash"] = False
with open(config_file, "w") as f:
json.dump(config, f, indent=4)
return f"Welcome screen is disabled." return f"Welcome screen is disabled."
def alltalk_welcome(analytics_enabled=False): def alltalk_welcome(analytics_enabled=False):
+1
View File
@@ -15,6 +15,7 @@ class TestAlltalkConfig(unittest.TestCase):
def test_default_config_path(self): def test_default_config_path(self):
expected_config_path = Path(__file__).parent.parent.resolve() / "confignew.json" expected_config_path = Path(__file__).parent.parent.resolve() / "confignew.json"
self.assertEqual(self.config.get_config_path(), expected_config_path) self.assertEqual(self.config.get_config_path(), expected_config_path)
self.assertEqual(AlltalkConfig.default_config_path(), expected_config_path)
def test_branding(self): def test_branding(self):
self.assertEqual(self.config.branding, "AllTalk ") self.assertEqual(self.config.branding, "AllTalk ")