Ë
    (täi’>  ã                  ó„  — d dl mZ d dlZd dlZd dlmZmZ d dlZddlm	Z	 ddl
mZmZ  e«       rJd dlZddlmZ dd	lmZ  e«       rd dlZej(                  ej*                  ej,                  ej.                  d
œZd dlmZ dd„Z	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z	 	 	 	 d	 	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z	 	 	 	 	 	 	 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z	 	 d	 	 	 	 	 	 	 dd„Z 	 	 dd„Z!	 	 d	 	 	 	 	 d d„Z"	 	 d	 	 	 	 	 	 	 	 	 d!d„Z#y)"é    )ÚannotationsN)ÚLiteralÚcasté   )Ú	deprecate)Úis_safetensors_availableÚis_torch_availableé   )ÚVaeImageProcessor)ÚVideoProcessor)Úfloat16Úfloat32Úbfloat16Úuint8)ÚImagec                ó¶   — | j                  d«      ry| j                  d«      ry| j                  d«      s| j                  d«      ry| j                  d«      ry	y
)Ns   ÿØÚjpegs   ‰PNG

Úpngs   GIF87as   GIF89aÚgifs   BMÚbmpÚunknown)Ú
startswith)Údatas    úk/Volumes/fast/ai/experiments/MLX_z-image/.venv/lib/python3.12/site-packages/diffusers/utils/remote_utils.pyÚdetect_image_typer   0   sM   € Ø‡��{Ô#ØØ	�‰Ð-Ô	.ØØ	�‰˜Ô	# t§¡°yÔ'AØØ	�‰˜Ô	ØØó    c                óÄ   — |j                   dk(  r|€|€t        d«      ‚|dk(  r(|dk(  r#|	s!t        |t        t        f«      st        d«      ‚|r|€t        dddd	¬
«       y y y )Né   z1`height` and `width` required for packed latents.ÚptÚpilz`processor` is required.Ú
do_scalingú1.0.0zQ`do_scaling` is deprecated, pass `scaling_factor` and `shift_factor` if required.F©Ústandard_warn)ÚndimÚ
ValueErrorÚ
isinstancer   r   r   )ÚendpointÚtensorÚ	processorr!   Úscaling_factorÚshift_factorÚoutput_typeÚreturn_typeÚimage_formatÚpartial_postprocessÚinput_tensor_typeÚoutput_tensor_typeÚheightÚwidths                 r   Úcheck_inputs_decoder5   <   sv   € ð  ‡{�{�aÒ˜F˜N¨u¨}ÜÐLÓMÐMà�tÒØ˜5Ò Ù#Ü˜9Ô'8¼.Ð&IÔJäÐ3Ó4Ð4Ù�nÐ,ÜØØØ_Øö		
ð -€zr   c                óÔ  — |dk(  s|dk(  ro|�m| j                   }| j                  }t        j                  |d   «      }|d   }t        |   }	t        j                  t        |«      |	¬«      j                  |«      }|dk(  r×|rS|dk(  rED �
cg c]%  }
t        j                  |
j                  «       «      ‘Œ' }}
t        |«      dk(  r�|d   }|S |dk(  r„}|S |�|dk(  r}|S t        |t        «      r7t        t         t        j                     |j#                  d¬«      d   «      }|S t        t        j                  |j%                  d¬«      d   «      }S |dk(  rf|dk(  ra|€_t        j&                  t)        j*                  | j                   «      «      j-                  d	«      }t/        | j                   «      }||_        |S |dk(  r‚|�€|dk(  rrj3                  dd
dd«      j5                  «       j                  «       dz  j7                  «       j9                  d«      D �
cg c]  }
t        j                  |
«      ‘Œ }}
|S |dk(  r}S |dk(  r|dk(  r| j                   }S c c}
w c c}
w )Nr   r    ÚshapeÚdtype©r8   r   r   )r-   ÚRGBr
   r   éÿ   r   Úmp4)ÚcontentÚheadersÚjsonÚloadsÚ	DTYPE_MAPÚtorchÚ
