Voice et bot modif

This commit is contained in:
pi 2026-06-16 17:09:34 +00:00
parent 189d56026b
commit 7333a22bcd
10774 changed files with 634644 additions and 933308 deletions

View file

@ -1,4 +1,3 @@
# coding=utf-8
# Copyright 2021 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
@ -83,10 +82,12 @@ Group-based control:
import io
import logging
import os
import threading
import warnings
from collections.abc import Iterator
from contextlib import contextmanager, nullcontext
from pathlib import Path
from typing import ContextManager, Iterator, Optional, Union
from typing import ContextManager
from tqdm.auto import tqdm as old_tqdm
@ -101,11 +102,10 @@ from ..constants import HF_HUB_DISABLE_PROGRESS_BARS
# If `HF_HUB_DISABLE_PROGRESS_BARS` is not defined (None), it implies that users can manage
# progress bar visibility through code. By default, progress bars are turned on.
progress_bar_states: dict[str, bool] = {}
def disable_progress_bars(name: Optional[str] = None) -> None:
class disable_progress_bars:
"""
Disable progress bars either globally or for a specified group.
@ -113,6 +113,11 @@ def disable_progress_bars(name: Optional[str] = None) -> None:
If no group name is provided, all progress bars are disabled. The operation
respects the `HF_HUB_DISABLE_PROGRESS_BARS` environment variable's setting.
Works as both a regular call and a context manager:
disable_progress_bars() # disables until enable_progress_bars()
with disable_progress_bars(): # disables for the block, re-enables on exit
...
Args:
name (`str`, *optional*):
The name of the group for which to disable the progress bars. If None,
@ -121,23 +126,36 @@ def disable_progress_bars(name: Optional[str] = None) -> None:
Raises:
Warning: If the environment variable precludes changes.
"""
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."
)
return
if name is None:
progress_bar_states.clear()
progress_bar_states["_global"] = False
else:
keys_to_remove = [key for key in progress_bar_states if key.startswith(f"{name}.")]
for key in keys_to_remove:
del progress_bar_states[key]
progress_bar_states[name] = False
def __init__(self, name: str | None = None) -> None:
self.name = name
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."
)
self._should_reenable = False
return
self._should_reenable = not are_progress_bars_disabled(name)
if name is None:
progress_bar_states.clear()
progress_bar_states["_global"] = False
else:
keys_to_remove = [key for key in progress_bar_states if key.startswith(f"{name}.")]
for key in keys_to_remove:
del progress_bar_states[key]
progress_bar_states[name] = False
def __enter__(self) -> "disable_progress_bars":
return self
def __exit__(self, *exc) -> None:
if self._should_reenable:
enable_progress_bars(self.name)
def enable_progress_bars(name: Optional[str] = None) -> None:
def enable_progress_bars(name: str | None = None) -> None:
"""
Enable progress bars either globally or for a specified group.
@ -169,7 +187,7 @@ def enable_progress_bars(name: Optional[str] = None) -> None:
progress_bar_states[name] = True
def are_progress_bars_disabled(name: Optional[str] = None) -> bool:
def are_progress_bars_disabled(name: str | None = None) -> bool:
"""
Check if progress bars are disabled globally or for a specific group.
@ -198,7 +216,7 @@ 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]:
def is_tqdm_disabled(log_level: int) -> bool | None:
"""
Determine if tqdm progress bars should be disabled based on logging level and environment settings.
@ -233,8 +251,31 @@ class tqdm(old_tqdm):
raise
# Prevent tqdm's default multiprocessing write-lock from spawning a resource
# tracker subprocess via fork_exec(). That path fails when stderr has an invalid
# fd (e.g. Textual TUIs that return -1 from sys.stderr.fileno()). Inter-process
# bar coordination on the HF subclass is not a supported use case. See #4065.
tqdm.set_lock(threading.RLock())
class silent_tqdm:
"""Fake tqdm object that does nothing."""
def __init__(self, *args, **kwargs):
pass
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
pass
def update(self, n: int | float | None = 1) -> None:
pass
@contextmanager
def tqdm_stream_file(path: Union[Path, str]) -> Iterator[io.BufferedReader]:
def tqdm_stream_file(path: Path | str) -> Iterator[io.BufferedReader]:
"""
Open a file as binary and wrap the `read` method to display a progress bar when it's streamed.
@ -267,7 +308,7 @@ def tqdm_stream_file(path: Union[Path, str]) -> Iterator[io.BufferedReader]:
f_read = f.read
def _inner_read(size: Optional[int] = -1) -> bytes:
def _inner_read(size: int | None = -1) -> bytes:
data = f_read(size)
pbar.update(len(data))
return data
@ -279,17 +320,39 @@ def tqdm_stream_file(path: Union[Path, str]) -> Iterator[io.BufferedReader]:
pbar.close()
def _create_progress_bar(*, cls: type[old_tqdm], log_level: int, name: str | None = None, **kwargs) -> old_tqdm:
"""Create a progress bar.
For our `tqdm` subclass (or subclasses of it): respects all disable signals
(`HF_HUB_DISABLE_PROGRESS_BARS`, `disable_progress_bars()`, log level) and uses
`disable=None` for TTY auto-detection (see https://github.com/huggingface/huggingface_hub/pull/2000),
unless `TQDM_POSITION=-1` forces bars on (https://github.com/huggingface/huggingface_hub/pull/2698).
For other classes: does not inject `disable` or `name`. the custom class is fully
responsible for its own behavior. Vanilla tqdm defaults to `disable=False` (bar shows).
Omits `name` which vanilla tqdm rejects with `TqdmKeyError`. See https://github.com/huggingface/huggingface_hub/issues/4050.
"""
# issubclass() crashes on non-class callables (e.g. functools.partial), guard with isinstance.
if not (isinstance(cls, type) and issubclass(cls, tqdm)):
return cls(**kwargs) # type: ignore[return-value]
# HF subclass: keep the historical log-level / TTY behavior. Group-based
# disabling is already handled in `tqdm.__init__`.
disable = is_tqdm_disabled(log_level)
return cls(disable=disable, name=name, **kwargs) # type: ignore[return-value]
def _get_progress_bar_context(
*,
desc: str,
log_level: int,
total: Optional[int] = None,
total: int | None = None,
initial: int = 0,
unit: str = "B",
unit_scale: bool = True,
name: Optional[str] = None,
tqdm_class: Optional[type[old_tqdm]] = None,
_tqdm_bar: Optional[tqdm] = None,
name: str | None = None,
tqdm_class: type[old_tqdm] | None = None,
_tqdm_bar: tqdm | None = None,
) -> ContextManager[tqdm]:
if _tqdm_bar is not None:
return nullcontext(_tqdm_bar)
@ -297,12 +360,13 @@ def _get_progress_bar_context(
# Makes it easier to use the same code path for both cases but in the later
# case, the progress bar is not closed when exiting the context manager.
return (tqdm_class or tqdm)( # type: ignore[return-value]
return _create_progress_bar( # type: ignore
cls=tqdm_class or tqdm,
log_level=log_level,
name=name,
unit=unit,
unit_scale=unit_scale,
total=total,
initial=initial,
desc=desc,
disable=is_tqdm_disabled(log_level=log_level),
name=name,
)