voice-changer/server/data/ModelSlot.py

139 lines
4.1 KiB
Python
Raw Normal View History

2023-06-16 10:49:55 +03:00
from typing import TypeAlias, Union
2023-06-16 11:12:03 +03:00
from const import MAX_SLOT_NUM, EnumInferenceTypes, EnumEmbedderTypes, VoiceChangerType
2023-06-16 10:49:55 +03:00
2023-06-23 08:00:40 +03:00
from dataclasses import dataclass, asdict, field
2023-06-16 10:49:55 +03:00
import os
import json
@dataclass
class ModelSlot:
voiceChangerType: VoiceChangerType | None = None
2023-06-21 01:23:13 +03:00
name: str = ""
description: str = ""
credit: str = ""
termsOfUseUrl: str = ""
iconFile: str = ""
2023-06-23 08:00:40 +03:00
speakers: dict = field(default_factory=lambda: {})
2023-06-16 10:49:55 +03:00
@dataclass
class RVCModelSlot(ModelSlot):
voiceChangerType: VoiceChangerType = "RVC"
modelFile: str = ""
indexFile: str = ""
defaultTune: int = 0
defaultIndexRatio: int = 1
defaultProtect: float = 0.5
isONNX: bool = False
modelType: str = EnumInferenceTypes.pyTorchRVC.value
samplingRate: int = -1
f0: bool = True
embChannels: int = 256
embOutputLayer: int = 9
useFinalProj: bool = True
deprecated: bool = False
embedder: str = EnumEmbedderTypes.hubert.value
sampleId: str = ""
2023-06-23 08:00:40 +03:00
speakers: dict = field(default_factory=lambda: {0: "target"})
2023-06-16 10:49:55 +03:00
2023-06-20 00:39:39 +03:00
@dataclass
class MMVCv13ModelSlot(ModelSlot):
voiceChangerType: VoiceChangerType = "MMVCv13"
modelFile: str = ""
configFile: str = ""
srcId: int = 107
dstId: int = 100
isONNX: bool = False
samplingRate: int = 24000
2023-06-23 08:00:40 +03:00
speakers: dict = field(default_factory=lambda: {107: "user", 100: "zundamon", 101: "sora", 102: "methane", 103: "tsumugi"})
2023-06-20 00:39:39 +03:00
@dataclass
class MMVCv15ModelSlot(ModelSlot):
voiceChangerType: VoiceChangerType = "MMVCv15"
modelFile: str = ""
configFile: str = ""
srcId: int = 0
dstId: int = 101
2023-06-21 01:23:13 +03:00
f0Factor: float = 1.0
2023-06-20 00:39:39 +03:00
isONNX: bool = False
samplingRate: int = 24000
2023-06-23 08:00:40 +03:00
speakers: dict = field(default_factory=lambda: {0: "user", 101: "zundamon", 102: "sora", 103: "methane", 104: "tsumugi"})
2023-06-20 00:39:39 +03:00
@dataclass
class SoVitsSvc40ModelSlot(ModelSlot):
voiceChangerType: VoiceChangerType = "so-vits-svc-40"
modelFile: str = ""
configFile: str = ""
clusterFile: str = ""
dstId: int = 0
isONNX: bool = False
sampleId: str = ""
2023-06-21 01:23:13 +03:00
defaultTune: int = 0
defaultClusterInferRatio: float = 0.0
noiseScale: float = 0.0
2023-06-23 08:00:40 +03:00
speakers: dict = field(default_factory=lambda: {1: "user"})
2023-06-20 00:39:39 +03:00
@dataclass
class DDSPSVCModelSlot(ModelSlot):
voiceChangerType: VoiceChangerType = "DDSP-SVC"
modelFile: str = ""
configFile: str = ""
diffModelFile: str = ""
diffConfigFile: str = ""
dstId: int = 0
isONNX: bool = False
sampleId: str = ""
2023-06-21 01:23:13 +03:00
defaultTune: int = 0
enhancer: bool = False
diffusion: bool = True
acc: int = 20
kstep: int = 100
2023-06-23 08:00:40 +03:00
speakers: dict = field(default_factory=lambda: {1: "user"})
2023-06-20 00:39:39 +03:00
ModelSlots: TypeAlias = Union[ModelSlot, RVCModelSlot, MMVCv13ModelSlot, MMVCv15ModelSlot, SoVitsSvc40ModelSlot, DDSPSVCModelSlot]
2023-06-16 10:49:55 +03:00
def loadSlotInfo(model_dir: str, slotIndex: int) -> ModelSlots:
slotDir = os.path.join(model_dir, str(slotIndex))
jsonFile = os.path.join(slotDir, "params.json")
if not os.path.exists(jsonFile):
return ModelSlot()
jsonDict = json.load(open(os.path.join(slotDir, "params.json")))
slotInfo = ModelSlot(**{k: v for k, v in jsonDict.items() if k in ModelSlot.__annotations__})
if slotInfo.voiceChangerType == "RVC":
return RVCModelSlot(**jsonDict)
2023-06-20 00:39:39 +03:00
elif slotInfo.voiceChangerType == "MMVCv13":
return MMVCv13ModelSlot(**jsonDict)
elif slotInfo.voiceChangerType == "MMVCv15":
return MMVCv15ModelSlot(**jsonDict)
elif slotInfo.voiceChangerType == "so-vits-svc-40":
return SoVitsSvc40ModelSlot(**jsonDict)
elif slotInfo.voiceChangerType == "DDSP-SVC":
return DDSPSVCModelSlot(**jsonDict)
2023-06-16 10:49:55 +03:00
else:
return ModelSlot()
2023-06-16 11:12:03 +03:00
def loadAllSlotInfo(model_dir: str):
slotInfos: list[ModelSlots] = []
for slotIndex in range(MAX_SLOT_NUM):
slotInfo = loadSlotInfo(model_dir, slotIndex)
slotInfos.append(slotInfo)
return slotInfos
2023-06-16 10:49:55 +03:00
def saveSlotInfo(model_dir: str, slotIndex: int, slotInfo: ModelSlots):
slotDir = os.path.join(model_dir, str(slotIndex))
json.dump(asdict(slotInfo), open(os.path.join(slotDir, "params.json"), "w"))