Ë
    -Œ:jÌ@  ã                   óÐ  — d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZ ddlmZ ddlmZ d	d
lmZmZmZmZmZmZ d	dlmZ d	dlmZmZ e
rd	dlmZ  ej>                  e «      Z! ejD                  d«      Z#dZ$dddœZ% G d„ d«      Z&e	 	 	 	 d8dee&   de'dz  de'de'de'dz  de'dz  de(e'e'f   dz  de)e'   dz  de*e)e(   e)e(   e'dz  f   fd„«       Z+ G d„ de«      Z, G d „ d!e«      Z-	 	 	 d9d"d#d$e(de'dz  de(e'e'f   dz  de'dz  ddfd%„Z.d&e(fd'„Z/d(e(fd)„Z0d*e(fd+„Z1d"d#d,e'ddfd-„Z2d"d#d.e(d/e3d,e'ddf
d0„Z4d.e(d1e&d/e3de)e'   fd2„Z5d3e)e(   d4e'de-fd5„Z6d"d#d6e)e'   d/e3de)e(   fd7„Z7y):z.Git LFS related type definitions and utilitiesé    N)ÚIterable)Úceil)Úgetsize)ÚTYPE_CHECKINGÚBinaryIOÚ	TypedDict)Úunquote)Ú	constantsé   )Úbuild_hf_headersÚfix_hf_endpoint_in_urlÚhf_raise_for_statusÚhttp_backoffÚloggingÚvalidate_hf_hub_args)ÚSliceFileObj)Úsha256Úsha_fileobj)ÚCommitOperationAddz^[0-9a-f]{40}$zlfs-multipart-uploadzapplication/vnd.git-lfs+json)ÚAcceptzContent-Typec            
       óâ   — e Zd ZdZ	 	 ddedededz  dedz  fd„Zedefd	„«       Z	e	j                  d
eddfd„«       Z	edefd„«       Zdefd„Zedefd„«       Zedefd„«       Zedefd„«       Zy)Ú
UploadInfoa¶  
    Data structure holding required information to determine whether a blob
    should be uploaded to the hub using the LFS protocol or the regular protocol.

    The SHA256 of the blob is computed lazily: creating an `UploadInfo` from a local path only reads
    the first 512 bytes of the file. The full file is read (and hashed) only if `sha256` is accessed
    before it has been set. When a file is uploaded through the Xet protocol, the SHA256 is computed
    during upload (single read pass) and set afterwards.

    Args:
        size (`int`):
            Size in bytes of the blob
        sample (`bytes`):
            First 512 bytes of the blob
        sha256 (`bytes`, *optional*):
            SHA256 hash of the blob, if already known. Otherwise computed lazily from `source_path`.
        source_path (`str`, *optional*):
            Path to the local file the blob comes from. Required to lazily compute `sha256` if not provided.
    NÚsizeÚsampler   Úsource_pathc                 óZ   — |€|€t        d«      ‚|| _        || _        || _        || _        y )Nz2Either `sha256` or `source_path` must be provided.)Ú
