Skip to content

Fix bad total size after resuming download (#3234) #3248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,6 @@ def _get_file_length_from_http_response(response: requests.Response) -> Optional

This function extracts the file size from the HTTP response headers, either from the
`Content-Range` or `Content-Length` header, if available (in that order).
The HTTP response object containing the headers.
`int` or `None`: The length of the file in bytes if the information is available,
otherwise `None`.

Args:
response (`requests.Response`):
Expand All @@ -329,6 +326,15 @@ def _get_file_length_from_http_response(response: requests.Response) -> Optional
`int` or `None`: The length of the file in bytes, or None if not available.
"""

# If HTTP response contains compressed body (e.g. gzip), the `Content-Length` header will
# contain the length of the compressed body, not the uncompressed file size.
# And at the start of transmission there's no way to know the uncompressed file size for gzip,
# thus we return None in that case.
content_encoding = response.headers.get("Content-Encoding", "identity").lower()
if content_encoding != "identity":
# gzip/br/deflate/zstd etc
return None

content_range = response.headers.get("Content-Range")
if content_range is not None:
return int(content_range.rsplit("/")[-1])
Expand Down Expand Up @@ -422,11 +428,7 @@ def http_get(
)

hf_raise_for_status(r)
content_length = _get_file_length_from_http_response(r)

# NOTE: 'total' is the total number of bytes to download, not the number of bytes in the file.
# If the file is compressed, the number of bytes in the saved file will be higher than 'total'.
total = resume_size + int(content_length) if content_length is not None else None
total: Optional[int] = _get_file_length_from_http_response(r)

if displayed_filename is None:
displayed_filename = url
Expand Down
2 changes: 2 additions & 0 deletions tests/test_file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ def test_etag_timeout_set_as_env_variable_parameter_ignored(self):
@with_production_testing
class TestExtraLargeFileDownloadPaths(unittest.TestCase):
@patch("huggingface_hub.file_download.constants.HF_HUB_ENABLE_HF_TRANSFER", False)
@patch("huggingface_hub.file_download.constants.HF_HUB_DISABLE_XET", True)
def test_large_file_http_path_error(self):
with SoftTemporaryDirectory() as cache_dir:
with self.assertRaises(
Expand All @@ -1226,6 +1227,7 @@ def test_large_file_http_path_error(self):
"hf_transfer not installed, so skipping large file download with hf_transfer check.",
)
@patch("huggingface_hub.file_download.constants.HF_HUB_ENABLE_HF_TRANSFER", True)
@patch("huggingface_hub.file_download.constants.HF_HUB_DISABLE_XET", True)
@patch("huggingface_hub.file_download.constants.MAX_HTTP_DOWNLOAD_SIZE", 44)
@patch("huggingface_hub.file_download.constants.DOWNLOAD_CHUNK_SIZE", 2) # make sure hf_download is used
def test_large_file_download_with_hf_transfer(self):
Expand Down