Skip to content

Commit 8693672

Browse files
fix snapshot download behavior in offline mode when downloading to a local dir (#3009)
* raise error when cache exists but local dir is specified in offline mode * fixes * remove unnecessary error Co-authored-by: Lucain <[email protected]> --------- Co-authored-by: Lucain <[email protected]>
1 parent fad6da5 commit 8693672

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/huggingface_hub/_snapshot_download.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,13 @@ def snapshot_download(
200200
commit_hash = f.read()
201201

202202
# Try to locate snapshot folder for this commit hash
203-
if commit_hash is not None:
203+
if commit_hash is not None and local_dir is None:
204204
snapshot_folder = os.path.join(storage_folder, "snapshots", commit_hash)
205205
if os.path.exists(snapshot_folder):
206206
# Snapshot folder exists => let's return it
207207
# (but we can't check if all the files are actually there)
208208
return snapshot_folder
209+
209210
# If local_dir is not None, return it if it exists and is not empty
210211
if local_dir is not None:
211212
local_dir = Path(local_dir)

tests/test_snapshot_download.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,17 @@ def test_download_model_to_local_dir_with_offline_mode(self):
162162
storage_folder = snapshot_download(self.repo_id, local_dir=tmpdir)
163163
self.assertEqual(str(tmpdir), storage_folder)
164164

165+
def test_offline_mode_with_cache_and_empty_local_dir(self):
166+
"""Test that when cache exists but an empty local_dir is specified in offline mode, we raise an error."""
167+
with SoftTemporaryDirectory() as tmpdir_cache:
168+
snapshot_download(self.repo_id, cache_dir=tmpdir_cache)
169+
170+
for offline_mode in OfflineSimulationMode:
171+
with offline(mode=offline_mode):
172+
with self.assertRaises(LocalEntryNotFoundError):
173+
with SoftTemporaryDirectory() as tmpdir:
174+
snapshot_download(self.repo_id, cache_dir=tmpdir_cache, local_dir=tmpdir)
175+
165176
def test_download_model_offline_mode_not_in_local_dir(self):
166177
"""Test when connection error but local_dir is empty."""
167178
with SoftTemporaryDirectory() as tmpdir:

0 commit comments

Comments
 (0)