diff --git a/src-python/config.py b/src-python/config.py index 40cb4470..40a8f1b9 100644 --- a/src-python/config.py +++ b/src-python/config.py @@ -228,6 +228,15 @@ class Config: if isinstance(value, dict): self._SELECTABLE_WHISPER_WEIGHT_TYPE_DICT = value + @property + def SELECTABLE_TRANSLATION_ENGINE_STATUS(self): + return self._SELECTABLE_TRANSLATION_ENGINE_STATUS + + @SELECTABLE_TRANSLATION_ENGINE_STATUS.setter + def SELECTABLE_TRANSLATION_ENGINE_STATUS(self, value): + if isinstance(value, dict): + self._SELECTABLE_TRANSLATION_ENGINE_STATUS = value + # Save Json Data ## Main Window @property @@ -952,6 +961,9 @@ class Config: self._SELECTABLE_WHISPER_WEIGHT_TYPE_DICT = {} for weight_type in self.SELECTABLE_WHISPER_WEIGHT_TYPE_LIST: self._SELECTABLE_WHISPER_WEIGHT_TYPE_DICT[weight_type] = False + self._SELECTABLE_TRANSLATION_ENGINE_STATUS = {} + for engine in self.SELECTABLE_TRANSLATION_ENGINE_LIST: + self._SELECTABLE_TRANSLATION_ENGINE_STATUS[engine] = False # Save Json Data ## Main Window diff --git a/src-python/model.py b/src-python/model.py index d9c0a4d7..97a0855e 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -165,7 +165,8 @@ class Model: languages = sorted(languages, key=lambda x: x['language']) return languages - def findTranslationEngines(self, source_lang, target_lang): + def findTranslationEngines(self, source_lang, target_lang, engines_status): + selectable_engines = [key for key, value in engines_status.items() if value is True] compatible_engines = [] for engine in list(translation_lang.keys()): languages = translation_lang.get(engine, {}).get("source", {}) @@ -174,11 +175,9 @@ class Model: language_list = list(languages.keys()) if all(e in language_list for e in source_langs) and all(e in language_list for e in target_langs): - compatible_engines.append(engine) + if engine in selectable_engines: + compatible_engines.append(engine) - if "DeepL_API" in compatible_engines: - if config.AUTH_KEYS["DeepL_API"] is None: - compatible_engines.remove('DeepL_API') return compatible_engines def getTranslate(self, translator_name, source_language, target_language, target_country, message): diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index ece8198d..e3634183 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -484,6 +484,7 @@ class Controller: engines = model.findTranslationEngines( config.SELECTED_YOUR_LANGUAGES[config.SELECTED_TAB_NO], config.SELECTED_TARGET_LANGUAGES[config.SELECTED_TAB_NO], + config.SELECTABLE_TRANSLATION_ENGINE_STATUS, ) return {"status":200, "result":engines} @@ -1029,6 +1030,7 @@ class Controller: def setDeeplAuthKey(self, data, *args, **kwargs) -> dict: printLog("Set DeepL Auth Key", data) + translator_name = "DeepL_API" try: data = str(data) if len(data) == 36 or len(data) == 39: @@ -1036,16 +1038,17 @@ class Controller: if result is True: key = data auth_keys = config.AUTH_KEYS - auth_keys["DeepL_API"] = key + auth_keys[translator_name] = key config.AUTH_KEYS = auth_keys + config.SELECTABLE_TRANSLATION_ENGINE_STATUS[translator_name] = True self.updateTranslationEngineAndEngineList() - response = {"status":200, "result":config.AUTH_KEYS["DeepL_API"]} + response = {"status":200, "result":config.AUTH_KEYS[translator_name]} else: response = { "status":400, "result":{ "message":"DeepL auth key length is not correct", - "data": config.AUTH_KEYS["DeepL_API"] + "data": config.AUTH_KEYS[translator_name] } } else: @@ -1053,7 +1056,7 @@ class Controller: "status":400, "result":{ "message":"Authentication failure of deepL auth key", - "data": config.AUTH_KEYS["DeepL_API"] + "data": config.AUTH_KEYS[translator_name] } } except Exception as e: @@ -1062,17 +1065,19 @@ class Controller: "status":400, "result":{ "message":f"Error {e}", - "data": config.AUTH_KEYS["DeepL_API"] + "data": config.AUTH_KEYS[translator_name] } } return response def delDeeplAuthKey(self, *args, **kwargs) -> dict: + translator_name = "DeepL_API" auth_keys = config.AUTH_KEYS - auth_keys["DeepL_API"] = None + auth_keys[translator_name] = None config.AUTH_KEYS = auth_keys + config.SELECTABLE_TRANSLATION_ENGINE_STATUS[translator_name] = False self.updateTranslationEngineAndEngineList() - return {"status":200, "result":config.AUTH_KEYS["DeepL_API"]} + return {"status":200, "result":config.AUTH_KEYS[translator_name]} @staticmethod def getCtranslate2WeightType(*args, **kwargs) -> dict: @@ -1429,8 +1434,12 @@ class Controller: return osc_message def changeToCTranslate2Process(self) -> None: + selected_engines = config.SELECTED_TRANSLATION_ENGINES[config.SELECTED_TAB_NO] + config.SELECTABLE_TRANSLATION_ENGINE_STATUS[selected_engines] = False config.SELECTED_TRANSLATION_ENGINES[config.SELECTED_TAB_NO] = "CTranslate2" - self.run(200, self.run_mapping["translation_engines"], "CTranslate2") + selectable_engines = self.getTranslationEngines()["result"] + self.run(200, self.run_mapping["selected_translation_engines"], config.SELECTED_TRANSLATION_ENGINES) + self.run(200, self.run_mapping["translation_engines"], selectable_engines) def startTranscriptionSendMessage(self) -> None: while self.device_access_status is False: @@ -1626,13 +1635,22 @@ class Controller: printLog("Start Initialization") removeLog() - printLog("Start check DeepL API Key") - if config.AUTH_KEYS["DeepL_API"] is not None: - if model.authenticationTranslatorDeepLAuthKey(auth_key=config.AUTH_KEYS["DeepL_API"]) is False: - # error update Auth key - auth_keys = config.AUTH_KEYS - auth_keys["DeepL_API"] = None - config.AUTH_KEYS = auth_keys + printLog("Init Translation Engine Status") + for engine in config.SELECTABLE_TRANSLATION_ENGINE_LIST: + match engine: + case "DeepL_API": + printLog("Start check DeepL API Key") + config.SELECTABLE_TRANSLATION_ENGINE_STATUS[engine] = False + if config.AUTH_KEYS[engine] is not None: + if model.authenticationTranslatorDeepLAuthKey(auth_key=config.AUTH_KEYS[engine]) is True: + config.SELECTABLE_TRANSLATION_ENGINE_STATUS[engine] = True + else: + # error update Auth key + auth_keys = config.AUTH_KEYS + auth_keys[engine] = None + config.AUTH_KEYS = auth_keys + case _: + config.SELECTABLE_TRANSLATION_ENGINE_STATUS[engine] = True self.initializationProgress(1)