Skip to content

Commit

Permalink
Merge branch 'main' into 1352-create-chunked-commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Wauplin committed Mar 29, 2023
2 parents e42a937 + 26dbe09 commit 167d320
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
40 changes: 40 additions & 0 deletions docs/source/package_reference/utilities.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,46 @@ Using these shouldn't be necessary if you use `huggingface_hub` and you don't mo

[[autodoc]] logging.get_logger

## Configure progress bars

Progress bars are a useful tool to display information to the user while a long-running task is being executed (e.g.
when downloading or uploading files). `huggingface_hub` exposes a [`~utils.tqdm`] wrapper to display progress bars in a
consistent way across the library.

By default, progress bars are enabled. You can disable them globally by setting `HF_HUB_DISABLE_PROGRESS_BARS`
environment variable. You can also enable/disable them using [`~utils.enable_progress_bars`] and
[`~utils.disable_progress_bars`]. If set, the environment variable has priority on the helpers.


```py
>>> from huggingface_hub import snapshot_download
>>> from huggingface_hub.utils import are_progress_bars_disabled, disable_progress_bars, enable_progress_bars

>>> # Disable progress bars globally
>>> disable_progress_bars()

>>> # Progress bar will not be shown !
>>> snapshot_download("gpt2")

>>> are_progress_bars_disabled()
True

>>> # Re-enable progress bars globally
>>> enable_progress_bars()
```

### are_progress_bars_disabled

[[autodoc]] huggingface_hub.utils.are_progress_bars_disabled

### disable_progress_bars

[[autodoc]] huggingface_hub.utils.disable_progress_bars

### enable_progress_bars

[[autodoc]] huggingface_hub.utils.enable_progress_bars

## Configure HTTP backend

In some environments, you might want to configure how HTTP calls are made, for example if you are using a proxy.
Expand Down
6 changes: 2 additions & 4 deletions src/huggingface_hub/_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,13 @@ def interpreter_login() -> None:
For more details, see [`login`].
"""
print( # docstyle-ignore
"""
print("""
_| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|
_| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
_|_|_|_| _| _| _| _|_| _| _|_| _| _| _| _| _| _|_| _|_|_| _|_|_|_| _| _|_|_|
_| _| _| _| _| _| _| _| _| _| _|_| _| _| _| _| _| _| _|
_| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|
"""
)
""")
if HfFolder.get_token() is not None:
print(
" A token is already saved on your machine. Run `huggingface-cli"
Expand Down
41 changes: 23 additions & 18 deletions src/huggingface_hub/utils/tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@

def disable_progress_bars() -> None:
"""
Disable globally progress bars used in `huggingface_hub` except if
`HF_HUB_DISABLE_PROGRESS_BARS` environment variable has been set.
Disable globally progress bars used in `huggingface_hub` except if `HF_HUB_DISABLE_PROGRESS_BARS` environment
variable has been set.
Use [`~utils.enable_progress_bars`] to re-enable them.
"""
if HF_HUB_DISABLE_PROGRESS_BARS is False:
warnings.warn(
"Cannot disable progress bars: environment variable"
" `HF_HUB_DISABLE_PROGRESS_BARS=0` is set and has priority."
"Cannot disable progress bars: environment variable `HF_HUB_DISABLE_PROGRESS_BARS=0` is set and has"
" priority."
)
return
global _hf_hub_progress_bars_disabled
Expand All @@ -92,21 +94,27 @@ def disable_progress_bars() -> None:

def enable_progress_bars() -> None:
"""
Enable globally progress bars used in `huggingface_hub` except if
`HF_HUB_DISABLE_PROGRESS_BARS` environment variable has been set.
Enable globally progress bars used in `huggingface_hub` except if `HF_HUB_DISABLE_PROGRESS_BARS` environment
variable has been set.
Use [`~utils.disable_progress_bars`] to disable them.
"""
if HF_HUB_DISABLE_PROGRESS_BARS is True:
warnings.warn(
"Cannot enable progress bars: environment variable"
" `HF_HUB_DISABLE_PROGRESS_BARS=1` is set and has priority."
"Cannot enable progress bars: environment variable `HF_HUB_DISABLE_PROGRESS_BARS=1` is set and has"
" priority."
)
return
global _hf_hub_progress_bars_disabled
_hf_hub_progress_bars_disabled = False


def are_progress_bars_disabled() -> bool:
"""Return whether progress bars are globally disabled or not."""
"""Return whether progress bars are globally disabled or not.
Progress bars used in `huggingface_hub` can be enable or disabled globally using [`~utils.enable_progress_bars`]
and [`~utils.disable_progress_bars`] or by setting `HF_HUB_DISABLE_PROGRESS_BARS` as environment variable.
"""
global _hf_hub_progress_bars_disabled
return _hf_hub_progress_bars_disabled

Expand All @@ -127,17 +135,14 @@ def __init__(self, *args, **kwargs):
@contextmanager
def tqdm_stream_file(path: Union[Path, str]) -> Iterator[io.BufferedReader]:
"""
Open a file as binary and wrap the `read` method to display a progress bar when it's
streamed.
Open a file as binary and wrap the `read` method to display a progress bar when it's streamed.
First implemented in `transformers` in 2019 but removed when switched to git-lfs.
Today used in `huggingface_hub` to show progress bar when uploading an LFS file to
the Hub. See github.com/huggingface/transformers/pull/2078#discussion_r354739608 for
implementation details.
First implemented in `transformers` in 2019 but removed when switched to git-lfs. Used in `huggingface_hub` to show
progress bar when uploading an LFS file to the Hub. See github.com/huggingface/transformers/pull/2078#discussion_r354739608
for implementation details.
Note: currently implementation handles only files stored on disk as it is the most
common use case. Could be extended to stream any `BinaryIO` object but we might
have to debug some corner cases.
Note: currently implementation handles only files stored on disk as it is the most common use case. Could be
extended to stream any `BinaryIO` object but we might have to debug some corner cases.
Example:
```py
Expand Down

0 comments on commit 167d320

Please sign in to comment.