👍️[Update] Model : Deviceの変更監視処理を変更

- デバイス監視処理実行時にデバイス選択を行うとアクセス干渉のために動作停止の可能性あり
This commit is contained in:
misyaguziya
2024-09-15 17:25:02 +09:00
parent 551bc424b8
commit 5deeb4da2d
3 changed files with 45 additions and 44 deletions

View File

@@ -430,16 +430,13 @@ class Model:
mic_host_name = default_device["host"]["name"] mic_host_name = default_device["host"]["name"]
mic_device_name = default_device["device"]["name"] mic_device_name = default_device["device"]["name"]
if mic_host_name != config.CHOICE_MIC_HOST or mic_device_name != config.CHOICE_MIC_DEVICE: if mic_host_name != config.CHOICE_MIC_HOST or mic_device_name != config.CHOICE_MIC_DEVICE:
config.CHOICE_MIC_HOST = mic_host_name fnc_mic(mic_host_name, mic_device_name)
config.CHOICE_MIC_DEVICE = mic_device_name
fnc_mic(config.CHOICE_MIC_DEVICE)
if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True: if config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True:
default_device = getDefaultOutputDevice() default_device = getDefaultOutputDevice()
speaker_device_name = default_device["device"]["name"] speaker_device_name = default_device["device"]["name"]
if speaker_device_name != config.CHOICE_SPEAKER_DEVICE: if speaker_device_name != config.CHOICE_SPEAKER_DEVICE:
config.CHOICE_SPEAKER_DEVICE = speaker_device_name fnc_speaker(speaker_device_name)
fnc_speaker(config.CHOICE_SPEAKER_DEVICE)
sleep(1) sleep(1)
if isinstance(self.th_check_device, threadFnc) is False: if isinstance(self.th_check_device, threadFnc) is False:

View File

