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

utils.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 appdirs
  2. import hashlib
  3. import io
  4. import logging
  5. import os
  6. import requests
  7. from requests.auth import HTTPBasicAuth
  8. from urllib.parse import urlparse
  9. def uri_to_url(uri, owner, repo):
  10. if uri.startswith('http'):
  11. return uri
  12. elif uri.startswith('repo://'):
  13. link_data = uri.split("repo://")[-1].split("/")
  14. commit, tree_path = link_data[0], "/".join(link_data[1:])
  15. return f"https://dagshub.com/api/v1/repos/{owner}/{repo}/raw/{commit}/{tree_path}"
  16. raise FileNotFoundError(f'Unkown URI {uri}')
  17. def cache_path():
  18. cache = appdirs.user_cache_dir(appname='relaxml')
  19. os.makedirs(cache, exist_ok=True)
  20. return cache
  21. def download_url(url, user, token):
  22. cache = cache_path()
  23. parsed_url = urlparse(url)
  24. url_filename = os.path.basename(parsed_url.path)
  25. url_hash = hashlib.md5(url.encode()).hexdigest()[:8]
  26. filepath = os.path.join(cache, url_hash + '__' + url_filename)
  27. if not os.path.exists(filepath):
  28. logging.info('Download {url} to {filepath}'.format(url=url, filepath=filepath))
  29. auth = HTTPBasicAuth(user, token)
  30. res = requests.get(url, stream=True, auth=auth)
  31. res.raise_for_status()
  32. with io.open(filepath, mode='wb') as f:
  33. f.write(res.content)
  34. return filepath
Tip!

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

Comments

Loading...