Skip to content

Commit a6a23ac

Browse files
authored
Add offline_mode helper (#3593)
* Add offline mode helper * add example
1 parent 6a8a871 commit a6a23ac

File tree

6 files changed

+34
-3
lines changed

6 files changed

+34
-3
lines changed

docs/source/en/package_reference/environment_variables.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@ If set, the log level for the `huggingface_hub` logger is set to DEBUG. Addition
119119

120120
If set, no HTTP calls will be made to the Hugging Face Hub. If you try to download files, only the cached files will be accessed. If no cache file is detected, an error is raised This is useful in case your network is slow and you don't care about having the latest version of a file.
121121

122-
If `HF_HUB_OFFLINE=1` is set as environment variable and you call any method of [`HfApi`], an [`~huggingface_hub.utils.OfflineModeIsEnabled`] exception will be raised.
122+
If `HF_HUB_OFFLINE=1` is set as environment variable and you call any method of [`HfApi`], an [`~huggingface_hub.errors.OfflineModeIsEnabled`] exception will be raised.
123123

124124
**Note:** even if the latest version of a file is cached, calling `hf_hub_download` still triggers a HTTP request to check that a new version is not available. Setting `HF_HUB_OFFLINE=1` will skip this call which speeds up your loading time.
125125

126+
If you want to check if offline mode is enabled or not, you can use the [`offline_mode`] helper.
127+
126128
### HF_HUB_DISABLE_IMPLICIT_TOKEN
127129

128130
Authentication is not mandatory for every request to the Hub. For instance, requesting

docs/source/en/package_reference/utilities.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ except HfHubHTTPError as e:
184184

185185
[[autodoc]] huggingface_hub.utils.hf_raise_for_status
186186

187+
### Check offline mode
188+
189+
You can programmatically check if offline mode is enabled using `offline_mode`. Offline mode is enabled by setting `HF_HUB_OFFLINE=1` as environment variable.
190+
191+
[[autodoc]] offline_mode
192+
187193
### HTTP errors
188194

189195
Here is a list of HTTP errors thrown in `huggingface_hub`.

src/huggingface_hub/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
"REPO_TYPE_SPACE",
135135
"TF2_WEIGHTS_NAME",
136136
"TF_WEIGHTS_NAME",
137+
"offline_mode",
137138
],
138139
"fastai_utils": [
139140
"_save_pretrained_fastai",
@@ -930,6 +931,7 @@
930931
"model_info",
931932
"move_repo",
932933
"notebook_login",
934+
"offline_mode",
933935
"paper_info",
934936
"parse_huggingface_oauth",
935937
"parse_safetensors_file_metadata",
@@ -1158,6 +1160,7 @@ def __dir__():
11581160
REPO_TYPE_SPACE, # noqa: F401
11591161
TF2_WEIGHTS_NAME, # noqa: F401
11601162
TF_WEIGHTS_NAME, # noqa: F401
1163+
offline_mode, # noqa: F401
11611164
)
11621165
from .fastai_utils import (
11631166
_save_pretrained_fastai, # noqa: F401

src/huggingface_hub/constants.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ def _as_int(value: Optional[str]) -> Optional[int]:
162162

163163
HF_HUB_OFFLINE = _is_true(os.environ.get("HF_HUB_OFFLINE") or os.environ.get("TRANSFORMERS_OFFLINE"))
164164

165+
166+
def offline_mode() -> bool:
167+
"""Returns whether we are in offline mode for the Hub.
168+
169+
When offline mode is enabled, all HTTP requests made with `get_session` will raise an `OfflineModeIsEnabled` exception.
170+
171+
Example:
172+
```py
173+
from huggingface_hub import offline_mode
174+
175+
def list_files(repo_id: str):
176+
if offline_mode():
177+
... # list files from local cache (degraded experience but still functional)
178+
else:
179+
... # list files from Hub (complete experience)
180+
```
181+
"""
182+
return HF_HUB_OFFLINE
183+
184+
165185
# File created to mark that the version check has been done.
166186
# Check is performed once per 24 hours at most.
167187
CHECK_FOR_UPDATE_DONE_PATH = os.path.join(HF_HOME, ".check_for_update_done")

src/huggingface_hub/utils/_http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def hf_request_event_hook(request: httpx.Request) -> None:
167167
- Add a request ID to the request headers
168168
- Log the request if debug mode is enabled
169169
"""
170-
if constants.HF_HUB_OFFLINE:
170+
if constants.offline_mode():
171171
raise OfflineModeIsEnabled(
172172
f"Cannot reach {request.url}: offline mode is enabled. To disable it, please unset the `HF_HUB_OFFLINE` environment variable."
173173
)

src/huggingface_hub/utils/_telemetry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def send_telemetry(
6464
... )
6565
```
6666
"""
67-
if constants.HF_HUB_OFFLINE or constants.HF_HUB_DISABLE_TELEMETRY:
67+
if constants.offline_mode() or constants.HF_HUB_DISABLE_TELEMETRY:
6868
return
6969

7070
_start_telemetry_thread() # starts thread only if doesn't exist yet

0 commit comments

Comments
 (0)