frombufferÚ	bytearrayÚreshaper   Ú	fromarrayÚnumpyÚlenr'   r   r   ÚlistÚpostprocess_videoÚpostprocessÚopenÚioÚBytesIOÚconvertr   ÚformatÚpermuteÚfloatÚroundÚastype)Úresponser*   r-   r.   r0   Úoutput_tensorÚ
parametersr7   r8   Útorch_dtypeÚimageÚoutputÚdetected_formats                r   Úpostprocess_decoder\   ^   s–  € ð �dÒ˜{¨eÒ3¸	Ð8MØ ×(Ñ(ˆØ×%Ñ%ˆ
Ü—
‘
˜: gÑ.Ó/ˆØ˜7Ñ#ˆÜ Ñ&ˆÜ×(Ñ(¬°=Ó)AÈÔU×]Ñ]Ð^cÓdˆØ�dÒÙØ˜eÒ#ÙFSÓTÁm¸Uœ%Ÿ/™/¨%¯+©+«-Õ8Àm�ÐTÜ�v“; !Ò#Ø# A™Y�Fð> €Mð=  Ò$Ø&�ð: €Mð7 Ð  K°4Ò$7Ø&�ð4 €Mô1 ˜i¬Ô8Ü!ÜœUŸ[™[Ñ)Ø!×3Ñ3°MÈuÐ3ÓUÐVWÑXó�Fð. €Mô% "ÜŸ™Ø!×-Ñ-¨mÈÐ-ÓOÐPQÑRó�Fð$ €Mð 
˜Ò	 +°Ò"6¸9Ð;LÜ—‘œBŸJ™J x×'7Ñ'7Ó8Ó9×AÑAÀ%ÓHˆÜ+¨H×,<Ñ,<Ó=ˆØ'ˆŒð €Mð 
˜Ò	 )Ð"7Ø˜%Òð ,×3Ñ3°A°q¸!¸QÓ?×EÑEÓG×MÑMÓOÐRUÑU×\Ñ\Ó^×eÑeÐfmÔnóán�Eô —‘ Õ&Ønð ð ð €Mð	 ˜DÒ Ø"ˆFð €Mð 
˜Ò	 +°Ò"6Ø×!Ñ!ˆØ€MùòC Uùò2s   Â
*I ÈI%c
                ó¬  — i }
|||t        | j                  «      t        | j                  «      j	                  d«      d   dœ}|r|�||d<   |r|�||d<   |r|€||d<   n|r	|€|€||d<   |�|	�
||d<   |	|d<   d	|
d
<   d	|
d<   |dk(  r|dk(  r|€d|
d<   n|dk(  r|dk(  r|€d|
d<   n
|dk(  rd|
d<   t
        j                  j                  | d«      }|||
dœS )NÚ.éÿÿÿÿ)r/   r-   r0   r7   r8   r+   r,   r!   r3   r4   ztensor/binaryzContent-TypeÚAcceptr    Újpgz
image/jpegr   z	image/pngr<   z
text/plainr)   ©r   Úparamsr>   )rI   r7   Ústrr8   ÚsplitÚsafetensorsrB   Ú_tobytes)r)   r*   r!   r+   r,   r-   r/   r0   r3   r4   r>   rW   Útensor_datas                r   Úprepare_decoderi   “   s7  € ð €Gà$Ø"Ø2Ü�f—l‘lÓ#Ü�V—\‘\Ó"×(Ñ(¨Ó-¨bÑ1ñ€Jñ �nÐ0Ø'5ˆ
Ð#Ñ$Ù�lÐ.Ø%1ˆ
�>Ñ"Ù�nÐ,Ø#-ˆ
�<Ò Ù	˜Ð.°<Ð3GØ#-ˆ
�<Ñ ØÐ˜eÐ/Ø%ˆ
�8ÑØ#ˆ
�7ÑØ-€GˆNÑØ'€GˆHÑØ�eÒ °Ò 5¸)Ð:KØ(ˆ�ÒØ	˜Ò	 ,°%Ò"7¸IÐ<MØ'ˆ�ÒØ	˜Ò	Ø(ˆ�ÑÜ×#Ñ#×,Ñ,¨V°XÓ>€KØ¨:À'ÑJÐJr   c                óN  — |
dk(  rt        dddd¬«       d}
|dk(  rt        ddd	d¬«       d}t        | |||||||||	|
|||«       t        ||||||||	||¬
«
      }t        j                  | fi |¤Ž}|j
                  st        |j                  «       «      ‚t        |||||	¬«      }|S )aM  
    Hugging Face Hybrid Inference that allow running VAE decode remotely.

    Args:
        endpoint (`str`):
            Endpoint for Remote Decode.
        tensor (`torch.Tensor`):
            Tensor to be decoded.
        processor (`VaeImageProcessor` or `VideoProcessor`, *optional*):
            Used with `return_type="pt"`, and `return_type="pil"` for Video models.
        do_scaling (`bool`, default `True`, *optional*):
            **DEPRECATED**. **pass `scaling_factor`/`shift_factor` instead.** **still set
            do_scaling=None/do_scaling=False for no scaling until option is removed** When `True` scaling e.g. `latents
            / self.vae.config.scaling_factor` is applied remotely. If `False`, input must be passed with scaling
            applied.
        scaling_factor (`float`, *optional*):
            Scaling is applied when passed e.g. [`latents /
            self.vae.config.scaling_factor`](https://github.com/huggingface/diffusers/blob/7007febae5cff000d4df9059d9cf35133e8b2ca9/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py#L1083C37-L1083C77).
            - SD v1: 0.18215
            - SD XL: 0.13025
            - Flux: 0.3611
            If `None`, input must be passed with scaling applied.
        shift_factor (`float`, *optional*):
            Shift is applied when passed e.g. `latents + self.vae.config.shift_factor`.
            - Flux: 0.1159
            If `None`, input must be passed with scaling applied.
        output_type (`"mp4"` or `"pil"` or `"pt", default `"pil"):
            **Endpoint** output type. Subject to change. Report feedback on preferred type.

            `"mp4": Supported by video models. Endpoint returns `bytes` of video. `"pil"`: Supported by image and video
            models.
                Image models: Endpoint returns `bytes` of an image in `image_format`. Video models: Endpoint returns
                `torch.Tensor` with partial `postprocessing` applied.
                    Requires `processor` as a flag (any `None` value will work).
            `"pt"`: Support by image and video models. Endpoint returns `torch.Tensor`.
                With `partial_postprocess=True` the tensor is postprocessed `uint8` image tensor.

            Recommendations:
                `"pt"` with `partial_postprocess=True` is the smallest transfer for full quality. `"pt"` with
                `partial_postprocess=False` is the most compatible with third party code. `"pil"` with
                `image_format="jpg"` is the smallest transfer overall.

        return_type (`"mp4"` or `"pil"` or `"pt", default `"pil"):
            **Function** return type.

            `"mp4": Function returns `bytes` of video. `"pil"`: Function returns `PIL.Image.Image`.
                With `output_type="pil" no further processing is applied. With `output_type="pt" a `PIL.Image.Image` is
                created.
                    `partial_postprocess=False` `processor` is required. `partial_postprocess=True` `processor` is
                    **not** required.
            `"pt"`: Function returns `torch.Tensor`.
                `processor` is **not** required. `partial_postprocess=False` tensor is `float16` or `bfloat16`, without
                denormalization. `partial_postprocess=True` tensor is `uint8`, denormalized.

        image_format (`"png"` or `"jpg"`, default `jpg`):
            Used with `output_type="pil"`. Endpoint returns `jpg` or `png`.

        partial_postprocess (`bool`, default `False`):
            Used with `output_type="pt"`. `partial_postprocess=False` tensor is `float16` or `bfloat16`, without
            denormalization. `partial_postprocess=True` tensor is `uint8`, denormalized.

        input_tensor_type (`"binary"`, default `"binary"`):
            Tensor transfer type.

        output_tensor_type (`"binary"`, default `"binary"`):
            Tensor transfer type.

        height (`int`, **optional**):
            Required for `"packed"` latents.

        width (`int`, **optional**):
            Required for `"packed"` latents.

    Returns:
        output (`Image.Image` or `list[Image.Image]` or `bytes` or `torch.Tensor`).
    Úbase64zinput_tensor_type='base64'r"   z9input_tensor_type='base64' is deprecated. Using `binary`.Fr#   Úbinaryzoutput_tensor_type='base64'z:output_tensor_type='base64' is deprecated. Using `binary`.)
r)   r*   r!   r+   r,   r-   r/   r0   r3   r4   )rU   r*   r-   r.   r0   )	r   r5   ri   ÚrequestsÚpostÚokÚRuntimeErrorr?   r\   )r(   r)   r*   r!   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   ÚkwargsrU   rZ   s                    r   Úremote_decoderr   ¾   sö   € ðx ˜HÒ$ÜØ(ØØGØõ		
ð %ÐØ˜XÒ%ÜØ)ØØHØõ		
ð &ÐÜØØØØØØØØØØØØØØôô  ØØØØ%Ø!ØØ!Ø/ØØô€Fô �}‰}˜XÑ0¨Ñ0€HØ�;Š;Ü˜8Ÿ=™=›?Ó+Ð+ÜØØØØØ/ô€Fð €Mr   c                 ó   — y )N© )r(   rY   r+   r,   s       r   Úcheck_inputs_encoderu   S  s   € ð 	r   c                óà   — | j                   }| j                  }t        j                  |d   «      }|d   }t        |   }t        j                  t        |«      |¬«      j                  |«      }|S )Nr7   r8   r9   )	r=   r>   r?   r@   rA   rB   rC   rD   rE   )rU   rV   rW   r7   r8   rX   s         r   Úpostprocess_encoderw   \  sj   € ð ×$Ñ$€MØ×!Ñ!€JÜ�J‰J�z 'Ñ*Ó+€EØ�wÑ€EÜ˜EÑ"€KÜ×$Ñ$¤Y¨}Ó%=À[ÔQ×YÑYÐZ_Ó`€MØÐr   c                ó¶  — i }i }|�||d<   |�||d<   t        | t        j                  «      rqt        j                  j	                  | j                  «       d«      }t        | j                  «      |d<   t        | j                  «      j                  d«      d   |d<   n7t        j                  «       }| j                  |d¬	«       |j                  «       }|||d
œS )Nr+   r,   r)   r7   r^   r_   r8   ÚPNG)rP   rb   )r'   rB   ÚTensorrf   rg   Ú
contiguousrI   r7   rd   r8   re   rM   rN   ÚsaveÚgetvalue)rY   r+   r,   r>   rW   r   Úbuffers          r   Úprepare_encoder   h  sÈ   € ð
 €GØ€JØÐ!Ø'5ˆ
