huggingface-hub 0.22.0rc0__tar.gz → 0.22.2__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.

Potentially problematic release.


This version of huggingface-hub might be problematic. Click here for more details.

Files changed (120) hide show
  1. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/PKG-INFO +1 -1
  2. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/__init__.py +1 -1
  3. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/_commit_api.py +1 -1
  4. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/file_download.py +78 -71
  5. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/hf_api.py +2 -7
  6. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/hf_file_system.py +77 -3
  7. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/hub_mixin.py +12 -2
  8. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_client.py +3 -1
  9. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/_async_client.py +3 -1
  10. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/lfs.py +7 -3
  11. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/__init__.py +8 -1
  12. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_http.py +14 -1
  13. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub.egg-info/PKG-INFO +1 -1
  14. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/LICENSE +0 -0
  15. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/MANIFEST.in +0 -0
  16. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/README.md +0 -0
  17. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/pyproject.toml +0 -0
  18. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/setup.cfg +0 -0
  19. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/setup.py +0 -0
  20. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/_commit_scheduler.py +0 -0
  21. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/_inference_endpoints.py +0 -0
  22. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/_login.py +0 -0
  23. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/_multi_commits.py +0 -0
  24. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/_snapshot_download.py +0 -0
  25. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/_space_api.py +0 -0
  26. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/_tensorboard_logger.py +0 -0
  27. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/_webhooks_payload.py +0 -0
  28. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/_webhooks_server.py +0 -0
  29. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/commands/__init__.py +0 -0
  30. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/commands/_cli_utils.py +0 -0
  31. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/commands/delete_cache.py +0 -0
  32. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/commands/download.py +0 -0
  33. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/commands/env.py +0 -0
  34. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/commands/huggingface_cli.py +0 -0
  35. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/commands/lfs.py +0 -0
  36. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/commands/scan_cache.py +0 -0
  37. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/commands/upload.py +0 -0
  38. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/commands/user.py +0 -0
  39. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/community.py +0 -0
  40. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/constants.py +0 -0
  41. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/errors.py +0 -0
  42. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/fastai_utils.py +0 -0
  43. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/__init__.py +0 -0
  44. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_common.py +0 -0
  45. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/__init__.py +0 -0
  46. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/__init__.py +0 -0
  47. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/audio_classification.py +0 -0
  48. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/audio_to_audio.py +0 -0
  49. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/automatic_speech_recognition.py +0 -0
  50. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/base.py +0 -0
  51. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/chat_completion.py +0 -0
  52. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/depth_estimation.py +0 -0
  53. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/document_question_answering.py +0 -0
  54. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/feature_extraction.py +0 -0
  55. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/fill_mask.py +0 -0
  56. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/image_classification.py +0 -0
  57. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/image_segmentation.py +0 -0
  58. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/image_to_image.py +0 -0
  59. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/image_to_text.py +0 -0
  60. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/object_detection.py +0 -0
  61. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/question_answering.py +0 -0
  62. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/sentence_similarity.py +0 -0
  63. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/summarization.py +0 -0
  64. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/table_question_answering.py +0 -0
  65. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/text2text_generation.py +0 -0
  66. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/text_classification.py +0 -0
  67. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/text_generation.py +0 -0
  68. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/text_to_audio.py +0 -0
  69. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/text_to_image.py +0 -0
  70. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/token_classification.py +0 -0
  71. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/translation.py +0 -0
  72. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/video_classification.py +0 -0
  73. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/visual_question_answering.py +0 -0
  74. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/zero_shot_classification.py +0 -0
  75. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/zero_shot_image_classification.py +0 -0
  76. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_generated/types/zero_shot_object_detection.py +0 -0
  77. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_templating.py +0 -0
  78. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference/_types.py +0 -0
  79. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/inference_api.py +0 -0
  80. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/keras_mixin.py +0 -0
  81. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/repocard.py +0 -0
  82. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/repocard_data.py +0 -0
  83. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/repository.py +0 -0
  84. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/serialization/__init__.py +0 -0
  85. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/serialization/_base.py +0 -0
  86. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/serialization/_numpy.py +0 -0
  87. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/serialization/_tensorflow.py +0 -0
  88. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/serialization/_torch.py +0 -0
  89. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/templates/datasetcard_template.md +0 -0
  90. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/templates/modelcard_template.md +0 -0
  91. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_cache_assets.py +0 -0
  92. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_cache_manager.py +0 -0
  93. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_chunk_utils.py +0 -0
  94. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_datetime.py +0 -0
  95. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_deprecation.py +0 -0
  96. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_errors.py +0 -0
  97. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_experimental.py +0 -0
  98. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_fixes.py +0 -0
  99. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_git_credential.py +0 -0
  100. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_headers.py +0 -0
  101. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_hf_folder.py +0 -0
  102. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_pagination.py +0 -0
  103. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_paths.py +0 -0
  104. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_runtime.py +0 -0
  105. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_safetensors.py +0 -0
  106. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_subprocess.py +0 -0
  107. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_telemetry.py +0 -0
  108. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_token.py +0 -0
  109. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_typing.py +0 -0
  110. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/_validators.py +0 -0
  111. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/endpoint_helpers.py +0 -0
  112. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/insecure_hashlib.py +0 -0
  113. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/logging.py +0 -0
  114. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/sha.py +0 -0
  115. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub/utils/tqdm.py +0 -0
  116. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub.egg-info/SOURCES.txt +0 -0
  117. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub.egg-info/dependency_links.txt +0 -0
  118. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub.egg-info/entry_points.txt +0 -0
  119. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub.egg-info/requires.txt +0 -0
  120. {huggingface_hub-0.22.0rc0 → huggingface_hub-0.22.2}/src/huggingface_hub.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: huggingface_hub
