MCPcopy
hub / github.com/huggingface/datasets / offline

Function offline

tests/utils.py:455–505  ·  view source on GitHub ↗

Simulate offline mode. There are three offline simulation modes: CONNECTION_FAILS (default mode): a ConnectionError is raised for each network call. CONNECTION_TIMES_OUT: a ReadTimeout or ConnectTimeout is raised for each network call. HF_HUB_OFFLINE_SET_TO_1: the HF_HUB_OFFLI

(mode: OfflineSimulationMode)

Source from the content-addressed store, hash-verified

453
454@contextmanager
455def offline(mode: OfflineSimulationMode):
456 """
457 Simulate offline mode.
458
459 There are three offline simulation modes:
460
461 CONNECTION_FAILS (default mode): a ConnectionError is raised for each network call.
462 CONNECTION_TIMES_OUT: a ReadTimeout or ConnectTimeout is raised for each network call.
463 HF_HUB_OFFLINE_SET_TO_1: the HF_HUB_OFFLINE_SET_TO_1 environment variable is set to 1.
464 This makes the http/ftp calls of the library instantly fail and raise an OfflineModeEnabled error.
465
466 The raised exceptions are either from the `requests` library (if `huggingface_hub<1.0.0`)
467 or from the `httpx` library (if `huggingface_hub>=1.0.0`).
468 """
469 # Enable offline mode
470 if mode is OfflineSimulationMode.HF_HUB_OFFLINE_SET_TO_1:
471 with patch("datasets.config.HF_HUB_OFFLINE", True):
472 yield
473 return
474
475 # Determine which exception to raise based on mode
476
477 def error_response(*args, **kwargs):
478 if mode is OfflineSimulationMode.CONNECTION_FAILS:
479 exc = httpx.ConnectError if IS_HF_HUB_1_x else requests.ConnectionError
480 elif mode is OfflineSimulationMode.CONNECTION_TIMES_OUT:
481 if kwargs.get("timeout") is None:
482 raise RequestWouldHangIndefinitelyError(
483 "Tried an HTTP call in offline mode with no timeout set. Please set a timeout."
484 )
485 exc = httpx.ReadTimeout if IS_HF_HUB_1_x else requests.ConnectTimeout
486 else:
487 raise ValueError("Please use a value from the OfflineSimulationMode enum.")
488 raise exc(f"Offline mode {mode}")
489
490 # Patch all client methods to raise the appropriate error
491 client_mock = Mock()
492 for method in ["head", "get", "post", "put", "delete", "request", "stream"]:
493 setattr(client_mock, method, Mock(side_effect=error_response))
494
495 # Patching is slightly different depending on hfh internals
496 patch_target = (
497 {"target": "huggingface_hub.utils._http._GLOBAL_CLIENT", "new": client_mock}
498 if IS_HF_HUB_1_x
499 else {
500 "target": "huggingface_hub.utils._http._get_session_from_cache",
501 "return_value": client_mock,
502 }
503 )
504 with patch(**patch_target):
505 yield
506
507
508@contextmanager

Calls

no outgoing calls