Jacky2305 commited on
Commit
f151a75
·
1 Parent(s): ddc16e7

Add retry logic and verification for model downloads

Browse files
Files changed (1) hide show
  1. app.py +44 -13
app.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import sys
3
  import shutil
4
  import subprocess
 
5
  from pathlib import Path
6
 
7
  # Hugging Face Spaces / CPU-only environment setup
@@ -17,6 +18,30 @@ def run(cmd, cwd=None):
17
  print(">>>", " ".join(map(str, cmd)), flush=True)
18
  subprocess.check_call(cmd, cwd=cwd)
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  # 1. Guarantee RVC existence regardless of build SDK
21
  if not (RVC_DIR / "webui.py").exists():
22
  print("RVC missing or incomplete. Performing clean clone...", flush=True)
@@ -46,24 +71,30 @@ for asset_dir in [
46
  asset_dir.mkdir(parents=True, exist_ok=True)
47
 
48
  # 4. Download OldChineseMan RVC model from model repo
49
- from huggingface_hub import hf_hub_download
50
-
51
  model_repo = "jacky3102/OldChineseMan_RVC"
52
  weights_dir = RVC_DIR / "assets" / "weights"
53
  indices_dir = RVC_DIR / "assets" / "indices"
54
 
55
- for fname in ["OldChineseMan.pth", "added_IVF24_Flat_nprobe_1_OldChineseMan_v2.index"]:
56
- target = weights_dir / fname if fname.endswith(".pth") else indices_dir / fname
57
- if not target.exists():
58
- print(f"Downloading {fname} from {model_repo}...", flush=True)
59
- hf_hub_download(
60
- repo_id=model_repo,
61
- filename=fname,
62
- local_dir=weights_dir if fname.endswith(".pth") else indices_dir,
63
- local_dir_use_symlinks=False,
64
- )
 
65
  else:
66
- print(f"{fname} already exists, skipping.", flush=True)
 
 
 
 
 
 
 
67
 
68
  # 5. Launch RVC WebUI (webui.py is the new entry point)
69
  run([
 
2
  import sys
3
  import shutil
4
  import subprocess
5
+ import time
6
  from pathlib import Path
7
 
8
  # Hugging Face Spaces / CPU-only environment setup
 
18
  print(">>>", " ".join(map(str, cmd)), flush=True)
19
  subprocess.check_call(cmd, cwd=cwd)
20
 
21
+ def download_with_retry(repo_id, filename, local_dir, max_retries=3):
22
+ """Download with retry logic"""
23
+ from huggingface_hub import hf_hub_download
24
+ for attempt in range(max_retries):
25
+ try:
26
+ print(f"Downloading {filename} from {repo_id} (attempt {attempt+1}/{max_retries})...", flush=True)
27
+ hf_hub_download(
28
+ repo_id=repo_id,
29
+ filename=filename,
30
+ local_dir=local_dir,
31
+ local_dir_use_symlinks=False,
32
+ )
33
+ target = Path(local_dir) / filename
34
+ if target.exists() and target.stat().st_size > 1000:
35
+ print(f"✓ {filename} downloaded successfully ({target.stat().st_size} bytes)", flush=True)
36
+ return True
37
+ else:
38
+ print(f"✗ {filename} download failed: file too small or missing", flush=True)
39
+ except Exception as e:
40
+ print(f"✗ Attempt {attempt+1} failed: {e}", flush=True)
41
+ if attempt < max_retries - 1:
42
+ time.sleep(5)
43
+ return False
44
+
45
  # 1. Guarantee RVC existence regardless of build SDK
46
  if not (RVC_DIR / "webui.py").exists():
47
  print("RVC missing or incomplete. Performing clean clone...", flush=True)
 
71
  asset_dir.mkdir(parents=True, exist_ok=True)
72
 
73
  # 4. Download OldChineseMan RVC model from model repo
 
 
74
  model_repo = "jacky3102/OldChineseMan_RVC"
75
  weights_dir = RVC_DIR / "assets" / "weights"
76
  indices_dir = RVC_DIR / "assets" / "indices"
77
 
78
+ files_to_download = [
79
+ ("OldChineseMan.pth", weights_dir),
80
+ ("added_IVF24_Flat_nprobe_1_OldChineseMan_v2.index", indices_dir),
81
+ ]
82
+
83
+ print("=== Downloading RVC model files ===", flush=True)
84
+ all_ok = True
85
+ for fname, target_dir in files_to_download:
86
+ target = target_dir / fname
87
+ if target.exists() and target.stat().st_size > 1000:
88
+ print(f"✓ {fname} already exists ({target.stat().st_size} bytes)", flush=True)
89
  else:
90
+ if not download_with_retry(model_repo, fname, target_dir):
91
+ print(f"✗ FAILED to download {fname}", flush=True)
92
+ all_ok = False
93
+
94
+ if not all_ok:
95
+ print("⚠ Some model files failed to download. Inference may fail.", flush=True)
96
+ else:
97
+ print("=== All model files ready ===", flush=True)
98
 
99
  # 5. Launch RVC WebUI (webui.py is the new entry point)
100
  run([