U
    9%e                     @   s   d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 ddl
mZmZ e rXddlmZ neZerlddlmZ G dd	 d	eZd
S )zFContains a logger to push training logs to the Hub, using Tensorboard.    )Path)TYPE_CHECKINGListOptionalUnion)CommitScheduler   )experimentalis_tensorboard_available)SummaryWriterc                       s   e Zd ZdZed d fddZdddddddd	ddd

eee ee	e
f eee ee eee eeee ef  eeee ef  ee d fddZ fddZ  ZS )HFSummaryWriteraL  
    Wrapper around the tensorboard's `SummaryWriter` to push training logs to the Hub.

    Data is logged locally and then pushed to the Hub asynchronously. Pushing data to the Hub is done in a separate
    thread to avoid blocking the training script. In particular, if the upload fails for any reason (e.g. a connection
    issue), the main script will not be interrupted. Data is automatically pushed to the Hub every `commit_every`
    minutes (default to every 5 minutes).

    <Tip warning={true}>

    `HFSummaryWriter` is experimental. Its API is subject to change in the future without prior notice.

    </Tip>

    Args:
        repo_id (`str`):
            The id of the repo to which the logs will be pushed.
        logdir (`str`, *optional*):
            The directory where the logs will be written. If not specified, a local directory will be created by the
            underlying `SummaryWriter` object.
        commit_every (`int` or `float`, *optional*):
            The frequency (in minutes) at which the logs will be pushed to the Hub. Defaults to 5 minutes.
        squash_history (`bool`, *optional*):
            Whether to squash the history of the repo after each commit. Defaults to `False`. Squashing commits is
            useful to avoid degraded performances on the repo when it grows too large.
        repo_type (`str`, *optional*):
            The type of the repo to which the logs will be pushed. Defaults to "model".
        repo_revision (`str`, *optional*):
            The revision of the repo to which the logs will be pushed. Defaults to "main".
        repo_private (`bool`, *optional*):
            Whether to create a private repo or not. Defaults to False. This argument is ignored if the repo already
            exists.
        path_in_repo (`str`, *optional*):
            The path to the folder in the repo where the logs will be pushed. Defaults to "tensorboard/".
        repo_allow_patterns (`List[str]` or `str`, *optional*):
            A list of patterns to include in the upload. Defaults to `"*.tfevents.*"`. Check out the
            [upload guide](https://huggingface.co/docs/huggingface_hub/guides/upload#upload-a-folder) for more details.
        repo_ignore_patterns (`List[str]` or `str`, *optional*):
            A list of patterns to exclude in the upload. Check out the
            [upload guide](https://huggingface.co/docs/huggingface_hub/guides/upload#upload-a-folder) for more details.
        token (`str`, *optional*):
            Authentication token. Will default to the stored token. See https://huggingface.co/settings/token for more
            details
        kwargs:
            Additional keyword arguments passed to `SummaryWriter`.

    Examples:
    ```py
    >>> from huggingface_hub import HFSummaryWriter

    # Logs are automatically pushed every 15 minutes
    >>> logger = HFSummaryWriter(repo_id="test_hf_logger", commit_every=15)
    >>> logger.add_scalar("a", 1)
    >>> logger.add_scalar("b", 2)
    ...

    # You can also trigger a push manually
    >>> logger.scheduler.trigger()
    ```

    ```py
    >>> from huggingface_hub import HFSummaryWriter

    # Logs are automatically pushed every 5 minutes (default) + when exiting the context manager
    >>> with HFSummaryWriter(repo_id="test_hf_logger") as logger:
    ...     logger.add_scalar("a", 1)
    ...     logger.add_scalar("b", 2)
    ```
    )returnc                    s   t  stdt | S )NzvYou must have `tensorboard` installed to use `HFSummaryWriter`. Please run `pip install --upgrade tensorboardX` first.)r
   ImportErrorsuper__new__)clsargskwargs	__class__ b/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/huggingface_hub/_tensorboard_logger.pyr   i   s
    zHFSummaryWriter.__new__N   FZtensorboardz*.tfevents.*)
logdircommit_everysquash_history	repo_typerepo_revisionrepo_privatepath_in_reporepo_allow_patternsrepo_ignore_patternstoken)repo_idr   r   r   r   r   r   r   r    r!   r"   c       
            s   t  jf d|i| t| jts@td| j dt| j d|d ksP|dkr^t| jj}n|	dd t| jj }t
| j|||||||	|
||d| _| jj| _| jj| _| jj| _d S )Nr   z%`self.logdir` must be a string. Got 'z
' of type . /)Zfolder_pathr   r#   r   revisionprivater"   Zallow_patternsignore_patternsZeveryr   )r   __init__
isinstancer   str
ValueErrortyper   namestripr   	schedulerr#   r   r'   r   )selfr#   r   r   r   r   r   r   r   r    r!   r"   r   r   r   r   r*   r   s,    

zHFSummaryWriter.__init__c                    s&   t  ||| | j }|  dS )zLPush to hub in a non-blocking way when exiting the logger's context manager.N)r   __exit__r1   triggerresult)r2   exc_typeexc_valexc_tbfuturer   r   r   r3      s    
zHFSummaryWriter.__exit__)__name__
__module____qualname____doc__r	   r   r,   r   r   intfloatboolr   r*   r3   __classcell__r   r   r   r   r   "   s6   F
1r   N)r=   pathlibr   typingr   r   r   r   Z!huggingface_hub._commit_schedulerr   utilsr	   r
   ZtensorboardXr   objectr   r   r   r   r   <module>   s   