o
    0j&4                     @   s  U d Z ddlZddlZddlZddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZ i Zeeef ed
< G dd dZd-dedB ddfddZd-dedB defddZdededB fddZG dd deZee  G dd dZ e	deeB deej! fddZ"ddde#e dededB defdd Z$ddd!d"dddd#d$eded%edB d&ed'ed(ededB d)e#e dB d*edB 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)Iterator)contextmanagernullcontext)Path)ContextManager)tqdm   )HF_HUB_DISABLE_PROGRESS_BARSprogress_bar_statesc                   @   s<   e Zd ZdZddedB ddfddZdddZdd	d
ZdS )disable_progress_barsa&  
    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.

    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,
            progress bars are disabled globally.

    Raises:
        Warning: If the environment variable precludes changes.
    Nnamereturnc                    sx    | _ tdu rtd d| _d S t  | _ d u r%t  dtd< d S  fddtD }|D ]}t|= q0dt < d S )NFzlCannot disable progress bars: environment variable `HF_HUB_DISABLE_PROGRESS_BARS=0` is set and has priority._globalc                        g | ]}|   d r|qS .
startswith.0keyr    [/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/huggingface_hub/utils/tqdm.py
<listcomp>        z2disable_progress_bars.__init__.<locals>.<listcomp>)r   r	   warningswarn_should_reenableare_progress_bars_disabledr
   clear)selfr   keys_to_remover   r   r   r   __init__   s   zdisable_progress_bars.__init__c                 C      | S Nr   r!   r   r   r   	__enter__      zdisable_progress_bars.__enter__c                 G   s   | j r
t| j d S d S r%   )r   enable_progress_barsr   )r!   excr   r   r   __exit__   s   zdisable_progress_bars.__exit__r%   )r   r   )r   N)__name__
__module____qualname____doc__strr#   r'   r+   r   r   r   r   r   l   s
    
r   r   r   c                    s`   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 )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    )r   r"   r   r   r   r   r)      s   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   r      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.
    TZTQDM_POSITIONz-1FN)loggingNOTSETosgetenv)r5   r   r   r   is_tqdm_disabled   s
   
r:   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   superr#   )r!   argskwargsr   	__class__r   r   r#      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)r=   __delattr__AttributeError)r!   rB   r@   r   r   rD      s   ztqdm.__delattr__)r,   r-   r.   r/   r#   r0   rD   __classcell__r   r   r@   r   r      s    r   c                   @   sD   e Zd ZdZdd Zdd Zdd Zdd	eeB d
B dd
fddZ	d
S )silent_tqdmz#Fake tqdm object that does nothing.c                 O      d S r%   r   )r!   r>   r?   r   r   r   r#     r(   zsilent_tqdm.__init__c                 C   r$   r%   r   r&   r   r   r   r'     r(   zsilent_tqdm.__enter__c                 C   rH   r%   r   )r!   exc_type	exc_value	tracebackr   r   r   r+     r(   zsilent_tqdm.__exit__   nNr   c                 C   rH   r%   r   )r!   rM   r   r   r   update  r(   zsilent_tqdm.update)rL   )
r,   r-   r.   r/   r#   r'   r+   intfloatrN   r   r   r   r   rG     s     rG   pathc                 #   s    t | tr
t| } | d4}|  j}tdd|d| jd|j ddt	dB d	t
f fd
d}||_|V    W d   dS 1 sFw   Y  dS )uQ  
    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:
    >>>     httpx.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descr1   sizeNr   c                    s    | } t| |S r%   )rN   len)rY   dataZf_readZpbarr   r   _inner_read7  s   z%tqdm_stream_file.<locals>._inner_read)r1   )
isinstancer0   r   openstatst_sizer   r   readrO   bytesclose)rQ   fZ
total_sizer]   r   r\   r   tqdm_stream_file  s$   


"rf   r   clsc                 K   s>   t | tr
t| ts| di |S t|}| d||d|S )a  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.
    )r;   r   Nr   )r^   type
issubclassr   r:   )rg   r5   r   r?   r;   r   r   r   _create_progress_barC  s   rj   rS   T)rV   rW   rT   rU   r   
tqdm_class	_tqdm_barrX   rV   rW   rT   rU   rk   rl   c        	   	   
   C   s,   |d urt |S t|pt||||||| dS )N)rg   r5   r   rT   rU   rV   rW   rX   )r   rj   r   )	rX   r5   rV   rW   rT   rU   r   rk   rl   r   r   r   _get_progress_bar_contextY  s   rm   r%   )&r/   ior6   r8   	threadingr   collections.abcr   
contextlibr   r   pathlibr   typingr   Z	tqdm.autor   Zold_tqdm	constantsr	   r
   dictr0   bool__annotations__r   r)   r   rO   r:   Zset_lockRLockrG   BufferedReaderrf   rh   rj   rm   r   r   r   r   <module>   sf   D2 (-
	
