-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmemo_model_manager.py
187 lines (162 loc) · 7.37 KB
/
memo_model_manager.py
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
#memo_model_manager.py
import os
import logging
import json
import shutil
from huggingface_hub import hf_hub_download
from modelscope import snapshot_download
import folder_paths
logger = logging.getLogger("memo")
class MemoModelManager:
def __init__(self):
self.models_base = folder_paths.models_dir
self._setup_paths()
self._ensure_model_structure()
def _setup_paths(self):
"""Initialize base paths structure"""
self.paths = {
"memo_base": os.path.join(self.models_base, "checkpoints", "memo"),
"wav2vec": os.path.join(self.models_base, "wav2vec", "facebook", "wav2vec2-base-960h"),
"emotion2vec": os.path.join(self.models_base, "emotion2vec", "iic", "emotion2vec_plus_large"),
"vae": os.path.join(self.models_base, "vae", "stabilityai", "sd-vae-ft-mse")
}
# Create directories
for path in self.paths.values():
os.makedirs(path, exist_ok=True)
# Create memo subfolders
for subdir in ["reference_net", "diffusion_net", "image_proj", "audio_proj",
"misc/audio_emotion_classifier", "misc/face_analysis", "misc/vocal_separator"]:
os.makedirs(os.path.join(self.paths["memo_base"], subdir), exist_ok=True)
def _direct_download(self, repo_id, filename, target_path, force=False):
"""Download directly to target path without extra nesting"""
try:
if not force and os.path.exists(target_path):
return target_path
download_path = hf_hub_download(
repo_id=repo_id,
filename=filename,
local_dir=os.path.dirname(target_path),
local_dir_use_symlinks=False
)
# Move if downloaded to wrong location
if download_path != target_path:
shutil.move(download_path, target_path)
return target_path
except Exception as e:
logger.warning(f"Failed to download {filename} from {repo_id} to {target_path}: {e}")
return None
def _setup_face_analysis(self):
"""Setup face analysis models with correct structure"""
face_dir = os.path.join(self.paths["memo_base"], "misc", "face_analysis")
models_dir = os.path.join(face_dir, "models") # Create a models subdirectory
os.makedirs(models_dir, exist_ok=True)
# Create models.json
models_json = {
"detection": ["scrfd_10g_bnkps"],
"recognition": ["glintr100"],
"analysis": ["genderage", "2d106det", "1k3d68"]
}
# Write models.json
models_json_path = os.path.join(face_dir, "models.json")
with open(models_json_path, "w") as f:
json.dump(models_json, f, indent=2)
# Create version.txt
version_path = os.path.join(face_dir, "version.txt")
with open(version_path, "w") as f:
f.write("0.7.3")
# Download model files if they don't exist
required_models = {
"scrfd_10g_bnkps.onnx": "scrfd_10g_bnkps",
"glintr100.onnx": "glintr100",
"genderage.onnx": "genderage",
"2d106det.onnx": "2d106det",
"1k3d68.onnx": "1k3d68",
"face_landmarker_v2_with_blendshapes.task": "face_landmarker"
}
for model_file, model_name in required_models.items():
target_path = os.path.join(models_dir, model_file) # Save in models subdirectory
if not os.path.exists(target_path):
self._direct_download(
"memoavatar/memo",
f"misc/face_analysis/models/{model_file}",
target_path
)
# Create symlink in parent directory for compatibility
parent_target = os.path.join(face_dir, model_file)
if not os.path.exists(parent_target):
if os.name == 'nt': # Windows
import shutil
shutil.copy2(target_path, parent_target)
else: # Unix-like
os.symlink(target_path, parent_target)
# Set environment variable for face models
os.environ["MEMO_FACE_MODELS"] = face_dir
return face_dir
def _ensure_model_structure(self):
"""Download all required models to correct locations"""
# Set up face analysis and environment variables first
face_dir = self._setup_face_analysis()
os.environ["MEMO_FACE_MODELS"] = face_dir
os.environ["MEMO_VOCAL_MODEL"] = os.path.join(
self.paths["memo_base"], "misc/vocal_separator/Kim_Vocal_2.onnx"
)
# Download memo components
components = {
"reference_net": ["config.json", "diffusion_pytorch_model.safetensors"],
"diffusion_net": ["config.json", "diffusion_pytorch_model.safetensors"],
"image_proj": ["config.json", "diffusion_pytorch_model.safetensors"],
"audio_proj": ["config.json", "diffusion_pytorch_model.safetensors"]
}
for component, files in components.items():
component_dir = os.path.join(self.paths["memo_base"], component)
for file in files:
self._direct_download(
"memoavatar/memo",
f"{component}/{file}",
os.path.join(component_dir, file)
)
# Download vocal separator
self._direct_download(
"memoavatar/memo",
"misc/vocal_separator/Kim_Vocal_2.onnx",
os.path.join(self.paths["memo_base"], "misc/vocal_separator/Kim_Vocal_2.onnx")
)
# Download emotion classifier
self._direct_download(
"memoavatar/memo",
"misc/audio_emotion_classifier/diffusion_pytorch_model.safetensors",
os.path.join(self.paths["memo_base"], "misc/audio_emotion_classifier/diffusion_pytorch_model.safetensors")
)
# Download wav2vec files
for file in ["config.json", "preprocessor_config.json", "pytorch_model.bin",
"special_tokens_map.json", "tokenizer_config.json", "vocab.json"]:
self._direct_download(
"facebook/wav2vec2-base-960h",
file,
os.path.join(self.paths["wav2vec"], file)
)
# Download emotion2vec
try:
snapshot_download(
"iic/emotion2vec_plus_large",
local_dir=self.paths["emotion2vec"]
)
except Exception as e:
logger.warning(f"Failed to download emotion2vec model: {e}")
# Download VAE
for file in ["config.json", "diffusion_pytorch_model.safetensors"]:
self._direct_download(
"stabilityai/sd-vae-ft-mse",
file,
os.path.join(self.paths["vae"], file)
)
def get_model_paths(self):
"""Return paths dictionary"""
return {
"memo_base": self.paths["memo_base"],
"face_models": os.path.join(self.paths["memo_base"], "misc/face_analysis"),
"vocal_separator": os.path.join(self.paths["memo_base"], "misc/vocal_separator/Kim_Vocal_2.onnx"),
"wav2vec": self.paths["wav2vec"],
"emotion2vec": self.paths["emotion2vec"],
"vae": self.paths["vae"]
}