+
    &j<                     @   ^ RI t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHtHt ^ RI	H
t
 ^ RIHtHt ^ RIHt ^ RIHt ^RIHtHtHt ]P,                  ! ]4      t0 Rmt0 Rmt] ! R R	4      4       tR
 R ltR R ltR R ltR R ltR R lt R R lt!R R lt"R R lt#R# )    N)	GeneratorIterable)contextmanager)	dataclassfield)Path)Any)DDUFCorruptedFileErrorDDUFExportErrorDDUFInvalidEntryNameErrorc                   t   a  ] tR t^#t o Rt]! RR7      t]V 3R lR l4       tRV 3R lR llt	V 3R lt
R	tV tR
# )	DDUFEntrya  Object representing a file entry in a DDUF file.

See [`read_dduf_file`] for how to read a DDUF file.

Attributes:
    filename (str):
        The name of the file in the DDUF archive.
    offset (int):
        The offset of the file in the DDUF archive.
    length (int):
        The length of the file in the DDUF archive.
    dduf_path (str):
        The path to the DDUF archive (for internal use).
F)reprc                6   < V ^8  d   QhRS[ S[RR3,          /# )   returnN)r   bytes)format__classdict__s   "{/Users/jameslopez/projects/CWCArchive/cwc-podcast/.venv/lib/python3.14/site-packages/huggingface_hub/serialization/_dduf.py__annotate__DDUFEntry.__annotate__;   s#     B B5$#45 B    c           	   #    "   V P                   P                  R4      ;_uu_ 4       p\        P                  ! VP                  4       ^ \        P                  R7      ;_uu_ 4       pW P
                  V P
                  V P                  ,            x  RRR4       RRR4       R#   + '       g   i     L; i  + '       g   i     R# ; i5i)zOpen the file as a memory-mapped file.

Useful to load safetensors directly from the file.

Example:
    ```py
    >>> import safetensors.torch
    >>> with entry.as_mmap() as mm:
    ...     tensors = safetensors.torch.load(mm)
    ```
rb)lengthaccessN)	dduf_pathopenmmapfilenoACCESS_READoffsetr   )selffmms   &  r   as_mmapDDUFEntry.as_mmap:   s{      ^^  &&!188:a8H8HIIRt{{T[['@AA J '&II '&&s4   $C	>B5$,B"	B5
C	"B2-B55C	 	C	c                &   < V ^8  d   QhRS[ RS[ /# )r   encodingr   str)r   r   s   "r   r   r   K   s     A A# AC Ar   c                   V P                   P                  R4      ;_uu_ 4       pVP                  V P                  4       VP	                  V P
                  4      P                  VR7      uuRRR4       #   + '       g   i     R# ; i)zRead the file as text.

Useful for '.txt' and '.json' entries.

Example:
    ```py
    >>> import json
    >>> index = json.loads(entry.read_text())
    ```
r   )r*   N)r   r   seekr#   readr   decode)r$   r*   r%   s   && r   	read_textDDUFEntry.read_textK   sX     ^^  &&!FF4;;66$++&--x-@ '&&&s   AA55B	c                J   < V ^8  d   Qh/ S[ ;R&   S[;R&   S[;R&   S[;R&   # )r   filenamer   r#   r   )r,   intr   )r   r   s   "r   r   r   #   s6     " M# $ K% & K' * '+ r    N)zutf-8)__name__
__module____qualname____firstlineno____doc__r   r   r   r'   r1   __annotate_func____static_attributes____classdictcell__)r   s   @r   r   r   #   s>     & 'IB B A AQ  r   r   c                |    V ^8  d   QhR\         P                  \        ,          R\        \        \        3,          /# )r   r   r   )osPathLiker,   dictr   )r   s   "r   r   r   [   s0     B BbkkC/ BDi4H Br   c           
        / p\        V 4      p \        P                  RV  24       \        P                  ! \        V 4      R4      ;_uu_ 4       pVP                  4        F  p\        P                  RVP                   24       VP                  \        P                  8w  d   \        R4      h \        VP                  4       \        Y#4      p\        TP                  YSP                   T R7      YP                  &   K  	  RRR4       RV9  d   \        R	4      h\"        P$                  ! VR,          P'                  4       4      p\)        WaP+                  4       4       \        P                  R
V  R\-        V4       R24       V#   \         d   p\        RTP                   24      ThRp?ii ; i  + '       g   i     L; i)a  
Read a DDUF file and return a dictionary of entries.

Only the metadata is read, the data is not loaded in memory.

Args:
    dduf_path (`str` or `os.PathLike`):
        The path to the DDUF file to read.

Returns:
    `dict[str, DDUFEntry]`:
        A dictionary of [`DDUFEntry`] indexed by filename.

Raises:
    - [`DDUFCorruptedFileError`]: If the DDUF file is corrupted (i.e. doesn't follow the DDUF format).

Example:
    ```python
    >>> import json
    >>> import safetensors.torch
    >>> from huggingface_hub import read_dduf_file

    # Read DDUF metadata
    >>> dduf_entries = read_dduf_file("FLUX.1-dev.dduf")

    # Returns a mapping filename <> DDUFEntry
    >>> dduf_entries["model_index.json"]
    DDUFEntry(filename='model_index.json', offset=66, length=587)

    # Load model index as JSON
    >>> json.loads(dduf_entries["model_index.json"].read_text())
    {'_class_name': 'FluxPipeline', '_diffusers_version': '0.32.0.dev0', '_name_or_path': 'black-forest-labs/FLUX.1-dev', ...

    # Load VAE weights using safetensors
    >>> with dduf_entries["vae/diffusion_pytorch_model.safetensors"].as_mmap() as mm:
    ...     state_dict = safetensors.torch.load(mm)
    ```
zReading DDUF file rzReading entry z)Data must not be compressed in DDUF file.z!Invalid entry name in DDUF file: N)r4   r#   r   r   model_index.json7Missing required 'model_index.json' entry in DDUF file.zDone reading DDUF file z. Found z entries)r   loggerinfozipfileZipFiler,   infolistdebugr4   compress_type
ZIP_STOREDr
   _validate_dduf_entry_namer   _get_data_offsetr   	file_sizejsonloadsr1   _validate_dduf_structurekeyslen)r   entrieszfrH   er#   indexs   &      r   read_dduf_filer[   [   sj   N GYI
KK$YK01	Y	-	-KKMDLL>$--9:!!W%7%77,-XYYi)$--8 &b/F%.vnnXa&GMM" " 
.$ ($%^__JJw12<<>?EULLN3
KK))HS\N(STN! - i,/PQUQ^Q^P_-`aghhi 
.	-s1   A F2/F=F2F/	F*	*F/	/F22G	c                    V ^8  d   QhR\         \        P                  ,          R\        \        \         \         \
        ,          \        ,          3,          ,          RR/# )r   r   rW   r   N)r,   r@   rA   r   tupler   r   )r   s   "r   r   r      sM     V7 V7cBKK&7 V7(5QTVY\`V`chVhQhKiBj V7os V7r   c                \   \         P                  RV  R24       \        4       pRp\        P                  ! \        V 4      R\        P                  4      ;_uu_ 4       pV F  w  rVWR9   d   \        RV 24      hVP                  V4       VR8X  d/    \        P                  ! \        V4      P                  4       4      p \        V4      p\         P!                  R	T R
24       \#        YET4       K  	  RRR4       Vf   \        R4      h \%        W24       \         P                  RT  24       R#   \        P                   d   p\        R4      ThRp?ii ; i  \         d   p\        RT 24      ThRp?ii ; i  + '       g   i     L; i  \&         d   p\        R4      ThRp?ii ; i)a  Write a DDUF file from an iterable of entries.

This is a lower-level helper than [`export_folder_as_dduf`] that allows more flexibility when serializing data.
In particular, you don't need to save the data on disk before exporting it in the DDUF file.

Args:
    dduf_path (`str` or `os.PathLike`):
        The path to the DDUF file to write.
    entries (`Iterable[tuple[str, Union[str, Path, bytes]]]`):
        An iterable of entries to write in the DDUF file. Each entry is a tuple with the filename and the content.
        The filename should be the path to the file in the DDUF archive.
        The content can be a string or a pathlib.Path representing a path to a file on the local disk or directly the content as bytes.

Raises:
    - [`DDUFExportError`]: If anything goes wrong during the export (e.g. invalid entry name, missing 'model_index.json', etc.).

Example:
    ```python
    # Export specific files from the local disk.
    >>> from huggingface_hub import export_entries_as_dduf
    >>> export_entries_as_dduf(
    ...     dduf_path="stable-diffusion-v1-4-FP16.dduf",
    ...     entries=[ # List entries to add to the DDUF file (here, only FP16 weights)
    ...         ("model_index.json", "path/to/model_index.json"),
    ...         ("vae/config.json", "path/to/vae/config.json"),
    ...         ("vae/diffusion_pytorch_model.fp16.safetensors", "path/to/vae/diffusion_pytorch_model.fp16.safetensors"),
    ...         ("text_encoder/config.json", "path/to/text_encoder/config.json"),
    ...         ("text_encoder/model.fp16.safetensors", "path/to/text_encoder/model.fp16.safetensors"),
    ...         # ... add more entries here
    ...     ]
    ... )
    ```

    ```python
    # Export state_dicts one by one from a loaded pipeline
    >>> from diffusers import DiffusionPipeline
    >>> from typing import Generator, Tuple
    >>> import safetensors.torch
    >>> from huggingface_hub import export_entries_as_dduf
    >>> pipe = DiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
    ... # ... do some work with the pipeline

    >>> def as_entries(pipe: DiffusionPipeline) -> Generator[tuple[str, bytes], None, None]:
    ...     # Build a generator that yields the entries to add to the DDUF file.
    ...     # The first element of the tuple is the filename in the DDUF archive (must use UNIX separator!). The second element is the content of the file.
    ...     # Entries will be evaluated lazily when the DDUF file is created (only 1 entry is loaded in memory at a time)
    ...     yield "vae/config.json", pipe.vae.to_json_string().encode()
    ...     yield "vae/diffusion_pytorch_model.safetensors", safetensors.torch.save(pipe.vae.state_dict())
    ...     yield "text_encoder/config.json", pipe.text_encoder.config.to_json_string().encode()
    ...     yield "text_encoder/model.safetensors", safetensors.torch.save(pipe.text_encoder.state_dict())
    ...     # ... add more entries here

    >>> export_entries_as_dduf(dduf_path="stable-diffusion-v1-4.dduf", entries=as_entries(pipe))
    ```
zExporting DDUF file ''NwzCan't add duplicate entry: rE   z#Failed to parse 'model_index.json'.zInvalid entry name: zAdding entry 'z' to DDUF filerF   zInvalid DDUF file structure.zDone writing DDUF file )rG   rH   setrI   rJ   r,   rN   r   addrR   rS   _load_contentr0   JSONDecodeErrorrO   r   rL   _dump_content_in_archiverT   r
   )r   rW   	filenamesrZ   archiver4   contentrY   s   &&      r   export_entries_as_ddufri      s   p KK'	{!45IE	Yg.@.@	A	AW!(H$%(CH:&NOOMM(#--X JJ}W'='D'D'FGEP4X> LL>(>BC$W@! ") 