Ð#Ñ$ØÐØ%1ˆ
�>Ñ"Ü�%œŸ™Ô&Ü× Ñ ×)Ñ)¨%×*:Ñ*:Ó*<¸hÓGˆÜ" 5§;¡;Ó/ˆ
�7ÑÜ! %§+¡+Ó.×4Ñ4°SÓ9¸"Ñ=ˆ
�7Òä—‘“ˆØ�
‰
�6 %ˆ
Ô(Ø�‰Ó ˆØ J¸7ÑCÐCr   c                óÌ   — t        | |||«       t        |||¬«      }t        j                  | fi |¤Ž}|j                  st        |j                  «       «      ‚t        |¬«      }|S )a%  
    Hugging Face Hybrid Inference that allow running VAE encode remotely.

    Args:
        endpoint (`str`):
            Endpoint for Remote Decode.
        image (`torch.Tensor` or `PIL.Image.Image`):
            Image to be encoded.
        scaling_factor (`float`, *optional*):
            Scaling is applied when passed e.g. [`latents * self.vae.config.scaling_factor`].
            - SD v1: 0.18215
            - SD XL: 0.13025
            - Flux: 0.3611
            If `None`, input must be passed with scaling applied.
        shift_factor (`float`, *optional*):
            Shift is applied when passed e.g. `latents - self.vae.config.shift_factor`.
            - Flux: 0.1159
            If `None`, input must be passed with scaling applied.

    Returns:
        output (`torch.Tensor`).
    )rY   r+   r,   )rU   )ru   r   rm   rn   ro   rp   r?   rw   )r(   rY   r+   r,   rq   rU   rZ   s          r   Úremote_encoder�   ~  sn   € ô8 ØØØØô	ô ØØ%Ø!ô€Fô
 �}‰}˜XÑ0¨Ñ0€HØ�;Š;Ü˜8Ÿ=™=›?Ó+Ð+ÜØô€Fð €Mr   )r   ÚbytesÚreturnrd   )NTNNr    r    ra   Frl   rl   NN)r(   rd   r)   ú'torch.Tensor'r*   ú-'VaeImageProcessor' | 'VideoProcessor' | Noner!   Úboolr+   úfloat | Noner,   r‡   r-   úLiteral['mp4', 'pil', 'pt']r.   rˆ   r/   úLiteral['png', 'jpg']r0   r†   r1   úLiteral['binary']r2   rŠ   r3   ú
