Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

download_model_urls.py 1.3 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
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
  1. import asyncio
  2. import sys
  3. from pathlib import Path
  4. from time import perf_counter
  5. from urllib.parse import urlsplit
  6. import aiofiles
  7. import aiohttp
  8. from torchvision import models
  9. from tqdm.asyncio import tqdm
  10. async def main(download_root):
  11. download_root.mkdir(parents=True, exist_ok=True)
  12. urls = {weight.url for name in models.list_models() for weight in iter(models.get_model_weights(name))}
  13. async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=None)) as session:
  14. await tqdm.gather(*[download(download_root, session, url) for url in urls])
  15. async def download(download_root, session, url):
  16. response = await session.get(url, params=dict(source="ci"))
  17. assert response.ok
  18. file_name = Path(urlsplit(url).path).name
  19. async with aiofiles.open(download_root / file_name, "wb") as f:
  20. async for data in response.content.iter_any():
  21. await f.write(data)
  22. if __name__ == "__main__":
  23. download_root = (
  24. (Path(sys.argv[1]) if len(sys.argv) > 1 else Path("~/.cache/torch/hub/checkpoints")).expanduser().resolve()
  25. )
  26. print(f"Downloading model weights to {download_root}")
  27. start = perf_counter()
  28. asyncio.get_event_loop().run_until_complete(main(download_root))
  29. stop = perf_counter()
  30. minutes, seconds = divmod(stop - start, 60)
  31. print(f"Download took {minutes:2.0f}m {seconds:2.0f}s")
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...