3
- Version: 0.22.0rc0
3
+ Version: 0.22.2
4
4
  Summary: Client library to download and publish models, datasets and other repos on the huggingface.co hub
5
5
  Home-page: https://github.com/huggingface/huggingface_hub
6
6
  Author: Hugging Face, Inc.
@@ -46,7 +46,7 @@ import sys
46
46
  from typing import TYPE_CHECKING
47
47
 
48
48
 
49
- __version__ = "0.22.0.rc0"
49
+ __version__ = "0.22.2"
50
50
 
51
51
  # Alphabetical order of definitions is ensured in tests
52
52
  # WARNING: any comment added in this dictionary definition will be lost when
@@ -399,7 +399,7 @@ def _upload_lfs_files(
399
399
  def _wrapped_lfs_upload(batch_action) -> None:
400
400
  try:
401
401
  operation = oid2addop[batch_action["oid"]]
402
- lfs_upload(operation=operation, lfs_batch_action=batch_action, headers=headers)
402
+ lfs_upload(operation=operation, lfs_batch_action=batch_action, headers=headers, endpoint=endpoint)
403
403
  except Exception as exc:
404
404
  raise RuntimeError(f"Error while uploading '{operation.path_in_repo}' to the Hub.") from exc
405
405
 
@@ -404,6 +404,7 @@ def http_get(
404
404
  expected_size: Optional[int] = None,
405
405
  displayed_filename: Optional[str] = None,
406
406
  _nb_retries: int = 5,
407
+ _tqdm_bar: Optional[tqdm] = None,
407
408
  ) -> None:
408
409
  """
409
410
  Download a remote file. Do not gobble up errors, and will return errors tailored to the Hugging Face Hub.
@@ -483,84 +484,90 @@ def http_get(
483
484
  )
484
485
 
485
486
  # Stream file to buffer
486
- with tqdm(
487
- unit="B",
488
- unit_scale=True,
489
- total=total,
490
- initial=resume_size,
491
- desc=displayed_filename,
492
- disable=True if (logger.getEffectiveLevel() == logging.NOTSET) else None,
493
- # ^ set `disable=None` rather than `disable=False` by default to disable progress bar when no TTY attached
494
- # see https://github.com/huggingface/huggingface_hub/pull/2000
495
- ) as progress:
496
- if hf_transfer and total is not None and total > 5 * DOWNLOAD_CHUNK_SIZE:
497
- supports_callback = "callback" in inspect.signature(hf_transfer.download).parameters
498
- if not supports_callback:
499
- warnings.warn(
500
- "You are using an outdated version of `hf_transfer`. "
501
- "Consider upgrading to latest version to enable progress bars "
502
- "using `pip install -U hf_transfer`."
503
- )
504
- try:
505
- hf_transfer.download(
506
- url=url,
507
- filename=temp_file.name,
508
- max_files=HF_TRANSFER_CONCURRENCY,
509
- chunk_size=DOWNLOAD_CHUNK_SIZE,
510
- headers=headers,
511
- parallel_failures=3,
512
- max_retries=5,
513
- **({"callback": progress.update} if supports_callback else {}),
514
- )
515
- except Exception as e:
516
- raise RuntimeError(
517
- "An error occurred while downloading using `hf_transfer`. Consider"
518
- " disabling HF_HUB_ENABLE_HF_TRANSFER for better error handling."
519
- ) from e
520
- if not supports_callback:
521
- progress.update(total)
522
- if expected_size is not None and expected_size != os.path.getsize(temp_file.name):
523
- raise EnvironmentError(
524
- consistency_error_message.format(
525
- actual_size=os.path.getsize(temp_file.name),
526
- )
527
- )
528
- return
529
- new_resume_size = resume_size
487
+ progress = _tqdm_bar
488
+ if progress is None:
489
+ progress = tqdm(
490
+ unit="B",
491
+ unit_scale=True,
492
+ total=total,
493
+ initial=resume_size,
494
+ desc=displayed_filename,
495
+ disable=True if (logger.getEffectiveLevel() == logging.NOTSET) else None,
496
+ # ^ set `disable=None` rather than `disable=False` by default to disable progress bar when no TTY attached
497
+ # see https://github.com/huggingface/huggingface_hub/pull/2000
498
+ )
499
+
500
+ if hf_transfer and total is not None and total > 5 * DOWNLOAD_CHUNK_SIZE:
501
+ supports_callback = "callback" in inspect.signature(hf_transfer.download).parameters
502
+ if not supports_callback:
503
+ warnings.warn(
504
+ "You are using an outdated version of `hf_transfer`. "
505
+ "Consider upgrading to latest version to enable progress bars "
506
+ "using `pip install -U hf_transfer`."
507
+ )
530
508
  try:
531
- for chunk in r.iter_content(chunk_size=DOWNLOAD_CHUNK_SIZE):
532
- if chunk: # filter out keep-alive new chunks
533
- progress.update(len(chunk))
534
- temp_file.write(chunk)
535
- new_resume_size += len(chunk)
536
- # Some data has been downloaded from the server so we reset the number of retries.
537
- _nb_retries = 5
538
- except (requests.ConnectionError, requests.ReadTimeout) as e:
539
- # If ConnectionError (SSLError) or ReadTimeout happen while streaming data from the server, it is most likely
540
- # a transient error (network outage?). We log a warning message and try to resume the download a few times
541
- # before giving up. Tre retry mechanism is basic but should be enough in most cases.
542
- if _nb_retries <= 0:
543
- logger.warning("Error while downloading from %s: %s\nMax retries exceeded.", url, str(e))
544
- raise
545
- logger.warning("Error while downloading from %s: %s\nTrying to resume download...", url, str(e))
546
- time.sleep(1)
547
- reset_sessions() # In case of SSLError it's best to reset the shared requests.Session objects
548
- return http_get(
509
+ hf_transfer.download(
549
510
  url=url,
550
- temp_file=temp_file,
551
- proxies=proxies,
552
- resume_size=new_resume_size,
553
- headers=initial_headers,
554
- expected_size=expected_size,
555
- _nb_retries=_nb_retries - 1,
511
+ filename=temp_file.name,
512
+ max_files=HF_TRANSFER_CONCURRENCY,
513
+ chunk_size=DOWNLOAD_CHUNK_SIZE,
514
+ headers=headers,
515
+ parallel_failures=3,
516
+ max_retries=5,
517
+ **({"callback": progress.update} if supports_callback else {}),
556
518
  )
557
-
558
- if expected_size is not None and expected_size != temp_file.tell():
519
+ except Exception as e:
520
+ raise RuntimeError(
521
+ "An error occurred while downloading using `hf_transfer`. Consider"
522
+ " disabling HF_HUB_ENABLE_HF_TRANSFER for better error handling."
523
+ ) from e
524
+ if not supports_callback:
525
+ progress.update(total)
526
+ if expected_size is not None and expected_size != os.path.getsize(temp_file.name):
559
527
  raise EnvironmentError(
560
528
  consistency_error_message.format(
561
- actual_size=temp_file.tell(),
529
+ actual_size=os.path.getsize(temp_file.name),
562
530
  )
563
531
  )
532
+ return
533
+ new_resume_size = resume_size
534
+ try:
535
+ for chunk in r.iter_content(chunk_size=DOWNLOAD_CHUNK_SIZE):
536
+ if chunk: # filter out keep-alive new chunks
537
+ progress.update(len(chunk))
538
+ temp_file.write(chunk)
539
+ new_resume_size += len(chunk)
540
+ # Some data has been downloaded from the server so we reset the number of retries.
541
+ _nb_retries = 5
542
+ except (requests.ConnectionError, requests.ReadTimeout) as e:
543
+ # If ConnectionError (SSLError) or ReadTimeout happen while streaming data from the server, it is most likely
544
+ # a transient error (network outage?). We log a warning message and try to resume the download a few times
545
+ # before giving up. Tre retry mechanism is basic but should be enough in most cases.
546
+ if _nb_retries <= 0:
547
+ logger.warning("Error while downloading from %s: %s\nMax retries exceeded.", url, str(e))
548
+ raise
549
+ logger.warning("Error while downloading from %s: %s\nTrying to resume download...", url, str(e))
550
+ time.sleep(1)
551
+ reset_sessions() # In case of SSLError it's best to reset the shared requests.Session objects
552
+ return http_get(
553
+ url=url,
554
+ temp_file=temp_file,
555
+ proxies=proxies,
556
+ resume_size=new_resume_size,
557
+ headers=initial_headers,
558
+ expected_size=expected_size,
559
+ _nb_retries=_nb_retries - 1,
560
+ _tqdm_bar=_tqdm_bar,
561
+ )
562
+
563
+ progress.close()
564
+
565
+ if expected_size is not None and expected_size != temp_file.tell():
566
+ raise EnvironmentError(
567
+ consistency_error_message.format(
568
+ actual_size=temp_file.tell(),
569
+ )
570
+ )
564
571
 
565
572
 
566
573
  @validate_hf_hub_args
@@ -82,8 +82,6 @@ from .community import (
82
82
  deserialize_event,
83
83
  )
84
84
  from .constants import (
85
- _HF_DEFAULT_ENDPOINT,
86
- _HF_DEFAULT_STAGING_ENDPOINT,
87
85
  DEFAULT_ETAG_TIMEOUT,
88
86
  DEFAULT_REQUEST_TIMEOUT,
89
87
  DEFAULT_REVISION,
@@ -123,6 +121,7 @@ from .utils import ( # noqa: F401 # imported for backward compatibility
123
121
  build_hf_headers,
124
122
  experimental,
125
123
  filter_repo_objects,
124
+ fix_hf_endpoint_in_url,
126
125
  get_session,
127
126
  hf_raise_for_status,
128
127
  logging,
@@ -185,11 +184,6 @@ def repo_type_and_id_from_hf_id(hf_id: str, hub_url: Optional[str] = None) -> Tu
185
184
  """
186
185
  input_hf_id = hf_id
187
186
 
188
- # check if a proxy has been set => if yes, update the returned URL to use the proxy
189
- if ENDPOINT not in (_HF_DEFAULT_ENDPOINT, _HF_DEFAULT_STAGING_ENDPOINT):
190
- hf_id = hf_id.replace(_HF_DEFAULT_ENDPOINT, ENDPOINT)
191
- hf_id = hf_id.replace(_HF_DEFAULT_STAGING_ENDPOINT, ENDPOINT)
192
-
193
187
  hub_url = re.sub(r"https?://", "", hub_url if hub_url is not None else ENDPOINT)
194
188
  is_hf_url = hub_url in hf_id and "@" not in hf_id
195
189
 
@@ -436,6 +430,7 @@ class RepoUrl(str):
436
430
  """
437
431
 
438
432
  def __new__(cls, url: Any, endpoint: Optional[str] = None):
433
+ url = fix_hf_endpoint_in_url(url, endpoint=endpoint)
439
434
  return super(RepoUrl, cls).__new__(cls, url)
440
435
 
441
436
  def __init__(self, url: Any, endpoint: Optional[str] = None) -> None:
@@ -6,15 +6,24 @@ from collections import deque
6
6
  from dataclasses import dataclass, field
7
7
  from datetime import datetime
8
8
  from itertools import chain
9
+ from pathlib import Path
9
10
  from typing import Any, Dict, List, NoReturn, Optional, Tuple, Union
10
11
  from urllib.parse import quote, unquote
11
12
 
12
13
  import fsspec
14
+ from fsspec.callbacks import _DEFAULT_CALLBACK, NoOpCallback, TqdmCallback
15
+ from fsspec.utils import isfilelike
13
16
  from requests import Response
14
17
 
15
18
  from ._commit_api import CommitOperationCopy, CommitOperationDelete
16
- from .constants import DEFAULT_REVISION, ENDPOINT, REPO_TYPE_MODEL, REPO_TYPES_MAPPING, REPO_TYPES_URL_PREFIXES
17
- from .file_download import hf_hub_url
19
+ from .constants import (
20
+ DEFAULT_REVISION,
21
+ ENDPOINT,
22
+ REPO_TYPE_MODEL,
23
+ REPO_TYPES_MAPPING,
24
+ REPO_TYPES_URL_PREFIXES,
25
+ )
26
+ from .file_download import hf_hub_url, http_get
18
27
  from .hf_api import HfApi, LastCommitInfo, RepoFile
19
28
  from .utils import (
20
29
  EntryNotFoundError,
@@ -591,6 +600,58 @@ class HfFileSystem(fsspec.AbstractFileSystem):
591
600
  url = url.replace("/resolve/", "/tree/", 1)
592
601
  return url
593
602
 
603
+ def get_file(self, rpath, lpath, callback=_DEFAULT_CALLBACK, outfile=None, **kwargs) -> None:
604
+ """Copy single remote file to local."""
605
+ revision = kwargs.get("revision")
606
+ unhandled_kwargs = set(kwargs.keys()) - {"revision"}
607
+ if not isinstance(callback, (NoOpCallback, TqdmCallback)) or len(unhandled_kwargs) > 0:
608
+ # for now, let's not handle custom callbacks
609
+ # and let's not handle custom kwargs
610
+ return super().get_file(rpath, lpath, callback=callback, outfile=outfile, **kwargs)
611
+
612
+ # Taken from https://github.com/fsspec/filesystem_spec/blob/47b445ae4c284a82dd15e0287b1ffc410e8fc470/fsspec/spec.py#L883
613
+ if isfilelike(lpath):
614
+ outfile = lpath
615
+ elif self.isdir(rpath):
616
+ os.makedirs(lpath, exist_ok=True)
617
+ return None
618
+
619
+ if isinstance(lpath, (str, Path)): # otherwise, let's assume it's a file-like object
620
+ os.makedirs(os.path.dirname(lpath), exist_ok=True)
621
+
622
+ # Open file if not already open
623
+ close_file = False
624
+ if outfile is None:
625
+ outfile = open(lpath, "wb")
626
+ close_file = True
627
+ initial_pos = outfile.tell()
628
+
629
+ # Custom implementation of `get_file` to use `http_get`.
630
+ resolve_remote_path = self.resolve_path(rpath, revision=revision)
631
+ expected_size = self.info(rpath, revision=revision)["size"]
632
+ callback.set_size(expected_size)
633
+ try:
634
+ http_get(
635
+ url=hf_hub_url(
636
+ repo_id=resolve_remote_path.repo_id,
637
+ revision=resolve_remote_path.revision,
638
+ filename=resolve_remote_path.path_in_repo,
639
+ repo_type=resolve_remote_path.repo_type,
640
+ endpoint=self.endpoint,
641
+ ),
642
+ temp_file=outfile,
643
+ displayed_filename=rpath,
644
+ expected_size=expected_size,
645
+ resume_size=0,
646
+ headers=self._api._build_hf_headers(),
647
+ _tqdm_bar=callback.tqdm if isinstance(callback, TqdmCallback) else None,
648
+ )
649
+ outfile.seek(initial_pos)
650
+ finally:
651
+ # Close file only if we opened it ourselves
652
+ if close_file:
653
+ outfile.close()
654
+
594
655
  @property
595
656
  def transaction(self):
596
657
  """A context within which files are committed together upon exit
@@ -618,6 +679,7 @@ class HfFileSystemFile(fsspec.spec.AbstractBufferedFile):
618
679
  raise FileNotFoundError(
619
680
  f"{e}.\nMake sure the repository and revision exist before writing data."
620
681
  ) from e
682
+ raise
621
683
  super().__init__(fs, self.resolved_path.unresolve(), **kwargs)
622
684
  self.fs: HfFileSystem
623
685
 
@@ -667,6 +729,18 @@ class HfFileSystemFile(fsspec.spec.AbstractBufferedFile):
667
729
  path=self.resolved_path.unresolve(),
668
730
  )
