[改善] 型注釈の追加とドキュメントの更新
- config.py, controller.py, model.py, mainloop.py, utils.py での型注釈の追加 - CODING_RULES.md と api.md のドキュメントを更新 - 不要なコードの削除とリファクタリング
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import copy
|
||||
from typing import Callable, Any
|
||||
from typing import Callable, Any, List, Optional
|
||||
from time import sleep
|
||||
from subprocess import Popen
|
||||
from threading import Thread
|
||||
@@ -11,10 +11,14 @@ from utils import removeLog, printLog, errorLogging, isConnectedNetwork, isValid
|
||||
|
||||
class Controller:
|
||||
def __init__(self) -> None:
|
||||
self.init_mapping = {}
|
||||
self.run_mapping = {}
|
||||
self.run = None
|
||||
self.device_access_status = True
|
||||
# typed attributes to satisfy static type checkers
|
||||
self.init_mapping: dict = {}
|
||||
self.run_mapping: dict = {}
|
||||
# initialize with a no-op callable so callers can safely call self.run
|
||||
def _noop_run(status: int, endpoint: str, payload: Any = None) -> None:
|
||||
return None
|
||||
self.run: Callable[[int, str, Any], None] = _noop_run
|
||||
self.device_access_status: bool = True
|
||||
|
||||
def setInitMapping(self, init_mapping:dict) -> None:
|
||||
self.init_mapping = init_mapping
|
||||
@@ -251,7 +255,7 @@ class Controller:
|
||||
|
||||
elif isinstance(message, str) and len(message) > 0:
|
||||
translation = []
|
||||
transliteration_message = []
|
||||
transliteration_message: List[Any] = []
|
||||
transliteration_translation = []
|
||||
if model.checkKeywords(message):
|
||||
self.run(
|
||||
@@ -407,7 +411,7 @@ class Controller:
|
||||
)
|
||||
elif isinstance(message, str) and len(message) > 0:
|
||||
translation = []
|
||||
transliteration_message = []
|
||||
transliteration_message: List[Any] = []
|
||||
transliteration_translation = []
|
||||
if model.checkKeywords(message):
|
||||
self.run(
|
||||
@@ -566,12 +570,12 @@ class Controller:
|
||||
translation_text = f" ({'/'.join(translation)})" if translation else ""
|
||||
model.logger.info(f"[RECEIVED] {message}{translation_text}")
|
||||
|
||||
def chatMessage(self, data) -> None:
|
||||
def chatMessage(self, data) -> dict:
|
||||
id = data["id"]
|
||||
message = data["message"]
|
||||
if len(message) > 0:
|
||||
translation = []
|
||||
transliteration_message = []
|
||||
transliteration_message: List[Any] = []
|
||||
transliteration_translation = []
|
||||
if config.ENABLE_TRANSLATION is False:
|
||||
pass
|
||||
@@ -739,6 +743,7 @@ class Controller:
|
||||
self.run_mapping["software_update_info"],
|
||||
software_update_info,
|
||||
)
|
||||
return {"status":200, "result": software_update_info}
|
||||
|
||||
@staticmethod
|
||||
def getComputeMode(*args, **kwargs) -> dict:
|
||||
@@ -800,11 +805,15 @@ class Controller:
|
||||
if is_vram_error:
|
||||
# Defaultのデバイス設定に戻す
|
||||
printLog("VRAM error detected, reverting device setting")
|
||||
self.run(
|
||||
400,
|
||||
self.run_mapping["error_translation_enable_vram_overflow"],
|
||||
{
|
||||
"message":"VRAM out of memory enabling translation",
|
||||
"data": error_message
|
||||
},
|
||||
)
|
||||
self.setDisableTranslation()
|
||||
config.SELECTED_TRANSLATION_COMPUTE_DEVICE = copy.deepcopy(config.SELECTABLE_COMPUTE_DEVICE_LIST[0])
|
||||
config.SELECTED_TRANSLATION_COMPUTE_TYPE = "auto"
|
||||
self.run(200, self.run_mapping["selected_translation_compute_device"], config.SELECTED_TRANSLATION_COMPUTE_DEVICE)
|
||||
self.run(200, self.run_mapping["selected_translation_compute_type"], config.SELECTED_TRANSLATION_COMPUTE_TYPE)
|
||||
self.run(
|
||||
400,
|
||||
self.run_mapping["enable_translation"],
|
||||
@@ -2234,13 +2243,13 @@ class Controller:
|
||||
th_stopCheckSpeakerEnergy.join()
|
||||
|
||||
@staticmethod
|
||||
def startThreadingDownloadCtranslate2Weight(weight_type:str, callback:Callable[[float], None], end_callback:Callable[[float], None]) -> None:
|
||||
def startThreadingDownloadCtranslate2Weight(weight_type:str, callback:Callable[[float], None], end_callback:Optional[Callable[..., None]] = None) -> None:
|
||||
th_download = Thread(target=model.downloadCTranslate2ModelWeight, args=(weight_type, callback, end_callback))
|
||||
th_download.daemon = True
|
||||
th_download.start()
|
||||
|
||||
@staticmethod
|
||||
def startThreadingDownloadWhisperWeight(weight_type:str, callback:Callable[[float], None], end_callback:Callable[[float], None]) -> None:
|
||||
def startThreadingDownloadWhisperWeight(weight_type:str, callback:Callable[[float], None], end_callback:Optional[Callable[..., None]] = None) -> None:
|
||||
th_download = Thread(target=model.downloadWhisperModelWeight, args=(weight_type, callback, end_callback))
|
||||
th_download.daemon = True
|
||||
th_download.start()
|
||||
@@ -2258,6 +2267,7 @@ class Controller:
|
||||
@staticmethod
|
||||
def setWatchdogCallback(callback) -> dict:
|
||||
model.setWatchdogCallback(callback)
|
||||
return {"status":200, "result":True}
|
||||
|
||||
@staticmethod
|
||||
def stopWatchdog(*args, **kwargs) -> dict:
|
||||
|
||||
Reference in New Issue
Block a user