Skip to content

Commit 7326048

Browse files
committed
feat: HuggingFace downloads for models
1 parent 8a4584f commit 7326048

File tree

4 files changed

+103
-41
lines changed

4 files changed

+103
-41
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Uv will automatically create a .venv directory for you with an appropriate pytho
5757

5858
### (Optional) Download Pretrained Models
5959

60-
Pretrained models are now downloaded automatically. If this doesn't work for you, you can manually download them [here](https://github.com/CorentinJ/Real-Time-Voice-Cloning/wiki/Pretrained-models).
60+
Pretrained models are now downloaded automatically. If this doesn't work for you, you can manually download them from [Hugging Face](https://huggingface.co/CorentinJ/SV2TTS/tree/main).
6161

6262
### (Optional) Download Datasets
6363

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ requires-python = ">=3.9,<3.10"
55

66
# Dependencies will be populated by `uv add -r requirements.txt`.
77
dependencies = [
8+
"huggingface-hub[hf_xet]>=0.26,<1",
89
"inflect==5.3.0",
910
"librosa==0.9.2",
1011
"matplotlib==3.5.1",

utils/default_models.py

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,41 @@
1-
import urllib.request
21
from pathlib import Path
3-
from threading import Thread
4-
from urllib.error import HTTPError
52

6-
from tqdm import tqdm
3+
from huggingface_hub import hf_hub_download
74

5+
HUGGINGFACE_REPO = "CorentinJ/SV2TTS"
86

97
default_models = {
10-
"encoder": ("https://drive.google.com/uc?export=download&id=1q8mEGwCkFy23KZsinbuvdKAQLqNKbYf1", 17090379),
11-
"synthesizer": ("https://drive.google.com/u/0/uc?id=1EqFMIbvxffxtjiVrtykroF6_mUh-5Z3s&export=download&confirm=t", 370554559),
12-
"vocoder": ("https://drive.google.com/uc?export=download&id=1cf2NO6FtI0jDuy8AV3Xgn6leO6dHjIgu", 53845290),
8+
"encoder": 17090379,
9+
"synthesizer": 370554559,
10+
"vocoder": 53845290,
1311
}
1412

1513

16-
class DownloadProgressBar(tqdm):
17-
def update_to(self, b=1, bsize=1, tsize=None):
18-
if tsize is not None:
19-
self.total = tsize
20-
self.update(b * bsize - self.n)
14+
def _download_model(model_name: str, target_dir: Path):
15+
hf_hub_download(
16+
repo_id=HUGGINGFACE_REPO,
17+
revision="main",
18+
filename=f"{model_name}.pt",
19+
local_dir=str(target_dir),
20+
local_dir_use_symlinks=False,
21+
)
2122

2223

23-
def download(url: str, target: Path, bar_pos=0):
24-
# Ensure the directory exists
25-
target.parent.mkdir(exist_ok=True, parents=True)
26-
27-
desc = f"Downloading {target.name}"
28-
with DownloadProgressBar(unit="B", unit_scale=True, miniters=1, desc=desc, position=bar_pos, leave=False) as t:
29-
try:
30-
urllib.request.urlretrieve(url, filename=target, reporthook=t.update_to)
31-
except HTTPError:
32-
return
24+
def ensure_default_models(models_dir: Path):
25+
target_dir = models_dir / "default"
26+
target_dir.mkdir(parents=True, exist_ok=True)
3327

28+
for model_name, expected_size in default_models.items():
29+
target_path = target_dir / f"{model_name}.pt"
3430

35-
def ensure_default_models(models_dir: Path):
36-
# Define download tasks
37-
jobs = []
38-
for model_name, (url, size) in default_models.items():
39-
target_path = models_dir / "default" / f"{model_name}.pt"
4031
if target_path.exists():
41-
if target_path.stat().st_size != size:
42-
print(f"File {target_path} is not of expected size, redownloading...")
43-
else:
32+
if target_path.stat().st_size == expected_size:
4433
continue
34+
print(f"File {target_path} is not of expected size, redownloading...")
4535

46-
thread = Thread(target=download, args=(url, target_path, len(jobs)))
47-
thread.start()
48-
jobs.append((thread, target_path, size))
49-
50-
# Run and join threads
51-
for thread, target_path, size in jobs:
52-
thread.join()
36+
_download_model(model_name, target_dir)
5337

54-
assert target_path.exists() and target_path.stat().st_size == size, \
55-
f"Download for {target_path.name} failed. You may download models manually instead.\n" \
56-
f"https://drive.google.com/drive/folders/1fU6umc5uQAVR2udZdHX-lDgXYzTyqG_j"
38+
assert target_path.exists() and target_path.stat().st_size == expected_size, (
39+
f"Download for {target_path.name} failed. You may download models manually instead.\n"
40+
f"https://huggingface.co/{HUGGINGFACE_REPO}"
41+
)

uv.lock

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)