File size: 4,962 Bytes
a42ddbb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | # model_manager.py
import torch
from TTS.api import TTS
from voice.clearvoice import ClearVoice
import os
os.environ["COQUI_TOS_AGREED"] = "1"
_model_cache = {}
def load_model():
global _model_cache
"""Load and cache all necessary models."""
device = "cuda" if torch.cuda.is_available() else "cpu"
if "xtts_v2" not in _model_cache:
_model_cache["xtts_v2"] = TTS(
"tts_models/multilingual/multi-dataset/xtts_v2"
).to(device)
if "FRCRN_SE_16K" not in _model_cache:
_model_cache["FRCRN_SE_16K"] = ClearVoice(
task="speech_enhancement", model_names=["FRCRN_SE_16K"]
)
if "MossFormer2_SE_48K" not in _model_cache:
_model_cache["MossFormer2_SE_48K"] = ClearVoice(
task="speech_enhancement", model_names=["MossFormer2_SE_48K"]
)
if "MossFormer2_SS_16K" not in _model_cache:
_model_cache["MossFormer2_SS_16K"] = ClearVoice(
task="speech_separation", model_names=["MossFormer2_SS_16K"]
)
if "AV_MossFormer2_TSE_16K" not in _model_cache:
_model_cache["AV_MossFormer2_TSE_16K"] = ClearVoice(
task="target_speaker_extraction", model_names=["AV_MossFormer2_TSE_16K"]
)
if "MossFormer2_SR_48K" not in _model_cache:
_model_cache["MossFormer2_SR_48K"] = ClearVoice(
task="speech_super_resolution", model_names=["MossFormer2_SR_48K"]
)
return _model_cache
def unload_model():
"""Clear cached models and free up memory."""
_model_cache.clear()
if torch.cuda.is_available():
torch.cuda.empty_cache()
def predict(task: str, **kwargs):
"""
Central prediction dispatcher based on task type.
Args:
task (str): Task type key.
kwargs: Task-specific arguments.
Returns:
Result of the model inference.
"""
if not _model_cache:
load_model()
if task == "clone_voice":
model = _model_cache["xtts_v2"]
return model.tts_to_file(
text=kwargs["text"],
speaker_wav=kwargs["speaker_wav"],
language=kwargs.get("language", "en"),
file_path=kwargs["file_path"],
)
elif task == "speech_enhancement_16k":
model = _model_cache["FRCRN_SE_16K"]
return model(
input_path=kwargs["input_path"],
online_write=kwargs.get("online_write", False),
)
elif task == "speech_enhancement_48k":
model = _model_cache["MossFormer2_SE_48K"]
return model(
input_path=kwargs["input_path"],
online_write=kwargs.get("online_write", False),
output_path=kwargs.get("output_path"),
)
elif task == "speech_separation":
model = _model_cache["MossFormer2_SS_16K"]
return model(
input_path=kwargs["input_path"],
online_write=kwargs.get("online_write", False),
)
elif task == "target_speaker_extraction":
model = _model_cache["AV_MossFormer2_TSE_16K"]
return model(
input_path=kwargs["input_path"],
online_write=True,
output_path=kwargs["output_path"],
)
elif task == "speech_super_resolution":
model = _model_cache["MossFormer2_SR_48K"]
return model(
input_path=kwargs["input_path"],
online_write=kwargs.get("online_write", False),
)
else:
raise ValueError(f"Unknown task type: {task}")
# from model_manager import predict
# # Assume you have speaker and output audio in bytes
# with open("speaker.wav", "rb") as f:
# speaker_bytes = f.read()
# text_to_speak = "Hello, this is a cloned voice."
# output_path = "cloned_voice.wav"
# predict(
# task="clone_voice",
# text=text_to_speak,
# speaker_wav=speaker_bytes,
# language="en",
# file_path=output_path
# )
# from model_manager import predict
# with open("noisy_16k.wav", "rb") as f:
# audio_bytes = f.read()
# predict(
# task="speech_enhancement_16k",
# input_audio=audio_bytes
# )
# from model_manager import predict
# with open("noisy_48k.wav", "rb") as f:
# audio_bytes = f.read()
# predict(
# task="speech_enhancement_48k",
# input_audio=audio_bytes,
# output_path="enhanced_48k.wav"
# )
# from model_manager import predict
# with open("overlapping_speakers.wav", "rb") as f:
# audio_bytes = f.read()
# predict(
# task="speech_separation",
# input_audio=audio_bytes
# )
# from model_manager import predict
# with open("mixture_with_target.wav", "rb") as f:
# audio_bytes = f.read()
# predict(
# task="target_speaker_extraction",
# input_audio=audio_bytes,
# output_path="target_extracted.wav"
# )
# from model_manager import predict
# with open("low_quality_48k.wav", "rb") as f:
# audio_bytes = f.read()
# predict(
# task="speech_super_resolution",
# input_audio=audio_bytes
# )
|