(url, filename=None)
| 199 | |
| 200 | |
| 201 | def download_file(url, filename=None): |
| 202 | import urllib.request |
| 203 | from tqdm import tqdm |
| 204 | |
| 205 | class DownloadProgressBar(tqdm): |
| 206 | def update_to(self, b=1, bsize=1, tsize=None): |
| 207 | if tsize is not None: |
| 208 | self.total = tsize |
| 209 | self.update(b * bsize - self.n) |
| 210 | |
| 211 | if filename is None: |
| 212 | filename = url.split('/')[-1] |
| 213 | |
| 214 | try: |
| 215 | with DownloadProgressBar(unit='B', unit_scale=True, miniters=1, desc=url.split('/')[-1]) as t: |
| 216 | urllib.request.urlretrieve(url, filename=filename, reporthook=t.update_to) |
| 217 | except Exception as e: |
| 218 | import logging |
| 219 | logging.warning(f'{type(e)}: {e}') |
| 220 | # Handle Failed Downloads from huggingface.co |
| 221 | if 'huggingface.co' in url: |
| 222 | url_new = url.replace('huggingface.co', 'hf-mirror.com') |
| 223 | try: |
| 224 | download_file(url_new, filename) |
| 225 | return filename |
| 226 | except Exception as e: |
| 227 | logging.warning(f'{type(e)}: {e}') |
| 228 | raise Exception(f'Failed to download {url}') |
| 229 | else: |
| 230 | raise Exception(f'Failed to download {url}') |
| 231 | |
| 232 | return filename |
| 233 | |
| 234 | |
| 235 | def ls(dirname='.', match=[], mode='all', level=1): |
no test coverage detected