voice-changer/server/restapi/MMVC_Rest_Fileuploader.py

150 lines
7.6 KiB
Python
Raw Normal View History

2023-03-13 15:07:35 +03:00
import os
import shutil
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
from fastapi import HTTPException, FastAPI, UploadFile, File, Form
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-04-10 18:21:17 +03:00
from const import MODEL_DIR, UPLOAD_DIR, ModelType
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"])
2022-12-31 12:56:23 +03:00
self.router.add_api_route("/upload_file", self.post_upload_file, methods=["POST"])
2023-01-07 18:25:21 +03:00
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"])
2022-12-31 13:04:53 +03:00
self.router.add_api_route("/load_model_for_train", self.post_load_model_for_train, methods=["POST"])
2023-01-07 18:25:21 +03:00
self.router.add_api_route("/extract_voices", self.post_extract_voices, methods=["POST"])
2023-04-10 18:21:17 +03:00
self.router.add_api_route("/model_type", self.post_model_type, methods=["POST"])
self.router.add_api_route("/model_type", self.get_model_type, methods=["GET"])
2023-04-13 02:00:28 +03:00
self.router.add_api_route("/onnx", self.get_onnx, methods=["GET"])
2022-12-31 12:56:23 +03:00
def post_upload_file(self, file: UploadFile = File(...), filename: str = Form(...)):
2023-01-10 16:49:16 +03:00
res = upload_file(UPLOAD_DIR, file, filename)
json_compatible_item_data = jsonable_encoder(res)
return JSONResponse(content=json_compatible_item_data)
2022-12-31 12:56:23 +03:00
2023-01-07 18:25:21 +03:00
def post_concat_uploaded_file(self, filename: str = Form(...), filenameChunkNum: int = Form(...)):
2023-04-14 03:18:34 +03:00
slot = 0
res = concat_file_chunks(slot, UPLOAD_DIR, filename, filenameChunkNum, UPLOAD_DIR)
2023-01-10 16:49:16 +03:00
json_compatible_item_data = jsonable_encoder(res)
return JSONResponse(content=json_compatible_item_data)
2023-03-13 15:07:35 +03:00
2023-01-07 18:25:21 +03:00
def get_info(self):
info = self.voiceChangerManager.get_info()
json_compatible_item_data = jsonable_encoder(info)
return JSONResponse(content=json_compatible_item_data)
def post_update_settings(self, key: str = Form(...), val: Union[int, str, float] = Form(...)):
print("post_update_settings", key, val)
info = self.voiceChangerManager.update_settings(key, val)
2023-01-08 10:18:20 +03:00
json_compatible_item_data = jsonable_encoder(info)
return JSONResponse(content=json_compatible_item_data)
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-01-07 18:25:21 +03:00
pyTorchModelFilename: str = Form(...),
onnxModelFilename: str = Form(...),
2023-03-13 15:07:35 +03:00
configFilename: str = Form(...),
clusterTorchModelFilename: str = Form(...),
2023-04-07 21:11:37 +03:00
featureFilename: str = Form(...),
indexFilename: str = Form(...),
2023-04-07 21:56:40 +03:00
isHalf: bool = Form(...),
2022-12-31 12:56:23 +03:00
):
2023-04-15 18:38:48 +03:00
# # Upload File Path
# pyTorchModelFilePath = os.path.join(UPLOAD_DIR, pyTorchModelFilename) if pyTorchModelFilename != "-" else None
# onnxModelFilePath = os.path.join(UPLOAD_DIR, onnxModelFilename) if onnxModelFilename != "-" else None
# configFilePath = os.path.join(UPLOAD_DIR, configFilename)
# clusterTorchModelFilePath = os.path.join(UPLOAD_DIR, clusterTorchModelFilename) if clusterTorchModelFilename != "-" else None
# featureFilePath = os.path.join(UPLOAD_DIR, featureFilename) if featureFilename != "-" else None
# indexFilePath = os.path.join(UPLOAD_DIR, indexFilename) if indexFilename != "-" else None
# # Stored File Path by Slot
# pyTorchModelStoredFilePath = os.path.join(UPLOAD_DIR, f"{slot}", pyTorchModelFilename) if pyTorchModelFilename != "-" else None
# onnxModelStoredFilePath = os.path.join(UPLOAD_DIR, f"{slot}", onnxModelFilename) if onnxModelFilename != "-" else None
# configStoredFilePath = os.path.join(UPLOAD_DIR, f"{slot}", configFilename)
# clusterTorchModelStoredFilePath = os.path.join(UPLOAD_DIR, f"{slot}", clusterTorchModelFilename) if clusterTorchModelFilename != "-" else None
# featureStoredFilePath = os.path.join(UPLOAD_DIR, f"{slot}", featureFilename) if featureFilename != "-" else None
# indexStoredFilePath = os.path.join(UPLOAD_DIR, f"{slot}", indexFilename) if indexFilename != "-" else None
# # Store File
# if pyTorchModelFilename != "-":
# pyTorchModelFilePath = os.path.join(UPLOAD_DIR, pyTorchModelFilename)
# pyTorchModelStoredFilePath = os.path.join(UPLOAD_DIR, f"{slot}", pyTorchModelFilename)
# shutil.move(pyTorchModelFilePath, pyTorchModelStoredFilePath)
paths = []
for x in [pyTorchModelFilename, onnxModelFilename, configFilename, clusterTorchModelFilename, featureFilename, indexFilename]:
if x != "-":
uploadPath = os.path.join(UPLOAD_DIR, x)
storeDir = os.path.join(UPLOAD_DIR, f"{slot}")
os.makedirs(storeDir, exist_ok=True)
storePath = os.path.join(storeDir, x)
shutil.move(uploadPath, storePath)
paths.push(storePath)
else:
paths.push(None)
pyTorchModelStoredFilePath, onnxModelStoredFilePath, configStoredFilePath, clusterTorchModelStoredFilePath, featureStoredFilePath, indexStoredFilePath = paths
info = self.voiceChangerManager.loadModel(slot, configStoredFilePath, pyTorchModelStoredFilePath, onnxModelStoredFilePath,
clusterTorchModelStoredFilePath, featureStoredFilePath, indexStoredFilePath,
2023-04-07 21:56:40 +03:00
isHalf)
2023-01-10 16:49:16 +03:00
json_compatible_item_data = jsonable_encoder(info)
return JSONResponse(content=json_compatible_item_data)
# return {"load": f"{configFilePath}, {pyTorchModelFilePath}, {onnxModelFilePath}"}
2022-12-31 12:56:23 +03:00
2022-12-31 13:04:53 +03:00
def post_load_model_for_train(
self,
modelGFilename: str = Form(...),
modelGFilenameChunkNum: int = Form(...),
modelDFilename: str = Form(...),
modelDFilenameChunkNum: int = Form(...),
):
modelGFilePath = concat_file_chunks(
UPLOAD_DIR, modelGFilename, modelGFilenameChunkNum, MODEL_DIR)
modelDFilePath = concat_file_chunks(
2023-03-13 15:07:35 +03:00
UPLOAD_DIR, modelDFilename, modelDFilenameChunkNum, MODEL_DIR)
2022-12-31 13:04:53 +03:00
return {"File saved": f"{modelGFilePath}, {modelDFilePath}"}
2022-12-31 12:56:23 +03:00
2023-01-07 18:25:21 +03:00
def post_extract_voices(
2022-12-31 13:04:53 +03:00
self,
zipFilename: str = Form(...),
zipFileChunkNum: int = Form(...),
):
zipFilePath = concat_file_chunks(
UPLOAD_DIR, zipFilename, zipFileChunkNum, UPLOAD_DIR)
shutil.unpack_archive(zipFilePath, "MMVC_Trainer/dataset/textful/")
2023-01-10 16:49:16 +03:00
return {"Zip file unpacked": f"{zipFilePath}"}
2023-04-10 18:21:17 +03:00
def post_model_type(
self,
modelType: ModelType = Form(...),
):
info = self.voiceChangerManager.switchModelType(modelType)
json_compatible_item_data = jsonable_encoder(info)
return JSONResponse(content=json_compatible_item_data)
def get_model_type(
self,
):
info = self.voiceChangerManager.getModelType(modelType)
json_compatible_item_data = jsonable_encoder(info)
return JSONResponse(content=json_compatible_item_data)
2023-04-13 02:00:28 +03:00
def get_onnx(self):
info = self.voiceChangerManager.export2onnx()
json_compatible_item_data = jsonable_encoder(info)
return JSONResponse(content=json_compatible_item_data)