ValueErrorr   r   Ú_sha256Ú_source_path)Úselfr   r   r   r   s        úU/var/www/html/tokenscope/api/venv/lib/python3.12/site-packages/huggingface_hub/lfs.pyÚ__init__zUploadInfo.__init__J   s7   € ð ˆ>˜kÐ1ÜÐQÓRÐRØˆŒ	ØˆŒØˆŒØ'ˆÕó    Úreturnc                 óò   — | j                   €I| j                  €J ‚t        | j                  d«      5 }t        |«      | _         ddd«       | j                   S | j                   S # 1 sw Y   | j                   S xY w)zZSHA256 of the blob. If not set yet, reads the whole file from `source_path` to compute it.NÚrb)r   r   Úopenr   )r    Úfiles     r!   r   zUploadInfo.sha256X   sk   € ð �<‰<ÐØ×$Ñ$Ð0Ð0Ð0Ü�d×'Ñ'¨Ó.ð 1°$Ü*¨4Ó0�”÷1à�|‰|Ðˆt�|‰|Ð÷1à�|‰|Ðús   ±A"Á"A6Úvaluec                 ó   — || _         y )N©r   )r    r)   s     r!   r   zUploadInfo.sha256a   s	   € àˆ�r#   c                 ó   — | j                   duS )zVWhether the SHA256 is already known (accessing `sha256` will not trigger a file read).Nr+   )r    s    r!   Ú	is_hashedzUploadInfo.is_hashede   s   € ð �|‰| 4Ð'Ð'r#   c                 óx   — | j                   �| j                   j                  «       nd}d| j                  › d|› d�S )Nz<not computed>zUploadInfo(size=z	, sha256=ú))r   Úhexr   )r    Úshas     r!   Ú__repr__zUploadInfo.__repr__j   s9   € Ø$(§L¡LÐ$<ˆd�l‰l×ÑÔ ÐBRˆØ! $§)¡) ¨I°c°U¸!Ð<Ð<r#   Úpathc                 ó˜   — t        |«      }t        |d«      5 }|j                  d«      d d }d d d «        | ||¬«      S # 1 sw Y   ŒxY w)Nr&   é   )r   r   r   )r   r'   Úpeek)Úclsr3   r   r(   r   s        r!   Ú	from_pathzUploadInfo.from_pathn   sO   € ä�t‹}ˆÜ�$˜Óð 	* Ø—Y‘Y˜s“^ D SÐ)ˆF÷	*á˜ V¸Ô>Ð>÷	*ð 	*ús   ˜A Á A	Údatac                 ób   — t        |«      j                  «       } | t        |«      |d d |¬«      S )Nr5   )r   r   r   )r   ÚdigestÚlen)r7   r9   r1   s      r!   Ú
from_byteszUploadInfo.from_bytesu   s-   € ä�T‹l×!Ñ!Ó#ˆÙœ˜D›	¨$¨t°¨*¸SÔAÐAr#   Úfileobjc                 óð   — |j                  d«      }|j                  dt        j                  «       t	        |«      }|j                  «       }|j                  dt        j                  «        | |||¬«      S )Nr5   r   )r   r   r   )ÚreadÚseekÚioÚSEEK_SETr   Útell)r7   r>   r   r1   r   s        r!   Úfrom_fileobjzUploadInfo.from_fileobjz   sX   € à—‘˜cÓ"ˆØ�‰�QœŸ™Ô$Ü˜'Ó"ˆØ�|‰|‹~ˆØ�‰�QœŸ™Ô$Ù˜ S°Ô8Ð8r#   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚintÚbytesÚstrr"   Úpropertyr   ÚsetterÚboolr-   r2   Úclassmethodr8   r=   r   rE   © r#   r!   r   r   5   sþ   „ ñð0  $Ø"&ñ(àð(ð ð(ð ˜‘ð	(ð
 ˜4‘Zó(ð ð˜ò ó ðð ‡]�]ð˜Eð  dò ó ðð ð(˜4ò (ó ð(ð=˜#ó =ð ð?˜Sò ?ó ð?ð ðB˜eò Bó ðBð ð9 8ò 9ó ñ9r#   r   Úupload_infosÚtokenÚ	repo_typeÚrepo_idÚrevisionÚendpointÚheadersÚ	transfersr$   c                 óô  — |�|nt         j                  }d}|t         j                  v rt         j                  |   }|› d|› |› d�}	d|�|nddg| D �
cg c])  }
|
j                  j	                  «       |
j
                  dœ‘Œ+ c}
