CoolFace
Apppublic

umair894/quickstart-trackio

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
commit_scheduler.cpython-312.pyc185 linesDownload Raw Back to __pycache__
12�1�h�<��"�ddlZddlZddlZddlZddlmZddlmZddlm	Z	m3Z4mZddlm
Z
ddlmZmZddlmZmZmZmZddlmZmZmZmZdd	lmZej:e�Zed5��Gd�d
��Z Gd�d�Z!Gd�de�Z"y)�N)�Future)�	dataclass)�SEEK_END�SEEK_SET�BytesIO)�Path)�Lock�Thread)�Callable�Dict�List�Union)�DEFAULT_IGNORE_PATTERNS�6CommitInfo�CommitOperationAdd�HfApi)�filter_repo_objectsT)�frozenc�:�eZdZUdZeed<eed<eed<eed<y)�
_FileToUploadzWTemporary dataclass to store info about files to upload. Not meant to be used directly.�7local_path�path_in_repo�8size_limit�
last_modifiedN)	�__name__�9__module__�__qualname__�__doc__r�__annotations__�str�int�float���C/usr/local/lib/python3.12/dist-packages/trackio/commit_scheduler.pyrrs��a������O��r$rc�&�eZdZdZdddddddddddd�dedeeefdeeefd	edzd10edzdedzde	dzd
edzde11eezdzde12eezdzde	dedzdegdfdzddfd�Z
dd�Zdd�Zdd�Zdd�Zdefd�Zdedzfd�Zdedzfd�Zy)�CommitSchedulera�
13    Scheduler to upload a local folder to the Hub at regular intervals (e.g. push to hub every 5 minutes).14 15    The recommended way to use the scheduler is to use it as a context manager. This ensures that the scheduler is16    properly stopped and the last commit is triggered when the script ends. The scheduler can also be stopped manually17    with the `stop` method. Checkout the [upload guide](https://huggingface.co/docs/huggingface_hub/guides/upload#scheduled-uploads)18    to learn more about how to use it.19 20    Args:21        repo_id (`str`):22            The id of the repo to commit to.23        folder_path (`str` or `Path`):24            Path to the local folder to upload regularly.25        every (`int` or `float`, *optional*):26            The number of minutes between each commit. Defaults to 5 minutes.27        path_in_repo (`str`, *optional*):28            Relative path of the directory in the repo, for example: `"checkpoints/"`. Defaults to the root folder29            of the repository.30        repo_type (`str`, *optional*):31            The type of the repo to commit to. Defaults to `model`.32        revision (`str`, *optional*):33            The revision of the repo to commit to. Defaults to `main`.34        private (`bool`, *optional*):35            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.36        token (`str`, *optional*):37            The token to use to commit to the repo. Defaults to the token saved on the machine.38        allow_patterns (`List[str]` or `str`, *optional*):39            If provided, only files matching at least one pattern are uploaded.40        ignore_patterns (`List[str]` or `str`, *optional*):41            If provided, files matching any of the patterns are not uploaded.42        squash_history (`bool`, *optional*):43            Whether to squash the history of the repo after each commit. Defaults to `False`. Squashing commits is44            useful to avoid degraded performances on the repo when it grows too large.45        hf_api (`HfApi`, *optional*):46            The [`HfApi`] client to use to commit to the Hub. Can be set with custom settings (user agent, token,...).47        on_before_commit (`Callable[[], None]`, *optional*):48            If specified, a function that will be called before the CommitScheduler lists files to create a commit.49 50    Example:51    ```py52    >>> from pathlib import Path53    >>> from huggingface_hub import CommitScheduler54 55    # Scheduler uploads every 10 minutes56    >>> csv_path = Path("watched_folder/data.csv")57    >>> CommitScheduler(repo_id="test_scheduler", repo_type="dataset", folder_path=csv_path.parent, every=10)58 59    >>> with csv_path.open("a") as f:60    ...     f.write("first line")61 62    # Some time later (...)63    >>> with csv_path.open("a") as f:64    ...     f.write("second line")65    ```66 67    Example using a context manager:68    ```py69    >>> from pathlib import Path70    >>> from huggingface_hub import CommitScheduler71 72    >>> with CommitScheduler(repo_id="test_scheduler", repo_type="dataset", folder_path="watched_folder", every=10) as scheduler:73    ...     csv_path = Path("watched_folder/data.csv")74    ...     with csv_path.open("a") as f:75    ...         f.write("first line")76    ...     (...)77    ...     with csv_path.open("a") as f:78    ...         f.write("second line")79 80    # Scheduler is now stopped and last commit have been triggered81    ```82    �NF)�everyr�	repo_type�revision�private�token�allow_patterns�ignore_patterns�squash_history�hf_api�on_before_commit�repo_id�folder_pathr)rr*r+r,r-r.r/r0r1r2�returnc
	���|xst|��|_|
|_t|�j	�j�|_|xsd|_|	|_|83�g}84nt|85t�r|86g}87|88tz|_|jj�rtd|j�d���|jjdd��|jj!|||d��}|j"|_||_||_||_i|_d|_|dkDstd	|�d���t/�|_||_||_t6j9d89|j�d|j"�d|j2�d
��t;|j<d��|_|j>jA�tCjD|jF�d|_$y)N)r-�z0'folder_path' must be a directory, not a file: '�'.T)�parents�exist_ok)r3r,r*r:rz)'every' must be a positive integer, not 'zScheduled job to push 'z' to 'z' every z	 minutes.)�target�daemonF)%r�apir2r�90expanduser�resolver4rr.�91isinstancer rr/�is_file�92ValueError�mkdir�create_repor3r*r+r-�
last_uploaded�last_push_timer	�lockr)r0�logger�infor93�_run_scheduler�_scheduler_thread�start�atexit�register�_push_to_hub�_CommitScheduler__stopped)�selfr3r4r)rr*r+r,r-r.r/r0r1r2�repo_urls               r%�__init__zCommitScheduler.__init__ls���"�/�U��/��� 0��� ��,�7�7�9�A�A�C���(�.�B���,����"� �O�
���
-�.�/�O�.�1H�H������#�#�%��B�4�CS�CS�BT�TV�W��
�	
�����t�d��;��8�8�'�'��W�	�D�(�94�� �'�'���"��� ��
���95�02���,0����q�y��H���r�R�S�S��F��	���96�,������%�d�&6�&6�%7�v�d�l�l�^�8�TX�T^�T^�S_�_h�i�	97�"(�t�/B�/B�4�!P������$�$�&�����)�)�*���r$c��d|_y)ziStop the scheduler.98 99        A stopped scheduler cannot be restarted. Mostly for tests purposes.100        TN)rP�rQs r%�stopzCommitScheduler.stop�s��101��r$c��|S�Nr#rUs r%�	__enter__zCommitScheduler.__enter__�s���r$c�`�|j�j�|j�yrX)�trigger�resultrV)rQ�exc_type�	exc_value�	tracebacks    r%�__exit__zCommitScheduler.__exit__�s ����������	�	��r$c��	|j�|_tj|jdz�|j102ry�E)z7Dumb thread waiting between each scheduled push to Hub.�<N)r[�last_future�time�sleepr)rPrUs r%rJzCommitScheduler._run_scheduler�s6���#�|�|�~�D���J�J�t�z�z�B��'��~�~��	r$c�L�|jj|j�S)z�Trigger a `push_to_hub` and return a future.103 104        This method is automatically called every `every` minutes. You can also call it manually to trigger a commit105        immediately, without waiting for the next scheduled commit.106        )r=�
run_as_futurerOrUs r%r[zCommitScheduler.trigger�s���x�x�%�%�d�&7�&7�8�8r$c�z�|jrytjd�	|j�}|jrQtjd�|j107j
|j|j|j��|S#t$r}tjd|����d}~wwxYw)Nz((Background) scheduled commit triggered.z$(Background) squashing repo history.)r3r*�branchzError while pushing to Hub: )rPrHrI�push_to_hubr0r=�super_squash_historyr3r*r+�	Exception�error)rQ�value�es   r%rOzCommitScheduler._push_to_hub�s����>�>�����>�?�	��$�$�&�E��"�"����B�C����-�-� �L�L�D�N�N�4�=�=�.���L���	��L�L�.�q�c�2�
�
��		�s�A.B�	B:�B5�5B:c108�X�|j5|j�|j�tjd�t	|j109j
d��D�cic]<}|j�r*|j|j110�j�|��>}}|jr|jjd��d�nd}g}t|j�|j|j��D]�}||}|j!�}|j"j%|��|j"||j&k7s�P|j)t+|||z|j,|j&�����	ddd�t/�dk(rtjd	�ytjd111�|D�cgc]#}t1|j2|j����%}	}tjd�|j4j7|j8|j:|	d
|j<��}112|D]%}|j>|j"|j2<�'tAj@�|_!|113Scc}w#1swY��xYwcc}w)a�114        Push folder to the Hub and return the commit info.115 116        <Tip warning={true}>117 118        This method is not meant to be called directly. It is run in the background by the scheduler, respecting a119        queue mechanism to avoid concurrent commits. Making a direct call to the method might lead to concurrency120        issues.121 122        </Tip>123 124        The default behavior of `push_to_hub` is to assume an append-only folder. It lists all files in the folder and125        uploads only changed files. If no changes are found, the method returns without committing anything. If you want126        to change this behavior, you can inherit from [`CommitScheduler`] and override this method. This can be useful127        for example to compress data together in a single file before committing. For more details and examples, check128        out our [integration guide](https://huggingface.co/docs/huggingface_hub/main/en/guides/upload#scheduled-uploads).129        Nz-Listing files to upload for scheduled commit.z**/*�/r7)r.r/)rrrrrz4Dropping schedule commit: no changed file to upload.z9Removing unchanged files since previous scheduled commit.)�path_or_fileobjrz%Uploading files for scheduled commit.zScheduled Commit)r3r*�130operations�commit_messager+)"rGr2rH�debug�sortedr4�globrA�relative_to�as_posixr�stripr�keysr.r/�statrE�get�st_mtime�appendr�st_size�lenrrr=�
create_commitr3r*r+rrdrF)rQ�path�relpath_to_abspath�prefix�files_to_upload�relpathrr|�file_to_upload�add_operations�commit_info�files            r%rjzCommitScheduler.push_to_hub�ss��&�Y�Y��$�$�0��%�%�'��L�L�H�I�131#��$�$�)�)�&�1��"��D��<�<�>�	� � ��!1�!1�2�;�;�=�t�C��
�"�<@�;L�;L��)�)�/�/��4�5�Q�7�RT�F�46�O�.�"�'�'�)�#�2�2� $� 4� 4���1320��8�133�!���(���&�&�*�*�:�6�>��)�)�*�5����F�#�*�*�%�'1�)/�'�)9�'+�|�|�*.�-�-�	���%�N���1�$��L�L�O�P��	���P�Q�#2�134�#2��

