Skip to content

fix snapshot download behavior in offline mode when downloading to a local dir #3009

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/huggingface_hub/_snapshot_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,13 @@ def snapshot_download(
commit_hash = f.read()

# Try to locate snapshot folder for this commit hash
if commit_hash is not None:
if commit_hash is not None and local_dir is None:
snapshot_folder = os.path.join(storage_folder, "snapshots", commit_hash)
if os.path.exists(snapshot_folder):
# Snapshot folder exists => let's return it
# (but we can't check if all the files are actually there)
return snapshot_folder

# If local_dir is not None, return it if it exists and is not empty
if local_dir is not None:
local_dir = Path(local_dir)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_snapshot_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ def test_download_model_to_local_dir_with_offline_mode(self):
storage_folder = snapshot_download(self.repo_id, local_dir=tmpdir)
self.assertEqual(str(tmpdir), storage_folder)

def test_offline_mode_with_cache_and_empty_local_dir(self):
"""Test that when cache exists but an empty local_dir is specified in offline mode, we raise an error."""
with SoftTemporaryDirectory() as tmpdir_cache:
snapshot_download(self.repo_id, cache_dir=tmpdir_cache)

for offline_mode in OfflineSimulationMode:
with offline(mode=offline_mode):
with self.assertRaises(LocalEntryNotFoundError):
with SoftTemporaryDirectory() as tmpdir:
snapshot_download(self.repo_id, cache_dir=tmpdir_cache, local_dir=tmpdir)

def test_download_model_offline_mode_not_in_local_dir(self):
"""Test when connection error but local_dir is empty."""
with SoftTemporaryDirectory() as tmpdir:
Expand Down
Loading