d	d
œ}|�dt        |«      i|d<   i t        ¥t        |¬«      ¥|xs i ¥}t        d|	||¬«      }t        |«       |j                  «       }|j                  dd«      }t        |t        «      st        d«      ‚|j                  d«      }t        |t         «      r|nd}|D �cg c]  }d|vsŒt#        |«      ‘Œ c}|D �cg c]  }d|v sŒt%        |«      ‘Œ c}|fS c c}
w c c}w c c}w )aå  
    Requests the LFS batch endpoint to retrieve upload instructions

    Learn more: https://github.com/git-lfs/git-lfs/blob/main/docs/api/batch.md

    Args:
        upload_infos (`Iterable` of `UploadInfo`):
            `UploadInfo` for the files that are being uploaded, typically obtained
            from `CommitOperationAdd.upload_info`
        token (`str` or `None`):
            An authentication token (see https://huggingface.co/settings/token).
            Pass `None` to fall back to the local cached token (or no token if unauthenticated).
        repo_type (`str`):
            Type of the repo to upload to: `"model"`, `"dataset"` or `"space"`.
        repo_id (`str`):
            A namespace (user or an organization) and a repo name separated
            by a `/`.
        revision (`str`, *optional*):
            The git revision to upload to.
        endpoint (`str`, *optional*):
            The Hub endpoint to send the request to. Defaults to the value of `HF_ENDPOINT`.
        headers (`dict`, *optional*):
            Additional headers to include in the request
        transfers (`list`, *optional*):
            List of transfer methods to use. Defaults to ["basic", "multipart"].

    Returns:
        `LfsBatchInfo`: 3-tuple:
            - First element is the list of upload instructions from the server
            - Second element is a list of errors, if any
            - Third element is the chosen transfer adapter if provided by the server (e.g. "basic", "multipart", "xet")

    Raises:
        [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
            If an argument is invalid or the server response is malformed.
        [`HfHubHTTPError`]
            If the server returned an error.
    NÚ ú/z.git/info/lfs/objects/batchÚuploadÚbasicÚ	multipart©Úoidr   r   )Ú	operationrY   ÚobjectsÚ	hash_algoÚnameÚref)rS   ÚPOST©rX   Újsonrc   zMalformed response from serverÚtransferÚerror)r
   ÚENDPOINTÚREPO_TYPES_URL_PREFIXESr   r0   r   r	   ÚLFS_HEADERSr   r   r   ri   ÚgetÚ
isinstanceÚlistr   rL   Ú_validate_batch_actionsÚ_validate_batch_error)rR   rS   rT   rU   rV   rW   rX   rY   Ú
url_prefixÚ	batch_urlr]   ÚpayloadÚrespÚ
batch_inforc   Úchosen_transferÚobjs                    r!   Úpost_lfs_batch_infor{   „   s¢  € ðb $Ð/‰x´Y×5GÑ5G€HØ€JØ”I×5Ñ5Ñ5Ü×6Ñ6°yÑAˆ
Ø�*˜A˜j˜\¨'¨Ð2MÐN€IàØ"+Ð"7‘Y¸gÀ{Ð=Sð 'ö
ð
 ð —}‘}×(Ñ(Ó*ØŸ™óò
ð ñ€Gð ÐØ ¤'¨(Ó"3Ð4ˆ�‰ðÜ
ðä
 Ô
'ðð Š=�bð€Gô
 ˜ 	°7ÀÔI€DÜ˜ÔØ—‘“€Jà�n‰n˜Y¨Ó-€GÜ�gœtÔ$ÜÐ9Ó:Ð:à —n‘n ZÓ0€OÜ)3°OÄSÔ)I‘oÈt€Oð 29ÖO¨#¸GÈ3Ò<NÔ	  Õ	%ÒOØ/6ÖI¨¸'ÀSº.Ô	˜sÕ	#ÒIØðð ùò9
