


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  lowerCAmelCase__:






           '''simple docstring'''





           @staticmethod
           def 					_lowerCamelCase (	*__snake_case				:   Any       ,   **__snake_case				:   Tuple	):





              '''simple docstring'''
              pass





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






    '''simple docstring'''





    @require_torch
    def 					_lowerCamelCase (	self				:   Optional[int]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   pipeline(
           model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''       ,   )
       UpperCAmelCase_							:      str						=   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
       UpperCAmelCase_							:      Any						=   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.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}],
               [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''c'''}, {'''score''': 0.333, '''label''': '''b'''}],
           ]       ,   )

       UpperCAmelCase_							:      int						=   image_classifier([image] * 5       ,   candidate_labels=['''A''', '''B''', '''C''']       ,   batch_size=2	)
       self.assertEqual(
           nested_simplify(__snake_case	)       ,   [
               [
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
               ],
           ]       ,   )





    @require_tf
    def 					_lowerCamelCase (	self				:   Tuple	):





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

       self.assertEqual(
           nested_simplify(__snake_case	)       ,   [{'''score''': 0.333, '''label''': '''a'''}, {'''score''': 0.333, '''label''': '''b'''}, {'''score''': 0.333, '''label''': '''c'''}]       ,   )

       UpperCAmelCase_							:      str						=   image_classifier([image] * 5       ,   candidate_labels=['''A''', '''B''', '''C''']       ,   batch_size=2	)
       self.assertEqual(
           nested_simplify(__snake_case	)       ,   [
               [
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
               ],
               [
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
                   {'''score''': 0.333, '''label''': ANY(__snake_case	)},
               ],
           ]       ,   )





    @slow
    @require_torch
    def 					_lowerCamelCase (	self				:   str	):





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

       self.assertEqual(
           nested_simplify(__snake_case	)       ,   [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ]       ,   )

       UpperCAmelCase_							:      int						=   image_classifier([image] * 5       ,   candidate_labels=['''cat''', '''plane''', '''remote''']       ,   batch_size=2	)
       self.assertEqual(
           nested_simplify(__snake_case	)       ,   [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5       ,   )





    @slow
    @require_tf
    def 					_lowerCamelCase (	self				:   List[Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[Any]						=   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
       UpperCAmelCase_							:      str						=   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
       UpperCAmelCase_							:      List[Any]						=   image_classifier(__snake_case       ,   candidate_labels=['''cat''', '''plane''', '''remote''']	)
       self.assertEqual(
           nested_simplify(__snake_case	)       ,   [
               {'''score''': 0.511, '''label''': '''remote'''},
               {'''score''': 0.485, '''label''': '''cat'''},
               {'''score''': 0.004, '''label''': '''plane'''},
           ]       ,   )

       UpperCAmelCase_							:      Tuple						=   image_classifier([image] * 5       ,   candidate_labels=['''cat''', '''plane''', '''remote''']       ,   batch_size=2	)
       self.assertEqual(
           nested_simplify(__snake_case	)       ,   [
               [
                   {'''score''': 0.511, '''label''': '''remote'''},
                   {'''score''': 0.485, '''label''': '''cat'''},
                   {'''score''': 0.004, '''label''': '''plane'''},
               ],
           ]
           * 5       ,   )



import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCamelCase				:  Tuple      					=							logging.get_logger(__name__)





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     Union[str, Any]			= 'encoder-decoder'
    A_  :     Optional[int]			= True





    def __init__(	self				:   Dict       ,   **__snake_case				:   Union[str, Any]	):





       '''simple docstring'''
       super().__init__(**__snake_case	)
       assert (
           "encoder" in kwargs and "decoder" in kwargs
       ), "Config has to be initialized with encoder and decoder config"
       UpperCAmelCase_							:      int						=   kwargs.pop('''encoder'''	)
       UpperCAmelCase_							:      List[Any]						=   encoder_config.pop('''model_type'''	)
       UpperCAmelCase_							:      int						=   kwargs.pop('''decoder'''	)
       UpperCAmelCase_							:      Union[str, Any]						=   decoder_config.pop('''model_type'''	)

       from ..auto.configuration_auto import AutoConfig

       UpperCAmelCase_							:      Optional[int]						=   AutoConfig.for_model(__snake_case       ,   **__snake_case	)
       UpperCAmelCase_							:      Any						=   AutoConfig.for_model(__snake_case       ,   **__snake_case	)
       UpperCAmelCase_							:      List[Any]						=   True





    @classmethod
    def 					_lowerCamelCase (	cls				:   Optional[Any]       ,   __snake_case				:   PretrainedConfig       ,   __snake_case				:   PretrainedConfig       ,   **__snake_case				:   Union[str, Any]	):





       '''simple docstring'''
       logger.info('''Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'''	)
       UpperCAmelCase_							:      Dict						=   True
       UpperCAmelCase_							:      str						=   True

       return cls(encoder=encoder_config.to_dict()       ,   decoder=decoder_config.to_dict()       ,   **__snake_case	)





    def 					_lowerCamelCase (	self				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   copy.deepcopy(self.__dict__	)
       UpperCAmelCase_							:      Tuple						=   self.encoder.to_dict()
       UpperCAmelCase_							:      Tuple						=   self.decoder.to_dict()
       UpperCAmelCase_							:      Tuple						=   self.__class__.model_type
       return output