



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_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


_lowercase												=		logging.get_logger(__name__)



class     _UpperCAmelCase			(       lowercase_     ):
     UpperCamelCase__		    =				['''pixel_values''']




     def __init__(				self ,						a__ = True ,						a__ = None ,						a__ = PILImageResampling.BILINEAR ,						a__ = True ,						a__ = None ,						a__ = True ,						a__ = 1 / 2_5_5 ,						a__ = True ,						a__ = None ,						a__ = None ,						**a__ ,						):
         super().__init__(**UpperCamelCase__)
         A__		 =					size if size is not None else {'''shortest_edge''': 2_5_6}
         A__		 =					get_size_dict(UpperCamelCase__ ,						default_to_square=UpperCamelCase__)
         A__		 =					crop_size if crop_size is not None else {'''height''': 2_2_4, '''width''': 2_2_4}
         A__		 =					get_size_dict(UpperCamelCase__)
         A__		 =					do_resize
         A__		 =					size
         A__		 =					resample
         A__		 =					do_center_crop
         A__		 =					crop_size
         A__		 =					do_rescale
         A__		 =					rescale_factor
         A__		 =					do_normalize
         A__		 =					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
         A__		 =					image_std if image_std is not None else IMAGENET_STANDARD_STD




     def 		snake_case_		(				self ,						a__ ,						a__ ,						a__ = PILImageResampling.BICUBIC ,						a__ = None ,						**a__ ,						):
         A__		 =					get_size_dict(UpperCamelCase__ ,						default_to_square=UpperCamelCase__)
         if "shortest_edge" not in size:
             raise ValueError(F"The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}")
         A__		 =					get_resize_output_image_size(UpperCamelCase__ ,						size=size['''shortest_edge'''] ,						default_to_square=UpperCamelCase__)
         return resize(UpperCamelCase__ ,						size=UpperCamelCase__ ,						resample=UpperCamelCase__ ,						data_format=UpperCamelCase__ ,						**UpperCamelCase__)




     def 		snake_case_		(				self ,						a__ ,						a__ ,						a__ = None ,						**a__ ,						):
         A__		 =					get_size_dict(UpperCamelCase__)
         return center_crop(UpperCamelCase__ ,						size=(size['''height'''], size['''width''']) ,						data_format=UpperCamelCase__ ,						**UpperCamelCase__)




     def 		snake_case_		(				self ,						a__ ,						a__ ,						a__ = None ,						**a__):
         return rescale(UpperCamelCase__ ,						scale=UpperCamelCase__ ,						data_format=UpperCamelCase__ ,						**UpperCamelCase__)




     def 		snake_case_		(				self ,						a__ ,						a__ ,						a__ ,						a__ = None ,						**a__ ,						):
         return normalize(UpperCamelCase__ ,						mean=UpperCamelCase__ ,						std=UpperCamelCase__ ,						data_format=UpperCamelCase__ ,						**UpperCamelCase__)




     def 		snake_case_		(				self ,						a__ ,						a__ = None ,						a__ = None ,						a__ = None ,						a__ = None ,						a__ = None ,						a__ = None ,						a__ = None ,						a__ = None ,						a__ = None ,						a__ = None ,						a__ = None ,						a__ = ChannelDimension.FIRST ,						**a__ ,						):
         A__		 =					do_resize if do_resize is not None else self.do_resize
         A__		 =					size if size is not None else self.size
         A__		 =					get_size_dict(UpperCamelCase__ ,						default_to_square=UpperCamelCase__)
         A__		 =					resample if resample is not None else self.resample
         A__		 =					do_center_crop if do_center_crop is not None else self.do_center_crop
         A__		 =					crop_size if crop_size is not None else self.crop_size
         A__		 =					get_size_dict(UpperCamelCase__)
         A__		 =					do_rescale if do_rescale is not None else self.do_rescale
         A__		 =					rescale_factor if rescale_factor is not None else self.rescale_factor
         A__		 =					do_normalize if do_normalize is not None else self.do_normalize
         A__		 =					image_mean if image_mean is not None else self.image_mean
         A__		 =					image_std if image_std is not None else self.image_std

         A__		 =					make_list_of_images(UpperCamelCase__)

         if not valid_images(UpperCamelCase__):
             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.''')

         if do_normalize and (image_mean is None or image_std is None):
             raise ValueError('''Image mean and std must be specified if do_normalize is True.''')

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

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

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

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

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

         A__		 =					[to_channel_dimension_format(UpperCamelCase__ ,						UpperCamelCase__) for image in images]

         A__		 =					{'''pixel_values''': images}
         return BatchFeature(data=UpperCamelCase__ ,						tensor_type=UpperCamelCase__)





# 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     _UpperCAmelCase			(       A__     ):
     UpperCamelCase__		    =				'''Salesforce/blip-image-captioning-base'''
     UpperCamelCase__		    =				(
         '''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.'''
     )
     UpperCamelCase__		    =				'''image_captioner'''
     UpperCamelCase__		    =				AutoModelForVisionaSeq

     UpperCamelCase__		    =				['''image''']
     UpperCamelCase__		    =				['''text''']




     def __init__(				self ,						*a__ ,						**a__):
         requires_backends(self ,						['''vision'''])
         super().__init__(*a__ ,						**a__)




     def 		snake_case_		(				self ,						a__):
         return self.pre_processor(images=a__ ,						return_tensors='''pt''')




     def 		snake_case_		(				self ,						a__):
         return self.model.generate(**a__)




     def 		snake_case_		(				self ,						a__):
         return self.pre_processor.batch_decode(a__ ,						skip_special_tokens=a__)[0].strip()
