
    
3j>                       S SK Jr  S SKrS SKrS SKJrJr  S SKrSSKJ	r	  SSK
JrJr  \" 5       (       aO  S SKrSSKJr  SS	KJr  \" 5       (       a  S SKr\R(                  \R*                  \R,                  \R.                  S
.rS SKJr  SS jr            S                           SS jjr    S         SS jjr         S                   SS jjr            S                             SS jjr  S       SS jjr   SS jr!  S     S S jjr"  S         S!S jjr#g)"    )annotationsN)Literalcast   )	deprecate)is_safetensors_availableis_torch_available   )VaeImageProcessor)VideoProcessor)float16float32bfloat16uint8)Imagec                    U R                  S5      (       a  gU R                  S5      (       a  gU R                  S5      (       d  U R                  S5      (       a  gU R                  S5      (       a  g	g
)Ns   jpegs   PNG

pngs   GIF87as   GIF89agifs   BMbmpunknown)
startswith)datas    V/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/utils/remote_utils.pydetect_image_typer   0   s\    {##	-	.	.		#	#ty'A'A			    c                    UR                   S:X  a  Uc  Uc  [        S5      eUS:X  a3  US:X  a-  U	(       d&  [        U[        [        45      (       d  [        S5      eU(       a  Uc  [        SSSS	S
9  g g g )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   <   sy      {{aFNu}LMMt5 #9'8.&IJJ344n,_		
 -zr   c                   US:X  d	  US:X  ao  Ubl  U R                   nU R                  n[        R                  " US   5      nUS   n[        U   n	[
        R                  " [        U5      U	S9R                  U5      nUS:X  a  U(       aZ  US:X  aJ  W V
s/ s H'  n
[        R                  " U
R                  5       5      PM)     nn
[        U5      S:X  a  US   nU$ US:X  a  WnW$ Ub  US:X  a  WnU$ [        U[        5      (       a4  [        [         [        R                     UR#                  WSS9S   5      nU$ [        [        R                  UR%                  WSS9S   5      n U$ US:X  ai  US:X  ac  Uc`  [        R&                  " [(        R*                  " U R                   5      5      R-                  S	5      n[/        U R                   5      nXl        U$ US:X  a  Ub  US:X  av  WR3                  SS
SS5      R5                  5       R                  5       S-  R7                  5       R9                  S5       V
s/ s H  n
[        R                  " U
5      PM     nn
U$ US:X  a  WnW$ US:X  a  US:X  a  U R                   nW$ s  sn
f s  sn
f )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TmU%//%++-8mTv;!##AYF> M= $&: M7  K4$7&4 M1 i88!U[[)!33Mu3UVWXF. M% "!--m-OPQRF$ M 
	+"69;LBJJx'7'789AA%H+H,<,<=' M 
	)"7% ,33Aq!Q?EEGMMORUU\\^eefmnnE &n   M	 D "F M 
	+"6!!MC U2s   .I?7 Jc
                   0 n
UUU[        U R                  5      [        U R                  5      R	                  S5      S   S.nU(       a  Ub  X;S'   U(       a  Ub  XKS'   U(       a  Uc  X+S'   OU(       a
  Uc  Uc  X+S'   Ub  U	b  XS'   XS'   S	U
S
'   S	U
S'   US:X  a  US:X  a	  Uc  SU
S'   O US:X  a  US:X  a	  Uc  SU
S'   OUS:X  a  SU
S'   [
        R                  R                  U S5      nXU
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      s)    G$"2fll#V\\"((-b1J n0'5#$l.%1>"n,#-< 	.<3G#-< e/%8#7-GN'GHe 5):K(		,%"7I<M'		(##,,VX>K'JJr   c                F   U
S:X  a  [        SSSSS9  Sn
US:X  a  [        SSS	SS9  Sn[        U UUUUUUUUU	U
UUU5        [        UUUUUUUU	UUS
9
n[        R                  " U 40 UD6nUR
                  (       d  [        UR                  5       5      e[        UUUUU	S9nU$ )aQ  
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0H;;8==?++/F Mr   c                    g )N )r(   rY   r+   r,   s       r   check_inputs_encoderu   S  s     	r   c                    U R                   nU R                  n[        R                  " US   5      nUS   n[        U   n[
        R                  " [        U5      US9R                  U5      nU$ )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JJz'*+EwEE"K$$Y}%=[QYYZ_`Mr   c                   0 n0 nUb  XS'   Ub  X$S'   [        U [        R                  5      (       aq  [        R                  R	                  U R                  5       S5      n[        U R                  5      US'   [        U R                  5      R                  S5      S   US'   O5[        R                  " 5       nU R                  USS	9  UR                  5       nXTU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4S9"=
7

6%
( 7CCr   c                    [        U UUU5        [        UUUS9n[        R                  " U 40 UD6nUR                  (       d  [        UR                  5       5      e[        US9nU$ )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0H;;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      si    # 	     ( F 30!!  ====NN	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   