2023-05-02 06:11:00 +03:00
|
|
|
from typing import Any, Protocol
|
|
|
|
|
|
|
|
import torch
|
|
|
|
from torch import device
|
|
|
|
|
|
|
|
from const import EnumEmbedderTypes
|
|
|
|
|
|
|
|
|
|
|
|
class Embedder(Protocol):
|
|
|
|
embedderType: EnumEmbedderTypes = EnumEmbedderTypes.hubert
|
|
|
|
file: str
|
|
|
|
isHalf: bool = True
|
|
|
|
dev: device
|
|
|
|
|
|
|
|
model: Any | None = None
|
|
|
|
|
|
|
|
def loadModel(self, file: str, dev: device, isHalf: bool = True):
|
2023-05-02 14:57:12 +03:00
|
|
|
...
|
2023-05-02 06:11:00 +03:00
|
|
|
|
|
|
|
def extractFeatures(self, feats: torch.Tensor, embChannels=256) -> torch.Tensor:
|
|
|
|
...
|
|
|
|
|
2023-05-02 14:57:12 +03:00
|
|
|
def setProps(
|
|
|
|
self,
|
|
|
|
embedderType: EnumEmbedderTypes,
|
|
|
|
file: str,
|
|
|
|
dev: device,
|
|
|
|
isHalf: bool = True,
|
|
|
|
):
|
|
|
|
self.embedderType = embedderType
|
|
|
|
self.file = file
|
|
|
|
self.isHalf = isHalf
|
|
|
|
self.dev = dev
|
|
|
|
|
2023-05-02 06:11:00 +03:00
|
|
|
def setHalf(self, isHalf: bool):
|
|
|
|
self.isHalf = isHalf
|
|
|
|
if self.model is not None and isHalf:
|
|
|
|
self.model = self.model.half()
|
2023-05-03 07:14:00 +03:00
|
|
|
elif self.model is not None and isHalf is False:
|
|
|
|
self.model = self.model.float()
|
2023-05-02 06:11:00 +03:00
|
|
|
|
|
|
|
def setDevice(self, dev: device):
|
|
|
|
self.dev = dev
|
|
|
|
if self.model is not None:
|
|
|
|
self.model = self.model.to(self.dev)
|
2023-05-03 07:14:00 +03:00
|
|
|
return self
|
2023-05-02 06:11:00 +03:00
|
|
|
|
2023-05-04 16:46:42 +03:00
|
|
|
def matchCondition(self, embedderType: EnumEmbedderTypes) -> bool:
|
2023-05-02 06:11:00 +03:00
|
|
|
# Check Type
|
|
|
|
if self.embedderType != embedderType:
|
|
|
|
print(
|
|
|
|
"[Voice Changer] embeder type is not match",
|
|
|
|
self.embedderType,
|
|
|
|
embedderType,
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
|
|
|
|
else:
|
|
|
|
return True
|