diff --git a/src-python/model.py b/src-python/model.py index 90ca6be0..7c5ebb16 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -430,16 +430,13 @@ class Model: mic_host_name = default_device["host"]["name"] mic_device_name = default_device["device"]["name"] if mic_host_name != config.CHOICE_MIC_HOST or mic_device_name != config.CHOICE_MIC_DEVICE: - config.CHOICE_MIC_HOST = mic_host_name - config.CHOICE_MIC_DEVICE = mic_device_name - fnc_mic(config.CHOICE_MIC_DEVICE) + fnc_mic(mic_host_name, mic_device_name) if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: default_device = getDefaultOutputDevice() speaker_device_name = default_device["device"]["name"] if speaker_device_name != config.CHOICE_SPEAKER_DEVICE: - config.CHOICE_SPEAKER_DEVICE = speaker_device_name - fnc_speaker(config.CHOICE_SPEAKER_DEVICE) + fnc_speaker(speaker_device_name) sleep(1) if isinstance(self.th_check_device, threadFnc) is False: diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index 46ef5a91..9d766593 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -5,7 +5,7 @@ from threading import Thread import re from config import config from model import model -from utils import isUniqueStrings, printLog, printResponse +from utils import isUniqueStrings, printLog # Common class DownloadSoftwareProgressBar: @@ -782,14 +782,17 @@ class UpdateSelectedDevice: def __init__(self, action): self.action = action - def set_mic(self, device) -> None: - printLog("Update Mic Device", device) + def set_mic(self, host, device) -> None: + config.CHOICE_MIC_HOST = host + config.CHOICE_MIC_DEVICE = device + printLog("Update Host/Mic Device", f"{host}/{device}") self.action("mic", { "status":200, - "result":device + "result":{"host":host, "device":device} }) def set_speaker(self, device) -> None: + config.CHOICE_SPEAKER_DEVICE = device printLog("Update Speaker Device", device) self.action("speaker", { "status":200, @@ -842,7 +845,7 @@ def callbackSetMicDevice(data, *args, **kwargs) -> dict: if config.ENABLE_CHECK_ENERGY_SEND is True: model.stopCheckMicEnergy() model.startCheckMicEnergy() - return {"status":200, "result":config.CHOICE_MIC_DEVICE} + return {"status":200, "result":{"host":config.CHOICE_MIC_HOST, "device":config.CHOICE_MIC_DEVICE}} def callbackSetMicEnergyThreshold(data, *args, **kwargs) -> dict: printLog("Set Mic Energy Threshold", data) @@ -1353,7 +1356,7 @@ def getListInputDevice(*args, **kwargs) -> dict: def getListOutputDevice(*args, **kwargs) -> dict: return {"status":200, "result": model.getListOutputDevice()} -def init(endpoints:dict, *args, **kwargs) -> None: +def init(actions:dict, *args, **kwargs) -> None: printLog("Start Initialization") printLog("Start check DeepL API Key") @@ -1371,9 +1374,8 @@ def init(endpoints:dict, *args, **kwargs) -> None: # check Downloaded CTranslate2 Model Weight printLog("Check Downloaded CTranslate2 Model Weight") if config.USE_TRANSLATION_FEATURE is True and model.checkCTranslatorCTranslate2ModelWeight() is False: - def callback(progress): - printResponse(200, endpoints["ctranslate2"], {"progress":progress}) - startThreadingDownloadCtranslate2Weight(callback) + download = DownloadCTranslate2ProgressBar(actions["download_ctranslate2"]) + startThreadingDownloadCtranslate2Weight(download.set) # set Transcription Engine printLog("Set Transcription Engine") @@ -1385,9 +1387,8 @@ def init(endpoints:dict, *args, **kwargs) -> None: # check Downloaded Whisper Model Weight printLog("Check Downloaded Whisper Model Weight") if config.USE_WHISPER_FEATURE is True and model.checkTranscriptionWhisperModelWeight() is False: - def callback(progress): - printResponse(200, endpoints["whisper"], {"progress":progress}) - startThreadingDownloadWhisperWeight(callback) + download = DownloadWhisperProgressBar(actions["download_whisper"]) + startThreadingDownloadWhisperWeight(download.set) # set word filter printLog("Set Word Filter") @@ -1413,8 +1414,5 @@ def init(endpoints:dict, *args, **kwargs) -> None: # init Auto device selection printLog("Init Auto Device Selection") if config.ENABLE_MIC_AUTOMATIC_SELECTION is True or config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: - def mic_callback(device): - printResponse(200, endpoints["check_mic_device"], device) - def speaker_callback(device): - printResponse(200, endpoints["check_speaker_device"], device) - model.startAutomaticDeviceSelection(mic_callback, speaker_callback) \ No newline at end of file + update_device = UpdateSelectedDevice(actions["update_selected_device"]) + model.startAutomaticDeviceSelection(update_device.set_mic, update_device.set_speaker) \ No newline at end of file diff --git a/src-python/webui_mainloop.py b/src-python/webui_mainloop.py index 59a4e4a5..f4c4a4cd 100644 --- a/src-python/webui_mainloop.py +++ b/src-python/webui_mainloop.py @@ -241,11 +241,11 @@ action_mapping = { "download":"/action/download_whisper_weight" }, "/controller/callback_enable_mic_automatic_selection": { - "mic":"/controller/callback_set_mic_device", + "mic":"/controller/callback_set_mic_host", "speaker":"/controller/callback_set_speaker_device", }, "/controller/callback_enable_speaker_automatic_selection": { - "mic":"/controller/callback_set_mic_device", + "mic":"/controller/callback_set_mic_host", "speaker":"/controller/callback_set_speaker_device", } } @@ -267,12 +267,16 @@ def handleControllerRequest(endpoint, data=None): status = 404 else: action_endpoint = action_mapping.get(endpoint, None) - if action_endpoint is not None: - response = handler(data, Action(action_endpoint).transmit) - else: - response = handler(data) - status = response.get("status", None) - result = response.get("result", None) + try: + if action_endpoint is not None: + response = handler(data, Action(action_endpoint).transmit) + else: + response = handler(data) + status = response.get("status", None) + result = response.get("result", None) + except Exception as e: + result = str(e) + status = 500 return result, status class Action: @@ -280,9 +284,12 @@ class Action: self.endpoints = endpoints def transmit(self, key:str, data:dict) -> None: - status = data.get("status", None) - result = data.get("result", None) - printResponse(status, self.endpoints[key], result) + if key not in self.endpoints: + printLog("Invalid endpoint", key) + else: + status = data.get("status", None) + result = data.get("result", None) + printResponse(status, self.endpoints[key], result) def main(): received_data = sys.stdin.readline().strip() @@ -292,7 +299,7 @@ def main(): endpoint = received_data.get("endpoint", None) data = received_data.get("data", None) data = encodeBase64(data) if data is not None else None - printLog(endpoint, data) + printLog(endpoint, {"receive_data":data}) try: match endpoint.split("/")[1]: @@ -305,14 +312,14 @@ def main(): except Exception as e: result = str(e) status = 500 + printLog(endpoint, {"send_data":result}) printResponse(status, endpoint, result) if __name__ == "__main__": controller.init({ - "ctranslate2": action_mapping["/controller/callback_download_ctranslate2_weight"]["download"], - "whisper": action_mapping["/controller/callback_download_whisper_weight"]["download"], - "check_mic_device": action_mapping["/controller/callback_enable_mic_automatic_selection"]["mic"], - "check_speaker_device": action_mapping["/controller/callback_enable_speaker_automatic_selection"]["speaker"], + "download_ctranslate2": Action(action_mapping["/controller/callback_download_ctranslate2_weight"]).transmit, + "download_whisper": Action(action_mapping["/controller/callback_download_whisper_weight"]).transmit, + "update_selected_device": Action(action_mapping["/controller/callback_enable_mic_automatic_selection"]).transmit, }) process = "main" @@ -327,12 +334,11 @@ if __name__ == "__main__": traceback.print_exc(file=f) case "test": - endpoint = "/controller/callback_download_ctranslate2_weight" - result, status = handleControllerRequest(endpoint) - printResponse(status, endpoint, result) - endpoint = "/controller/callback_download_whisper_weight" - result, status = handleControllerRequest(endpoint) - printResponse(status, endpoint, result) + for _ in range(100): + time.sleep(0.5) + endpoint = "/controller/list_mic_host" + result, status = handleControllerRequest(endpoint) + printResponse(status, endpoint, result) case "test_all": import time