ùò: 	PùÚIs   Á.E+Ä2	E0Ä<E0Å	E5ÅE5c                   ó"   — e Zd ZU eed<   eed<   y)ÚPayloadPartTÚ
partNumberÚetagN)rF   rG   rH   rJ   Ú__annotations__rL   rQ   r#   r!   r}   r}   à   s   … ØƒOØ
„Ir#   r}   c                   ó,   — e Zd ZU dZeed<   ee   ed<   y)ÚCompletionPayloadTz?Payload that will be sent to the Hub when uploading multi-part.ra   ÚpartsN)rF   rG   rH   rI   rL   r€   rq   r}   rQ   r#   r!   r‚   r‚   å   s   … ÙIà	ƒHØ�ÑÔr#   r‚   rb   r   Úlfs_batch_actionc                 ó(  — t        |«       |j                  d«      }|€$t        j                  d| j                  › d�«       y|d   d   }t        |«       |d   j                  d«      }|�t        |«       |j                  di «      }|j                  d«      }	t        |d	   |¬
«      }
|	�	 t        |	«      }	t        | ||	|
¬«       nt        | |
¬«       |�xt        |«       t        |d	   |«      }t        d|t        ||¬«      | j                  j                  j!                  «       | j                  j"                  dœ¬«      }t%        |«       t        j                  | j                  › d�«       y# t        t        f$ r t        d|	› d�«      ‚w xY w)a½  
    Handles uploading a given object to the Hub with the LFS protocol.

    Can be a No-op if the content of the file is already present on the hub large file storage.

    Args:
        operation (`CommitOperationAdd`):
            The add operation triggering this upload.
        lfs_batch_action (`dict`):
            Upload instructions from the LFS batch endpoint for this object. See [`~utils.lfs.post_lfs_batch_info`] for
            more details.
        token (`str`, *optional*):
            An authentication token (see https://huggingface.co/settings/token). Used to call the
            optional LFS verify step at the end of the upload. If `None`, falls back to the local
            cached token.
        headers (`dict`, *optional*):
            Headers to include in the request, including authentication and user agent headers.
        endpoint (`str`, *optional*):
            The Hub endpoint to send the request to. Defaults to the value of `HF_ENDPOINT`.

    Raises:
        [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError)
            If `lfs_batch_action` is improperly formatted
        [`HfHubHTTPError`]
            If the upload resulted in an error
    ÚactionsNzContent of file z. is already present upstream - skipping uploadr]   ÚverifyÚheaderÚ
chunk_sizeÚhref)rW   zTMalformed response from LFS batch endpoint: `chunk_size` should be an integer. Got 'z'.)rb   rˆ   r‰   Ú
upload_url)rb   r‹   rg   )rS   rX   r`   rh   z: Upload successful)rr   ro   ÚloggerÚdebugÚpath_in_repoÚ_validate_lfs_actionr   rJ   r   Ú	TypeErrorÚ_upload_multi_partÚ_upload_single_partr   r   Úupload_infor   r0   r   r   )rb   r„   rS   rX   rW   r†   Úupload_actionÚverify_actionrˆ   r‰   r‹   Ú
verify_urlÚverify_resps                r!   Ú
lfs_uploadr˜   ì   s�  € ôD Ð,Ô-Ø×"Ñ" 9Ó-€GØ€ä�‰Ð'¨	×(>Ñ(>Ð'?Ð?mÐnÔoØð % YÑ/°Ñ9€MÜ˜Ô'Ø$ YÑ/×3Ñ3°HÓ=€MØÐ Ü˜]Ô+ð ×Ñ˜x¨Ó,€FØ—‘˜LÓ)€JÜ'¨°fÑ(=ÈÔQ€JØÐð	Ü˜Z›ˆJô
 	 Y°vÈ*ÐakÖlä i¸JÕGð Ð Ü˜]Ô+Ü+¨M¸&Ñ,AÀ8ÓLˆ
Ü"ØØÜ$¨5¸'ÔBØ"×.Ñ.×5Ñ5×9Ñ9Ó;ÀY×EZÑEZ×E_ÑE_Ñ`ô	
ˆô 	˜KÔ(Ü
‡L�L�I×*Ñ*Ð+Ð+>Ð?Õ@øô' œIÐ&ò 	ÜØfÐgqÐfrÐrtÐuóð ð	ús   Â-E2 Å2FÚ
lfs_actionc                 óº   — t        | j                  d«      t        «      r0| j                  d«      �*t        | j                  d«      t        «      st	        d«      ‚| S )ú.validates response from the LFS batch endpointrŠ   rˆ   z"lfs_action is improperly formatted)rp   ro   rL   Údictr   )r™   s    r!   r�   r�   9  sK   € ô 	�:—>‘> &Ó)¬3Ô/Ø�^‰^˜HÓ%Ð-´¸J¿N¹NÈ8Ó<TÔVZÔ1[äÐ=Ó>Ð>ØÐr#   Úlfs_batch_actionsc                 óP  — t        | j                  d«      t        «      rt        | j                  d«      t        «      st	        d«      ‚| j                  di «      j                  d«      }| j                  di «      j                  d«      }|�t        |«       |�t        |«       | S )r›   ra   r   z)lfs_batch_actions is improperly formattedr†   r]   r‡   )rp   ro   rL   rJ   r   r�   )r�   r”   r•   s      r!   rr   rr   C  s—   € äÐ(×,Ñ,¨UÓ3´SÔ9¼jÐIZ×I^ÑI^Ð_eÓIfÔhkÔ>lÜÐDÓEÐEà%×)Ñ)¨)°RÓ8×<Ñ<¸XÓF€MØ%×)Ñ)¨)°RÓ8×<Ñ<¸XÓF€MØÐ Ü˜]Ô+ØÐ Ü˜]Ô+ØÐr#   Úlfs_batch_errorc                 ól  — t        | j                  d«      t        «      rt        | j                  d«      t        «      st	        d«      ‚| j                  d«      }t        |t
        «      r>t        |j                  d«      t        «      rt        |j                  d«      t        «      st	        d«      ‚| S )r›   ra   r   z'lfs_batch_error is improperly formattedrk   ÚmessageÚcode)rp   ro   rL   rJ   r   rœ   )rŸ   Ú