B( }WXXE 2 KK))56% ++ X)*OPVWWX
 - P%(<XJ&GHaOP 
B	A0 " E<=1DEsf   4E<-D1 E)E<F 1E	E	E	E<E9	%E4	4E9	9E<<F	F+F&&F+c                    V ^8  d   QhR\         \        P                  ,          R\         \        P                  ,          RR/# )r   r   folder_pathr   N)r,   r@   rA   )r   s   "r   r   r      s4     !> !>S2;;%6 !>S2;;EV !>[_ !>r   c                P   a \        S4      oR V3R llp\        W! 4       4       R# )a  
Export a folder as a DDUF file.

AUses [`export_entries_as_dduf`] under the hood.

Args:
    dduf_path (`str` or `os.PathLike`):
        The path to the DDUF file to write.
    folder_path (`str` or `os.PathLike`):
        The path to the folder containing the diffusion model.

Example:
    ```python
    >>> from huggingface_hub import export_folder_as_dduf
    >>> export_folder_as_dduf(dduf_path="FLUX.1-dev.dduf", folder_path="path/to/FLUX.1-dev")
    ```
c                \    V ^8  d   QhR\         \        \        \        3,          ,          /# )r   r   )r   r]   r,   r   )r   s   "r   r   +export_folder_as_dduf.<locals>.__annotate__  s!     3 3(5d+;"< 3r   c               3     <"   \        S4      P                  R 4       F  p V P                  4       '       g   K  V P                  \        9  d   \
        P                  RV  R24       KK  V P                  S4      p\        VP                  4      ^8  d   \
        P                  RV  R24       K  VP                  4       V 3x  K  	  R# 5i)z**/*zSkipping file 'z' (file type not allowed)z"' (nested directories not allowed)N)r   globis_filesuffixDDUF_ALLOWED_ENTRIESrG   rL   relative_torV   partsas_posix)pathpath_in_archiverk   s     r   _iterate_over_folder3export_folder_as_dduf.<locals>._iterate_over_folder  s     %**62D<<>>{{"66tf4MNO"..{;O?(()Q.tf4VWX!**,d22 3s   CC	N)r   ri   )r   rk   ry   s   &f r   export_folder_as_ddufr{      s)    $ {#K3 3 9&:&<=r   c                    V ^8  d   QhR\         P                  R\        R\        \        P                  ,          \
        ,          RR/# )r   rg   r4   rh   r   N)rI   rJ   r,   r@   rA   r   )r   s   "r   r   r     sE     	g 	ggoo 	g 	gsUWU`U`O`chOh 	gmq 	gr   c           	         V P                  VR RR7      ;_uu_ 4       p\        V\        \        34      '       dF   \        V4      pVP                  R4      ;_uu_ 4       p\        P
                  ! WSR4       RRR4       M7\        V\        4      '       d   VP                  V4       M\        RV R24      hRRR4       R#   + '       g   i     L; i  + '       g   i     R# ; i)r`   T)force_zip64r   NzInvalid content type for z. Must be str, Path or bytes.i   )	r   
isinstancer,   r   shutilcopyfileobjr   writer   )rg   r4   rh   
archive_fhcontent_path
content_fhs   &&&   r   re   re     s    	h	6	6*gT{++=L""4((J"":?K )(''W%!$=hZGd"eff 
7	6 )( 
7	6	6s%   ACC 	6A C CCC$	c                \    V ^8  d   QhR\         \        ,          \        ,          R\        /# )r   rh   r   )r,   r   r   )r   s   "r   r   r   )  s'     
i 
i3:- 
i% 
ir   c                    \        V \        \        34      '       d   \        V 4      P                  4       # \        V \        4      '       d   V # \        R\        V 4       R24      h)zgLoad the content of an entry as bytes.

Used only for small checks (not to dump content into archive).
z6Invalid content type. Must be str, Path or bytes. Got .)r   r,   r   
read_bytesr   r   type)rh   s   &r   rc   rc   )  sX    
 'C;''G}''))	GU	#	# VW[\cWdVeefghhr   c                0    V ^8  d   QhR\         R\         /# )r   
entry_namer   r+   )r   s   "r   r   r   6  s      # # r   c                 
   R V P                  R 4      R,          ,           \        9  d   \        RV  24      hRV 9   d   \        RV  R 24      hV P                  R4      p V P	                  R4      ^8  d   \        RV  R 24      hV # )r   zFile type not allowed: \z0Entry names must use UNIX separators ('/'). Got /z-DDUF only supports 1 level of directory. Got )splitrs   r   stripcount)r   s   &r   rO   rO   6  s    
