default_models.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import urllib.request
  2. from pathlib import Path
  3. from threading import Thread
  4. from urllib.error import HTTPError
  5. from tqdm import tqdm
  6. default_models = {
  7. "encoder": ("https://drive.google.com/uc?export=download&id=1q8mEGwCkFy23KZsinbuvdKAQLqNKbYf1", 17090379),
  8. "synthesizer": ("https://drive.google.com/u/0/uc?id=1EqFMIbvxffxtjiVrtykroF6_mUh-5Z3s&export=download&confirm=t", 370554559),
  9. "vocoder": ("https://drive.google.com/uc?export=download&id=1cf2NO6FtI0jDuy8AV3Xgn6leO6dHjIgu", 53845290),
  10. }
  11. class DownloadProgressBar(tqdm):
  12. def update_to(self, b=1, bsize=1, tsize=None):
  13. if tsize is not None:
  14. self.total = tsize
  15. self.update(b * bsize - self.n)
  16. def download(url: str, target: Path, bar_pos=0):
  17. # Ensure the directory exists
  18. target.parent.mkdir(exist_ok=True, parents=True)
  19. desc = f"Downloading {target.name}"
  20. with DownloadProgressBar(unit="B", unit_scale=True, miniters=1, desc=desc, position=bar_pos, leave=False) as t:
  21. try:
  22. urllib.request.urlretrieve(url, filename=target, reporthook=t.update_to)
  23. except HTTPError:
  24. return
  25. def ensure_default_models(models_dir: Path):
  26. # Define download tasks
  27. jobs = []
  28. for model_name, (url, size) in default_models.items():
  29. target_path = models_dir / "default" / f"{model_name}.pt"
  30. if target_path.exists():
  31. if target_path.stat().st_size != size:
  32. print(f"File {target_path} is not of expected size, redownloading...")
  33. else:
  34. continue
  35. thread = Thread(target=download, args=(url, target_path, len(jobs)))
  36. thread.start()
  37. jobs.append((thread, target_path, size))
  38. # Run and join threads
  39. for thread, target_path, size in jobs:
  40. thread.join()
  41. assert target_path.exists() and target_path.stat().st_size == size, \
  42. f"Download for {target_path.name} failed. You may download models manually instead.\n" \
  43. f"https://drive.google.com/drive/folders/1fU6umc5uQAVR2udZdHX-lDgXYzTyqG_j"