Skip to content

Commit 17be1c5

Browse files
authored
Add support for progress bars in hf_transfer uploads (#1804)
* Add support for progress bars in hf_transfer uploads * apply suggestion
1 parent d306c90 commit 17be1c5

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

docs/source/en/guides/download.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ If you are running on a machine with high bandwidth, you can increase your downl
191191

192192
<Tip>
193193

194-
Progress bars are supported when downloading with `hf_transfer` starting from version `0.1.4`. Consider upgrading (`pip install -U hf-transfer`) if you plan to enable faster downloads.
194+
Progress bars are supported in `hf_transfer` starting from version `0.1.4`. Consider upgrading (`pip install -U hf-transfer`) if you plan to enable faster downloads.
195195

196196
</Tip>
197197

docs/source/en/guides/upload.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,13 @@ be re-uploaded twice but checking it client-side can still save some time.
460460
- **Use `hf_transfer`**: this is a Rust-based [library](https://github.com/huggingface/hf_transfer) meant to speed up
461461
uploads on machines with very high bandwidth. To use it, you must install it (`pip install hf_transfer`) and enable it
462462
by setting `HF_HUB_ENABLE_HF_TRANSFER=1` as an environment variable. You can then use `huggingface_hub` normally.
463-
Disclaimer: this is a power user tool. It is tested and production-ready but lacks user-friendly features like progress
464-
bars or advanced error handling. For more details, please refer to this [section](https://huggingface.co/docs/huggingface_hub/hf_transfer).
463+
Disclaimer: this is a power user tool. It is tested and production-ready but lacks user-friendly features like advanced error handling or proxies. For more details, please refer to this [section](https://huggingface.co/docs/huggingface_hub/hf_transfer).
464+
465+
<Tip>
466+
467+
Progress bars are supported in `hf_transfer` starting from version `0.1.4`. Consider upgrading (`pip install -U hf-transfer`) if you plan to enable faster uploads.
468+
469+
</Tip>
465470

466471
## (legacy) Upload files with Git LFS
467472

src/huggingface_hub/lfs.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
"""Git LFS related type definitions and utilities"""
16+
import inspect
1617
import io
1718
import os
1819
import re
@@ -29,7 +30,7 @@
2930
from huggingface_hub.constants import ENDPOINT, HF_HUB_ENABLE_HF_TRANSFER, REPO_TYPES_URL_PREFIXES
3031
from huggingface_hub.utils import get_session
3132

32-
from .utils import get_token_to_send, hf_raise_for_status, http_backoff, logging, validate_hf_hub_args
33+
from .utils import get_token_to_send, hf_raise_for_status, http_backoff, logging, tqdm, validate_hf_hub_args
3334
from .utils.sha import sha256, sha_fileobj
3435

3536

@@ -389,21 +390,37 @@ def _upload_parts_hf_transfer(
389390
" not available in your environment. Try `pip install hf_transfer`."
390391
)
391392

392-
try:
393-
return multipart_upload(
394-
file_path=operation.path_or_fileobj,
395-
parts_urls=sorted_parts_urls,
396-
chunk_size=chunk_size,
397-
max_files=128,
398-
parallel_failures=127, # could be removed
399-
max_retries=5,
393+
supports_callback = "callback" in inspect.signature(multipart_upload).parameters
394+
if not supports_callback:
395+
warnings.warn(
396+
"You are using an outdated version of `hf_transfer`. Consider upgrading to latest version to enable progress bars using `pip install -U hf_transfer`."
400397
)
401-
except Exception as e:
402-
raise RuntimeError(
403-
"An error occurred while uploading using `hf_transfer`. Consider disabling HF_HUB_ENABLE_HF_TRANSFER for"
404-
" better error handling."
405-
) from e
406398

399+
total = operation.upload_info.size
400+
desc = operation.path_in_repo
401+
if len(desc) > 40:
402+
desc = f"(…){desc[-40:]}"
403+
disable = bool(logger.getEffectiveLevel() == logging.NOTSET)
404+
405+
with tqdm(unit="B", unit_scale=True, total=total, initial=0, desc=desc, disable=disable) as progress:
406+
try:
407+
output = multipart_upload(
408+
file_path=operation.path_or_fileobj,
409+
parts_urls=sorted_parts_urls,
410+
chunk_size=chunk_size,
411+
max_files=128,
412+
parallel_failures=127, # could be removed
413+
max_retries=5,
414+
**({"callback": progress.update} if supports_callback else {}),
415+
)
416+
except Exception as e:
417+
raise RuntimeError(
418+
"An error occurred while uploading using `hf_transfer`. Consider disabling HF_HUB_ENABLE_HF_TRANSFER for"
419+
" better error handling."
420+
) from e
421+
if not supports_callback:
422+
progress.update(total)
423+
return output
407424

408425
class SliceFileObj(AbstractContextManager):
409426
"""

0 commit comments

Comments
 (0)