
    HTh                          S r SSKJr  SSKJrJrJrJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr   SS	KJr  S
r\(       a  SS	KJr   " S S\5      rg! \ a     SS	KJr  Sr N*! \ a    \rSr  N7f = ff = f)zFContains a logger to push training logs to the Hub, using Tensorboard.    )Path)TYPE_CHECKINGListOptionalUnion   )CommitScheduler)EntryNotFoundError)	ModelCard)experimental)SummaryWriterTFc                      ^  \ rS rSrSr\SU 4S jj5       rSSSSSSSSSSS	.
S
\S\\   S\	\
\4   S\S\\   S\\   S\\   S\\   S\\	\\   \4      S\\	\\   \4      S\\   4U 4S jjjrU 4S jrSrU =r$ )HFSummaryWriter/   a  
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                 N   > [         (       d  [        S5      e[        TU ]  U 5      $ )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      [/var/www/auris/envauris/lib/python3.13/site-packages/huggingface_hub/_tensorboard_logger.pyr   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U ]  " SSU0UD6  [        U R                  [        5      (       d/  [        SU R                   S[        U R                  5       S35      eUb  US:X  a   [        U R                  5      R                  nO4UR                  S5      S-   [        U R                  5      R                  -   n[        U R                  UUUUUUU	U
UUS9U l        U R                  R                  U l        U R                  R                  U l        U R                  R                  U l         [         R"                  " U R                  U R                  S9nUR&                  R)                  S	/ 5      nS
U;  aD  UR+                  S
5        XR&                  S	'   UR-                  U R                  U R                  S9  g g ! [$         a    [!        S5      n N~f = f)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   cardr5   r   s                  r   r7   HFSummaryWriter.__init__   s   " 	11&1 $++s++DT[[MQ[\`aealal\m[nnopqq <2#5,11L'--c2S84;L;Q;QQL )%" .0)
 ~~--11!^^44	!>>$,,$..YD yy}}VR(d*KK+, $IIfT\\T^^L + " 	!R=D	!s   3)G GGc                 z   > [         TU ]  XU5        U R                  R                  5       nUR	                  5         g)zLPush to hub in a non-blocking way when exiting the logger's context manager.N)r   __exit__r>   triggerresult)rD   exc_typeexc_valexc_tbfuturer   s        r   rH   HFSummaryWriter.__exit__   s-    F3'')r   )r)   r#   r"   r>   )returnr   )__name__
__module____qualname____firstlineno____doc__r   r   r9   r   r   intfloatboolr   r7   rH   __static_attributes____classcell__)r   s   @r   r   r   /   s   GR $ $ !%*+$#''+'+&3?M@D#:M:M 	:M
 CJ':M :M C=:M  }:M tn:M sm:M &eDIsN&;<:M 'uT#Y^'<=:M }:M :Mx r   r   N)rU   pathlibr   typingr   r   r   r   _commit_schedulerr	   errorsr
   repocardr   utilsr   tensorboardXr   r   r   torch.utils.tensorboardobjectr   r6   r   r   <module>rd      s}    M  7 7 . &  ,*"& *Sm S  ,,9&+# ,&+#,,s)   A A2A  
A.*A2-A..A2