👍️[Update] Model : tauri -> python のencode処理を整理
This commit is contained in:
@@ -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)
|
||||
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)
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
Reference in New Issue
Block a user