voice-changer/server/voice_changer/RVC/pipeline/PipelineGenerator.py

76 lines
2.2 KiB
Python
Raw Normal View History

2023-05-04 17:50:52 +03:00
import os
import traceback
import faiss
2023-06-16 12:04:33 +03:00
from data.ModelSlot import RVCModelSlot
2023-05-04 17:50:52 +03:00
from voice_changer.RVC.deviceManager.DeviceManager import DeviceManager
from voice_changer.RVC.embedder.EmbedderManager import EmbedderManager
from voice_changer.RVC.inferencer.InferencerManager import InferencerManager
from voice_changer.RVC.pipeline.Pipeline import Pipeline
from voice_changer.RVC.pitchExtractor.PitchExtractorManager import PitchExtractorManager
2023-06-16 12:04:33 +03:00
def createPipeline(modelSlot: RVCModelSlot, gpu: int, f0Detector: str):
2023-05-04 17:50:52 +03:00
dev = DeviceManager.get_instance().getDevice(gpu)
half = DeviceManager.get_instance().halfPrecisionAvailable(gpu)
# Inferencer 生成
try:
2023-06-16 12:04:33 +03:00
inferencer = InferencerManager.getInferencer(modelSlot.modelType, modelSlot.modelFile, gpu)
2023-05-04 17:50:52 +03:00
except Exception as e:
print("[Voice Changer] exception! loading inferencer", e)
traceback.print_exc()
# Embedder 生成
try:
embedder = EmbedderManager.getEmbedder(
modelSlot.embedder,
# emmbedderFilename,
half,
dev,
)
except Exception as e:
print("[Voice Changer] exception! loading embedder", e)
traceback.print_exc()
# pitchExtractor
pitchExtractor = PitchExtractorManager.getPitchExtractor(f0Detector, gpu)
2023-05-04 17:50:52 +03:00
# index, feature
2023-05-24 09:09:25 +03:00
index = _loadIndex(modelSlot)
2023-05-04 17:50:52 +03:00
pipeline = Pipeline(
embedder,
inferencer,
pitchExtractor,
index,
modelSlot.samplingRate,
dev,
half,
)
return pipeline
2023-06-16 12:04:33 +03:00
def _loadIndex(modelSlot: RVCModelSlot):
2023-05-04 17:50:52 +03:00
# Indexのロード
print("[Voice Changer] Loading index...")
# ファイル指定がない場合はNone
2023-05-24 09:09:25 +03:00
if modelSlot.indexFile is None:
2023-05-14 22:24:58 +03:00
print("[Voice Changer] Index is None, not used")
2023-05-24 09:09:25 +03:00
return None
2023-05-04 17:50:52 +03:00
# ファイル指定があってもファイルがない場合はNone
2023-05-24 09:09:25 +03:00
if os.path.exists(modelSlot.indexFile) is not True:
return None
2023-05-04 17:50:52 +03:00
try:
2023-05-24 14:49:24 +03:00
print("Try loading...", modelSlot.indexFile)
2023-05-04 17:50:52 +03:00
index = faiss.read_index(modelSlot.indexFile)
except: # NOQA
2023-05-04 17:50:52 +03:00
print("[Voice Changer] load index failed. Use no index.")
traceback.print_exc()
2023-05-24 09:09:25 +03:00
return None
2023-05-04 17:50:52 +03:00
2023-05-24 09:09:25 +03:00
return index