
    +hn'                     b   S SK JrJrJrJr  S SKrS SKrS SK	r	SSK
Jr  SSKJr  SSKJrJrJr  SSKJr  SS	KJrJr  \R.                  " \5      rS
\\\R4                  R4                  \	R6                  4   4S jrS\\\R4                  R4                  \	R6                  4   4S jr " S S\5      rg)    )ListOptionalTupleUnionN   )UNet2DModel)RePaintScheduler)PIL_INTERPOLATION	deprecatelogging)randn_tensor   )DiffusionPipelineImagePipelineOutputimagec           
      J   Sn[        SSUSS9  [        U [        R                  5      (       a  U $ [        U [        R
                  R
                  5      (       a  U /n [        U S   [        R
                  R
                  5      (       a  U S   R                  u  p#S X#4 5       u  p#U  Vs/ s H6  n[        R                  " UR                  X#4[        S   S	95      S S S 24   PM8     n n[        R                  " U SS
9n [        R                  " U 5      R                  [        R                  5      S-  n U R                  SSSS5      n SU -  S-
  n [        R                  " U 5      n U $ [        U S   [        R                  5      (       a  [        R                   " U SS9n U $ s  snf )NzThe preprocess method is deprecated and will be removed in diffusers 1.0.0. Please use VaeImageProcessor.preprocess(...) instead
preprocessz1.0.0F)standard_warnr   c              3   0   #    U  H  oUS -  -
  v   M     g7f)   N .0xs     q/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/deprecated/repaint/pipeline_repaint.py	<genexpr>$_preprocess_image.<locals>.<genexpr>+   s     *6aAE	6   lanczosresampleaxis     o@r         g       @g      ?dim)r   
isinstancetorchTensorPILImagesizenparrayresizer
   concatenateastypefloat32	transpose
from_numpycat)r   deprecation_messagewhis        r   _preprocess_imager<   !   s_    ]lG%8N%&&	E399??	+	+%(CIIOO,,Qx}}*A6*ejkej`a!((A64Ei4P(QRSWYZSZ[ejku1-&&rzz2U:1a+ec!  ' L 
E!Hell	+	+		%Q'L ls   )=F maskc           
         [        U [        R                  5      (       a  U $ [        U [        R                  R                  5      (       a  U /n [        U S   [        R                  R                  5      (       a  U S   R
                  u  pS X4 5       u  pU  Vs/ s HE  n[        R                  " UR                  S5      R                  X4[        S   S95      S S S 24   PMG     n n[        R                  " U SS9n U R                  [        R                  5      S-  n SX S:  '   S	X S:  '   [        R                  " U 5      n U $ [        U S   [        R                  5      (       a  [        R                  " U SS
9n U $ s  snf )Nr   c              3   0   #    U  H  oUS -  -
  v   M     g7f)    Nr   r   s     r   r   #_preprocess_mask.<locals>.<genexpr>@   s     +FqAF
Fr   Lnearestr    r"   r$         ?r%   r'   )r)   r*   r+   r,   r-   r.   r/   r0   convertr1   r
   r2   r3   r4   r6   r7   )r=   r9   r:   ms       r   _preprocess_maskrG   8   s?   $%%	D#))//	*	*v$q'399??++Aw||+QF+quvqulm3..v@QR[@\.]^_cef_fgquv~~d+{{2::&.CZS[% K 
DGU\\	*	*yy1%K ws   AE?c                     ^  \ rS rSr% Sr\\S'   \\S'   SrS\S\4U 4S jjr	\
R                  " 5              SS\\
R                  \R                  R                  4   S\\
R                  \R                  R                  4   S\S	\S
\S\S\\\
R&                  \\
R&                     4      S\\   S\S\\\4   4S jj5       rSrU =r$ )RePaintPipelineL   a  
Pipeline for image inpainting using RePaint.

This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods
implemented for all pipelines (downloading, saving, running on a particular device, etc.).

Parameters:
    unet ([`UNet2DModel`]):
        A `UNet2DModel` to denoise the encoded image latents.
    scheduler ([`RePaintScheduler`]):
        A `RePaintScheduler` to be used in combination with `unet` to denoise the encoded image.
unet	schedulerc                 @   > [         TU ]  5         U R                  XS9  g )N)rK   rL   )super__init__register_modules)selfrK   rL   	__class__s      r   rO   RePaintPipeline.__init__^   s    4=    r   
mask_imagenum_inference_stepsetajump_lengthjump_n_sample	generatoroutput_typereturn_dictreturnc
           	         Un
[        U
5      n
U
R                  U R                  U R                  R                  S9n
[        U5      nUR                  U R                  U R                  R                  S9nU
R                  S   n[        U[        5      (       a*  [        U5      U:w  a  [        S[        U5       SU S35      eU
R                  n[        XU R                  U R                  R                  S9nU R                  R                  X5X`R                  5        X@R                  l        U R                  R                  S   S-   n[        U[        5      (       a  US   OUn[!        U R#                  U R                  R                  5      5       Hl  u  pX:  aD  U R                  X5      R$                  nU R                  R'                  UXXU5      R(                  nOU R                  R+                  XU5      nUnMn     US-  S	-   R-                  SS5      nUR/                  5       R1                  SSS
S5      R3                  5       nUS:X  a  U R5                  U5      nU	(       d  U4$ [7        US9$ )a  
The call function to the pipeline for generation.

Args:
    image (`torch.Tensor` or `PIL.Image.Image`):
        The original image to inpaint on.
    mask_image (`torch.Tensor` or `PIL.Image.Image`):
        The mask_image where 0.0 define which part of the original image to inpaint.
    num_inference_steps (`int`, *optional*, defaults to 1000):
        The number of denoising steps. More denoising steps usually lead to a higher quality image at the
        expense of slower inference.
    eta (`float`):
        The weight of the added noise in a diffusion step. Its value is between 0.0 and 1.0; 0.0 corresponds to
        DDIM and 1.0 is the DDPM scheduler.
    jump_length (`int`, *optional*, defaults to 10):
        The number of steps taken forward in time before going backward in time for a single jump ("j" in
        RePaint paper). Take a look at Figure 9 and 10 in the
        [paper](https://huggingface.co/papers/2201.09865).
    jump_n_sample (`int`, *optional*, defaults to 10):
        The number of times to make a forward time jump for a given chosen time sample. Take a look at Figure 9
        and 10 in the [paper](https://huggingface.co/papers/2201.09865).
    generator (`torch.Generator`, *optional*):
        A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make
        generation deterministic.
    output_type (`str`, `optional`, defaults to `"pil"`):
        The output format of the generated image. Choose between `PIL.Image` or `np.array`.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`ImagePipelineOutput`] instead of a plain tuple.

Example:

```py
>>> from io import BytesIO
>>> import torch
>>> import PIL
>>> import requests
>>> from diffusers import RePaintPipeline, RePaintScheduler


>>> def download_image(url):
...     response = requests.get(url)
...     return PIL.Image.open(BytesIO(response.content)).convert("RGB")


>>> img_url = "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/celeba_hq_256.png"
>>> mask_url = "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/mask_256.png"

>>> # Load the original image and the mask as PIL images
>>> original_image = download_image(img_url).resize((256, 256))
>>> mask_image = download_image(mask_url).resize((256, 256))

>>> # Load the RePaint scheduler and pipeline based on a pretrained DDPM model
>>> scheduler = RePaintScheduler.from_pretrained("google/ddpm-ema-celebahq-256")
>>> pipe = RePaintPipeline.from_pretrained("google/ddpm-ema-celebahq-256", scheduler=scheduler)
>>> pipe = pipe.to("cuda")

>>> generator = torch.Generator(device="cuda").manual_seed(0)
>>> output = pipe(
...     image=original_image,
...     mask_image=mask_image,
...     num_inference_steps=250,
...     eta=0.0,
...     jump_length=10,
...     jump_n_sample=10,
...     generator=generator,
... )
>>> inpainted_image = output.images[0]
```

Returns:
    [`~pipelines.ImagePipelineOutput`] or `tuple`:
        If `return_dict` is `True`, [`~pipelines.ImagePipelineOutput`] is returned, otherwise a `tuple` is
        returned where the first element is a list with the generated images.
)devicedtyper   z/You have passed a list of generators of length z+, but requested an effective batch size of z@. Make sure the batch size matches the length of the generators.)rZ   r_   r`   r%   r&   rD   r   pil)images)r<   to_execution_devicerK   r`   rG   shaper)   listlen
ValueErrorr   rL   set_timestepsrW   	timesteps	enumerateprogress_barsamplestepprev_sample	undo_stepclampcpupermutenumpynumpy_to_pilr   )rQ   r   rU   rV   rW   rX   rY   rZ   r[   r\   original_image
batch_sizeimage_shapet_lastr;   tmodel_outputs                    r   __call__RePaintPipeline.__call__b   s.   p *>:'**$2H2HPTPYPYP_P_*`%j1
]]$*@*@		]X
#))!,
 i&&3y>Z+GA#i.AQ R&<'gi 
 %**[dF\F\dhdmdmdsdst 	$$%8}VlVlm ))!,q0$.y$$?$?IaLY	d//0H0HIJDAz#yy299++L!N`ijvv 00	JF K S''1-		##Aq!Q/557%%%e,E8O"%00rT   r   )   g        
   r   Nra   T)__name__
__module____qualname____firstlineno____doc__r   __annotations__r	   model_cpu_offload_seqrO   r*   no_gradr   r+   r,   r-   intfloatr   	Generatorr   strboolr   r   r|   __static_attributes____classcell__)rR   s   @r   rI   rI   L   s&    ">[ >5E > ]]_
 $'MQ%* D1U\\399??23D1 %,,		78D1 !	D1
 D1 D1 D1 E%//43H"HIJD1 c]D1 D1 
"E)	*D1 D1rT   rI   )typingr   r   r   r   rt   r/   	PIL.Imager,   r*   modelsr   
schedulersr	   utilsr
   r   r   utils.torch_utilsr   pipeline_utilsr   r   
get_loggerr   loggerr-   r+   r<   rG   rI   r   rT   r   <module>r      s     0 /    " + ; ; . D 
		H	%U4%,,#FG .5syy!DE ([1' [1rT   