o
    h)                     @   sn  U d Z ddlZddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZmZ ddlmZ ddlmZ i Zeeef ed	< d%d
ee ddfddZd%d
ee ddfddZd%d
ee defddZdedee fddZG dd deZedee	ef deej fddZddddddddededee ded ed!ed
ee d"ee dee fd#d$Z dS )&u	  Utility helpers to handle progress bars in `huggingface_hub`.

Example:
    1. Use `huggingface_hub.utils.tqdm` as you would use `tqdm.tqdm` or `tqdm.auto.tqdm`.
    2. To disable progress bars, either use `disable_progress_bars()` helper or set the
       environment variable `HF_HUB_DISABLE_PROGRESS_BARS` to 1.
    3. To re-enable progress bars, use `enable_progress_bars()`.
    4. To check whether progress bars are disabled, use `are_progress_bars_disabled()`.

NOTE: Environment variable `HF_HUB_DISABLE_PROGRESS_BARS` has the priority.

Example:
    ```py
    >>> from huggingface_hub.utils import are_progress_bars_disabled, disable_progress_bars, enable_progress_bars, tqdm

    # Disable progress bars globally
    >>> disable_progress_bars()

    # Use as normal `tqdm`
    >>> for _ in tqdm(range(5)):
    ...    pass

    # Still not showing progress bars, as `disable=False` is overwritten to `True`.
    >>> for _ in tqdm(range(5), disable=False):
    ...    pass

    >>> are_progress_bars_disabled()
    True

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

    # Progress bar will be shown !
    >>> for _ in tqdm(range(5)):
    ...   pass
    100%|███████████████████████████████████████| 5/5 [00:00<00:00, 117817.53it/s]
    ```

