From 7ae86268a9df40708e6fde85cc68d297bf2111eb Mon Sep 17 00:00:00 2001 From: misyaguziya Date: Tue, 3 Sep 2024 18:18:58 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8D=EF=B8=8F[Update]=20Model=20:=20tau?= =?UTF-8?q?ri=20->=20python=20=E3=81=AEencode=E5=87=A6=E7=90=86=E3=82=92?= =?UTF-8?q?=E6=95=B4=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/utils.py | 29 ++++++++++- src-python/webui_controller.py | 18 ++----- src-python/webui_mainloop.py | 82 +++++++----------------------- src-ui/logics/useStdoutToPython.js | 2 +- 4 files changed, 51 insertions(+), 80 deletions(-) diff --git a/src-python/utils.py b/src-python/utils.py index f1ab1c5e..2ff40bb8 100644 --- a/src-python/utils.py +++ b/src-python/utils.py @@ -1,3 +1,4 @@ +import base64 from typing import Any import json import random @@ -73,5 +74,31 @@ def splitList(lst:list, split_count:int, to_shuffle:bool=False): split_lists.append(sub_list) return split_lists +def encodeBase64(data:str) -> dict: + return json.loads(base64.b64decode(data).decode('utf-8')) + def printLog(log:str, data:Any=None) -> None: - print(json.dumps({"status":348, "log":log, "data":str(data)}), flush=True) \ No newline at end of file + response = { + "status": 348, + "log": log, + "data": data, + } + + with open('process.log', 'a', encoding="utf-8") as f: + f.write(f"log: {response}\n") + + response = json.dumps(response) + print(response, flush=True) + +def printResponse(status:int, endpoint:str, result:Any=None) -> None: + response = { + "status": status, + "endpoint": endpoint, + "result": result, + } + + with open('process.log', 'a', encoding="utf-8") as f: + f.write(f"log: {response}\n") + + response = json.dumps(response) + print(response, flush=True) \ No newline at end of file diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index 12660461..afef99ad 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -5,7 +5,7 @@ from subprocess import Popen from threading import Thread from config import config from model import model -from utils import getKeyByValue, isUniqueStrings, printLog +from utils import getKeyByValue, isUniqueStrings, printLog, printResponse # Common class DownloadSoftwareProgressBar: @@ -1282,13 +1282,7 @@ def init(endpoints:dict, *args, **kwargs) -> None: printLog("Check Downloaded CTranslate2 Model Weight") if config.USE_TRANSLATION_FEATURE is True and model.checkCTranslatorCTranslate2ModelWeight() is False: def callback(progress): - print(json.dumps({ - "endpoint":endpoints["ctranslate2"], - "status":200, - "result":{ - "progress":progress - } - }), flush=True) + printResponse(200, endpoints["ctranslate2"], {"progress":progress}) printLog("Download CTranslate2 Model Weight") model.downloadCTranslate2ModelWeight(callback) @@ -1303,13 +1297,7 @@ def init(endpoints:dict, *args, **kwargs) -> None: printLog("Check Downloaded Whisper Model Weight") if config.USE_WHISPER_FEATURE is True and model.checkTranscriptionWhisperModelWeight() is False: def callback(progress): - print(json.dumps({ - "endpoint":endpoints["whisper"], - "status":200, - "result":{ - "progress":progress - } - }), flush=True) + printResponse(200, endpoints["whisper"], {"progress":progress}) model.downloadWhisperModelWeight(callback) # set word filter diff --git a/src-python/webui_mainloop.py b/src-python/webui_mainloop.py index 9adb6b8c..70f87475 100644 --- a/src-python/webui_mainloop.py +++ b/src-python/webui_mainloop.py @@ -3,8 +3,7 @@ import json import time from config import config import webui_controller as controller -from utils import printLog -import base64 +from utils import printLog, printResponse, encodeBase64 config_mapping = { "/config/version": "VERSION", @@ -271,53 +270,30 @@ class Action: def transmit(self, key:str, data:dict) -> None: status = data.get("status", None) result = data.get("result", None) - response = { - "endpoint": self.endpoints[key], - "status": status, - "result": result, - } - response = json.dumps(response) - print(response, flush=True) + printResponse(status, self.endpoints[key], result) def main(): received_data = sys.stdin.readline().strip() received_data = json.loads(received_data) - with open('process.log', 'a') as f: - f.write(f"received_data: {received_data}\n") - if received_data: endpoint = received_data.get("endpoint", None) data = received_data.get("data", None) - if data is not None: - data = json.loads(base64.b64decode(data).decode('utf-8')) - - with open('process.log', 'a') as f: - f.write(f"received_data : endpoint: {endpoint}, data:{data}\n") + data = encodeBase64(data) if data is not None else None + printLog(endpoint, data) try: match endpoint.split("/")[1]: case "config": - result_data, status = handleConfigRequest(endpoint) + result, status = handleConfigRequest(endpoint) case "controller": - result_data, status = handleControllerRequest(endpoint, data) + result, status = handleControllerRequest(endpoint, data) case _: pass except Exception as e: - result_data = str(e) + result = str(e) status = 500 - - response = { - "status": status, - "endpoint": endpoint, - "result": result_data, - } - - response = json.dumps(response) - with open('process.log', 'a') as f: - f.write(f"response: {response}\n") - - print(response, flush=True) + printResponse(status, endpoint, result) if __name__ == "__main__": controller.init({ @@ -325,7 +301,7 @@ if __name__ == "__main__": "whisper": action_mapping["/controller/callback_download_whisper_weight"]["download"], }) - process = "main" + process = "test_all" match process: case "main": try: @@ -337,32 +313,18 @@ if __name__ == "__main__": traceback.print_exc(file=f) case "test": - response_data, status = handleControllerRequest("/controller/callback_download_ctranslate2_weight") - response = { - "status": status, - "endpoint": "/controller/callback_download_ctranslate2_weight", - "result": response_data, - } - response = json.dumps(response) - response_data, status = handleControllerRequest("/controller/callback_download_whisper_weight") - response = { - "status": status, - "endpoint": "/controller/callback_download_whisper_weight", - "result": response_data, - } - response = json.dumps(response) + 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) case "test_all": import time for endpoint, value in config_mapping.items(): - response_data, status = handleConfigRequest(endpoint) - response = { - "status": status, - "endpoint": endpoint, - "result": response_data, - } - response = json.dumps(response) - print(response, flush=True) + result, status = handleConfigRequest(endpoint) + printResponse(status, endpoint, result) time.sleep(0.1) for endpoint, value in controller_mapping.items(): @@ -454,12 +416,6 @@ if __name__ == "__main__": case _: data = None - response_data, status = handleControllerRequest(endpoint, data) - response = { - "status": status, - "endpoint": endpoint, - "result": response_data, - } - response = json.dumps(response) - print(response, flush=True) + result, status = handleControllerRequest(endpoint, data) + printResponse(status, endpoint, result) time.sleep(0.5) \ No newline at end of file diff --git a/src-ui/logics/useStdoutToPython.js b/src-ui/logics/useStdoutToPython.js index 553cf207..03dc39a7 100644 --- a/src-ui/logics/useStdoutToPython.js +++ b/src-ui/logics/useStdoutToPython.js @@ -4,7 +4,7 @@ import { encode } from 'js-base64' export const useStdoutToPython = () => { const asyncStdoutToPython = async (path, value) => { let send_object = { endpoint: path }; - if (value) send_object.data = encode(value); + if (value) send_object.data = encode(JSON.stringify(value)); // send to python const backend_subprocess = store.backend_subprocess;