





"""simple docstring"""




import unittest

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

from .test_pipelines_common import ANY


if is_vision_available():
   from PIL import Image
else:

   class  _UpperCAmelCase  :



         '''simple docstring'''

         @staticmethod
         def 					lowerCamelCase_			(     *snake_case_					,      **snake_case_  ):

           """simple docstring"""

           pass

@is_pipeline_test
@require_vision
class  _UpperCAmelCase  (   unittest.TestCase		):



      '''simple docstring'''

      @require_torch
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Union[str, Any]										=  pipeline(
            model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification'					,      )
        A_  :       List[Any]										=  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
        A_  :       Tuple										=  image_classifier(snake_case_					,      candidate_labels=['a', 'b', 'c']  )

        # The floating scores are so close, we enter floating error approximation and the order is not guaranteed across
        # python and torch versions.
        self.assertIn(
            nested_simplify(snake_case_  )					,      [
                [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'b'}, {'score': 0.3_33, 'label': 'c'}],
                [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'c'}, {'score': 0.3_33, 'label': 'b'}],
            ]					,      )

        A_  :       Dict										=  image_classifier([image] * 5					,      candidate_labels=['A', 'B', 'C']					,      batch_size=2  )
        self.assertEqual(
            nested_simplify(snake_case_  )					,      [
                [
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                ],
                [
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                ],
                [
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                ],
                [
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                ],
                [
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                ],
            ]					,      )

      @require_tf
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Any										=  pipeline(
            model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification'					,      framework='tf'  )
        A_  :       int										=  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
        A_  :       int										=  image_classifier(snake_case_					,      candidate_labels=['a', 'b', 'c']  )

        self.assertEqual(
            nested_simplify(snake_case_  )					,      [{'score': 0.3_33, 'label': 'a'}, {'score': 0.3_33, 'label': 'b'}, {'score': 0.3_33, 'label': 'c'}]					,      )

        A_  :       Any										=  image_classifier([image] * 5					,      candidate_labels=['A', 'B', 'C']					,      batch_size=2  )
        self.assertEqual(
            nested_simplify(snake_case_  )					,      [
                [
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                ],
                [
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                ],
                [
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                ],
                [
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                ],
                [
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                    {'score': 0.3_33, 'label': ANY(snake_case_  )},
                ],
            ]					,      )

      @slow
      @require_torch
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Optional[int]										=  pipeline(
            task='zero-shot-image-classification'					,      model='openai/clip-vit-base-patch32'					,      )
        # This is an image of 2 cats with remotes and no planes
        A_  :       int										=  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
        A_  :       str										=  image_classifier(snake_case_					,      candidate_labels=['cat', 'plane', 'remote']  )

        self.assertEqual(
            nested_simplify(snake_case_  )					,      [
                {'score': 0.5_11, 'label': 'remote'},
                {'score': 0.4_85, 'label': 'cat'},
                {'score': 0.0_04, 'label': 'plane'},
            ]					,      )

        A_  :       Optional[int]										=  image_classifier([image] * 5					,      candidate_labels=['cat', 'plane', 'remote']					,      batch_size=2  )
        self.assertEqual(
            nested_simplify(snake_case_  )					,      [
                [
                    {'score': 0.5_11, 'label': 'remote'},
                    {'score': 0.4_85, 'label': 'cat'},
                    {'score': 0.0_04, 'label': 'plane'},
                ],
            ]
            * 5					,      )

      @slow
      @require_tf
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Dict										=  pipeline(
            task='zero-shot-image-classification'					,      model='openai/clip-vit-base-patch32'					,      framework='tf'  )
        # This is an image of 2 cats with remotes and no planes
        A_  :       Union[str, Any]										=  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
        A_  :       int										=  image_classifier(snake_case_					,      candidate_labels=['cat', 'plane', 'remote']  )
        self.assertEqual(
            nested_simplify(snake_case_  )					,      [
                {'score': 0.5_11, 'label': 'remote'},
                {'score': 0.4_85, 'label': 'cat'},
                {'score': 0.0_04, 'label': 'plane'},
            ]					,      )

        A_  :       Dict										=  image_classifier([image] * 5					,      candidate_labels=['cat', 'plane', 'remote']					,      batch_size=2  )
        self.assertEqual(
            nested_simplify(snake_case_  )					,      [
                [
                    {'score': 0.5_11, 'label': 'remote'},
                    {'score': 0.4_85, 'label': 'cat'},
                    {'score': 0.0_04, 'label': 'plane'},
                ],
            ]
            * 5					,      )






"""simple docstring"""




import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType

class  _UpperCAmelCase  (   UpperCAmelCase__		):



      '''simple docstring'''
      lowercase_      : Dict	    =   ["""image_processor""", """tokenizer"""]
      lowercase_      : Union[str, Any]	    =   """ViltImageProcessor"""
      lowercase_      : Any	    =   ("""BertTokenizer""", """BertTokenizerFast""")

      def __init__(     self					,      snake_case_=None					,      snake_case_=None					,      **snake_case_  ):

        """simple docstring"""

        A_  :       Union[str, Any]										=  None
        if "feature_extractor" in kwargs:
          warnings.warn(
              'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
              ' instead.'					,      snake_case_					,      )
          A_  :       Dict										=  kwargs.pop('feature_extractor'  )

        A_  :       Dict										=  image_processor if image_processor is not None else feature_extractor
        if image_processor is None:
          raise ValueError('You need to specify an `image_processor`.'  )
        if tokenizer is None:
          raise ValueError('You need to specify a `tokenizer`.'  )

        super().__init__(snake_case_					,      snake_case_  )
        A_  :       List[str]										=  self.image_processor

      def __call__(     self					,      snake_case_					,      snake_case_ = None					,      snake_case_ = True					,      snake_case_ = False					,      snake_case_ = None					,      snake_case_ = None					,      snake_case_ = 0					,      snake_case_ = None					,      snake_case_ = None					,      snake_case_ = None					,      snake_case_ = False					,      snake_case_ = False					,      snake_case_ = False					,      snake_case_ = False					,      snake_case_ = True					,      snake_case_ = None					,      **snake_case_					,      ):

        """simple docstring"""

        A_  :       str										=  self.tokenizer(
            text=snake_case_					,      add_special_tokens=snake_case_					,      padding=snake_case_					,      truncation=snake_case_					,      max_length=snake_case_					,      stride=snake_case_					,      pad_to_multiple_of=snake_case_					,      return_token_type_ids=snake_case_					,      return_attention_mask=snake_case_					,      return_overflowing_tokens=snake_case_					,      return_special_tokens_mask=snake_case_					,      return_offsets_mapping=snake_case_					,      return_length=snake_case_					,      verbose=snake_case_					,      return_tensors=snake_case_					,      **snake_case_					,      )
        # add pixel_values + pixel_mask
        A_  :       Optional[int]										=  self.image_processor(snake_case_					,      return_tensors=snake_case_  )
        encoding.update(snake_case_  )

        return encoding

      def 					lowerCamelCase_			(     self					,      *snake_case_					,      **snake_case_  ):

        """simple docstring"""

        return self.tokenizer.batch_decode(*snake_case_					,      **snake_case_  )

      def 					lowerCamelCase_			(     self					,      *snake_case_					,      **snake_case_  ):

        """simple docstring"""

        return self.tokenizer.decode(*snake_case_					,      **snake_case_  )

      @property
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        A_  :       Any										=  self.tokenizer.model_input_names
        A_  :       Any										=  self.image_processor.model_input_names
        return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names  )  )

      @property
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        warnings.warn(
            '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'					,      snake_case_					,      )
        return self.image_processor_class

      @property
      def 					lowerCamelCase_			(     self  ):

        """simple docstring"""

        warnings.warn(
            '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'					,      snake_case_					,      )
        return self.image_processor