Skip to content

Commit

Permalink
Consider empty subfolder as None in and (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wauplin authored Aug 31, 2022
1 parent 48ddc62 commit 4af799e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def hf_hub_url(
- [1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
"""
if subfolder == "":
subfolder = None
if subfolder is not None:
filename = f"{subfolder}/{filename}"

Expand Down Expand Up @@ -1032,6 +1034,8 @@ def hf_hub_download(
if isinstance(cache_dir, Path):
cache_dir = str(cache_dir)

if subfolder == "":
subfolder = None
if subfolder is not None:
# This is used to create a URL, and not a local path, hence the forward slash.
filename = f"{subfolder}/{filename}"
Expand Down
41 changes: 41 additions & 0 deletions tests/test_file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
import os
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

import pytest
Expand Down Expand Up @@ -247,6 +248,46 @@ def test_download_from_a_renamed_repo_with_cached_download(self):
)
self.assertTrue(os.path.exists(filepath))

def test_hf_hub_download_with_empty_subfolder(self):
"""
Check subfolder arg is processed correctly when empty string is passed to
`hf_hub_download`.
See https://github.com/huggingface/huggingface_hub/issues/1016.
"""
filepath = Path(
hf_hub_download(
DUMMY_MODEL_ID,
filename=CONFIG_NAME,
subfolder="", # Subfolder should be processed as `None`
)
)

# Check file exists and is not in a subfolder in cache
# e.g: "(...)/snapshots/<commit-id>/config.json"
self.assertTrue(filepath.is_file())
self.assertEqual(filepath.name, CONFIG_NAME)
self.assertEqual(Path(filepath).parent.parent.name, "snapshots")

def test_hf_hub_url_with_empty_subfolder(self):
"""
Check subfolder arg is processed correctly when empty string is passed to
`hf_hub_url`.
See https://github.com/huggingface/huggingface_hub/issues/1016.
"""
url = hf_hub_url(
DUMMY_MODEL_ID,
filename=CONFIG_NAME,
subfolder="", # Subfolder should be processed as `None`
)
self.assertTrue(
url.endswith(
# "./resolve/main/config.json" and not "./resolve/main//config.json"
f"{DUMMY_MODEL_ID}/resolve/main/config.json",
)
)

def test_hf_hub_download_legacy(self):
filepath = hf_hub_download(
DUMMY_MODEL_ID,
Expand Down

0 comments on commit 4af799e

Please # to comment.