Download HFDown.py from Nacloud/123: direct link, hf CLI and curl.
- Browser
- Download file 2.49 kB
-
https://huggingface.co/Nacloud/123/resolve/main/HFDown.py
- Command line
-
hf download hf://Nacloud/123/HFDown.py
-
curl -L -o HFDown.py https://huggingface.co/Nacloud/123/resolve/main/HFDown.py
2.49 kB
| from huggingface_hub import HfFileSystem | |
| from huggingface_hub import snapshot_download | |
| import re | |
| import os | |
| os.environ['HF_HUB_ENABLE_HF_TRANSFER'] = "1" | |
| hfs = HfFileSystem() | |
| def detect_repo(repo_id, revision): | |
| try: | |
| files = hfs.glob(repo_id + '/**', revision=revision) | |
| if len(files) > 0: | |
| return 'model', files | |
| except Exception as _: | |
| pass | |
| try: | |
| files = hfs.glob('datasets/' + repo_id + '/**', revision=revision) | |
| files = [f[len('datasets/'):] for f in files] | |
| if len(files) > 0: | |
| return 'dataset', files | |
| except Exception as _: | |
| pass | |
| try: | |
| files = hfs.glob('spaces/' + repo_id + '/**', revision=revision) | |
| files = [f[len('spaces/'):] for f in files] | |
| if len(files) > 0: | |
| return 'space', files | |
| except Exception as _: | |
| pass | |
| return 'invalid', None | |
| repo_id = input('Huggingface Repo: ') | |
| revision = input('Revision: ') | |
| if revision.strip() == '': | |
| revision = None | |
| repo_type, files = detect_repo(repo_id, revision) | |
| if repo_type == 'invalid': | |
| print('Invalid repo name or revision.') | |
| exit(0) | |
| print('Repo Type:', repo_type) | |
| print('File List') | |
| for i, f in enumerate(files): | |
| print(f'{i:>5}: {f[len(repo_id) + 1:]}') | |
| dst_name = ''.join(re.findall('[a-zA-Z0-9-_.]+', repo_id.replace('/', '_'))) | |
| if len(user_dst_name := dst_name.strip()) > 0: | |
| dst_name = user_dst_name | |
| file_rules = input('Please input the expected files ids (+ for include and - for exclude): ') | |
| if file_rules.startswith('+'): | |
| file_rules = file_rules[1:].strip() | |
| included = [files[int(i.strip())][len(repo_id) + 1:] for i in file_rules.split(' ') if i.strip() != ''] | |
| snapshot_download(repo_id, revision=revision, cache_dir=f'cache/{dst_name}',local_dir=f'repos/{dst_name}', | |
| local_dir_use_symlinks=False, allow_patterns=included, repo_type=repo_type) | |
| elif file_rules.startswith('-'): | |
| file_rules = file_rules[1:].strip() | |
| excluded = [files[int(i.strip())][len(repo_id) + 1:] for i in file_rules.split(' ') if i.strip() != ''] | |
| snapshot_download(repo_id, revision=revision, cache_dir=f'cache/{dst_name}',local_dir=f'repos/{dst_name}', | |
| local_dir_use_symlinks=False, ignore_patterns=excluded, repo_type=repo_type) | |
| else: | |
| snapshot_download(repo_id, revision=revision, cache_dir=f'cache/{dst_name}',local_dir=f'repos/{dst_name}', | |
| local_dir_use_symlinks=False, repo_type=repo_type) |