a
    hb                     @   sv   d dl mZmZ ddlmZmZ ddlmZmZm	Z	 e rRd dl
mZ ddlmZ ee	dd	d
G dd deZdS )    )AnyUnion   )add_end_docstringsis_vision_available   )GenericTensorPipelinebuild_pipeline_init_args)Image)
load_imageT)Zhas_image_processora  
        image_processor_kwargs (`dict`, *optional*):
                Additional dictionary of keyword arguments passed along to the image processor e.g.
                {"size": {"height": 100, "width": 100}}
        pool (`bool`, *optional*, defaults to `False`):
            Whether or not to return the pooled output. If `False`, the model will return the raw hidden states.
    c                       s   e Zd ZdZdZdZdZdZdddZde	e
ef ddd	Zd
d ZdddZee
ded ee
 f eee d fddZ  ZS )ImageFeatureExtractionPipelinea+  
    Image feature extraction pipeline uses no model head. This pipeline extracts the hidden states from the base
    transformer, which can be used as features in downstream tasks.

    Example:

    ```python
    >>> from transformers import pipeline

    >>> extractor = pipeline(model="google/vit-base-patch16-224", task="image-feature-extraction")
    >>> result = extractor("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png", return_tensors=True)
    >>> result.shape  # This is a tensor of shape [1, sequence_lenth, hidden_dimension] representing the input image.
    torch.Size([1, 197, 768])
    ```

    Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial)

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

    All vision models may be used for this pipeline. See a list of all models, including community-contributed models on
    [huggingface.co/models](https://huggingface.co/models).
    FTNc                 K   sR   |d u ri n|}i }|d ur$||d< |d ur4||d< d|v rH|d |d< |i |fS )Npoolreturn_tensorstimeout )selfimage_processor_kwargsr   r   kwargsZpreprocess_paramsZpostprocess_paramsr   r   k/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/pipelines/image_feature_extraction.py_sanitize_parameters5   s    z3ImageFeatureExtractionPipeline._sanitize_parameters)returnc                 K   s>   t ||d}| j|fd| ji|}| jdkr:|| j}|S )N)r   r   pt)r   Zimage_processor	frameworktoZdtype)r   imager   r   model_inputsr   r   r   
preprocessC   s
    
z)ImageFeatureExtractionPipeline.preprocessc                 C   s   | j f i |}|S )N)model)r   r   model_outputsr   r   r   _forwardJ   s    z'ImageFeatureExtractionPipeline._forwardc                 C   sj   |d ur|nd}|r.d|vr$t d|d }n|d }|r>|S | jdkrP| S | jdkrf|  S d S )NFZpooler_outputzeNo pooled output was returned. Make sure the model has a `pooler` layer when using the `pool` option.r   r   tf)
ValueErrorr   tolistnumpy)r   r   r   r   outputsr   r   r   postprocessN   s    


z*ImageFeatureExtractionPipeline.postprocesszImage.Image)argsr   r   c                    s   t  j|i |S )a  
        Extract the features of the input(s).

        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:
            A nested list of `float`: The features computed by the model.
        )super__call__)r   r'   r   	__class__r   r   r)   b   s    z'ImageFeatureExtractionPipeline.__call__)NNN)N)NF)__name__
__module____qualname____doc__Z_load_processorZ_load_image_processorZ_load_feature_extractorZ_load_tokenizerr   dictstrr   r   r    r&   r   listr   r)   __classcell__r   r   r*   r   r      s   

r   N)typingr   r   utilsr   r   baser   r	   r
   ZPILr   Zimage_utilsr   r   r   r   r   r   <module>   s   
