Spaces:
Running on Zero
Running on Zero
Add startup extraction of shipped voice-model zips so conversions work after a container restart
Browse files1123456789101112131415161718192021222324252627282930313233343536373823456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778from pathlib import Pathfrom pathlib import Path
import requests
MDX_DOWNLOAD_LINK = 'https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/'
RVC_DOWNLOAD_LINK = 'https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/'
BASE_DIR = Path(__file__).resolve().parent.parent
mdxnet_models_dir = BASE_DIR / 'mdxnet_models'
rvc_models_dir = BASE_DIR / 'rvc_models'
mdxnet_models_dir.mkdir(parents=True, exist_ok=True)
rvc_models_dir.mkdir(parents=True, exist_ok=True)
def dl_model(link, model_name, dir_name):
model_path = dir_name / model_name
if model_path.exists():
# print(f"{model_name} already exists, skipping download.")
return
print(f"Downloading {model_name}...")
with requests.get(f'{link}{model_name}', stream=True) as r:
r.raise_for_status()
with open(model_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
if __name__ == '__main__':
mdx_model_names = ['UVR-MDX-NET-Inst_HQ_4.onnx', 'UVR-MDX-NET-Voc_FT.onnx', 'UVR_MDXNET_KARA_2.onnx', 'Reverb_HQ_By_FoxJoy.onnx']
for model in mdx_model_names:
dl_model(MDX_DOWNLOAD_LINK, model, mdxnet_models_dir)
rvc_model_names = ['hubert_base.pt', 'rmvpe.pt']
for model in rvc_model_names:
dl_model(RVC_DOWNLOAD_LINK, model, rvc_models_dir)
print('All models ready!')import requestsimport zipfile
MDX_DOWNLOAD_LINK = 'https://github.com/TRvlvr/model_repo/releases/download/all_public_uvr_models/'RVC_DOWNLOAD_LINK = 'https://huggingface.co/lj1995/VoiceConversionWebUI/resolve/main/'
BASE_DIR = Path(__file__).resolve().parent.parentmdxnet_models_dir = BASE_DIR / 'mdxnet_models'rvc_models_dir = BASE_DIR / 'rvc_models'
mdxnet_models_dir.mkdir(parents=True, exist_ok=True)rvc_models_dir.mkdir(parents=True, exist_ok=True)
def dl_model(link, model_name, dir_name): model_path = dir_name / model_name if model_path.exists(): # print(f"{model_name} already exists, skipping download.") return
print(f"Downloading {model_name}...") with requests.get(f'{link}{model_name}', stream=True) as r: r.raise_for_status() with open(model_path, 'wb') as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk)
def extract_shipped_voice_models(): """Extract voice-model .zip files already shipped in rvc_models/<name>/.
The repo stores each voice model as a .zip (e.g. rvc_models/Suru/Suru.zip), but the app only looks for an extracted .pth file. A fresh container therefore has folders with only a .zip and every conversion fails with "No model file exists". The download endpoint refuses to re-download because the folder already exists, so nothing ever extracts the .zip.
This step runs at every container startup (before the web UI starts) and extracts any .zip whose folder has no .pth yet. The .zip files are left untouched, so the originals are preserved and the step is idempotent: folders that already have a .pth are skipped. """ if not rvc_models_dir.is_dir(): return for model_dir in sorted(rvc_models_dir.iterdir()): if not model_dir.is_dir(): continue try: files = [p for p in model_dir.iterdir() if p.is_file()] except OSError: continue if any(p.suffix == '.pth' for p in files): continue for z in files: if z.suffix != '.zip': continue print(f"Extracting shipped voice model {z.name} into {model_dir} ...") try: with zipfile.ZipFile(z, 'r') as zf: zf.extractall(model_dir) print(f"Extracted {z.name}.") except Exception as e: print(f"Failed to extract {z.name}: {e}")
if __name__ == '__main__': mdx_model_names = ['UVR-MDX-NET-Inst_HQ_4.onnx', 'UVR-MDX-NET-Voc_FT.onnx', 'UVR_MDXNET_KARA_2.onnx', 'Reverb_HQ_By_FoxJoy.onnx'] for model in mdx_model_names: dl_model(MDX_DOWNLOAD_LINK, model, mdxnet_models_dir)
rvc_model_names = ['hubert_base.pt', 'rmvpe.pt'] for model in rvc_model_names: dl_model(RVC_DOWNLOAD_LINK, model, rvc_models_dir)
extract_shipped_voice_models()
print('All models ready!')src/download_models.py+78-38