Ë
    Q^(h¦   ã                   óÜ   — 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 ddlmZ ddlmZ 	 dd	lmZ d
Zerdd	lmZ  G d„ de«      Zy# e$ r 	 dd	lmZ dZn# e$ r eZdZY nw xY wY Œ4w xY w)zFContains a logger to push training logs to the Hub, using Tensorboard.é    )ÚPath)ÚTYPE_CHECKINGÚListÚOptionalÚUnioné   )ÚCommitScheduler)ÚEntryNotFoundError)Ú	ModelCard)Úexperimental)ÚSummaryWriterTFc                   óâ   ‡ — e Zd ZdZedˆ fd„«       Zdddddddddddœ
d	ed
ee   dee	e
f   dedee   dee   dee   dee   deeee   ef      deeee   ef      dee   fˆ fd„Zˆ fd„Zˆ xZS )ÚHFSummaryWritera£  
    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 make the repo private. If `None` (default), the repo will be public unless the organization's default is private. This value 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:
    ```diff
    # Taken from https://pytorch.org/docs/stable/tensorboard.html
    - from torch.utils.tensorboard import SummaryWriter
    + from huggingface_hub import HFSummaryWriter

    import numpy as np

    - writer = SummaryWriter()
    + writer = HFSummaryWriter(repo_id="username/my-trained-model")

    for n_iter in range(100):
        writer.add_scalar('Loss/train', np.random.random(), n_iter)
        writer.add_scalar('Loss/test', np.random.random(), n_iter)
        writer.add_scalar('Accuracy/train', np.random.random(), n_iter)
        writer.add_scalar('Accuracy/test', np.random.random(), n_iter)
    ```

    ```py
    >>> from huggingface_hub import HFSummaryWriter

    # Logs are automatically pushed every 15 minutes (5 by default) + when exiting the context manager
    >>> with HFSummaryWriter(repo_id="test_hf_logger", commit_every=15) as logger:
    ...     logger.add_scalar("a", 1)
    ...     logger.add_scalar("b", 2)
    ```
    c                 óD   •— t         st        d«      ‚t        ‰| �  | «      S )NzvYou must have `tensorboard` installed to use `HFSummaryWriter`. Please run `pip install --upgrade tensorboardX` first.)Úis_summary_writer_availableÚImportErrorÚsuperÚ__new__)ÚclsÚargsÚkwargsÚ	__class__s      €úa/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/huggingface_hub/_tensorboard_logger.pyr   zHFSummaryWriter.__new__y   s)   ø€ å*Üð(óð ô ‰w‰˜sÓ#Ð#ó    Né   FÚ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       
         ó¸  •— t        ‰| �  dd|i|¤Ž t        | j                  t        «      s/t        d| j                  › dt        | j                  «      › d�«      ‚|�|dk(  r t        | j                  «      j                  }n4|j                  d«      dz   t        | j                  «      j                  z   }t        | j                  |||||||	|
||¬«      | _        | j                  j                  | _        | j                  j                  | _        | j                  j                  | _        	 t!        j"                  | j                  | j                  ¬«      }|j&                  j)                  d	g «      }d
|vrH|j+                  d
«       ||j&                  d	<   |j-                  | j                  | j                  ¬«       y y # t$        $ r t!        d«      }Y Œw xY w)Nr   z%`self.logdir` must be a string. Got 'z
' of type ú.Ú ú/)Úfolder_pathr#   r'   r    ÚrevisionÚprivater&   Úallow_patternsÚignore_patternsÚeveryr   )Úrepo_id_or_pathr    Útagszhf-summary-writer)r'   r    © )r   Ú__init__Ú
isinstancer   ÚstrÚ
ValueErrorÚtyper   ÚnameÚstripr	   Ú	schedulerr'   r    r-   r!   r   Úloadr
   ÚdataÚgetÚappendÚpush_to_hub)Úselfr'   r   r   r   r    r!   r"   r#   r$   r%   r&   r   Úcardr3   r   s                  €r   r5   zHFSummaryWriter.__init__‚   sž  ø€ ô" 	‰ÑÑ1 Ð1¨&Ò1ô ˜$Ÿ+™+¤sÔ+ÜÐDÀTÇ[Á[ÀMÐQ[Ô\`Ðae×alÑalÓ\mÐ[nÐnoÐpÓqÐqð Ð <°2Ò#5Ü §¡Ó,×1Ñ1‰Là'×-Ñ-¨cÓ2°SÑ8¼4ÀÇÁÓ;L×;QÑ;QÑQˆLô )ØŸ™Ø%ØØØ"Ø ØØ.Ø0ØØ)ô
ˆŒð —~‘~×-Ñ-ˆŒØŸ™×1Ñ1ˆŒØ!Ÿ^™^×4Ñ4ˆÔð	!Ü—>‘>°$·,±,È$Ï.É.ÔYˆDð �y‰y�}‰}˜V RÓ(ˆØ dÑ*Ø�K‰KÐ+Ô,Ø $ˆD�I‰I�fÑØ×Ñ T§\¡\¸T¿^¹^ÐÕLð +øô "ò 	!Ü˜R“=ŠDð	!ús   Ä.+G ÇGÇGc                 ó|   •— t         ‰| �  |||«       | j                  j                  «       }|j	                  «        y)zLPush to hub in a non-blocking way when exiting the logger's context manager.N)r   Ú__exit__r<   ÚtriggerÚresult)rB   Úexc_typeÚexc_valÚexc_tbÚfuturer   s        €r   rE   zHFSummaryWriter.__exit__¾   s/   ø€ ä‰Ñ˜ 7¨FÔ3Ø—‘×'Ñ'Ó)ˆØ�‰�r   )Úreturnr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r7   r   r   ÚintÚfloatÚboolr   r5   rE   Ú__classcell__)r   s   @r   r   r   /   s  ø„ ñGðR ô$ó ð$ð !%Ø*+Ø$Ø#'Ø'+Ø'+Ø&3Ø?MØ@DØ#ò:Màð:Mð ˜‘ð	:Mð
 ˜C ˜JÑ'ð:Mð ð:Mð ˜C‘=ð:Mð   ‘}ð:Mð ˜t‘nð:Mð ˜s‘mð:Mð & e¨D°©I°s¨NÑ&;Ñ<ð:Mð ' u¨T°#©Y¸¨^Ñ'<Ñ=ð:Mð ˜‰}õ:M÷xð r   r   N)rP   Úpathlibr   Útypingr   r   r   r   Ú_commit_schedulerr	   Úerrorsr
   Úrepocardr   Úutilsr   ÚtensorboardXr   r   r   Útorch.utils.tensorboardÚobjectr   r4   r   r   ú<module>r^      s}   ðñ Må ß 7Ó 7å .Ý &Ý Ý ð,Ý*à"&Ðñ Ý*ôS�mõ Søð ò ,ð,Ý9à&+Ñ#øØò ,àˆØ&+Ò#ð,üð,ús5   ®A
 Á
A+ÁAÁA+Á	A%Á"A+Á$A%Á%A+Á*A+