669
731
 
732
+ def read(self, length=-1):
733
+ """Read remote file.
734
+
735
+ If `length` is not provided or is -1, the entire file is downloaded and read. On POSIX systems and if
736
+ `hf_transfer` is not enabled, the file is loaded in memory directly. Otherwise, the file is downloaded to a
737
+ temporary file and read from there.
738
+ """
739
+ if self.mode == "rb" and (length is None or length == -1) and self.loc == 0:
740
+ with self.fs.open(self.path, "rb", block_size=0) as f: # block_size=0 enables fast streaming
741
+ return f.read()
742
+ return super().read(length)
743
+
670
744
  def url(self) -> str:
671
745
  return self.fs.url(self.path)
672
746
 
@@ -695,7 +769,7 @@ class HfFileSystemStreamFile(fsspec.spec.AbstractBufferedFile):
695
769
  raise FileNotFoundError(
696
770
  f"{e}.\nMake sure the repository and revision exist before writing data."
697
771
  ) from e
698
- # avoid an unecessary .info() call to instantiate .details
772
+ # avoid an unnecessary .info() call to instantiate .details
699
773
  self.details = {"name": self.resolved_path.unresolve(), "size": None}
700
774
  super().__init__(
701
775
  fs, self.resolved_path.unresolve(), mode=mode, block_size=block_size, cache_type=cache_type, **kwargs
@@ -262,6 +262,12 @@ class ModelHubMixin:
262
262
  save_directory = Path(save_directory)
263
263
  save_directory.mkdir(parents=True, exist_ok=True)
264
264
 
265
+ # Remove config.json if already exists. After `_save_pretrained` we don't want to overwrite config.json
266
+ # as it might have been saved by the custom `_save_pretrained` already. However we do want to overwrite
267
+ # an existing config.json if it was not saved by `_save_pretrained`.
268
+ config_path = save_directory / CONFIG_NAME
269
+ config_path.unlink(missing_ok=True)
270
+
265
271
  # save model weights/files (framework-specific)
266
272
  self._save_pretrained(save_directory)
267
273
 
@@ -271,7 +277,6 @@ class ModelHubMixin:
271
277
  if config is not None:
272
278
  if is_dataclass(config):
273
279
  config = asdict(config) # type: ignore[arg-type]
274
- config_path = save_directory / CONFIG_NAME
275
280
  if not config_path.exists():
276
281
  config_str = json.dumps(config, sort_keys=True, indent=2)
277
282
  config_path.write_text(config_str)
@@ -398,7 +403,12 @@ class ModelHubMixin:
398
403
  # Forward config to model initialization
399
404
  model_kwargs["config"] = config
400
405
 
401
- if any(param.kind == inspect.Parameter.VAR_KEYWORD for param in cls._hub_mixin_init_parameters.values()):
406
+ # Inject config if `**kwargs` are expected
407
+ if is_dataclass(cls):
408
+ for key in cls.__dataclass_fields__:
409
+ if key not in model_kwargs and key in config:
410
+ model_kwargs[key] = config[key]
411
+ elif any(param.kind == inspect.Parameter.VAR_KEYWORD for param in cls._hub_mixin_init_parameters.values()):
402
412
  for key, value in config.items():
403
413
  if key not in model_kwargs:
404
414
  model_kwargs[key] = value
@@ -548,7 +548,9 @@ class InferenceClient:
548
548
  if _is_chat_completion_server(model):
549
549
  # First, let's consider the server has a `/v1/chat/completions` endpoint.
550
550
  # If that's the case, we don't have to render the chat template client-side.
551
- model_url = self._resolve_url(model) + "/v1/chat/completions"
551
+ model_url = self._resolve_url(model)
552
+ if not model_url.endswith("/chat/completions"):
553
+ model_url += "/v1/chat/completions"
552
554
 
553
555
  try:
554
556
  data = self.post(
@@ -548,7 +548,9 @@ class AsyncInferenceClient:
548
548
  if _is_chat_completion_server(model):
549
549
  # First, let's consider the server has a `/v1/chat/completions` endpoint.
550
550
  # If that's the case, we don't have to render the chat template client-side.
551
- model_url = self._resolve_url(model) + "/v1/chat/completions"
551
+ model_url = self._resolve_url(model)
552
+ if not model_url.endswith("/chat/completions"):
553
+ model_url += "/v1/chat/completions"
552
554
 
553
555
  try:
554
556
  data = await self.post(
@@ -31,6 +31,7 @@ from huggingface_hub.constants import ENDPOINT, HF_HUB_ENABLE_HF_TRANSFER, REPO_
31
31
 
32
32
  from .utils import (
33
33
  build_hf_headers,
34
+ fix_hf_endpoint_in_url,
34
35
  get_session,
35
36
  hf_raise_for_status,
36
37
  http_backoff,
@@ -193,6 +194,7 @@ def lfs_upload(
193
194
  lfs_batch_action: Dict,
194
195
  token: Optional[str] = None,
195
196
  headers: Optional[Dict[str, str]] = None,
197
+ endpoint: Optional[str] = None,
196
198
  ) -> None:
197
199
  """
198
200
  Handles uploading a given object to the Hub with the LFS protocol.
@@ -230,6 +232,7 @@ def lfs_upload(
230
232
  # 2. Upload file (either single part or multi-part)
231
233
  header = upload_action.get("header", {})
232
234
  chunk_size = header.get("chunk_size")
235
+ upload_url = fix_hf_endpoint_in_url(upload_action["href"], endpoint=endpoint)
233
236
  if chunk_size is not None:
234
237
  try:
235
238
  chunk_size = int(chunk_size)
@@ -237,15 +240,16 @@ def lfs_upload(
237
240
  raise ValueError(
238
241
  f"Malformed response from LFS batch endpoint: `chunk_size` should be an integer. Got '{chunk_size}'."
239
242
  )
240
- _upload_multi_part(operation=operation, header=header, chunk_size=chunk_size, upload_url=upload_action["href"])
243
+ _upload_multi_part(operation=operation, header=header, chunk_size=chunk_size, upload_url=upload_url)
241
244
  else:
242
- _upload_single_part(operation=operation, upload_url=upload_action["href"])
245
+ _upload_single_part(operation=operation, upload_url=upload_url)
243
246
 
244
247
  # 3. Verify upload went well
245
248
  if verify_action is not None:
246
249
  _validate_lfs_action(verify_action)
250
+ verify_url = fix_hf_endpoint_in_url(verify_action["href"], endpoint)
247
251
  verify_resp = get_session().post(
248
- verify_action["href"],
252
+ verify_url,
249
253
  headers=build_hf_headers(token=token, headers=headers),
250
254
  json={"oid": operation.upload_info.sha256.hex(), "size": operation.upload_info.size},
251
255
  )
@@ -47,7 +47,14 @@ from ._fixes import SoftTemporaryDirectory, WeakFileLock, yaml_dump
47
47
  from ._git_credential import list_credential_helpers, set_git_credential, unset_git_credential
48
48
  from ._headers import LocalTokenNotFoundError, build_hf_headers, get_token_to_send
49
49
  from ._hf_folder import HfFolder
50
- from ._http import OfflineModeIsEnabled, configure_http_backend, get_session, http_backoff, reset_sessions
50
+ from ._http import (
51
+ OfflineModeIsEnabled,
52
+ configure_http_backend,
53
+ fix_hf_endpoint_in_url,
54
+ get_session,
55
+ http_backoff,
56
+ reset_sessions,
57
+ )
51
58
  from ._pagination import paginate
52
59
  from ._paths import IGNORE_GIT_FOLDER_PATTERNS, filter_repo_objects
53
60
  from ._runtime import (
@@ -21,7 +21,7 @@ import time
21
21
  import uuid
22
22
  from functools import lru_cache
23
23
  from http import HTTPStatus
24
- from typing import Callable, Tuple, Type, Union
24
+ from typing import Callable, Optional, Tuple, Type, Union
25
25
 
26
26
  import requests
27
27
  from requests import Response
@@ -306,3 +306,16 @@ def http_backoff(
306
306
 
307
307
  # Update sleep time for next retry
308
308
  sleep_time = min(max_wait_time, sleep_time * 2) # Exponential backoff
309
+
310
+
311
+ def fix_hf_endpoint_in_url(url: str, endpoint: Optional[str]) -> str:
312
+ """Replace the default endpoint in a URL by a custom one.
313
+
314
+ This is useful when using a proxy and the Hugging Face Hub returns a URL with the default endpoint.
315
+ """
316
+ endpoint = endpoint or constants.ENDPOINT
317
+ # check if a proxy has been set => if yes, update the returned URL to use the proxy
318
+ if endpoint not in (None, constants._HF_DEFAULT_ENDPOINT, constants._HF_DEFAULT_STAGING_ENDPOINT):
319
+ url = url.replace(constants._HF_DEFAULT_ENDPOINT, endpoint)
320
+ url = url.replace(constants._HF_DEFAULT_STAGING_ENDPOINT, endpoint)
321
+ return url
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: huggingface-hub
3
- Version: 0.22.0rc0
3
+ Version: 0.22.2
4
4
  Summary: Client library to download and publish models, datasets and other repos on the huggingface.co hub
5
5
  Home-page: https://github.com/huggingface/huggingface_hub
6
6
  Author: Hugging Face, Inc.