error_infos     r!   rs   rs   Q  s‹   € ä�×*Ñ*¨5Ó1´3Ô7¼JÀ×GZÑGZÐ[aÓGbÔdgÔ<hÜÐBÓCÐCØ ×$Ñ$ WÓ-€Jä�:œtÔ$Ü�z—~‘~ iÓ0´#Ô6Ü�z—~‘~ fÓ-¬sÔ3äÐBÓCÐCØÐr#   r‹   c                 ó„   — | j                  d¬«      5 }t        d||¬«      }t        |«       ddd«       y# 1 sw Y   yxY w)aK  
    Uploads `fileobj` as a single PUT HTTP request (basic LFS transfer protocol)

    Args:
        upload_url (`str`):
            The URL to PUT the file to.
        fileobj:
            The file-like object holding the data to upload.

    Raises:
        [`HfHubHTTPError`]
            If the upload resulted in an error.
    T©Ú	with_tqdmÚPUT©r9   N)Úas_filer   r   )rb   r‹   r>   Úresponses       r!   r’   r’   _  sB   € ð 
×	Ñ	 TÐ	Ó	*ð &¨gä  z¸Ô@ˆÜ˜HÔ%÷&÷ &ñ &ús   “6¶?rˆ   r‰   c           	      óä   — t        || j                  |¬«      }t        | ||¬«      }t        d|t	        || j                  j
                  j                  «       «      t        ¬«      }t        |«       y)z@
    Uploads file using HF multipart LFS transfer protocol.
    )rˆ   r“   r‰   )rb   Úsorted_parts_urlsr‰   rg   )ri   rX   N)	Ú_get_sorted_parts_urlsr“   Ú_upload_parts_iterativelyr   Ú_get_completion_payloadr   r0   rn   r   )rb   rˆ   r‰   r‹   r¬   Úresponse_headersÚcompletion_ress          r!   r‘   r‘   s  sn   € ô
 /°fÈ)×J_ÑJ_ÐlvÔwÐô 1ØÐ/@ÈZôÐô "ØØÜ$Ð%5°y×7LÑ7L×7SÑ7S×7WÑ7WÓ7YÓZÜô	€Nô ˜Õ'r#   r“   c           
      óV  — t        | j                  «       D ��cg c]1  \  }}|j                  «       rt        |«      dkD  rt	        |d«      |f‘Œ3 c}}d„ ¬«      D ��cg c]  \  }}|‘Œ	 }}}t        |«      }|t        |j                  |z  «      k7  rt        d«      ‚|S c c}}w c c}}w )Nr   é
   c                 ó   — | d   S )Nr   rQ   )Úts    r!   ú<lambda>z(_get_sorted_parts_urls.<locals>.<lambda>“  s
   € ˜!˜A™$€ r#   )Úkeyz0Invalid server response to upload large LFS file)ÚsortedÚitemsÚisdigitr<   rJ   r   r   r   )rˆ   r“   r‰   Úpart_numr‹   Ú_Úsorted_part_upload_urlsÚ	num_partss           r!   r­   r­   Š  s¶   € ô $ð -3¯L©L«N÷á(�H˜jØ×#Ñ#Ô%¬#¨h«-¸!Ò*;ô �X˜rÓ" JÒ/óñ
 ô
÷
áˆAˆzò 	ð
Ðñ 
ô Ð+Ó,€IØ”D˜×)Ñ)¨JÑ6Ó7Ò7ÜÐKÓLÐLØ"Ð"ùóùó
s   ™6B
ÁB%r°   ra   c                 ó¼   — g }t        | «      D ]H  \  }}|j                  d«      }|�|dk(  rt        d|› d|dz   › �«      ‚|j                  |dz   |dœ«       ŒJ ||dœS )Nr   r[   zInvalid etag (`z`) returned for part r   )r~   r   )ra   rƒ   )Ú	enumeratero   r   Úappend)r°   ra   rƒ   Úpart_numberrˆ   r   s         r!   r¯   r¯   œ  s�   € Ø "€EÜ(Ð)9Ó:ò 	
Ñˆ�VØ�z‰z˜&Ó!ˆØˆ<˜4 2š:Ü˜¨t¨fÐ4IÈ+ÐXYÉ/ÐIZÐ[Ó\Ð\Ø�‰à)¨A™oØñõ	
ð		
ð  Ñ'Ð'r#   r¬   c                 ó6  — g }| j                  d¬«      5 }t        |«      D ]S  \  }}t        |||z  |¬«      5 }t        d||¬«      }t	        |«       |j                  |j                  «       d d d «       ŒU 	 d d d «       |S # 1 sw Y   ŒjxY w# 1 sw Y   |S xY w)NTr¥   )Ú	seek_fromÚ
read_limitr§   r¨   )r©   rÀ   r   r   r   rÁ   rX   )	rb   r¬   r‰   rX   r>   Úpart_idxÚpart_upload_urlÚfileobj_sliceÚpart_upload_ress	            r!   r®   r®   «  s±   € ð €GØ	×	Ñ	 TÐ	Ó	*ð 
8¨gÜ)2Ð3DÓ)Eò 		8Ñ%ˆH�oÜØØ$ xÑ/Ø%ôð 8ð ä".¨u°oÈMÔ"Z�Ü# OÔ4Ø—‘˜×6Ñ6Ô7÷8ð 8ñ		8÷
8ð €N÷8ð 8ú÷
8ð €Nús"   •#B¸5BÁ-
BÂBÂBÂB)NNNN)NNN)8rI   rB   ÚreÚcollections.abcr   Úmathr   Úos.pathr   Útypingr   r   r   Úurllib.parser	   Úhuggingface_hubr
   Úutilsr   r   r   r   r   r   Ú
