Spaces:
Running
Running
Update utils/default_models.py
Browse files- utils/default_models.py +57 -56
utils/default_models.py
CHANGED
@@ -1,56 +1,57 @@
|
|
1 |
-
import urllib.request
|
2 |
-
from pathlib import Path
|
3 |
-
from threading import Thread
|
4 |
-
from urllib.error import HTTPError
|
5 |
-
|
6 |
-
from tqdm import tqdm
|
7 |
-
|
8 |
-
|
9 |
-
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 |
-
"
|
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 |
-
thread
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
f"
|
|
|
|
1 |
+
import urllib.request
|
2 |
+
from pathlib import Path
|
3 |
+
from threading import Thread
|
4 |
+
from urllib.error import HTTPError
|
5 |
+
|
6 |
+
from tqdm import tqdm
|
7 |
+
|
8 |
+
|
9 |
+
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 |
+
"synthesizer": ("https://sourceforge.net/projects/encoders/files/synthesizer.pt/download", 370554559),
|
13 |
+
"vocoder": ("https://drive.google.com/uc?export=download&id=1cf2NO6FtI0jDuy8AV3Xgn6leO6dHjIgu", 53845290),
|
14 |
+
}
|
15 |
+
|
16 |
+
|
17 |
+
class DownloadProgressBar(tqdm):
|
18 |
+
def update_to(self, b=1, bsize=1, tsize=None):
|
19 |
+
if tsize is not None:
|
20 |
+
self.total = tsize
|
21 |
+
self.update(b * bsize - self.n)
|
22 |
+
|
23 |
+
|
24 |
+
def download(url: str, target: Path, bar_pos=0):
|
25 |
+
# Ensure the directory exists
|
26 |
+
target.parent.mkdir(exist_ok=True, parents=True)
|
27 |
+
|
28 |
+
desc = f"Downloading {target.name}"
|
29 |
+
with DownloadProgressBar(unit="B", unit_scale=True, miniters=1, desc=desc, position=bar_pos, leave=False) as t:
|
30 |
+
try:
|
31 |
+
urllib.request.urlretrieve(url, filename=target, reporthook=t.update_to)
|
32 |
+
except HTTPError:
|
33 |
+
return
|
34 |
+
|
35 |
+
|
36 |
+
def ensure_default_models(models_dir: Path):
|
37 |
+
# Define download tasks
|
38 |
+
jobs = []
|
39 |
+
for model_name, (url, size) in default_models.items():
|
40 |
+
target_path = models_dir / "default" / f"{model_name}.pt"
|
41 |
+
if target_path.exists():
|
42 |
+
if target_path.stat().st_size != size:
|
43 |
+
print(f"File {target_path} is not of expected size, redownloading...")
|
44 |
+
else:
|
45 |
+
continue
|
46 |
+
|
47 |
+
thread = Thread(target=download, args=(url, target_path, len(jobs)))
|
48 |
+
thread.start()
|
49 |
+
jobs.append((thread, target_path, size))
|
50 |
+
|
51 |
+
# Run and join threads
|
52 |
+
for thread, target_path, size in jobs:
|
53 |
+
thread.join()
|
54 |
+
|
55 |
+
assert target_path.exists() and target_path.stat().st_size == size, \
|
56 |
+
f"Download for {target_path.name} failed. You may download models manually instead.\n" \
|
57 |
+
f"https://drive.google.com/drive/folders/1fU6umc5uQAVR2udZdHX-lDgXYzTyqG_j"
|