Zc"2&&.BB'*A*(NOOz'*Z[eZffg(hii!!#&Jq '*WXbWccd(effr   c                J    V ^8  d   QhR\         R\        \        ,          RR/# )r   rZ   entry_namesr   N)r	   r   r,   )r   s   "r   r   r   A  s%      C hsm  r   c                  aa \        V \        4      '       g   \        R\        V 4       R24      hS Uu0 uF$  pRV9   g   K  VP	                  R4      ^ ,          kK&  	  ppV Fy  oSV 9  d   \        RS R24      h\
        ;QJ d$    VV3R l\         4       F  '       g   K   RM	  RM! VV3R l\         4       4      '       d   Ke  \        R	S R
\         R24      h	  R# u upi )ai  
Consistency checks on the DDUF file structure.

Rules:
- The 'model_index.json' entry is required and must contain a dictionary.
- Each folder name must correspond to an entry in 'model_index.json'.
- Each folder must contain at least a config file ('config.json', 'tokenizer_config.json', 'preprocessor_config.json', 'scheduler_config.json').

Args:
    index (Any):
        The content of the 'model_index.json' entry.
    entry_names (Iterable[str]):
        The list of entry names in the DDUF file.

Raises:
    - [`DDUFCorruptedFileError`]: If the DDUF file is corrupted (i.e. doesn't follow the DDUF format).
z>Invalid 'model_index.json' content. Must be a dictionary. Got r   r   zMissing required entry 'z' in 'model_index.json'.c              3   8   <"   T F  pS R V 2S9   x  K  	  R# 5i)r   Nr6   ).0required_entryr   folders   & r   	<genexpr>+_validate_dduf_structure.<locals>.<genexpr>Z  s$     rUq>fXQ~./;>Uqs   TFz!Missing required file in folder 'z!'. Must contains at least one of N)r   rB   r
   r   r   anyDDUF_FOLDER_REQUIRED_ENTRIES)rZ   r   entrydduf_foldersr   s   &f  @r   rT   rT   A  s    $ eT""$'efjkpfqerrs%tuu5@Q[EC5L'EKK$Q''[LQ(+CF8Kc)deesrUqrsssrUqrrr(3F8;\]y\zz{| 	  Rs   	C"C"c                d    V ^8  d   QhR\         P                  R\         P                  R\        /# )r   rX   rH   r   )rI   rJ   ZipInfor5   )r   s   "r   r   r   `  s)     " " " "C "r   c                   V P                   f   \        R4      hVP                  pV P                   P                  V4       V P                   P	                  ^4      p\        V4      ^8  d   \        R4      h\        P                  VR,          R4      p\        P                  VR,          R4      pV^,           V,           V,           pV# )a  
Calculate the data offset for a file in a ZIP archive.

Args:
    zf (`zipfile.ZipFile`):
        The opened ZIP file. Must be opened in read mode.
    info (`zipfile.ZipInfo`):
        The file info.

Returns:
    int: The offset of the file data in the ZIP archive.
z+ZipFile object must be opened in read mode.zIncomplete local file header.:      Nlittle:r      N)fpr
   header_offsetr.   r/   rV   r5   
from_bytes)rX   rH   r   local_file_headerfilename_lenextra_field_lendata_offsets   &&     r   rP   rP   `  s     
uu}$%RSS &&M EEJJ}

2
"$%DEE >>"3E":HELnn%6u%=xHO  "$|3oEKr   >   .txt.json.model.safetensors>   config.jsonscheduler_config.jsontokenizer_config.jsonpreprocessor_config.json)$rR   loggingr    r@   r   rI   collections.abcr   r   
contextlibr   dataclassesr   r   pathlibr   typingr	   errorsr
   r   r   	getLoggerr7   rG   rs   r   r   r[   ri   r{   re   rc   rO   rT   rP   r6   r   r   <module>r      s       	   / % (   W W 
		8	$    4A 4A 4AnBJV7r!>H	g
i>"r   