@@ -5,7 +5,7 @@ from threading import Thread
import re import re
from config import config from config import config
from model import model from model import model
from utils import isUniqueStrings, printLog, printResponse from utils import isUniqueStrings, printLog
# Common # Common
class DownloadSoftwareProgressBar: class DownloadSoftwareProgressBar:
@@ -782,14 +782,17 @@ class UpdateSelectedDevice:
def __init__(self, action): def __init__(self, action):
self.action = action self.action = action
def set_mic(self, device) -> None: def set_mic(self, host, device) -> None:
printLog("Update Mic Device", device) config.CHOICE_MIC_HOST = host
config.CHOICE_MIC_DEVICE = device
printLog("Update Host/Mic Device", f"{host}/{device}")
self.action("mic", { self.action("mic", {
"status":200, "status":200,
"result":device "result":{"host":host, "device":device}
}) })
def set_speaker(self, device) -> None: def set_speaker(self, device) -> None:
config.CHOICE_SPEAKER_DEVICE = device
printLog("Update Speaker Device", device) printLog("Update Speaker Device", device)
self.action("speaker", { self.action("speaker", {
"status":200, "status":200,
@@ -842,7 +845,7 @@ def callbackSetMicDevice(data, *args, **kwargs) -> dict:
if config.ENABLE_CHECK_ENERGY_SEND is True: if config.ENABLE_CHECK_ENERGY_SEND is True:
model.stopCheckMicEnergy() model.stopCheckMicEnergy()
model.startCheckMicEnergy() 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: def callbackSetMicEnergyThreshold(data, *args, **kwargs) -> dict:
printLog("Set Mic Energy Threshold", data) printLog("Set Mic Energy Threshold", data)
@@ -1353,7 +1356,7 @@ def getListInputDevice(*args, **kwargs) -> dict:
def getListOutputDevice(*args, **kwargs) -> dict: def getListOutputDevice(*args, **kwargs) -> dict:
return {"status":200, "result": model.getListOutputDevice()} 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 Initialization")
printLog("Start check DeepL API Key") printLog("Start check DeepL API Key")
@@ -1371,9 +1374,8 @@ def init(endpoints:dict, *args, **kwargs) -> None:
# check Downloaded CTranslate2 Model Weight # check Downloaded CTranslate2 Model Weight
printLog("Check Downloaded CTranslate2 Model Weight") printLog("Check Downloaded CTranslate2 Model Weight")
if config.USE_TRANSLATION_FEATURE is True and model.checkCTranslatorCTranslate2ModelWeight() is False: if config.USE_TRANSLATION_FEATURE is True and model.checkCTranslatorCTranslate2ModelWeight() is False:
def callback(progress): download = DownloadCTranslate2ProgressBar(actions["download_ctranslate2"])
printResponse(200, endpoints["ctranslate2"], {"progress":progress}) startThreadingDownloadCtranslate2Weight(download.set)
startThreadingDownloadCtranslate2Weight(callback)
# set Transcription Engine # set Transcription Engine
printLog("Set Transcription Engine") printLog("Set Transcription Engine")
@@ -1385,9 +1387,8 @@ def init(endpoints:dict, *args, **kwargs) -> None:
# check Downloaded Whisper Model Weight # check Downloaded Whisper Model Weight
printLog("Check Downloaded Whisper Model Weight") printLog("Check Downloaded Whisper Model Weight")
if config.USE_WHISPER_FEATURE is True and model.checkTranscriptionWhisperModelWeight() is False: if config.USE_WHISPER_FEATURE is True and model.checkTranscriptionWhisperModelWeight() is False:
def callback(progress): download = DownloadWhisperProgressBar(actions["download_whisper"])
printResponse(200, endpoints["whisper"], {"progress":progress}) startThreadingDownloadWhisperWeight(download.set)
startThreadingDownloadWhisperWeight(callback)
# set word filter # set word filter
printLog("Set Word Filter") printLog("Set Word Filter")
@@ -1413,8 +1414,5 @@ def init(endpoints:dict, *args, **kwargs) -> None:
# init Auto device selection # init Auto device selection
printLog("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: if config.ENABLE_MIC_AUTOMATIC_SELECTION is True or config.ENABLE_SPEAKER_AUTOMATIC_SELECTION is True:
def mic_callback(device): update_device = UpdateSelectedDevice(actions["update_selected_device"])
printResponse(200, endpoints["check_mic_device"], device) model.startAutomaticDeviceSelection(update_device.set_mic, update_device.set_speaker)
def speaker_callback(device):
printResponse(200, endpoints["check_speaker_device"], device)
model.startAutomaticDeviceSelection(mic_callback, speaker_callback)

View File

@@ -241,11 +241,11 @@ action_mapping = {
"download":"/action/download_whisper_weight" "download":"/action/download_whisper_weight"
}, },
"/controller/callback_enable_mic_automatic_selection": { "/controller/callback_enable_mic_automatic_selection": {
"mic":"/controller/callback_set_mic_device", "mic":"/controller/callback_set_mic_host",
"speaker":"/controller/callback_set_speaker_device", "speaker":"/controller/callback_set_speaker_device",
}, },
"/controller/callback_enable_speaker_automatic_selection": { "/controller/callback_enable_speaker_automatic_selection": {
"mic":"/controller/callback_set_mic_device", "mic":"/controller/callback_set_mic_host",
"speaker":"/controller/callback_set_speaker_device", "speaker":"/controller/callback_set_speaker_device",
} }
} }
@@ -267,12 +267,16 @@ def handleControllerRequest(endpoint, data=None):
status = 404 status = 404
else: else:
action_endpoint = action_mapping.get(endpoint, None) action_endpoint = action_mapping.get(endpoint, None)
if action_endpoint is not None: try:
response = handler(data, Action(action_endpoint).transmit) if action_endpoint is not None:
else: response = handler(data, Action(action_endpoint).transmit)
response = handler(data) else:
status = response.get("status", None) response = handler(data)
result = response.get("result", None) status = response.get("status", None)
result = response.get("result", None)
except Exception as e:
result = str(e)
status = 500
return result, status return result, status
class Action: class Action:
@@ -280,9 +284,12 @@ class Action:
self.endpoints = endpoints self.endpoints = endpoints
def transmit(self, key:str, data:dict) -> None: def transmit(self, key:str, data:dict) -> None:
status = data.get("status", None) if key not in self.endpoints:
result = data.get("result", None) printLog("Invalid endpoint", key)
printResponse(status, self.endpoints[key], result) else:
status = data.get("status", None)
result = data.get("result", None)
printResponse(status, self.endpoints[key], result)
def main(): def main():
received_data = sys.stdin.readline().strip() received_data = sys.stdin.readline().strip()
@@ -292,7 +299,7 @@ def main():
endpoint = received_data.get("endpoint", None) endpoint = received_data.get("endpoint", None)
data = received_data.get("data", None) data = received_data.get("data", None)
data = encodeBase64(data) if data is not None else None data = encodeBase64(data) if data is not None else None
printLog(endpoint, data) printLog(endpoint, {"receive_data":data})
try: try:
match endpoint.split("/")[1]: match endpoint.split("/")[1]:
@@ -305,14 +312,14 @@ def main():
except Exception as e: except Exception as e:
result = str(e) result = str(e)
status = 500 status = 500
printLog(endpoint, {"send_data":result})
printResponse(status, endpoint, result) printResponse(status, endpoint, result)
if __name__ == "__main__": if __name__ == "__main__":
controller.init({ controller.init({
"ctranslate2": action_mapping["/controller/callback_download_ctranslate2_weight"]["download"], "download_ctranslate2": Action(action_mapping["/controller/callback_download_ctranslate2_weight"]).transmit,
"whisper": action_mapping["/controller/callback_download_whisper_weight"]["download"], "download_whisper": Action(action_mapping["/controller/callback_download_whisper_weight"]).transmit,
"check_mic_device": action_mapping["/controller/callback_enable_mic_automatic_selection"]["mic"], "update_selected_device": Action(action_mapping["/controller/callback_enable_mic_automatic_selection"]).transmit,
"check_speaker_device": action_mapping["/controller/callback_enable_speaker_automatic_selection"]["speaker"],
}) })
process = "main" process = "main"
@@ -327,12 +334,11 @@ if __name__ == "__main__":
traceback.print_exc(file=f) traceback.print_exc(file=f)
case "test": case "test":
endpoint = "/controller/callback_download_ctranslate2_weight" for _ in range(100):
result, status = handleControllerRequest(endpoint) time.sleep(0.5)
printResponse(status, endpoint, result) endpoint = "/controller/list_mic_host"
endpoint = "/controller/callback_download_whisper_weight" result, status = handleControllerRequest(endpoint)
result, status = handleControllerRequest(endpoint) printResponse(status, endpoint, result)
printResponse(status, endpoint, result)
case "test_all": case "test_all":
import time import time