👍️[Update] Model : tauri -> python のencode処理を整理
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import base64
|
||||||
from typing import Any
|
from typing import Any
|
||||||
import json
|
import json
|
||||||
import random
|
import random
|
||||||
@@ -73,5 +74,31 @@ def splitList(lst:list, split_count:int, to_shuffle:bool=False):
|
|||||||
split_lists.append(sub_list)
|
split_lists.append(sub_list)
|
||||||
return split_lists
|
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:
|
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 threading import Thread
|
||||||
from config import config
|
from config import config
|
||||||
from model import model
|
from model import model
|
||||||
from utils import getKeyByValue, isUniqueStrings, printLog
|
from utils import getKeyByValue, isUniqueStrings, printLog, printResponse
|
||||||
|
|
||||||
# Common
|
# Common
|
||||||
class DownloadSoftwareProgressBar:
|
class DownloadSoftwareProgressBar:
|
||||||
@@ -1282,13 +1282,7 @@ def init(endpoints:dict, *args, **kwargs) -> None:
|
|||||||
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):
|
def callback(progress):
|
||||||
print(json.dumps({
|
printResponse(200, endpoints["ctranslate2"], {"progress":progress})
|
||||||
"endpoint":endpoints["ctranslate2"],
|
|
||||||
"status":200,
|
|
||||||
"result":{
|
|
||||||
"progress":progress
|
|
||||||
}
|
|
||||||
}), flush=True)
|
|
||||||
printLog("Download CTranslate2 Model Weight")
|
printLog("Download CTranslate2 Model Weight")
|
||||||
model.downloadCTranslate2ModelWeight(callback)
|
model.downloadCTranslate2ModelWeight(callback)
|
||||||
|
|
||||||
@@ -1303,13 +1297,7 @@ def init(endpoints:dict, *args, **kwargs) -> None:
|
|||||||
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):
|
def callback(progress):
|
||||||
print(json.dumps({
|
printResponse(200, endpoints["whisper"], {"progress":progress})
|
||||||
"endpoint":endpoints["whisper"],
|
|
||||||
"status":200,
|
|
||||||
"result":{
|
|
||||||
"progress":progress
|
|
||||||
}
|
|
||||||
}), flush=True)
|
|
||||||
model.downloadWhisperModelWeight(callback)
|
model.downloadWhisperModelWeight(callback)
|
||||||
|
|
||||||
# set word filter
|
# set word filter
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ import json
|
|||||||
import time
|
import time
|
||||||
from config import config
|
from config import config
|
||||||
import webui_controller as controller
|
import webui_controller as controller
|
||||||
from utils import printLog
|
from utils import printLog, printResponse, encodeBase64
|
||||||
import base64
|
|
||||||
|
|
||||||
config_mapping = {
|
config_mapping = {
|
||||||
"/config/version": "VERSION",
|
"/config/version": "VERSION",
|
||||||
@@ -271,53 +270,30 @@ class Action:
|
|||||||
def transmit(self, key:str, data:dict) -> None:
|
def transmit(self, key:str, data:dict) -> None:
|
||||||
status = data.get("status", None)
|
status = data.get("status", None)
|
||||||
result = data.get("result", None)
|
result = data.get("result", None)
|
||||||
response = {
|
printResponse(status, self.endpoints[key], result)
|
||||||
"endpoint": self.endpoints[key],
|
|
||||||
"status": status,
|
|
||||||
"result": result,
|
|
||||||
}
|
|
||||||
response = json.dumps(response)
|
|
||||||
print(response, flush=True)
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
received_data = sys.stdin.readline().strip()
|
received_data = sys.stdin.readline().strip()
|
||||||
received_data = json.loads(received_data)
|
received_data = json.loads(received_data)
|
||||||
|
|
||||||
with open('process.log', 'a') as f:
|
|
||||||
f.write(f"received_data: {received_data}\n")
|
|
||||||
|
|
||||||
if received_data:
|
if received_data:
|
||||||
endpoint = received_data.get("endpoint", None)
|
endpoint = received_data.get("endpoint", None)
|
||||||
data = received_data.get("data", None)
|
data = received_data.get("data", None)
|
||||||
if data is not None:
|
data = encodeBase64(data) if data is not None else None
|
||||||
data = json.loads(base64.b64decode(data).decode('utf-8'))
|
printLog(endpoint, data)
|
||||||
|
|
||||||
with open('process.log', 'a') as f:
|
|
||||||
f.write(f"received_data : endpoint: {endpoint}, data:{data}\n")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
match endpoint.split("/")[1]:
|
match endpoint.split("/")[1]:
|
||||||
case "config":
|
case "config":
|
||||||
result_data, status = handleConfigRequest(endpoint)
|
result, status = handleConfigRequest(endpoint)
|
||||||
case "controller":
|
case "controller":
|
||||||
result_data, status = handleControllerRequest(endpoint, data)
|
result, status = handleControllerRequest(endpoint, data)
|
||||||
case _:
|
case _:
|
||||||
pass
|
pass
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
result_data = str(e)
|
result = str(e)
|
||||||
status = 500
|
status = 500
|
||||||
|
printResponse(status, endpoint, result)
|
||||||
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)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
controller.init({
|
controller.init({
|
||||||
@@ -325,7 +301,7 @@ if __name__ == "__main__":
|
|||||||
"whisper": action_mapping["/controller/callback_download_whisper_weight"]["download"],
|
"whisper": action_mapping["/controller/callback_download_whisper_weight"]["download"],
|
||||||
})
|
})
|
||||||
|
|
||||||
process = "main"
|
process = "test_all"
|
||||||
match process:
|
match process:
|
||||||
case "main":
|
case "main":
|
||||||
try:
|
try:
|
||||||
@@ -337,32 +313,18 @@ if __name__ == "__main__":
|
|||||||
traceback.print_exc(file=f)
|
traceback.print_exc(file=f)
|
||||||
|
|
||||||
case "test":
|
case "test":
|
||||||
response_data, status = handleControllerRequest("/controller/callback_download_ctranslate2_weight")
|
endpoint = "/controller/callback_download_ctranslate2_weight"
|
||||||
response = {
|
result, status = handleControllerRequest(endpoint)
|
||||||
"status": status,
|
printResponse(status, endpoint, result)
|
||||||
"endpoint": "/controller/callback_download_ctranslate2_weight",
|
endpoint = "/controller/callback_download_whisper_weight"
|
||||||
"result": response_data,
|
result, status = handleControllerRequest(endpoint)
|
||||||
}
|
printResponse(status, endpoint, result)
|
||||||
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)
|
|
||||||
|
|
||||||
case "test_all":
|
case "test_all":
|
||||||
import time
|
import time
|
||||||
for endpoint, value in config_mapping.items():
|
for endpoint, value in config_mapping.items():
|
||||||
response_data, status = handleConfigRequest(endpoint)
|
result, status = handleConfigRequest(endpoint)
|
||||||
response = {
|
printResponse(status, endpoint, result)
|
||||||
"status": status,
|
|
||||||
"endpoint": endpoint,
|
|
||||||
"result": response_data,
|
|
||||||
}
|
|
||||||
response = json.dumps(response)
|
|
||||||
print(response, flush=True)
|
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
for endpoint, value in controller_mapping.items():
|
for endpoint, value in controller_mapping.items():
|
||||||
@@ -454,12 +416,6 @@ if __name__ == "__main__":
|
|||||||
case _:
|
case _:
|
||||||
data = None
|
data = None
|
||||||
|
|
||||||
response_data, status = handleControllerRequest(endpoint, data)
|
result, status = handleControllerRequest(endpoint, data)
|
||||||
response = {
|
printResponse(status, endpoint, result)
|
||||||
"status": status,
|
|
||||||
"endpoint": endpoint,
|
|
||||||
"result": response_data,
|
|
||||||
}
|
|
||||||
response = json.dumps(response)
|
|
||||||
print(response, flush=True)
|
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
@@ -4,7 +4,7 @@ import { encode } from 'js-base64'
|
|||||||
export const useStdoutToPython = () => {
|
export const useStdoutToPython = () => {
|
||||||
const asyncStdoutToPython = async (path, value) => {
|
const asyncStdoutToPython = async (path, value) => {
|
||||||
let send_object = { endpoint: path };
|
let send_object = { endpoint: path };
|
||||||
if (value) send_object.data = encode(value);
|
if (value) send_object.data = encode(JSON.stringify(value));
|
||||||
|
|
||||||
// send to python
|
// send to python
|
||||||
const backend_subprocess = store.backend_subprocess;
|
const backend_subprocess = store.backend_subprocess;
|
||||||
|
|||||||
Reference in New Issue
Block a user