2023-05-07 23:51:24 +03:00
|
|
|
import json
|
2023-03-13 15:07:35 +03:00
|
|
|
import os
|
2023-01-08 10:18:20 +03:00
|
|
|
from typing import Union
|
2022-12-31 12:56:23 +03:00
|
|
|
from fastapi import APIRouter
|
|
|
|
from fastapi.encoders import jsonable_encoder
|
|
|
|
from fastapi.responses import JSONResponse
|
2023-04-27 17:38:25 +03:00
|
|
|
from fastapi import UploadFile, File, Form
|
2022-12-31 12:56:23 +03:00
|
|
|
|
2022-12-31 14:25:28 +03:00
|
|
|
from restapi.mods.FileUploader import upload_file, concat_file_chunks
|
2022-12-31 12:56:23 +03:00
|
|
|
from voice_changer.VoiceChangerManager import VoiceChangerManager
|
|
|
|
|
2023-07-12 18:59:48 +03:00
|
|
|
from const import MODEL_DIR, UPLOAD_DIR
|
2023-06-21 03:18:51 +03:00
|
|
|
from voice_changer.utils.LoadModelParams import LoadModelParamFile, LoadModelParams
|
2023-04-28 00:39:51 +03:00
|
|
|
|
2023-04-27 17:38:25 +03:00
|
|
|
|
2023-01-14 19:30:46 +03:00
|
|
|
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
|
|
|
os.makedirs(MODEL_DIR, exist_ok=True)
|
2022-12-31 12:56:23 +03:00
|
|
|
|
2023-03-13 15:07:35 +03:00
|
|
|
|
2022-12-31 12:56:23 +03:00
|
|
|
class MMVC_Rest_Fileuploader:
|
2023-03-13 15:07:35 +03:00
|
|
|
def __init__(self, voiceChangerManager: VoiceChangerManager):
|
2022-12-31 12:56:23 +03:00
|
|
|
self.voiceChangerManager = voiceChangerManager
|
|
|
|
self.router = APIRouter()
|
2023-01-07 18:25:21 +03:00
|
|
|
self.router.add_api_route("/info", self.get_info, methods=["GET"])
|
2023-05-06 22:18:18 +03:00
|
|
|
self.router.add_api_route("/performance", self.get_performance, methods=["GET"])
|
2023-06-17 04:08:32 +03:00
|
|
|
self.router.add_api_route("/upload_file", self.post_upload_file, methods=["POST"])
|
|
|
|
self.router.add_api_route("/concat_uploaded_file", self.post_concat_uploaded_file, methods=["POST"])
|
|
|
|
self.router.add_api_route("/update_settings", self.post_update_settings, methods=["POST"])
|
2022-12-31 12:56:23 +03:00
|
|
|
self.router.add_api_route("/load_model", self.post_load_model, methods=["POST"])
|
2023-04-13 02:00:28 +03:00
|
|
|
self.router.add_api_route("/onnx", self.get_onnx, methods=["GET"])
|
2023-06-17 04:08:32 +03:00
|
|
|
self.router.add_api_route("/merge_model", self.post_merge_models, methods=["POST"])
|
|
|
|
self.router.add_api_route("/update_model_default", self.post_update_model_default, methods=["POST"])
|
|
|
|
self.router.add_api_route("/update_model_info", self.post_update_model_info, methods=["POST"])
|
|
|
|
self.router.add_api_route("/upload_model_assets", self.post_upload_model_assets, methods=["POST"])
|
2022-12-31 12:56:23 +03:00
|
|
|
|
|
|
|
def post_upload_file(self, file: UploadFile = File(...), filename: str = Form(...)):
|
2023-06-17 04:08:32 +03:00
|
|
|
try:
|
|
|
|
res = upload_file(UPLOAD_DIR, file, filename)
|
|
|
|
json_compatible_item_data = jsonable_encoder(res)
|
|
|
|
return JSONResponse(content=json_compatible_item_data)
|
|
|
|
except Exception as e:
|
2023-06-22 03:21:39 +03:00
|
|
|
print("[Voice Changer] post_upload_file ex:", e)
|
2023-06-17 04:08:32 +03:00
|
|
|
|
|
|
|
def post_concat_uploaded_file(self, filename: str = Form(...), filenameChunkNum: int = Form(...)):
|
|
|
|
try:
|
|
|
|
res = concat_file_chunks(UPLOAD_DIR, filename, filenameChunkNum, UPLOAD_DIR)
|
|
|
|
json_compatible_item_data = jsonable_encoder(res)
|
|
|
|
return JSONResponse(content=json_compatible_item_data)
|
|
|
|
except Exception as e:
|
2023-06-22 03:21:39 +03:00
|
|
|
print("[Voice Changer] post_concat_uploaded_file ex:", e)
|
2023-03-13 15:07:35 +03:00
|
|
|
|
2023-01-07 18:25:21 +03:00
|
|
|
def get_info(self):
|
2023-06-17 04:08:32 +03:00
|
|
|
try:
|
|
|
|
info = self.voiceChangerManager.get_info()
|
|
|
|
json_compatible_item_data = jsonable_encoder(info)
|
|
|
|
return JSONResponse(content=json_compatible_item_data)
|
|
|
|
except Exception as e:
|
2023-06-22 03:21:39 +03:00
|
|
|
print("[Voice Changer] get_info ex:", e)
|
2023-01-07 18:25:21 +03:00
|
|
|
|
2023-05-06 22:18:18 +03:00
|
|
|
def get_performance(self):
|
2023-06-17 04:08:32 +03:00
|
|
|
try:
|
|
|
|
info = self.voiceChangerManager.get_performance()
|
|
|
|
json_compatible_item_data = jsonable_encoder(info)
|
|
|
|
return JSONResponse(content=json_compatible_item_data)
|
|
|
|
except Exception as e:
|
2023-06-22 03:21:39 +03:00
|
|
|
print("[Voice Changer] get_performance ex:", e)
|
2023-06-17 04:08:32 +03:00
|
|
|
|
|
|
|
def post_update_settings(self, key: str = Form(...), val: Union[int, str, float] = Form(...)):
|
|
|
|
try:
|
|
|
|
print("[Voice Changer] update configuration:", key, val)
|
|
|
|
info = self.voiceChangerManager.update_settings(key, val)
|
|
|
|
json_compatible_item_data = jsonable_encoder(info)
|
|
|
|
return JSONResponse(content=json_compatible_item_data)
|
|
|
|
except Exception as e:
|
2023-06-22 03:21:39 +03:00
|
|
|
print("[Voice Changer] post_update_settings ex:", e)
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2023-01-08 10:18:20 +03:00
|
|
|
|
2022-12-31 12:56:23 +03:00
|
|
|
def post_load_model(
|
|
|
|
self,
|
2023-04-15 18:38:48 +03:00
|
|
|
slot: int = Form(...),
|
2023-04-07 21:56:40 +03:00
|
|
|
isHalf: bool = Form(...),
|
2023-04-21 23:42:24 +03:00
|
|
|
params: str = Form(...),
|
2022-12-31 12:56:23 +03:00
|
|
|
):
|
2023-06-17 04:08:32 +03:00
|
|
|
try:
|
|
|
|
paramDict = json.loads(params)
|
2023-06-17 09:35:43 +03:00
|
|
|
print("paramDict", paramDict)
|
2023-06-21 03:18:51 +03:00
|
|
|
loadModelparams = LoadModelParams(**paramDict)
|
|
|
|
loadModelparams.files = [LoadModelParamFile(**x) for x in paramDict["files"]]
|
|
|
|
# print("paramDict", loadModelparams)
|
2023-06-17 04:08:32 +03:00
|
|
|
|
2023-06-21 03:18:51 +03:00
|
|
|
info = self.voiceChangerManager.loadModel(loadModelparams)
|
2023-06-17 04:08:32 +03:00
|
|
|
json_compatible_item_data = jsonable_encoder(info)
|
|
|
|
return JSONResponse(content=json_compatible_item_data)
|
|
|
|
except Exception as e:
|
2023-06-22 03:21:39 +03:00
|
|
|
print("[Voice Changer] post_load_model ex:", e)
|
2023-07-19 04:20:30 +03:00
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
|
2023-04-13 02:00:28 +03:00
|
|
|
def get_onnx(self):
|
2023-06-17 04:08:32 +03:00
|
|
|
try:
|
|
|
|
info = self.voiceChangerManager.export2onnx()
|
|
|
|
json_compatible_item_data = jsonable_encoder(info)
|
|
|
|
return JSONResponse(content=json_compatible_item_data)
|
|
|
|
except Exception as e:
|
2023-06-22 03:21:39 +03:00
|
|
|
print("[Voice Changer] get_onnx ex:", e)
|
2023-09-09 23:48:34 +03:00
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2023-04-30 20:34:01 +03:00
|
|
|
|
|
|
|
def post_merge_models(self, request: str = Form(...)):
|
2023-06-17 04:08:32 +03:00
|
|
|
try:
|
|
|
|
print(request)
|
|
|
|
info = self.voiceChangerManager.merge_models(request)
|
|
|
|
json_compatible_item_data = jsonable_encoder(info)
|
|
|
|
return JSONResponse(content=json_compatible_item_data)
|
|
|
|
except Exception as e:
|
2023-06-22 03:21:39 +03:00
|
|
|
print("[Voice Changer] post_merge_models ex:", e)
|
2023-08-04 21:02:43 +03:00
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2023-05-20 22:21:54 +03:00
|
|
|
|
|
|
|
def post_update_model_default(self):
|
2023-06-17 04:08:32 +03:00
|
|
|
try:
|
|
|
|
info = self.voiceChangerManager.update_model_default()
|
|
|
|
json_compatible_item_data = jsonable_encoder(info)
|
|
|
|
return JSONResponse(content=json_compatible_item_data)
|
|
|
|
except Exception as e:
|
2023-06-22 03:21:39 +03:00
|
|
|
print("[Voice Changer] post_update_model_default ex:", e)
|
2023-07-17 01:21:06 +03:00
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
2023-06-07 21:08:59 +03:00
|
|
|
|
|
|
|
def post_update_model_info(self, newData: str = Form(...)):
|
2023-06-17 04:08:32 +03:00
|
|
|
try:
|
|
|
|
info = self.voiceChangerManager.update_model_info(newData)
|
|
|
|
json_compatible_item_data = jsonable_encoder(info)
|
|
|
|
return JSONResponse(content=json_compatible_item_data)
|
|
|
|
except Exception as e:
|
2023-06-22 03:21:39 +03:00
|
|
|
print("[Voice Changer] post_update_model_info ex:", e)
|
2023-06-07 21:08:59 +03:00
|
|
|
|
|
|
|
def post_upload_model_assets(self, params: str = Form(...)):
|
2023-06-17 04:08:32 +03:00
|
|
|
try:
|
|
|
|
info = self.voiceChangerManager.upload_model_assets(params)
|
|
|
|
json_compatible_item_data = jsonable_encoder(info)
|
|
|
|
return JSONResponse(content=json_compatible_item_data)
|
|
|
|
except Exception as e:
|
2023-06-22 03:21:39 +03:00
|
|
|
print("[Voice Changer] post_update_model_info ex:", e)
|