'''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 (
    get_resize_output_image_size,
    normalize,
    pad,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging
from ...utils.import_utils import is_vision_available


__SCREAMING_SNAKE_CASE :  List[Any]  =						logging.get_logger(__name__)


if is_vision_available():
  import PIL

class 			lowerCamelCase_			(snake_case__					):
      '''simple docstring'''

      __UpperCamelCase:						Optional[Any]      			=		["pixel_values"]




      def __init__( self		:       List[Any]		,      A		:       bool = True		,      A		:       Dict[str, int] = None		,      A		:       PILImageResampling = PILImageResampling.BILINEAR		,      A		:       bool = True		,      A		:       bool = False		,      A		:       bool = True		,      A		:       bool = True		,      A		:       Union[int, float] = 1 / 255		,      A		:       bool = True		,      A		:       Optional[Union[float, List[float]]] = None		,      A		:       Optional[Union[float, List[float]]] = None		,      **A		:       Any		,      ):
             super().__init__(**A )

             _UpperCAmelCase  :						Optional[Any]							   =					size if size is not None else {"height": 2560, "width": 1920}
             if isinstance(A		,      (tuple, list) ):
                    # The previous feature extractor size parameter was in (width, height) format
                    _UpperCAmelCase  :						str							   =					size[::-1]
             _UpperCAmelCase  :						Optional[int]							   =					get_size_dict(A )

             _UpperCAmelCase  :						List[str]							   =					do_resize
             _UpperCAmelCase  :						Optional[Any]							   =					size
             _UpperCAmelCase  :						str							   =					resample
             _UpperCAmelCase  :						Optional[Any]							   =					do_thumbnail
             _UpperCAmelCase  :						Union[str, Any]							   =					do_align_long_axis
             _UpperCAmelCase  :						Optional[Any]							   =					do_pad
             _UpperCAmelCase  :						List[Any]							   =					do_rescale
             _UpperCAmelCase  :						str							   =					rescale_factor
             _UpperCAmelCase  :						int							   =					do_normalize
             _UpperCAmelCase  :						Tuple							   =					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
             _UpperCAmelCase  :						Tuple							   =					image_std if image_std is not None else IMAGENET_STANDARD_STD




      def   _A			( self		:       str		,      A		:       np.ndarray		,      A		:       Dict[str, int]		,      A		:       Optional[Union[str, ChannelDimension]] = None ):
             _UpperCAmelCase   ,						_UpperCAmelCase  :						Optional[int]							   =					get_image_size(A )
             _UpperCAmelCase   ,						_UpperCAmelCase  :						str							   =					size["height"], size["width"]

             if (output_width < output_height and input_width > input_height) or (
                 output_width > output_height and input_width < input_height
             ):
                    _UpperCAmelCase  :						Optional[Any]							   =					np.rotaa(A		,      3 )

             if data_format is not None:
                    _UpperCAmelCase  :						str							   =					to_channel_dimension_format(A		,      A )

             return image




      def   _A			( self		:       Tuple		,      A		:       np.ndarray		,      A		:       Dict[str, int]		,      A		:       bool = False		,      A		:       Optional[Union[str, ChannelDimension]] = None		,      ):
             _UpperCAmelCase   ,						_UpperCAmelCase  :						Optional[int]							   =					size["height"], size["width"]
             _UpperCAmelCase   ,						_UpperCAmelCase  :						Any							   =					get_image_size(A )

             _UpperCAmelCase  :						List[str]							   =					output_width - input_width
             _UpperCAmelCase  :						int							   =					output_height - input_height

             if random_padding:
                    _UpperCAmelCase  :						Any							   =					np.random.randint(low=0		,      high=delta_height + 1 )
                    _UpperCAmelCase  :						List[Any]							   =					np.random.randint(low=0		,      high=delta_width + 1 )
             else:
                    _UpperCAmelCase  :						Union[str, Any]							   =					delta_height // 2
                    _UpperCAmelCase  :						Optional[int]							   =					delta_width // 2

             _UpperCAmelCase  :						Dict							   =					delta_height - pad_top
             _UpperCAmelCase  :						int							   =					delta_width - pad_left

             _UpperCAmelCase  :						Any							   =					((pad_top, pad_bottom), (pad_left, pad_right))
             return pad(A		,      A		,      data_format=A )




      def   _A			( self		:       int		,      *A		:       str		,      **A		:       Tuple ):
             logger.info("pad is deprecated and will be removed in version 4.27. Please use pad_image instead." )
             return self.pad_image(*A		,      **A )




      def   _A			( self		:       int		,      A		:       np.ndarray		,      A		:       Dict[str, int]		,      A		:       PILImageResampling = PILImageResampling.BICUBIC		,      A		:       Optional[Union[str, ChannelDimension]] = None		,      **A		:       int		,      ):
             _UpperCAmelCase   ,						_UpperCAmelCase  :						Any							   =					get_image_size(A )
             _UpperCAmelCase   ,						_UpperCAmelCase  :						int							   =					size["height"], size["width"]

             # We always resize to the smallest of either the input or output size.
             _UpperCAmelCase  :						Optional[Any]							   =					min(A		,      A )
             _UpperCAmelCase  :						int							   =					min(A		,      A )

             if height == input_height and width == input_width:
                    return image

             if input_height > input_width:
                    _UpperCAmelCase  :						Union[str, Any]							   =					int(input_width * height / input_height )
             elif input_width > input_height:
                    _UpperCAmelCase  :						Union[str, Any]							   =					int(input_height * width / input_width )

             return resize(
                 A		,      size=(height, width)		,      resample=A		,      reducing_gap=2.0		,      data_format=A		,      **A )




      def   _A			( self		:       str		,      A		:       np.ndarray		,      A		:       Dict[str, int]		,      A		:       PILImageResampling = PILImageResampling.BICUBIC		,      A		:       Optional[Union[str, ChannelDimension]] = None		,      **A		:       str		,      ):
             _UpperCAmelCase  :						Union[str, Any]							   =					get_size_dict(A )
             _UpperCAmelCase  :						Optional[int]							   =					min(size["height"]		,      size["width"] )
             _UpperCAmelCase  :						List[str]							   =					get_resize_output_image_size(A		,      size=A		,      default_to_square=A )
             _UpperCAmelCase  :						List[Any]							   =					resize(A		,      size=A		,      resample=A		,      data_format=A		,      **A )
             return resized_image




      def   _A			( self		:       Optional[int]		,      A		:       np.ndarray		,      A		:       Union[int, float]		,      A		:       Optional[Union[str, ChannelDimension]] = None		,      **A		:       Dict		,      ):
             return rescale(A		,      scale=A		,      data_format=A		,      **A )




      def   _A			( self		:       Optional[int]		,      A		:       np.ndarray		,      A		:       Union[float, List[float]]		,      A		:       Union[float, List[float]]		,      A		:       Optional[Union[str, ChannelDimension]] = None		,      **A		:       List[str]		,      ):
             return normalize(A		,      mean=A		,      std=A		,      data_format=A		,      **A )





      def   _A			( self		:       Tuple		,      A		:       ImageInput		,      A		:       bool = None		,      A		:       Dict[str, int] = None		,      A		:       PILImageResampling = None		,      A		:       bool = None		,      A		:       bool = None		,      A		:       bool = None		,      A		:       bool = False		,      A		:       bool = None		,      A		:       float = None		,      A		:       bool = None		,      A		:       Optional[Union[float, List[float]]] = None		,      A		:       Optional[Union[float, List[float]]] = None		,      A		:       Optional[Union[str, TensorType]] = None		,      A		:       Optional[ChannelDimension] = ChannelDimension.FIRST		,      **A		:       Dict		,      ):
             _UpperCAmelCase  :						Tuple							   =					do_resize if do_resize is not None else self.do_resize
             _UpperCAmelCase  :						Dict							   =					size if size is not None else self.size
             if isinstance(A		,      (tuple, list) ):
                    # Previous feature extractor had size in (width, height) format
                    _UpperCAmelCase  :						Dict							   =					size[::-1]
             _UpperCAmelCase  :						Any							   =					get_size_dict(A )
             _UpperCAmelCase  :						int							   =					resample if resample is not None else self.resample
             _UpperCAmelCase  :						str							   =					do_thumbnail if do_thumbnail is not None else self.do_thumbnail
             _UpperCAmelCase  :						str							   =					do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
             _UpperCAmelCase  :						Any							   =					do_pad if do_pad is not None else self.do_pad
             _UpperCAmelCase  :						int							   =					do_rescale if do_rescale is not None else self.do_rescale
             _UpperCAmelCase  :						Optional[int]							   =					rescale_factor if rescale_factor is not None else self.rescale_factor
             _UpperCAmelCase  :						List[Any]							   =					do_normalize if do_normalize is not None else self.do_normalize
             _UpperCAmelCase  :						int							   =					image_mean if image_mean is not None else self.image_mean
             _UpperCAmelCase  :						int							   =					image_std if image_std is not None else self.image_std

             _UpperCAmelCase  :						Any							   =					make_list_of_images(A )

             if not valid_images(A ):
                    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_rescale and rescale_factor is None:
                    raise ValueError("Rescale factor must be specified if do_rescale is True." )

             if do_pad and size is None:
                    raise ValueError("Size must be specified if do_pad 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.
             _UpperCAmelCase  :						Optional[Any]							   =					[to_numpy_array(A ) for image in images]

             if do_align_long_axis:
                    _UpperCAmelCase  :						Optional[Any]							   =					[self.align_long_axis(A		,      size=A ) for image in images]

             if do_resize:
                    _UpperCAmelCase  :						Union[str, Any]							   =					[self.resize(image=A		,      size=A		,      resample=A ) for image in images]

             if do_thumbnail:
                    _UpperCAmelCase  :						Dict							   =					[self.thumbnail(image=A		,      size=A ) for image in images]

             if do_pad:
                    _UpperCAmelCase  :						Any							   =					[self.pad_image(image=A		,      size=A		,      random_padding=A ) for image in images]

             if do_rescale:
                    _UpperCAmelCase  :						Optional[Any]							   =					[self.rescale(image=A		,      scale=A ) for image in images]

             if do_normalize:
                    _UpperCAmelCase  :						Union[str, Any]							   =					[self.normalize(image=A		,      mean=A		,      std=A ) for image in images]

             _UpperCAmelCase  :						Optional[Any]							   =					[to_channel_dimension_format(A		,      A ) for image in images]

             _UpperCAmelCase  :						List[str]							   =					{"pixel_values": images}
             return BatchFeature(data=A		,      tensor_type=A )


'''simple docstring'''




import unittest

import requests

from transformers import MODEL_FOR_VISION_2_SEQ_MAPPING, TF_MODEL_FOR_VISION_2_SEQ_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_torch_available():
  from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
  __SCREAMING_SNAKE_CASE :  Any  =						False


if is_vision_available():
  from PIL import Image
else:

  class 			lowerCamelCase_			:
        '''simple docstring'''

        @staticmethod
        def   _A			( *A		:       str		,      **A		:       Optional[Any] ):
               pass

@is_pipeline_test
@require_vision
class 			lowerCamelCase_			(unittest.TestCase					):
      '''simple docstring'''

      __UpperCamelCase:						Union[str, Any]      			=		MODEL_FOR_VISION_2_SEQ_MAPPING
      __UpperCamelCase:						int      			=		TF_MODEL_FOR_VISION_2_SEQ_MAPPING




      def   _A			( self		:       Any		,      A		:       Any		,      A		:       str		,      A		:       Optional[int] ):
             _UpperCAmelCase  :						Dict							   =					pipeline("image-to-text"		,      model=A		,      tokenizer=A		,      image_processor=A )
             _UpperCAmelCase  :						Union[str, Any]							   =					[
                 Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ),
                 "./tests/fixtures/tests_samples/COCO/000000039769.png",
             ]
             return pipe, examples




      def   _A			( self		:       Tuple		,      A		:       Union[str, Any]		,      A		:       Optional[Any] ):
             _UpperCAmelCase  :						List[str]							   =					pipe(A )
             self.assertEqual(
                 A		,      [
                     [{"generated_text": ANY(A )}],
                     [{"generated_text": ANY(A )}],
                 ]		,      )




      @require_tf
      def   _A			( self		:       Any ):
             _UpperCAmelCase  :						Any							   =					pipeline("image-to-text"		,      model="hf-internal-testing/tiny-random-vit-gpt2"		,      framework="tf" )
             _UpperCAmelCase  :						int							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

             _UpperCAmelCase  :						List[str]							   =					pipe(A )
             self.assertEqual(
                 A		,      [
                     {
                         "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                     },
                 ]		,      )

             _UpperCAmelCase  :						Tuple							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                 ]		,      )

             _UpperCAmelCase  :						Dict							   =					pipe(A		,      max_new_tokens=1 )
             self.assertEqual(
                 A		,      [{"generated_text": "growth"}]		,      )




      @require_torch
      def   _A			( self		:       Union[str, Any] ):
             _UpperCAmelCase  :						List[str]							   =					pipeline("image-to-text"		,      model="hf-internal-testing/tiny-random-vit-gpt2" )
             _UpperCAmelCase  :						Optional[int]							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

             _UpperCAmelCase  :						Optional[Any]							   =					pipe(A )
             self.assertEqual(
                 A		,      [
                     {
                         "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                     },
                 ]		,      )

             _UpperCAmelCase  :						str							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                 ]		,      )




      @require_torch
      def   _A			( self		:       Union[str, Any] ):
             _UpperCAmelCase  :						Any							   =					pipeline("image-to-text"		,      model="hf-internal-testing/tiny-random-BlipForConditionalGeneration" )
             _UpperCAmelCase  :						Any							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"
             _UpperCAmelCase  :						int							   =					"a photo of"

             _UpperCAmelCase  :						str							   =					pipe(A		,      prompt=A )
             self.assertTrue(outputs[0]["generated_text"].startswith(A ) )




      @slow
      @require_torch
      def   _A			( self		:       List[Any] ):
             _UpperCAmelCase  :						Dict							   =					pipeline("image-to-text"		,      model="ydshieh/vit-gpt2-coco-en" )
             _UpperCAmelCase  :						int							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

             _UpperCAmelCase  :						List[str]							   =					pipe(A )
             self.assertEqual(A		,      [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}] )

             _UpperCAmelCase  :						int							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}],
                     [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}],
                 ]		,      )




      @slow
      @require_torch
      def   _A			( self		:       Optional[Any] ):
             _UpperCAmelCase  :						Tuple							   =					pipeline("image-to-text"		,      model="Salesforce/blip-image-captioning-base" )
             _UpperCAmelCase  :						int							   =					"https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
             _UpperCAmelCase  :						Optional[int]							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						Any							   =					pipe(A )
             self.assertEqual(A		,      [{"generated_text": "a pink pokemon pokemon with a blue shirt and a blue shirt"}] )




      @slow
      @require_torch
      def   _A			( self		:       List[Any] ):
             _UpperCAmelCase  :						List[str]							   =					pipeline("image-to-text"		,      model="microsoft/git-base-coco" )
             _UpperCAmelCase  :						Tuple							   =					"https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
             _UpperCAmelCase  :						Dict							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						List[str]							   =					pipe(A )
             self.assertEqual(A		,      [{"generated_text": "a cartoon of a purple character."}] )




      @slow
      @require_torch
      def   _A			( self		:       int ):
             _UpperCAmelCase  :						Any							   =					pipeline("image-to-text"		,      model="Salesforce/blip-image-captioning-base" )
             _UpperCAmelCase  :						List[str]							   =					"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             _UpperCAmelCase  :						int							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						Dict							   =					"a photography of"

             _UpperCAmelCase  :						Optional[int]							   =					pipe(A		,      prompt=A )
             self.assertEqual(A		,      [{"generated_text": "a photography of a volcano"}] )

             with self.assertRaises(A ):
                    _UpperCAmelCase  :						List[Any]							   =					pipe([image, image]		,      prompt=[prompt, prompt] )




      @slow
      @require_torch
      def   _A			( self		:       Union[str, Any] ):
             _UpperCAmelCase  :						Optional[int]							   =					pipeline("image-to-text"		,      model="microsoft/git-base-coco" )
             _UpperCAmelCase  :						Optional[Any]							   =					"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             _UpperCAmelCase  :						List[str]							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						List[Any]							   =					"a photo of a"

             _UpperCAmelCase  :						Tuple							   =					pipe(A		,      prompt=A )
             self.assertEqual(A		,      [{"generated_text": "a photo of a tent with a tent and a tent in the background."}] )

             with self.assertRaises(A ):
                    _UpperCAmelCase  :						List[str]							   =					pipe([image, image]		,      prompt=[prompt, prompt] )




      @unittest.skipIf(
          not is_torch_greater_or_equal_than_1_11		,      reason="`Pix2StructImageProcessor` requires `torch>=1.11.0`." )
      @slow
      @require_torch
      def   _A			( self		:       Union[str, Any] ):
             _UpperCAmelCase  :						str							   =					pipeline("image-to-text"		,      model="google/pix2struct-ai2d-base" )
             _UpperCAmelCase  :						List[Any]							   =					"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             _UpperCAmelCase  :						List[Any]							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						Optional[int]							   =					"What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"

             _UpperCAmelCase  :						str							   =					pipe(A		,      prompt=A )
             self.assertEqual(A		,      [{"generated_text": "ash cloud"}] )

             with self.assertRaises(A ):
                    _UpperCAmelCase  :						List[str]							   =					pipe([image, image]		,      prompt=[prompt, prompt] )





      @slow
      @require_tf
      def   _A			( self		:       List[Any] ):
             _UpperCAmelCase  :						str							   =					pipeline("image-to-text"		,      model="ydshieh/vit-gpt2-coco-en"		,      framework="tf" )
             _UpperCAmelCase  :						str							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

             _UpperCAmelCase  :						Any							   =					pipe(A )
             self.assertEqual(A		,      [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}] )

             _UpperCAmelCase  :						Optional[Any]							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}],
                     [{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}],
                 ]		,      )

