Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable tqdm progress in cloud environments #2698

Merged
merged 5 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
from .utils._runtime import _PY_VERSION # noqa: F401 # for backward compatibility
from .utils._typing import HTTP_METHOD_T
from .utils.sha import sha_fileobj
from .utils.tqdm import is_tqdm_disabled


logger = logging.get_logger(__name__)
Expand Down Expand Up @@ -402,9 +403,7 @@ def http_get(
total=total,
initial=resume_size,
desc=displayed_filename,
disable=True if (logger.getEffectiveLevel() == logging.NOTSET) else None,
# ^ set `disable=None` rather than `disable=False` by default to disable progress bar when no TTY attached
# see https://github.com/huggingface/huggingface_hub/pull/2000
disable=is_tqdm_disabled(logger.getEffectiveLevel()),
name="huggingface_hub.http_get",
)
if _tqdm_bar is None
Expand Down
7 changes: 2 additions & 5 deletions src/huggingface_hub/lfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
)
from .utils._lfs import SliceFileObj
from .utils.sha import sha256, sha_fileobj
from .utils.tqdm import is_tqdm_disabled


if TYPE_CHECKING:
Expand Down Expand Up @@ -430,17 +431,13 @@ def _upload_parts_hf_transfer(
if len(desc) > 40:
desc = f"(…){desc[-40:]}"

# set `disable=None` rather than `disable=False` by default to disable progress bar when no TTY attached
# see https://github.com/huggingface/huggingface_hub/pull/2000
disable = True if (logger.getEffectiveLevel() == logging.NOTSET) else None

with tqdm(
unit="B",
unit_scale=True,
total=total,
initial=0,
desc=desc,
disable=disable,
disable=is_tqdm_disabled(logger.getEffectiveLevel()),
name="huggingface_hub.lfs_upload",
) as progress:
try:
Expand Down
15 changes: 15 additions & 0 deletions src/huggingface_hub/utils/tqdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
"""

import io
import logging
import os
import warnings
from contextlib import contextmanager
from pathlib import Path
Expand Down Expand Up @@ -196,6 +198,19 @@ def are_progress_bars_disabled(name: Optional[str] = None) -> bool:
return not progress_bar_states.get("_global", True)


def is_tqdm_disabled(log_level: int) -> Optional[bool]:
"""
Determine if tqdm progress bars should be disabled based on logging level and environment settings.

see https://github.com/huggingface/huggingface_hub/pull/2000 and https://github.com/huggingface/huggingface_hub/pull/2698.
"""
if log_level == logging.NOTSET:
return True
if os.getenv("TQDM_POSITION") == "-1":
return False
return None


class tqdm(old_tqdm):
"""
Class to override `disable` argument in case progress bars are globally disabled.
Expand Down
Loading