Spaces:
Sleeping
Sleeping
File size: 7,092 Bytes
2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 55b9bab fea49f2 55b9bab 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 55b9bab fea49f2 55b9bab fea49f2 55b9bab fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 fea49f2 2376414 | 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | """
Model storage module: persist voice reference files to HuggingFace Dataset repo.
"""
import os
import logging
from datetime import datetime
logger = logging.getLogger(__name__)
MODELS_REPO_ID = None
LOCAL_MODELS_DIR = "/tmp/rvc_models"
def init_storage(repo_id):
"""Initialize storage with the HF dataset repo ID."""
global MODELS_REPO_ID
MODELS_REPO_ID = repo_id
os.makedirs(LOCAL_MODELS_DIR, exist_ok=True)
logger.info("Storage initialized with repo: {}".format(repo_id))
def upload_model(model_name, pth_path, index_path=None, big_npy_path=None, reference_path=None):
"""Upload model files to HF dataset repo."""
if not MODELS_REPO_ID:
logger.warning("No HF repo configured. Model saved locally only.")
return False
try:
from huggingface_hub import HfApi
api = HfApi()
# Upload .pth marker
if pth_path and os.path.exists(pth_path):
api.upload_file(
path_or_fileobj=pth_path,
path_in_repo="models/{}/{}.pth".format(model_name, model_name),
repo_id=MODELS_REPO_ID,
repo_type="dataset",
)
logger.info("Uploaded {}.pth to HF".format(model_name))
# Upload reference audio
if reference_path and os.path.exists(reference_path):
ref_filename = os.path.basename(reference_path)
api.upload_file(
path_or_fileobj=reference_path,
path_in_repo="models/{}/{}".format(model_name, ref_filename),
repo_id=MODELS_REPO_ID,
repo_type="dataset",
)
logger.info("Uploaded {} to HF".format(ref_filename))
# Upload .index file if exists (backward compat)
if index_path and os.path.exists(index_path):
api.upload_file(
path_or_fileobj=index_path,
path_in_repo="models/{}/{}.index".format(model_name, model_name),
repo_id=MODELS_REPO_ID,
repo_type="dataset",
)
# Upload metadata
metadata = {
"name": model_name,
"created": datetime.now().isoformat(),
"engine": "seed-vc",
}
import json
import tempfile
with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
json.dump(metadata, f)
meta_path = f.name
try:
api.upload_file(
path_or_fileobj=meta_path,
path_in_repo="models/{}/metadata.json".format(model_name),
repo_id=MODELS_REPO_ID,
repo_type="dataset",
)
finally:
os.unlink(meta_path)
return True
except Exception as e:
logger.error("Failed to upload model: {}".format(e))
return False
def download_model(model_name):
"""Download model from HF dataset repo. Returns (pth_path, reference_path)."""
if not MODELS_REPO_ID:
return _get_local_model(model_name)
try:
from huggingface_hub import hf_hub_download
local_dir = os.path.join(LOCAL_MODELS_DIR, model_name)
os.makedirs(local_dir, exist_ok=True)
pth_path = hf_hub_download(
repo_id=MODELS_REPO_ID,
repo_type="dataset",
filename="models/{}/{}.pth".format(model_name, model_name),
local_dir=local_dir,
)
# Try to download reference audio
ref_path = None
try:
ref_path = hf_hub_download(
repo_id=MODELS_REPO_ID,
repo_type="dataset",
filename="models/{}/{}_ref.wav".format(model_name, model_name),
local_dir=local_dir,
)
except Exception:
# Try .index for backward compat with old RVC models
try:
ref_path = hf_hub_download(
repo_id=MODELS_REPO_ID,
repo_type="dataset",
filename="models/{}/{}.index".format(model_name, model_name),
local_dir=local_dir,
)
except Exception:
pass
return pth_path, ref_path
except Exception as e:
logger.error("Failed to download model from HF: {}".format(e))
return _get_local_model(model_name)
def _get_local_model(model_name):
"""Get model from local storage."""
local_dir = os.path.join(LOCAL_MODELS_DIR, model_name)
pth_path = os.path.join(local_dir, "{}.pth".format(model_name))
ref_path = os.path.join(local_dir, "{}_ref.wav".format(model_name))
if os.path.exists(pth_path):
return pth_path, ref_path if os.path.exists(ref_path) else None
return None, None
def get_reference_path(model_name):
"""Get the reference audio path for a model."""
local_dir = os.path.join(LOCAL_MODELS_DIR, model_name)
ref_path = os.path.join(local_dir, "{}_ref.wav".format(model_name))
if os.path.exists(ref_path):
return ref_path
# Search in subdirectories (HF download structure)
for root, dirs, files in os.walk(local_dir):
for f in files:
if f.endswith("_ref.wav"):
return os.path.join(root, f)
return None
def list_models():
"""List all available models."""
models = set()
if MODELS_REPO_ID:
try:
from huggingface_hub import HfApi
api = HfApi()
files = api.list_repo_files(MODELS_REPO_ID, repo_type="dataset")
for f in files:
if f.startswith("models/") and f.endswith(".pth"):
parts = f.split("/")
if len(parts) >= 3:
models.add(parts[1])
except Exception as e:
logger.error("Failed to list models from HF: {}".format(e))
if os.path.exists(LOCAL_MODELS_DIR):
for name in os.listdir(LOCAL_MODELS_DIR):
model_dir = os.path.join(LOCAL_MODELS_DIR, name)
if os.path.isdir(model_dir):
pth = os.path.join(model_dir, "{}.pth".format(name))
if os.path.exists(pth):
models.add(name)
return sorted(models)
def delete_model(model_name):
"""Delete a model from HF repo and local storage."""
if MODELS_REPO_ID:
try:
from huggingface_hub import HfApi
api = HfApi()
files = api.list_repo_files(MODELS_REPO_ID, repo_type="dataset")
for f in files:
if f.startswith("models/{}/".format(model_name)):
api.delete_file(f, MODELS_REPO_ID, repo_type="dataset")
logger.info("Deleted {} from HF repo".format(model_name))
except Exception as e:
logger.error("Failed to delete from HF: {}".format(e))
import shutil
local_dir = os.path.join(LOCAL_MODELS_DIR, model_name)
if os.path.exists(local_dir):
shutil.rmtree(local_dir)
logger.info("Deleted {} from local storage".format(model_name))
return True
|