int | Noner4   r‹   )Nr    r    F)
rU   úrequests.Responser*   r…   r-   rˆ   r.   rˆ   r0   r†   )	NTNNr    ra   FNN)r)   r„   r*   r…   r!   r†   r+   r‡   r,   r‡   r-   rˆ   r/   r‰   r0   r†   r3   r‹   r4   r‹   )r(   rd   r)   r„   r*   r…   r!   r†   r+   r‡   r,   r‡   r-   rˆ   r.   rˆ   r/   r‰   r0   r†   r1   rŠ   r2   rŠ   r3   r‹   r4   r‹   rƒ   z8Image.Image | list[Image.Image] | bytes | 'torch.Tensor')NN)r(   rd   rY   ú'torch.Tensor' | Image.Imager+   r‡   r,   r‡   )rU   rŒ   )rY   r�   r+   r‡   r,   r‡   )
r(   rd   rY   r�   r+   r‡   r,   r‡   rƒ   r„   )$Ú
__future__r   rM   r?   Útypingr   r   rm   Údeprecation_utilsr   Úimport_utilsr   r	   rB   Úimage_processorr   Úvideo_processorr   Úsafetensors.torchrf   r   r   r   r   rA   ÚPILr   r   r5   r\   ri   rr   ru   rw   r   r�   rt   r   r   Ú<module>r–      sc  ðõ  #ã 	Û ß  ã å (ß Fñ ÔÛå3Ý0áÔ!Û ð —=‘=Ø—=‘=Ø—N‘NØ—‘ñ	€Iõ ó	ð @DØØ#'Ø!%Ø/4Ø/4Ø*/Ø %Ø+3Ø,4ØØð
Øð
àð
ð =ð
ð ð	
ð
 !ð
ð ð
ð -ð
ð -ð
ð (ð
ð ð
ð )ð
ð *ð
ð ð
ð ó
ðH @DØ/4Ø/4Ø %ð2Øð2à<ð2ð -ð2ð -ð	2ð
 ó2ðn @DØØ#'Ø!%Ø/4Ø*/Ø %ØØð(KØð(Kà<ð(Kð ð(Kð !ð	(Kð
 ð(Kð -ð(Kð (ð(Kð ð(Kð ð(Kð ó(Kð\ @DØØ#'Ø!%Ø/4Ø/4Ø*/Ø %Ø+3Ø,4ØØðRØðRàðRð =ðRð ð	Rð
 !ðRð ðRð -ðRð -ðRð (ðRð ðRð )ðRð *ðRð ðRð ðRð >óRðp $(Ø!%ð		Øð	à'ð	ð !ð	ð ó		ð	Øó	ð $(Ø!%ðDØ'ðDà ðDð óDð2 $(Ø!%ð	-Øð-à'ð-ð !ð-ð ð	-ð
 ô-r   