
    rh                         d dl mZ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)    )AnyUnionoverloadN   )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            	            e Zd ZdZdZdZdZdZ fdZd Z	e
deedf   ded	dfd
       Ze
deee   ed   f   ded	ed   fd       Zdeeee   ded   f   de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).
    FTc                 f    t        |   |i | t        | d       | j                  t               y )Nvision)super__init__r   check_model_typer   )selfargskwargs	__class__s      x/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/transformers/pipelines/image_to_image.pyr   zImageToImagePipeline.__init__K   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_parametersP   sP    +1)+<i(& *0*=N;' .2DDDr   imageszImage.Imager   returnc                      y Nr#   r   r(   r   s      r   __call__zImageToImagePipeline.__call__\   s    [^r   c                      y r+   r#   r,   s      r   r-   zImageToImagePipeline.__call___   s    mpr   c                 $    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   r-   )r   r(   r   r   s      r   r-   zImageToImagePipeline.__call__b   s    4 w1&11r   c                 *     | j                   di |}|S )Nr#   )model)r   model_inputsmodel_outputss      r   _forwardzImageToImagePipeline._forward~   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.preprocess   sJ    5'2%%eWT%J>>T!YYt//0Fr   c                    g }d|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@)r@   datasqueezefloatcpuclamp_numpynpmoveaxisroundastypeuint8appendr   	fromarraylen)r   r3   r(   outputsoutputs        r   postprocessz ImageToImagePipeline.postprocess   s    },#22G 	3F[[((*002668??1EKKMF[[rBFun++-44RXX>FMM%//&12		3 Vqv7fQi7r   r+   )__name__
__module____qualname____doc___load_processor_load_image_processor_load_feature_extractor_load_tokenizerr   r'   r   r   strr   r-   listr4   r>   rT   __classcell__)r   s   @r   r   r   '   s    : O #OF

E ^uS-%78^C^M^ ^puT#Y]0C%CDpPSpX\]jXkp p2CcM4;NNO2[^2	}d=11	228
8r   r   )typingr   r   r   rI   rJ   utilsr   r   r	   r
   r   baser   r   PILr   image_utilsr   models.auto.modeling_autor   
get_loggerrU   loggerr   r#   r   r   <module>rh      sr    ( '   5 (R			H	% ,FGk88 k8 Hk8r   