hfutils 0.0.2__tar.gz → 0.0.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {hfutils-0.0.2/hfutils.egg-info → hfutils-0.0.4}/PKG-INFO +46 -1
- {hfutils-0.0.2 → hfutils-0.0.4}/README.md +39 -0
- hfutils-0.0.4/hfutils/archive/__init__.py +24 -0
- hfutils-0.0.4/hfutils/archive/rar.py +28 -0
- hfutils-0.0.4/hfutils/archive/sevenz.py +32 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/archive/zip.py +1 -1
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/config/meta.py +1 -1
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/entry/download.py +7 -4
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/operate/download.py +38 -30
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/operate/upload.py +1 -2
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/utils/__init__.py +1 -0
- hfutils-0.0.4/hfutils/utils/temp.py +18 -0
- {hfutils-0.0.2 → hfutils-0.0.4/hfutils.egg-info}/PKG-INFO +46 -1
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils.egg-info/SOURCES.txt +6 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils.egg-info/requires.txt +9 -0
- hfutils-0.0.4/requirements-7z.txt +1 -0
- hfutils-0.0.4/requirements-rar.txt +1 -0
- hfutils-0.0.4/requirements-transfer.txt +1 -0
- hfutils-0.0.2/hfutils/archive/__init__.py +0 -11
- {hfutils-0.0.2 → hfutils-0.0.4}/LICENSE +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/MANIFEST.in +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/__init__.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/archive/base.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/archive/tar.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/config/__init__.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/entry/__init__.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/entry/base.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/entry/cli.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/entry/dispatch.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/entry/upload.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/operate/__init__.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/operate/base.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/utils/binary.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/utils/download.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/utils/tqdm_.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils/utils/walk.py +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils.egg-info/dependency_links.txt +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils.egg-info/entry_points.txt +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/hfutils.egg-info/top_level.txt +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/requirements-build.txt +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/requirements-doc.txt +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/requirements-test.txt +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/requirements.txt +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/setup.cfg +0 -0
- {hfutils-0.0.2 → hfutils-0.0.4}/setup.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: hfutils
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.4
|
|
4
4
|
Summary: Useful utilities for huggingface
|
|
5
5
|
Home-page: https://github.com/deepghs/hfutils
|
|
6
6
|
Author: narugo1992
|
|
@@ -24,6 +24,8 @@ Requires-Dist: huggingface_hub>=0.16
|
|
|
24
24
|
Requires-Dist: tqdm
|
|
25
25
|
Requires-Dist: requests
|
|
26
26
|
Requires-Dist: click>=7
|
|
27
|
+
Provides-Extra: 7z
|
|
28
|
+
Requires-Dist: py7zr; extra == "7z"
|
|
27
29
|
Provides-Extra: test
|
|
28
30
|
Requires-Dist: coverage>=5; extra == "test"
|
|
29
31
|
Requires-Dist: mock>=4.0.3; extra == "test"
|
|
@@ -61,6 +63,10 @@ Requires-Dist: ipykernel>=6.15; extra == "doc"
|
|
|
61
63
|
Requires-Dist: py-cpuinfo>=8.0.0; extra == "doc"
|
|
62
64
|
Requires-Dist: click>=7.0.0; extra == "doc"
|
|
63
65
|
Requires-Dist: pandas; extra == "doc"
|
|
66
|
+
Provides-Extra: rar
|
|
67
|
+
Requires-Dist: rarfile; extra == "rar"
|
|
68
|
+
Provides-Extra: transfer
|
|
69
|
+
Requires-Dist: hf-transfer; extra == "transfer"
|
|
64
70
|
Provides-Extra: build
|
|
65
71
|
Requires-Dist: pyinstaller<5,>=4.7; extra == "build"
|
|
66
72
|
|
|
@@ -221,3 +227,42 @@ Explore additional options for downloading:
|
|
|
221
227
|
hfutils download -h
|
|
222
228
|
|
|
223
229
|
```
|
|
230
|
+
|
|
231
|
+
## Supported Formats
|
|
232
|
+
|
|
233
|
+
By default, we support the `zip` and `tar` formats, including `.zip`, `.tar`, `.tar.gz`, `.tar.bz2`, and `.tar.xz`.
|
|
234
|
+
|
|
235
|
+
If you require support for `rar` and `7z` files, install the extra dependencies using the following command:
|
|
236
|
+
|
|
237
|
+
```shell
|
|
238
|
+
pip install hfutils[rar,7z]
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
**NOTE:** Creating RAR archive files is not supported. We use the [rarfile](https://github.com/markokr/rarfile) library,
|
|
242
|
+
which lacks the functionality for creating RAR files.
|
|
243
|
+
|
|
244
|
+
## How to Access Private Repositories
|
|
245
|
+
|
|
246
|
+
Simply configure the `HF_TOKEN` environment variable by using your HuggingFace access token.
|
|
247
|
+
Note that write permissions are required if you plan to upload any content.
|
|
248
|
+
|
|
249
|
+
## How to Use Hf-Transfer for Acceleration
|
|
250
|
+
|
|
251
|
+
If you are using the PyPI CLI, you need to install `hfutils` with the following command:
|
|
252
|
+
|
|
253
|
+
```shell
|
|
254
|
+
pip install hfutils[transfer]
|
|
255
|
+
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
If you are using a precompiled executable file, the transfer module is integrated inside; simply use it.
|
|
259
|
+
|
|
260
|
+
Enable Hf-Transfer acceleration by setting the environment variable `HF_HUB_ENABLE_HF_TRANSFER` to `1`.
|
|
261
|
+
|
|
262
|
+
## How to Change the Temporary Directory
|
|
263
|
+
|
|
264
|
+
The temporary directory is utilized for storing partially downloaded files, consuming a considerable amount of disk
|
|
265
|
+
space.
|
|
266
|
+
|
|
267
|
+
If your disk, especially the C drive on Windows, does not have sufficient space, simply set the `TMPDIR` to designate
|
|
268
|
+
another directory as the temporary directory.
|
|
@@ -155,3 +155,42 @@ Explore additional options for downloading:
|
|
|
155
155
|
hfutils download -h
|
|
156
156
|
|
|
157
157
|
```
|
|
158
|
+
|
|
159
|
+
## Supported Formats
|
|
160
|
+
|
|
161
|
+
By default, we support the `zip` and `tar` formats, including `.zip`, `.tar`, `.tar.gz`, `.tar.bz2`, and `.tar.xz`.
|
|
162
|
+
|
|
163
|
+
If you require support for `rar` and `7z` files, install the extra dependencies using the following command:
|
|
164
|
+
|
|
165
|
+
```shell
|
|
166
|
+
pip install hfutils[rar,7z]
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
**NOTE:** Creating RAR archive files is not supported. We use the [rarfile](https://github.com/markokr/rarfile) library,
|
|
170
|
+
which lacks the functionality for creating RAR files.
|
|
171
|
+
|
|
172
|
+
## How to Access Private Repositories
|
|
173
|
+
|
|
174
|
+
Simply configure the `HF_TOKEN` environment variable by using your HuggingFace access token.
|
|
175
|
+
Note that write permissions are required if you plan to upload any content.
|
|
176
|
+
|
|
177
|
+
## How to Use Hf-Transfer for Acceleration
|
|
178
|
+
|
|
179
|
+
If you are using the PyPI CLI, you need to install `hfutils` with the following command:
|
|
180
|
+
|
|
181
|
+
```shell
|
|
182
|
+
pip install hfutils[transfer]
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
If you are using a precompiled executable file, the transfer module is integrated inside; simply use it.
|
|
187
|
+
|
|
188
|
+
Enable Hf-Transfer acceleration by setting the environment variable `HF_HUB_ENABLE_HF_TRANSFER` to `1`.
|
|
189
|
+
|
|
190
|
+
## How to Change the Temporary Directory
|
|
191
|
+
|
|
192
|
+
The temporary directory is utilized for storing partially downloaded files, consuming a considerable amount of disk
|
|
193
|
+
space.
|
|
194
|
+
|
|
195
|
+
If your disk, especially the C drive on Windows, does not have sufficient space, simply set the `TMPDIR` to designate
|
|
196
|
+
another directory as the temporary directory.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Overview:
|
|
3
|
+
Archive pack and unpack management.
|
|
4
|
+
|
|
5
|
+
Supported Formats:
|
|
6
|
+
|
|
7
|
+
.. include:: supported_types.demo.py.txt
|
|
8
|
+
|
|
9
|
+
.. note::
|
|
10
|
+
If you require support for 7z and RAR formats, simply install ``hfutils`` using the following code:
|
|
11
|
+
|
|
12
|
+
.. code:: shell
|
|
13
|
+
|
|
14
|
+
pip install hfutils[7z]
|
|
15
|
+
pip install hfutils[rar]
|
|
16
|
+
|
|
17
|
+
.. warning::
|
|
18
|
+
The creation of archive files in the RAR format is not supported, as we utilize the `rarfile <https://github.com/markokr/rarfile>`_ library, which does not offer functionality for creating RAR files.
|
|
19
|
+
"""
|
|
20
|
+
from .base import register_archive_type, archive_pack, archive_unpack, get_archive_type, get_archive_extname
|
|
21
|
+
from .rar import _rar_pack, _rar_unpack
|
|
22
|
+
from .sevenz import _7z_pack, _7z_unpack
|
|
23
|
+
from .tar import _tarfile_pack, _tarfile_unpack
|
|
24
|
+
from .zip import _zip_pack, _zip_unpack
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from .base import register_archive_type
|
|
4
|
+
from ..utils import tqdm
|
|
5
|
+
|
|
6
|
+
try:
|
|
7
|
+
import rarfile
|
|
8
|
+
except ImportError: # pragma: no cover
|
|
9
|
+
rarfile = None
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _rar_pack(directory, zip_file, silent: bool = False):
|
|
13
|
+
_ = directory, zip_file, silent
|
|
14
|
+
raise RuntimeError('RAR format packing is not supported.')
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _rar_unpack(rar_file, directory, silent: bool = False):
|
|
18
|
+
directory = os.fspath(directory)
|
|
19
|
+
os.makedirs(directory, exist_ok=True)
|
|
20
|
+
with rarfile.RarFile(rar_file, 'r') as zf:
|
|
21
|
+
progress = tqdm(zf.namelist(), silent=silent, desc=f'Unpacking {directory!r} ...')
|
|
22
|
+
for rarinfo in progress:
|
|
23
|
+
progress.set_description(rarinfo)
|
|
24
|
+
zf.extract(rarinfo, directory)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
if rarfile is not None:
|
|
28
|
+
register_archive_type('rar', ['.rar'], _rar_pack, _rar_unpack)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from .base import register_archive_type
|
|
4
|
+
from ..utils import tqdm, walk_files
|
|
5
|
+
|
|
6
|
+
try:
|
|
7
|
+
import py7zr
|
|
8
|
+
except ImportError: # pragma: no cover
|
|
9
|
+
py7zr = None
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _7z_pack(directory, sz_file, silent: bool = False):
|
|
13
|
+
with py7zr.SevenZipFile(sz_file, 'w') as zf:
|
|
14
|
+
progress = tqdm(walk_files(directory), silent=silent, desc=f'Packing {directory!r} ...')
|
|
15
|
+
for file in progress:
|
|
16
|
+
progress.set_description(file)
|
|
17
|
+
zf.write(os.path.join(directory, file), file)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _7z_unpack(sz_file, directory, silent: bool = False):
|
|
21
|
+
directory = os.fspath(directory)
|
|
22
|
+
os.makedirs(directory, exist_ok=True)
|
|
23
|
+
with py7zr.SevenZipFile(sz_file, 'r') as zf:
|
|
24
|
+
progress = tqdm(zf.getnames(), silent=silent, desc=f'Unpacking {directory!r} ...')
|
|
25
|
+
for name in progress:
|
|
26
|
+
progress.set_description(name)
|
|
27
|
+
zf.extract(path=directory, targets=[name])
|
|
28
|
+
zf.reset()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
if py7zr is not None:
|
|
32
|
+
register_archive_type('7z', ['.7z'], _7z_pack, _7z_unpack)
|
|
@@ -25,7 +25,7 @@ def _zip_unpack(zip_file, directory, silent: bool = False):
|
|
|
25
25
|
directory = os.fspath(directory)
|
|
26
26
|
os.makedirs(directory, exist_ok=True)
|
|
27
27
|
with zipfile.ZipFile(zip_file, 'r') as zf:
|
|
28
|
-
progress = tqdm(zf.namelist(), silent=silent, desc=f'
|
|
28
|
+
progress = tqdm(zf.namelist(), silent=silent, desc=f'Unpacking {directory!r} ...')
|
|
29
29
|
for zipinfo in progress:
|
|
30
30
|
progress.set_description(zipinfo)
|
|
31
31
|
zf.extract(zipinfo, directory)
|
|
@@ -42,10 +42,12 @@ def _add_download_subcommand(cli: click.Group) -> click.Group:
|
|
|
42
42
|
help='Output path for download.')
|
|
43
43
|
@click.option('-R', '--revision', 'revision', type=str, default='main',
|
|
44
44
|
help='Revision of repository.', show_default=True)
|
|
45
|
+
@click.option('-n', '--max_workers', 'max_workers', type=int, default=8,
|
|
46
|
+
help='Max threads to download.', show_default=True)
|
|
45
47
|
@command_wrap()
|
|
46
48
|
def download(repo_id: str, repo_type: RepoTypeTyping,
|
|
47
49
|
file_in_repo: Optional[str], archive_in_repo: Optional[str], dir_in_repo: Optional[str],
|
|
48
|
-
output_path: str, revision: str):
|
|
50
|
+
output_path: str, revision: str, max_workers: int):
|
|
49
51
|
"""
|
|
50
52
|
Download data from HuggingFace repositories.
|
|
51
53
|
|
|
@@ -63,10 +65,12 @@ def _add_download_subcommand(cli: click.Group) -> click.Group:
|
|
|
63
65
|
:type output_path: str
|
|
64
66
|
:param revision: Revision of repository.
|
|
65
67
|
:type revision: str
|
|
68
|
+
:param max_workers: Max workers to download
|
|
69
|
+
:type max_workers: int
|
|
66
70
|
"""
|
|
67
71
|
if not file_in_repo and not archive_in_repo and not dir_in_repo:
|
|
68
72
|
raise NoRemotePathAssignedWithDownload('No remote path in repository assigned.\n'
|
|
69
|
-
|
|
73
|
+
'One of the -f, -a, or -d option is required.')
|
|
70
74
|
|
|
71
75
|
if file_in_repo:
|
|
72
76
|
if archive_in_repo:
|
|
@@ -79,7 +83,6 @@ def _add_download_subcommand(cli: click.Group) -> click.Group:
|
|
|
79
83
|
file_in_repo=file_in_repo,
|
|
80
84
|
repo_type=repo_type,
|
|
81
85
|
revision=revision,
|
|
82
|
-
silent=False,
|
|
83
86
|
)
|
|
84
87
|
|
|
85
88
|
elif archive_in_repo:
|
|
@@ -91,7 +94,6 @@ def _add_download_subcommand(cli: click.Group) -> click.Group:
|
|
|
91
94
|
file_in_repo=archive_in_repo,
|
|
92
95
|
repo_type=repo_type,
|
|
93
96
|
revision=revision,
|
|
94
|
-
silent=False,
|
|
95
97
|
)
|
|
96
98
|
|
|
97
99
|
elif dir_in_repo:
|
|
@@ -102,6 +104,7 @@ def _add_download_subcommand(cli: click.Group) -> click.Group:
|
|
|
102
104
|
repo_type=repo_type,
|
|
103
105
|
revision=revision,
|
|
104
106
|
silent=False,
|
|
107
|
+
max_workers=max_workers,
|
|
105
108
|
)
|
|
106
109
|
|
|
107
110
|
else:
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import os.path
|
|
2
|
+
import shutil
|
|
2
3
|
from concurrent.futures import ThreadPoolExecutor
|
|
3
4
|
from typing import List
|
|
4
5
|
|
|
5
|
-
from
|
|
6
|
-
from huggingface_hub import hf_hub_url
|
|
7
|
-
|
|
8
|
-
from .base import RepoTypeTyping, _get_hf_token, list_files_in_repository, _IGNORE_PATTERN_UNSET
|
|
6
|
+
from .base import RepoTypeTyping, list_files_in_repository, _IGNORE_PATTERN_UNSET, get_hf_client
|
|
9
7
|
from ..archive import archive_unpack
|
|
10
|
-
from ..utils import
|
|
8
|
+
from ..utils import tqdm, TemporaryDirectory
|
|
11
9
|
|
|
12
10
|
|
|
13
11
|
def download_file_to_file(local_file: str, repo_id: str, file_in_repo: str,
|
|
14
|
-
repo_type: RepoTypeTyping = 'dataset', revision: str = 'main',
|
|
12
|
+
repo_type: RepoTypeTyping = 'dataset', revision: str = 'main',
|
|
13
|
+
resume_download: bool = True):
|
|
15
14
|
"""
|
|
16
15
|
Download a file from a Hugging Face repository and save it to a local file.
|
|
17
16
|
|
|
@@ -25,27 +24,33 @@ def download_file_to_file(local_file: str, repo_id: str, file_in_repo: str,
|
|
|
25
24
|
:type repo_type: RepoTypeTyping
|
|
26
25
|
:param revision: The revision of the repository (e.g., branch, tag, commit hash).
|
|
27
26
|
:type revision: str
|
|
28
|
-
:param
|
|
29
|
-
:type
|
|
27
|
+
:param resume_download: Resume the existing download.
|
|
28
|
+
:type resume_download: bool
|
|
30
29
|
"""
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
30
|
+
hf_client = get_hf_client()
|
|
31
|
+
relative_filename = os.path.join(*file_in_repo.split("/"))
|
|
32
|
+
with TemporaryDirectory() as td:
|
|
33
|
+
temp_path = os.path.join(td, relative_filename)
|
|
34
|
+
try:
|
|
35
|
+
hf_client.hf_hub_download(
|
|
36
|
+
repo_id=repo_id,
|
|
37
|
+
repo_type=repo_type,
|
|
38
|
+
filename=file_in_repo,
|
|
39
|
+
revision=revision,
|
|
40
|
+
local_dir=td,
|
|
41
|
+
force_download=True,
|
|
42
|
+
local_dir_use_symlinks=False,
|
|
43
|
+
resume_download=resume_download,
|
|
44
|
+
)
|
|
45
|
+
finally:
|
|
46
|
+
if os.path.exists(temp_path):
|
|
47
|
+
if os.path.dirname(local_file):
|
|
48
|
+
os.makedirs(os.path.dirname(local_file), exist_ok=True)
|
|
49
|
+
shutil.move(temp_path, local_file)
|
|
45
50
|
|
|
46
51
|
|
|
47
52
|
def download_archive_as_directory(local_directory: str, repo_id: str, file_in_repo: str,
|
|
48
|
-
repo_type: RepoTypeTyping = 'dataset', revision: str = 'main'
|
|
53
|
+
repo_type: RepoTypeTyping = 'dataset', revision: str = 'main'):
|
|
49
54
|
"""
|
|
50
55
|
Download an archive file from a Hugging Face repository and extract it to a local directory.
|
|
51
56
|
|
|
@@ -59,18 +64,17 @@ def download_archive_as_directory(local_directory: str, repo_id: str, file_in_re
|
|
|
59
64
|
:type repo_type: RepoTypeTyping
|
|
60
65
|
:param revision: The revision of the repository (e.g., branch, tag, commit hash).
|
|
61
66
|
:type revision: str
|
|
62
|
-
:param silent: If True, suppress progress bar output.
|
|
63
|
-
:type silent: bool
|
|
64
67
|
"""
|
|
65
68
|
with TemporaryDirectory() as td:
|
|
66
69
|
archive_file = os.path.join(td, os.path.basename(file_in_repo))
|
|
67
|
-
download_file_to_file(archive_file, repo_id, file_in_repo, repo_type, revision
|
|
68
|
-
archive_unpack(archive_file, local_directory
|
|
70
|
+
download_file_to_file(archive_file, repo_id, file_in_repo, repo_type, revision)
|
|
71
|
+
archive_unpack(archive_file, local_directory)
|
|
69
72
|
|
|
70
73
|
|
|
71
74
|
def download_directory_as_directory(local_directory: str, repo_id: str, dir_in_repo: str = '.',
|
|
72
75
|
repo_type: RepoTypeTyping = 'dataset', revision: str = 'main',
|
|
73
|
-
silent: bool = False, ignore_patterns: List[str] = _IGNORE_PATTERN_UNSET
|
|
76
|
+
silent: bool = False, ignore_patterns: List[str] = _IGNORE_PATTERN_UNSET,
|
|
77
|
+
resume_download: bool = True, max_workers: int = 8):
|
|
74
78
|
"""
|
|
75
79
|
Download all files in a directory from a Hugging Face repository to a local directory.
|
|
76
80
|
|
|
@@ -88,6 +92,10 @@ def download_directory_as_directory(local_directory: str, repo_id: str, dir_in_r
|
|
|
88
92
|
:type silent: bool
|
|
89
93
|
:param ignore_patterns: List of file patterns to ignore.
|
|
90
94
|
:type ignore_patterns: List[str]
|
|
95
|
+
:param max_workers: Max workers when downloading. Default is ``8``.
|
|
96
|
+
:type max_workers: int
|
|
97
|
+
:param resume_download: Resume the existing download.
|
|
98
|
+
:type resume_download: bool
|
|
91
99
|
"""
|
|
92
100
|
files = list_files_in_repository(repo_id, repo_type, dir_in_repo, revision, ignore_patterns)
|
|
93
101
|
progress = tqdm(files, silent=silent, desc=f'Downloading {dir_in_repo!r} ...')
|
|
@@ -99,11 +107,11 @@ def download_directory_as_directory(local_directory: str, repo_id: str, dir_in_r
|
|
|
99
107
|
file_in_repo=f'{dir_in_repo}/{rel_file}',
|
|
100
108
|
repo_type=repo_type,
|
|
101
109
|
revision=revision,
|
|
102
|
-
|
|
110
|
+
resume_download=resume_download,
|
|
103
111
|
)
|
|
104
112
|
progress.update()
|
|
105
113
|
|
|
106
|
-
tp = ThreadPoolExecutor()
|
|
114
|
+
tp = ThreadPoolExecutor(max_workers=max_workers)
|
|
107
115
|
for file in files:
|
|
108
116
|
tp.submit(_download_one_file, file)
|
|
109
117
|
tp.shutdown(wait=True)
|
|
@@ -3,12 +3,11 @@ import os.path
|
|
|
3
3
|
import re
|
|
4
4
|
from typing import Optional, List
|
|
5
5
|
|
|
6
|
-
from hbutils.system import TemporaryDirectory
|
|
7
6
|
from huggingface_hub import CommitOperationAdd, CommitOperationDelete
|
|
8
7
|
|
|
9
8
|
from .base import RepoTypeTyping, get_hf_client, list_files_in_repository, _IGNORE_PATTERN_UNSET
|
|
10
9
|
from ..archive import get_archive_type, archive_pack
|
|
11
|
-
from ..utils import walk_files
|
|
10
|
+
from ..utils import walk_files, TemporaryDirectory
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
def upload_file_to_file(local_file, repo_id: str, file_in_repo: str,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import os
|
|
2
|
+
from functools import lru_cache
|
|
3
|
+
|
|
4
|
+
from hbutils.system import TemporaryDirectory as _OriginTemporaryDirectory
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@lru_cache()
|
|
8
|
+
def _create_tmp_dir(dir_):
|
|
9
|
+
if dir_:
|
|
10
|
+
os.makedirs(dir_, exist_ok=True)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class TemporaryDirectory(_OriginTemporaryDirectory):
|
|
14
|
+
# noinspection PyShadowingBuiltins
|
|
15
|
+
def __init__(self, suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False):
|
|
16
|
+
dir = dir or os.environ.get('TMPDIR') or None
|
|
17
|
+
_create_tmp_dir(dir)
|
|
18
|
+
_OriginTemporaryDirectory.__init__(self, suffix, prefix, dir, ignore_cleanup_errors)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: hfutils
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.4
|
|
4
4
|
Summary: Useful utilities for huggingface
|
|
5
5
|
Home-page: https://github.com/deepghs/hfutils
|
|
6
6
|
Author: narugo1992
|
|
@@ -24,6 +24,8 @@ Requires-Dist: huggingface_hub>=0.16
|
|
|
24
24
|
Requires-Dist: tqdm
|
|
25
25
|
Requires-Dist: requests
|
|
26
26
|
Requires-Dist: click>=7
|
|
27
|
+
Provides-Extra: 7z
|
|
28
|
+
Requires-Dist: py7zr; extra == "7z"
|
|
27
29
|
Provides-Extra: test
|
|
28
30
|
Requires-Dist: coverage>=5; extra == "test"
|
|
29
31
|
Requires-Dist: mock>=4.0.3; extra == "test"
|
|
@@ -61,6 +63,10 @@ Requires-Dist: ipykernel>=6.15; extra == "doc"
|
|
|
61
63
|
Requires-Dist: py-cpuinfo>=8.0.0; extra == "doc"
|
|
62
64
|
Requires-Dist: click>=7.0.0; extra == "doc"
|
|
63
65
|
Requires-Dist: pandas; extra == "doc"
|
|
66
|
+
Provides-Extra: rar
|
|
67
|
+
Requires-Dist: rarfile; extra == "rar"
|
|
68
|
+
Provides-Extra: transfer
|
|
69
|
+
Requires-Dist: hf-transfer; extra == "transfer"
|
|
64
70
|
Provides-Extra: build
|
|
65
71
|
Requires-Dist: pyinstaller<5,>=4.7; extra == "build"
|
|
66
72
|
|
|
@@ -221,3 +227,42 @@ Explore additional options for downloading:
|
|
|
221
227
|
hfutils download -h
|
|
222
228
|
|
|
223
229
|
```
|
|
230
|
+
|
|
231
|
+
## Supported Formats
|
|
232
|
+
|
|
233
|
+
By default, we support the `zip` and `tar` formats, including `.zip`, `.tar`, `.tar.gz`, `.tar.bz2`, and `.tar.xz`.
|
|
234
|
+
|
|
235
|
+
If you require support for `rar` and `7z` files, install the extra dependencies using the following command:
|
|
236
|
+
|
|
237
|
+
```shell
|
|
238
|
+
pip install hfutils[rar,7z]
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
**NOTE:** Creating RAR archive files is not supported. We use the [rarfile](https://github.com/markokr/rarfile) library,
|
|
242
|
+
which lacks the functionality for creating RAR files.
|
|
243
|
+
|
|
244
|
+
## How to Access Private Repositories
|
|
245
|
+
|
|
246
|
+
Simply configure the `HF_TOKEN` environment variable by using your HuggingFace access token.
|
|
247
|
+
Note that write permissions are required if you plan to upload any content.
|
|
248
|
+
|
|
249
|
+
## How to Use Hf-Transfer for Acceleration
|
|
250
|
+
|
|
251
|
+
If you are using the PyPI CLI, you need to install `hfutils` with the following command:
|
|
252
|
+
|
|
253
|
+
```shell
|
|
254
|
+
pip install hfutils[transfer]
|
|
255
|
+
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
If you are using a precompiled executable file, the transfer module is integrated inside; simply use it.
|
|
259
|
+
|
|
260
|
+
Enable Hf-Transfer acceleration by setting the environment variable `HF_HUB_ENABLE_HF_TRANSFER` to `1`.
|
|
261
|
+
|
|
262
|
+
## How to Change the Temporary Directory
|
|
263
|
+
|
|
264
|
+
The temporary directory is utilized for storing partially downloaded files, consuming a considerable amount of disk
|
|
265
|
+
space.
|
|
266
|
+
|
|
267
|
+
If your disk, especially the C drive on Windows, does not have sufficient space, simply set the `TMPDIR` to designate
|
|
268
|
+
another directory as the temporary directory.
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
LICENSE
|
|
2
2
|
MANIFEST.in
|
|
3
3
|
README.md
|
|
4
|
+
requirements-7z.txt
|
|
4
5
|
requirements-build.txt
|
|
5
6
|
requirements-doc.txt
|
|
7
|
+
requirements-rar.txt
|
|
6
8
|
requirements-test.txt
|
|
9
|
+
requirements-transfer.txt
|
|
7
10
|
requirements.txt
|
|
8
11
|
setup.py
|
|
9
12
|
hfutils/__init__.py
|
|
@@ -15,6 +18,8 @@ hfutils.egg-info/requires.txt
|
|
|
15
18
|
hfutils.egg-info/top_level.txt
|
|
16
19
|
hfutils/archive/__init__.py
|
|
17
20
|
hfutils/archive/base.py
|
|
21
|
+
hfutils/archive/rar.py
|
|
22
|
+
hfutils/archive/sevenz.py
|
|
18
23
|
hfutils/archive/tar.py
|
|
19
24
|
hfutils/archive/zip.py
|
|
20
25
|
hfutils/config/__init__.py
|
|
@@ -32,5 +37,6 @@ hfutils/operate/upload.py
|
|
|
32
37
|
hfutils/utils/__init__.py
|
|
33
38
|
hfutils/utils/binary.py
|
|
34
39
|
hfutils/utils/download.py
|
|
40
|
+
hfutils/utils/temp.py
|
|
35
41
|
hfutils/utils/tqdm_.py
|
|
36
42
|
hfutils/utils/walk.py
|
|
@@ -4,6 +4,9 @@ tqdm
|
|
|
4
4
|
requests
|
|
5
5
|
click>=7
|
|
6
6
|
|
|
7
|
+
[7z]
|
|
8
|
+
py7zr
|
|
9
|
+
|
|
7
10
|
[build]
|
|
8
11
|
pyinstaller<5,>=4.7
|
|
9
12
|
|
|
@@ -28,6 +31,9 @@ py-cpuinfo>=8.0.0
|
|
|
28
31
|
click>=7.0.0
|
|
29
32
|
pandas
|
|
30
33
|
|
|
34
|
+
[rar]
|
|
35
|
+
rarfile
|
|
36
|
+
|
|
31
37
|
[test]
|
|
32
38
|
coverage>=5
|
|
33
39
|
mock>=4.0.3
|
|
@@ -45,3 +51,6 @@ testtools>=2
|
|
|
45
51
|
where>=1.0.2
|
|
46
52
|
responses>=0.20.0
|
|
47
53
|
natsort
|
|
54
|
+
|
|
55
|
+
[transfer]
|
|
56
|
+
hf-transfer
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
py7zr
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rarfile
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
hf-transfer
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
Overview:
|
|
3
|
-
Archive pack and unpack management.
|
|
4
|
-
|
|
5
|
-
Supported Formats:
|
|
6
|
-
|
|
7
|
-
.. include:: supported_types.demo.py.txt
|
|
8
|
-
"""
|
|
9
|
-
from .base import register_archive_type, archive_pack, archive_unpack, get_archive_type, get_archive_extname
|
|
10
|
-
from .tar import _tarfile_pack, _tarfile_unpack
|
|
11
|
-
from .zip import _zip_pack, _zip_unpack
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|