Skip to content

Commit 26a7e6d

Browse files
winglianCyrilvallez
authored andcommitted
fix broken offline mode when loading tokenizer from hub (#40669)
* fix broken offline mode when loading tokenizer from hub * formatting * make quality * fix import order
1 parent d56f916 commit 26a7e6d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/transformers/utils/hub.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def list_repo_templates(
176176
]
177177
except (GatedRepoError, RepositoryNotFoundError, RevisionNotFoundError):
178178
raise # valid errors => do not catch
179-
except (HTTPError, requests.exceptions.ConnectionError):
179+
except (HTTPError, OfflineModeIsEnabled, requests.exceptions.ConnectionError):
180180
pass # offline mode, internet down, etc. => try local files
181181

182182
# check local files

tests/utils/test_hub_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from pathlib import Path
2020

2121
from huggingface_hub import hf_hub_download
22+
from huggingface_hub.errors import LocalEntryNotFoundError, OfflineModeIsEnabled
2223
from requests.exceptions import HTTPError
2324

2425
from transformers.utils import (
@@ -29,6 +30,7 @@
2930
WEIGHTS_NAME,
3031
cached_file,
3132
has_file,
33+
list_repo_templates,
3234
)
3335

3436

@@ -198,3 +200,12 @@ def test_cached_files_exception_raised(self):
198200
with self.assertRaises(ModuleNotFoundError):
199201
# The error should be re-raised by cached_files, not caught in the exception handling block
200202
cached_file(RANDOM_BERT, "nonexistent.json")
203+
204+
205+
class OfflineModeTests(unittest.TestCase):
206+
def test_list_repo_templates_w_offline(self):
207+
with mock.patch("transformers.utils.hub.list_repo_tree", side_effect=OfflineModeIsEnabled()):
208+
with mock.patch(
209+
"transformers.utils.hub.snapshot_download", side_effect=LocalEntryNotFoundError("no snapshot found")
210+
):
211+
self.assertEqual(list_repo_templates(RANDOM_BERT, local_files_only=False), [])

0 commit comments

Comments
 (0)