Ë
    T^(hž  ã                   óæ   — d dl mZmZ d dlZddlmZmZmZm	Z	m
Z
 ddlmZmZ  e«       rd dlmZ ddlmZ  e«       rdd	lmZ  e	j(                  e«      Z e ed
¬«      «       G d„ de«      «       Zy)é    )ÚListÚUnionNé   )Úadd_end_docstringsÚis_torch_availableÚis_vision_availableÚloggingÚrequires_backendsé   )ÚPipelineÚbuild_pipeline_init_args)ÚImage)Ú
load_image)Ú&MODEL_FOR_IMAGE_TO_IMAGE_MAPPING_NAMEST)Úhas_image_processorc                   óz   ‡ — e Zd ZdZˆ fd„Zd„ Zdeeee   ded   f   deded   f   fˆ fd„Z	d„ Z
dd	„Zd
„ Zˆ xZS )ÚImageToImagePipelineao  
    Image to Image pipeline using any `AutoModelForImageToImage`. This pipeline generates an image based on a previous
    image input.

    Example:

    ```python
    >>> from PIL import Image
    >>> import requests

    >>> from transformers import pipeline

    >>> upscaler = pipeline("image-to-image", model="caidas/swin2SR-classical-sr-x2-64")
    >>> img = Image.open(requests.get("http://images.cocodataset.org/val2017/000000039769.jpg", stream=True).raw)
    >>> img = img.resize((64, 64))
    >>> upscaled_img = upscaler(img)
    >>> img.size
    (64, 64)

    >>> upscaled_img.size
    (144, 144)
    ```

    This image to image pipeline can currently be loaded from [`pipeline`] using the following task identifier:
    `"image-to-image"`.

    See the list of available models on [huggingface.co/models](https://huggingface.co/models?filter=image-to-image).
    c                 óf   •— t        ‰| �  |i |¤Ž t        | d«       | j                  t        «       y )NÚvision)ÚsuperÚ__init__r
   Úcheck_model_typer   )ÚselfÚargsÚkwargsÚ	__class__s      €úc/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/pipelines/image_to_image.pyr   zImageToImagePipeline.__init__F   s.   ø€ Ü‰Ñ˜$Ð) &Ò)Ü˜$ Ô)Ø×ÑÔDÕEó    c                 óH   — i }i }i }d|v r|d   |d<   d|v r|d   |d<   |||fS )NÚtimeoutÚ	head_mask© )r   r   Úpreprocess_paramsÚpostprocess_paramsÚforward_paramss        r   Ú_sanitize_parametersz)ImageToImagePipeline._sanitize_parametersK   sP   € ØÐØÐØˆà˜ÑØ+1°)Ñ+<Ð˜iÑ(Ø˜&Ñ Ø*0°Ñ*=ˆN˜;Ñ'à  .Ð2DÐDÐDr   ÚimageszImage.ImageÚreturnc                 ó$   •— t        ‰| �  |fi |¤ŽS )a›  
        Transform the image(s) passed as inputs.

        Args:
            images (`str`, `List[str]`, `PIL.Image` or `List[PIL.Image]`):
                The pipeline handles three types of images:

                - A string containing a http link pointing to an image
                - A string containing a local path to an image
                - An image loaded in PIL directly

                The pipeline accepts either a single image or a batch of images, which must then be passed as a string.
                Images in a batch must all be in the same format: all as http links, all as local paths, or all as PIL
                images.
            timeout (`float`, *optional*, defaults to None):
                The maximum time in seconds to wait for fetching images from the web. If None, no timeout is used and
                the call may block forever.

        Return:
            An image (Image.Image) or a list of images (List["Image.Image"]) containing result(s). If the input is a
            single image, the return will be also a single image, if the input is a list of several images, it will
            return a list of transformed images.
        )r   Ú__call__)r   r'   r   r   s      €r   r*   zImageToImagePipeline.__call__W   s   ø€ ô4 ‰wÑ Ñ1¨&Ñ1Ð1r   c                 ó*   —  | j                   di |¤Ž}|S )Nr"   )Úmodel)r   Úmodel_inputsÚmodel_outputss      r   Ú_forwardzImageToImagePipeline._forwards   s   € Ø"˜Ÿ
™
Ñ2 \Ñ2ˆØÐr   c                 óœ   — t        ||¬«      }| j                  |gd¬«      }| j                  dk(  r|j                  | j                  «      }|S )N)r    Úpt)r'   Úreturn_tensors)r   Úimage_processorÚ	frameworkÚtoÚtorch_dtype)r   Úimager    Úinputss       r   Ú
preprocesszImageToImagePipeline.preprocessw   sJ   € Ü˜5¨'Ô2ˆØ×%Ñ%¨e¨WÀTÐ%ÓJˆØ�>‰>˜TÒ!Ø—Y‘Y˜t×/Ñ/Ó0ˆFØˆr   c                 óú  — g }d|j                  «       v r|j                  }D ]Â  }|j                  j                  «       j	                  «       j                  «       j                  dd«      j                  «       }t        j                  |dd¬«      }|dz  j                  «       j                  t        j                  «      }|j                  t        j                  |«      «       ŒÄ t!        |«      dkD  r|S |d   S )NÚreconstructionr   r   éÿÿÿÿ)ÚsourceÚdestinationg     ào@)Úkeysr;   ÚdataÚsqueezeÚfloatÚcpuÚclamp_ÚnumpyÚnpÚmoveaxisÚroundÚastypeÚuint8Úappendr   Ú	fromarrayÚlen)r   r.   r'   ÚoutputsÚoutputs        r   Úpostprocessz ImageToImagePipeline.postprocess~   sÏ   € ØˆØ˜}×1Ñ1Ó3Ñ3Ø#×2Ñ2ˆGØò 	3ˆFØ—[‘[×(Ñ(Ó*×0Ñ0Ó2×6Ñ6Ó8×?Ñ?ÀÀ1ÓE×KÑKÓMˆFÜ—[‘[ °¸rÔBˆFØ˜u‘n×+Ñ+Ó-×4Ñ4´R·X±XÓ>ˆFØ�M‰Mœ%Ÿ/™/¨&Ó1Õ2ð		3ô ˜V› qšˆvÐ7¨f°Q©iÐ7r   )N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r&   r   Ústrr   r*   r/   r9   rP   Ú__classcell__)r   s   @r   r   r   '   s^   ø„ ñô:Fò

Eð2Ø˜C  c¡¨M¸4ÀÑ;NÐNÑOð2à	ˆ}˜d =Ñ1Ð1Ñ	2õ2ò8óö
8r   r   )Útypingr   r   rE   rF   Úutilsr   r   r   r	   r
   Úbaser   r   ÚPILr   Úimage_utilsr   Úmodels.auto.modeling_autor   Ú
get_loggerrQ   Úloggerr   r"   r   r   ú<module>r_      so   ð÷ ã ÷õ ÷ 5ñ ÔÝå(áÔÝRà	ˆ×	Ñ	˜HÓ	%€ñ Ñ,ÀÔFÓGô`8˜8ó `8ó Hñ`8r   