utils._lfsr   Ú	utils.shar   r   Ú_commit_apir   Ú
get_loggerrF   rŒ   ÚcompileÚ	OID_REGEXÚLFS_MULTIPART_UPLOAD_COMMANDrn   r   rL   rœ   rq   Útupler{   r}   r‚   r˜   r�   rr   rs   r’   rJ   r‘   r­   r¯   r®   rQ   r#   r!   ú<module>rÚ      sª  ðñ 5ã 	Û 	Ý $Ý Ý ß 5Ñ 5Ý  å %÷÷ õ %ß *ñ Ý/à	ˆ×	Ñ	˜HÓ	%€àˆB�J‰JÐ(Ó)€	à5Ð ð -Ø2ñ€÷L9ñ L9ð^ ð  ØØ%)Ø"&ñXØ˜:Ñ&ðXà�‰:ðXð ðXð ð	Xð
 �D‰jðXð �D‰jðXð �#�s�(‰^˜dÑ"ðXð �C‰y˜4ÑðXð ˆ4�‰:�t˜D‘z 3¨¡:Ð-Ñ.òXó ðXôv�9ô ô
˜ô ð Ø%)ØñJAØ#ðJAàðJAð �‰:ðJAð �#�s�(‰^˜dÑ"ð	JAð
 �D‰jðJAð 
óJAðZ Tó ð¨tó ð¨4ó ð&Ð#7ð &ÀSð &ÈTó &ð((Ð"6ð (Àð (ÐRUð (Ðcfð (Ðkoó (ð.# 4ð #°jð #Ècð #ÐVZÐ[^ÑV_ó #ð$(¨d°4©jð (¸sð (ÐGYó (ðØ#ðØ8<¸S¹	ðØORðà	ˆ$�Zôr#   