|
13 | 13 | # See the License for the specific language governing permissions and
|
14 | 14 | # limitations under the License.
|
15 | 15 | """Git LFS related type definitions and utilities"""
|
| 16 | +import inspect |
16 | 17 | import io
|
17 | 18 | import os
|
18 | 19 | import re
|
|
29 | 30 | from huggingface_hub.constants import ENDPOINT, HF_HUB_ENABLE_HF_TRANSFER, REPO_TYPES_URL_PREFIXES
|
30 | 31 | from huggingface_hub.utils import get_session
|
31 | 32 |
|
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 |
33 | 34 | from .utils.sha import sha256, sha_fileobj
|
34 | 35 |
|
35 | 36 |
|
@@ -389,21 +390,37 @@ def _upload_parts_hf_transfer(
|
389 | 390 | " not available in your environment. Try `pip install hf_transfer`."
|
390 | 391 | )
|
391 | 392 |
|
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`." |
400 | 397 | )
|
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 |
406 | 398 |
|
| 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 |
407 | 424 |
|
408 | 425 | class SliceFileObj(AbstractContextManager):
|
409 | 426 | """
|
|
0 commit comments