






"""simple docstring"""





from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_DEFAULT_MEAN,
    IMAGENET_DEFAULT_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_batched,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


_SCREAMING_SNAKE_CASE					:				Any					    =						logging.get_logger(__name__)






class      a						(					__snake_case		):
    SCREAMING_SNAKE_CASE						:     Optional[int]						= ["""pixel_values"""]






    def __init__(       self     :							Dict       ,						__SCREAMING_SNAKE_CASE     :							bool = True       ,						__SCREAMING_SNAKE_CASE     :							Optional[Dict[str, int]] = None       ,						__SCREAMING_SNAKE_CASE     :							PILImageResampling = PILImageResampling.BICUBIC       ,						__SCREAMING_SNAKE_CASE     :							bool = True       ,						__SCREAMING_SNAKE_CASE     :							bool = True       ,						__SCREAMING_SNAKE_CASE     :							Union[int, float] = 1 / 255       ,						__SCREAMING_SNAKE_CASE     :							Dict[str, int] = None       ,						__SCREAMING_SNAKE_CASE     :							bool = True       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[float, List[float]]] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[float, List[float]]] = None       ,						**__SCREAMING_SNAKE_CASE     :							Optional[Any]       ,						)     ->		None:
      super().__init__(**__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						size if size is not None else {'height': 224, 'width': 224}
      lowerCamelCase_         =						get_size_dict(__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						crop_size if crop_size is not None else {'height': 224, 'width': 224}
      lowerCamelCase_         =						get_size_dict(__SCREAMING_SNAKE_CASE       ,						default_to_square=__SCREAMING_SNAKE_CASE       ,						param_name='crop_size'					)

      lowerCamelCase_         =						do_resize
      lowerCamelCase_         =						do_rescale
      lowerCamelCase_         =						do_normalize
      lowerCamelCase_         =						do_center_crop
      lowerCamelCase_         =						crop_size
      lowerCamelCase_         =						size
      lowerCamelCase_         =						resample
      lowerCamelCase_         =						rescale_factor
      lowerCamelCase_         =						image_mean if image_mean is not None else IMAGENET_DEFAULT_MEAN
      lowerCamelCase_         =						image_std if image_std is not None else IMAGENET_DEFAULT_STD






    def    UpperCamelCase       (       self     :							Tuple       ,						__SCREAMING_SNAKE_CASE     :							np.ndarray       ,						__SCREAMING_SNAKE_CASE     :							Dict[str, int]       ,						__SCREAMING_SNAKE_CASE     :							PILImageResampling = PILImageResampling.BILINEAR       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[str, ChannelDimension]] = None       ,						**__SCREAMING_SNAKE_CASE     :							Union[str, Any]       ,						)     ->		np.ndarray:
      lowerCamelCase_         =						get_size_dict(__SCREAMING_SNAKE_CASE					)

      if "shortest_edge" in size:
        lowerCamelCase_         =						get_resize_output_image_size(__SCREAMING_SNAKE_CASE       ,						size=size['shortest_edge']       ,						default_to_square=__SCREAMING_SNAKE_CASE					)
        # size = get_resize_output_image_size(image, size["shortest_edge"], size["longest_edge"])
      elif "height" in size and "width" in size:
        lowerCamelCase_         =						(size['height'], size['width'])
      else:
        raise ValueError(F'''Size must contain \'height\' and \'width\' keys or \'shortest_edge\' key. Got {size.keys()}'''					)
      return resize(__SCREAMING_SNAKE_CASE       ,						size=__SCREAMING_SNAKE_CASE       ,						resample=__SCREAMING_SNAKE_CASE       ,						data_format=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							Tuple       ,						__SCREAMING_SNAKE_CASE     :							np.ndarray       ,						__SCREAMING_SNAKE_CASE     :							Dict[str, int]       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[str, ChannelDimension]] = None       ,						**__SCREAMING_SNAKE_CASE     :							Any       ,						)     ->		np.ndarray:
      lowerCamelCase_         =						get_size_dict(__SCREAMING_SNAKE_CASE					)
      if "height" not in size or "width" not in size:
        raise ValueError(F'''The `size` parameter must contain the keys (height, width). Got {size.keys()}'''					)
      return center_crop(__SCREAMING_SNAKE_CASE       ,						size=(size['height'], size['width'])       ,						data_format=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							Optional[Any]       ,						__SCREAMING_SNAKE_CASE     :							np.ndarray       ,						__SCREAMING_SNAKE_CASE     :							float       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[str, ChannelDimension]] = None       ,						**__SCREAMING_SNAKE_CASE     :							List[str]					)     ->		np.ndarray:
      return rescale(__SCREAMING_SNAKE_CASE       ,						scale=__SCREAMING_SNAKE_CASE       ,						data_format=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							Optional[Any]       ,						__SCREAMING_SNAKE_CASE     :							np.ndarray       ,						__SCREAMING_SNAKE_CASE     :							Union[float, List[float]]       ,						__SCREAMING_SNAKE_CASE     :							Union[float, List[float]]       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[str, ChannelDimension]] = None       ,						**__SCREAMING_SNAKE_CASE     :							Dict       ,						)     ->		np.ndarray:
      return normalize(__SCREAMING_SNAKE_CASE       ,						mean=__SCREAMING_SNAKE_CASE       ,						std=__SCREAMING_SNAKE_CASE       ,						data_format=__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							List[str]       ,						__SCREAMING_SNAKE_CASE     :							ImageInput       ,						__SCREAMING_SNAKE_CASE     :							Optional[bool] = None       ,						__SCREAMING_SNAKE_CASE     :							Dict[str, int] = None       ,						__SCREAMING_SNAKE_CASE     :							PILImageResampling = None       ,						__SCREAMING_SNAKE_CASE     :							bool = None       ,						__SCREAMING_SNAKE_CASE     :							int = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[bool] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[float] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[bool] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[float, List[float]]] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[float, List[float]]] = None       ,						__SCREAMING_SNAKE_CASE     :							Optional[Union[str, TensorType]] = None       ,						__SCREAMING_SNAKE_CASE     :							Union[str, ChannelDimension] = ChannelDimension.FIRST       ,						**__SCREAMING_SNAKE_CASE     :							Optional[Any]       ,						)     ->		BatchFeature:
      lowerCamelCase_         =						do_resize if do_resize is not None else self.do_resize
      lowerCamelCase_         =						do_rescale if do_rescale is not None else self.do_rescale
      lowerCamelCase_         =						do_normalize if do_normalize is not None else self.do_normalize
      lowerCamelCase_         =						do_center_crop if do_center_crop is not None else self.do_center_crop
      lowerCamelCase_         =						crop_size if crop_size is not None else self.crop_size
      lowerCamelCase_         =						get_size_dict(__SCREAMING_SNAKE_CASE       ,						param_name='crop_size'       ,						default_to_square=__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						resample if resample is not None else self.resample
      lowerCamelCase_         =						rescale_factor if rescale_factor is not None else self.rescale_factor
      lowerCamelCase_         =						image_mean if image_mean is not None else self.image_mean
      lowerCamelCase_         =						image_std if image_std is not None else self.image_std

      lowerCamelCase_         =						size if size is not None else self.size
      lowerCamelCase_         =						get_size_dict(__SCREAMING_SNAKE_CASE					)

      if not is_batched(__SCREAMING_SNAKE_CASE					):
        lowerCamelCase_         =						[images]

      if not valid_images(__SCREAMING_SNAKE_CASE					):
        raise ValueError(
            'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
            'torch.Tensor, tf.Tensor or jax.ndarray.'					)

      if do_resize and size is None:
        raise ValueError('Size must be specified if do_resize is True.'					)

      if do_center_crop and crop_size is None:
        raise ValueError('Crop size must be specified if do_center_crop is True.'					)

      if do_rescale and rescale_factor is None:
        raise ValueError('Rescale factor must be specified if do_rescale is True.'					)

      # All transformations expect numpy arrays.
      lowerCamelCase_         =						[to_numpy_array(__SCREAMING_SNAKE_CASE					) for image in images]

      if do_resize:
        lowerCamelCase_         =						[self.resize(image=__SCREAMING_SNAKE_CASE       ,						size=__SCREAMING_SNAKE_CASE       ,						resample=__SCREAMING_SNAKE_CASE					) for image in images]

      if do_center_crop:
        lowerCamelCase_         =						[self.center_crop(image=__SCREAMING_SNAKE_CASE       ,						size=__SCREAMING_SNAKE_CASE					) for image in images]

      if do_rescale:
        lowerCamelCase_         =						[self.rescale(image=__SCREAMING_SNAKE_CASE       ,						scale=__SCREAMING_SNAKE_CASE					) for image in images]

      if do_normalize:
        lowerCamelCase_         =						[self.normalize(image=__SCREAMING_SNAKE_CASE       ,						mean=__SCREAMING_SNAKE_CASE       ,						std=__SCREAMING_SNAKE_CASE					) for image in images]

      lowerCamelCase_         =						[to_channel_dimension_format(__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE					) for image in images]

      lowerCamelCase_         =						{'pixel_values': images}
      return BatchFeature(data=__SCREAMING_SNAKE_CASE       ,						tensor_type=__SCREAMING_SNAKE_CASE					)












"""simple docstring"""





# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
  from PIL import Image






class      a						(					__snake_case		):
    SCREAMING_SNAKE_CASE						:     Dict						= """Salesforce/blip-image-captioning-base"""
    SCREAMING_SNAKE_CASE						:     int						= (
        """This is a tool that generates a description of an image. It takes an input named `image` which should be the """
        """image to caption, and returns a text that contains the description in English."""
    )
    SCREAMING_SNAKE_CASE						:     Optional[int]						= """image_captioner"""
    SCREAMING_SNAKE_CASE						:     Optional[Any]						= AutoModelForVisionaSeq

    SCREAMING_SNAKE_CASE						:     int						= ["""image"""]
    SCREAMING_SNAKE_CASE						:     Optional[Any]						= ["""text"""]






    def __init__(       self     :							Optional[Any]       ,						*__SCREAMING_SNAKE_CASE     :							Optional[Any]       ,						**__SCREAMING_SNAKE_CASE     :							List[Any]					)     ->		Optional[Any]:
      requires_backends(self       ,						['vision']					)
      super().__init__(*__SCREAMING_SNAKE_CASE       ,						**__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							List[Any]       ,						__SCREAMING_SNAKE_CASE     :							"Image"					)     ->		Dict:
      return self.pre_processor(images=__SCREAMING_SNAKE_CASE       ,						return_tensors='pt'					)






    def    UpperCamelCase       (       self     :							List[str]       ,						__SCREAMING_SNAKE_CASE     :							Tuple					)     ->		str:
      return self.model.generate(**__SCREAMING_SNAKE_CASE					)






    def    UpperCamelCase       (       self     :							Union[str, Any]       ,						__SCREAMING_SNAKE_CASE     :							List[str]					)     ->		Optional[Any]:
      return self.pre_processor.batch_decode(__SCREAMING_SNAKE_CASE       ,						skip_special_tokens=__SCREAMING_SNAKE_CASE					)[0].strip()




