Skip to content

Commit b11644d

Browse files
committed
Add offline mode helper
1 parent 6a8a871 commit b11644d

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ 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+
return HF_HUB_OFFLINE
172+
173+
165174
# File created to mark that the version check has been done.
166175
# Check is performed once per 24 hours at most.
167176
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)