�!/� 9� 9�+�8�8�	
�#2�	�135�	���<�=��h�h�,�,��L�L��n�n�%�-��]�]�-�136��$�D�26�2D�2D�D���t���/�$�#�i�i�k������"��Y��Z137s,�AJ�%AJ�&B/J�7J�(J'�J�J$�r5N)r5r')rrrrr rrr!r"�bool�listrrrSrVrYr`rJrr[rrOrjr#r$r%r'r'#sT��F�Z$%�#'� $�#�#� �15�26�$�#�6:�>��>��3��9�%�	>�138�S�%�Z� �>��D�j�
>���:�>���*�>����>��T�z�>��S�	�C��$�.�>��c��S��4�/�>��>����>�#�2�t�8�,�t�3�>� 139�!>�@����9��9��j�4�/��&Y�Z�$�.�Yr$r'c���eZdZdZdeeefdeddfd�Zd�fd�Z	defd�Z140defd	�Zd141ef�fd�Zdefd�Z
efd
ededefd�Zddedzdefd�Z�xZS)�
PartialFileIOaA file-like object that reads only the first part of a file.142 143    Useful to upload a file to the Hub when the user might still be appending data to it. Only the first part of the144    file is uploaded (i.e. the part that was available when the filesystem was first scanned).145 146    In practice, only used internally by the CommitScheduler to regularly push a folder to the Hub with minimal147    disturbance for the user. The object is passed to `CommitOperationAdd`.148 149    Only supports `read`, `tell` and `seek` methods.150 151    Args:152        file_path (`str` or `Path`):153            Path to the file to read.154        size_limit (`int`):155            The maximum number of bytes to read from the file. If the file is larger than this, only the first part156            will be read (and uploaded).157    �	file_pathrr5Nc���t|�|_|jjd�|_t	|tj|jj��j�|_	y)N�rb)158r�159_file_path�open�_file�min�os�fstat�filenor��_size_limit)rQr�rs   r%rSzPartialFileIO.__init__NsN���y�/����_�_�)�)�$�/��160��z�2�8�8�D�J�J�4E�4E�4G�+H�+P�+P�Q��r$c�T��|jj�t�|�
�SrX)r��close�super�__del__)rQ�	__class__s �r%r�zPartialFileIO.__del__Ss ����161162�����w�� � r$c�<�d|j�d|j�d�S)Nz<PartialFileIO file_path=z size_limit=�>)r�r�rUs r%�__repr__zPartialFileIO.__repr__Ws&��'����'8��T�EU�EU�DV�VW�X�	163r$c��|jSrX)r�rUs r%�__len__zPartialFileIO.__len__\s�����r$�namec�j��|jd�s|dvrt�|�	|�Std|�d���)N�_)�read�tell�seekz PartialFileIO does not support 'r8)�164startswithr��__getattribute__�NotImplementedError)rQr�r�s  �r%r�zPartialFileIO.__getattribute___sB����?�?�3��4�,165�$166167�7�+�D�1�1�!�$D�T�F�"�"M�N�Nr$c�6�|jj�S)z!Return the current file position.)r�r�rUs r%r�zPartialFileIO.tellhs���z�z��� � r$�_PartialFileIO__offset�_PartialFileIO__whencec���|tk(rt|�|z}t}|jj	||�}||j168kDr%|jj	|j169�S|S)z�Change the stream position to the given offset.170 171        Behavior is the same as a regular file, except that the position is capped to the size limit.172        )rr�rr�r�r�)rQr�r��poss    r%r�zPartialFileIO.seekls_��173�x���4�y�8�+�H��H��j�j�o�o�h��1����!�!�!��:�:�?�?�4�#3�#3�4�4��174r$�_PartialFileIO__sizec���|jj�}|�|dkr|j|z175}nt||j|z176�}|jj	|�S)z�Read at most `__size` bytes from the file.177 178        Behavior is the same as a regular file, except that it is capped to the size limit.179        r)r�r�r�r�r�)rQr��current�truncated_sizes    r%r�zPartialFileIO.read{s[��180�*�*�/�/�#���>�V�a�Z�!�-�-��7�N�!���)9�)9�G�)C�D�N��z�z���~�.�.r$r�)�����)rrrrrr rr!rSr�r�r�r�r�rr��bytesr��
__classcell__)r�s@r%r�r�;s�����$R�%��T�	�"2�R��R��R�181!�182�#�183184 �� �O�S�O�!�c�!�3;�
�S�
�C�
�s�
�/�3��:�/�u�/r$r�)#rM�loggingr�rd�concurrent.futuresr�dataclassesr�iorrr�pathlibr�	threadingr	r185�typingrrr
r�huggingface_hub.hf_apirrrr�huggingface_hub.utilsr�	getLoggerrrHrr'r�r#r$r%�<module>r�s�����	��%�!�*�*��"�.�.���6�	��	�	�8�	$���$������U�U�pL/�G�L/r$