Group-based control:
    ```python
    # Disable progress bars for a specific group
    >>> disable_progress_bars("peft.foo")

    # Check state of different groups
    >>> assert not are_progress_bars_disabled("peft"))
    >>> assert not are_progress_bars_disabled("peft.something")
    >>> assert are_progress_bars_disabled("peft.foo"))
    >>> assert are_progress_bars_disabled("peft.foo.bar"))

    # Enable progress bars for a subgroup
    >>> enable_progress_bars("peft.foo.bar")

    # Check if enabling a subgroup affects the parent group
    >>> assert are_progress_bars_disabled("peft.foo"))
    >>> assert not are_progress_bars_disabled("peft.foo.bar"))

    # No progress bar for `name="peft.foo"`
    >>> for _ in tqdm(range(5), name="peft.foo"):
    ...     pass

    # Progress bar will be shown for `name="peft.foo.bar"`
    >>> for _ in tqdm(range(5), name="peft.foo.bar"):
    ...     pass
    100%|███████████████████████████████████████| 5/5 [00:00<00:00, 117817.53it/s]

    ```
    N)contextmanagernullcontext)Path)ContextManagerDictIteratorOptionalUnion)tqdm   )HF_HUB_DISABLE_PROGRESS_BARSprogress_bar_statesnamereturnc                    `   t du rtd dS  du rt  dtd< dS  fddtD }|D ]}t|= q$dt < dS )a4  
    Disable progress bars either globally or for a specified group.

    This function updates the state of progress bars based on a group name.
    If no group name is provided, all progress bars are disabled. The operation
    respects the `HF_HUB_DISABLE_PROGRESS_BARS` environment variable's setting.

    Args:
        name (`str`, *optional*):
            The name of the group for which to disable the progress bars. If None,
            progress bars are disabled globally.

    Raises:
        Warning: If the environment variable precludes changes.
    FzlCannot disable progress bars: environment variable `HF_HUB_DISABLE_PROGRESS_BARS=0` is set and has priority.N_globalc                        g | ]}|   d r|qS .
startswith.0keyr    n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/huggingface_hub/utils/tqdm.py
<listcomp>        z)disable_progress_bars.<locals>.<listcomp>r   warningswarnr   clearr   keys_to_remover   r   r   r   disable_progress_barsl      r%   c                    r   )a  
    Enable progress bars either globally or for a specified group.

    This function sets the progress bars to enabled for the specified group or globally
    if no group is specified. The operation is subject to the `HF_HUB_DISABLE_PROGRESS_BARS`
    environment setting.

    Args:
        name (`str`, *optional*):
            The name of the group for which to enable the progress bars. If None,
            progress bars are enabled globally.

    Raises:
        Warning: If the environment variable precludes changes.
    TzkCannot enable progress bars: environment variable `HF_HUB_DISABLE_PROGRESS_BARS=1` is set and has priority.Nr   c                    r   r   r   r   r   r   r   r      r   z(enable_progress_bars.<locals>.<listcomp>r   r#   r   r   r   enable_progress_bars   r&   r'   c                 C   sb   t du rdS | du rtdd S | r*| tv rt|   S d| ddd } | stdd S )a  
    Check if progress bars are disabled globally or for a specific group.

    This function returns whether progress bars are disabled for a given group or globally.
    It checks the `HF_HUB_DISABLE_PROGRESS_BARS` environment variable first, then the programmatic
    settings.

    Args:
        name (`str`, *optional*):
            The group name to check; if None, checks the global setting.

    Returns:
        `bool`: True if progress bars are disabled, False otherwise.
    TNr   r   )r   r   getjoinsplitr   r   r   r   are_progress_bars_disabled   s   
r,   	log_levelc                 C   s$   | t jkrdS tddkrdS dS )z
    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.
    TTQDM_POSITIONz-1FN)loggingNOTSETosgetenvr-   r   r   r   is_tqdm_disabled   s
   
r4   c                       s6   e Zd ZdZ fddZdeddf fddZ  ZS )	r
   z
    Class to override `disable` argument in case progress bars are globally disabled.

    Taken from https://github.com/tqdm/tqdm/issues/619#issuecomment-619639324.
    c                    s2   | dd }t|rd|d< t j|i | d S )Nr   Tdisable)popr,   super__init__)selfargskwargsr   	__class__r   r   r8      s   ztqdm.__init__attrr   Nc                    s2   z	t  | W dS  ty   |dkr Y dS w )zBFix for https://github.com/huggingface/huggingface_hub/issues/1603_lockN)r7   __delattr__AttributeError)r9   r>   r<   r   r   r@      s   ztqdm.__delattr__)__name__
__module____qualname____doc__r8   strr@   __classcell__r   r   r<   r   r
      s    r
   pathc                 #   s    t | tr
t| } | d4}|  j}tdd|d| jd|j ddt	t
 dtf fd	d
}||_|V    W d   dS 1 sFw   Y  dS )uT  
    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. 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.

    Example:
    ```py
    >>> with tqdm_stream_file("config.json") as f:
    >>>     requests.put(url, data=f)
    config.json: 100%|█████████████████████████| 8.19k/8.19k [00:02<00:00, 3.72kB/s]
    ```
    rbBTr   )unit
unit_scaletotalinitialdescr(   sizer   c                    s    | } t| |S N)updatelen)rP   dataf_readpbarr   r   _inner_read  s   z%tqdm_stream_file.<locals>._inner_readN)r(   )
isinstancerF   r   openstatst_sizer
   r   readr   intbytesclose)rH   f
total_sizerX   r   rU   r   tqdm_stream_file   s$   


"rc   rJ   T)rM   rN   rK   rL   r   	_tqdm_barrO   rM   rN   rK   rL   rd   c              	   C   s,   |d urt |S t||||| t|d|dS )Nr3   )rK   rL   rM   rN   rO   r5   r   )r   r
   r4   )rO   r-   rM   rN   rK   rL   r   rd   r   r   r   _get_progress_bar_context  s   re   rQ   )!rE   ior/   r1   r    
contextlibr   r   pathlibr   typingr   r   r   r   r	   	tqdm.autor
   old_tqdm	constantsr   r   rF   bool__annotations__r%   r'   r,   r^   r4   BufferedReaderrc   re   r   r   r   r   <module>   sV   D  "1	
