





'''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 				__SCREAMING_SNAKE_CASE      :
          '''simple docstring'''



          @staticmethod
          def 	_lowerCamelCase		(				*UpperCamelCase     :			List[Any] ,**UpperCamelCase     :			Dict	)		->	List[Any]:
               pass



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



      @require_torch
      def 	_lowerCamelCase		(				self     :			List[Any]	)		->	Tuple:
           _lowercase				:   Dict	    =				pipeline(
               model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification' ,)
           _lowercase				:   Dict	    =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	)
           _lowercase				:   int	    =				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_3_3, 'label': 'a'}, {'score': 0.3_3_3, 'label': 'b'}, {'score': 0.3_3_3, 'label': 'c'}],
                   [{'score': 0.3_3_3, 'label': 'a'}, {'score': 0.3_3_3, 'label': 'c'}, {'score': 0.3_3_3, 'label': 'b'}],
               ] ,)

           _lowercase				:   List[str]	    =				image_classifier([image] * 5 ,candidate_labels=['A', 'B', 'C'] ,batch_size=2	)
           self.assertEqual(
               nested_simplify(snake_case__	) ,[
                   [
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                   ],
                   [
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                   ],
                   [
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                   ],
                   [
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                   ],
                   [
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                       {'score': 0.3_3_3, 'label': ANY(snake_case__	)},
                   ],
               ] ,)

      @require_tf
      def 	_lowerCamelCase		(				self     :			int	)		->	List[Any]:
           _lowercase				:   Optional[int]	    =				pipeline(
               model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification' ,framework='tf'	)
           _lowercase				:   Dict	    =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	)
           _lowercase				:   str	    =				image_classifier(snake_case__ ,candidate_labels=['a', 'b', 'c']	)

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

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

      @slow
      @require_torch
      def 	_lowerCamelCase		(				self     :			str	)		->	List[str]:
           _lowercase				:   Dict	    =				pipeline(
               task='zero-shot-image-classification' ,model='openai/clip-vit-base-patch32' ,)
           # This is an image of 2 cats with remotes and no planes
           _lowercase				:   Any	    =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	)
           _lowercase				:   Any	    =				image_classifier(snake_case__ ,candidate_labels=['cat', 'plane', 'remote']	)

           self.assertEqual(
               nested_simplify(snake_case__	) ,[
                   {'score': 0.5_1_1, 'label': 'remote'},
                   {'score': 0.4_8_5, 'label': 'cat'},
                   {'score': 0.0_0_4, 'label': 'plane'},
               ] ,)

           _lowercase				:   Optional[Any]	    =				image_classifier([image] * 5 ,candidate_labels=['cat', 'plane', 'remote'] ,batch_size=2	)
           self.assertEqual(
               nested_simplify(snake_case__	) ,[
                   [
                       {'score': 0.5_1_1, 'label': 'remote'},
                       {'score': 0.4_8_5, 'label': 'cat'},
                       {'score': 0.0_0_4, 'label': 'plane'},
                   ],
               ]
               * 5 ,)



      @slow
      @require_tf
      def 	_lowerCamelCase		(				self     :			Dict	)		->	List[Any]:
           _lowercase				:   Union[str, 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
           _lowercase				:   Dict	    =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'	)
           _lowercase				:   Optional[int]	    =				image_classifier(snake_case__ ,candidate_labels=['cat', 'plane', 'remote']	)
           self.assertEqual(
               nested_simplify(snake_case__	) ,[
                   {'score': 0.5_1_1, 'label': 'remote'},
                   {'score': 0.4_8_5, 'label': 'cat'},
                   {'score': 0.0_0_4, 'label': 'plane'},
               ] ,)

           _lowercase				:   int	    =				image_classifier([image] * 5 ,candidate_labels=['cat', 'plane', 'remote'] ,batch_size=2	)
           self.assertEqual(
               nested_simplify(snake_case__	) ,[
                   [
                       {'score': 0.5_1_1, 'label': 'remote'},
                       {'score': 0.4_8_5, 'label': 'cat'},
                       {'score': 0.0_0_4, 'label': 'plane'},
                   ],
               ]
               * 5 ,)






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


UpperCAmelCase_							=				logging.get_logger(__name__)

UpperCAmelCase_							=				{
    """google/canine-s""": """https://huggingface.co/google/canine-s/resolve/main/config.json""",
    # See all CANINE models at https://huggingface.co/models?filter=canine
}
class     UpperCamelCase__					(     lowerCamelCase__				):






		'''simple docstring'''






		__a       :       Optional[int]       =	"""canine"""






		def __init__(							self,   snake_case__=7_68,   snake_case__=12,   snake_case__=12,   snake_case__=30_72,   snake_case__="gelu",   snake_case__=0.1,   snake_case__=0.1,   snake_case__=1_63_84,   snake_case__=16,   snake_case__=0.02,   snake_case__=1E-12,   snake_case__=0,   snake_case__=0XE_0_0_0,   snake_case__=0XE_0_0_1,   snake_case__=4,   snake_case__=4,   snake_case__=8,   snake_case__=1_63_84,   snake_case__=1_28,   **snake_case__,   )       -> Optional[int]:






									"""simple docstring"""
									super().__init__(pad_token_id=snake_case__,   bos_token_id=snake_case__,   eos_token_id=snake_case__,   **snake_case__  )

									lowercase_	:   int  				=     max_position_embeddings
									lowercase_	:   Union[str, Any]  				=     hidden_size
									lowercase_	:   Optional[Any]  				=     num_hidden_layers
									lowercase_	:   str  				=     num_attention_heads
									lowercase_	:   List[Any]  				=     intermediate_size
									lowercase_	:   Optional[Any]  				=     hidden_act
									lowercase_	:   List[str]  				=     hidden_dropout_prob
									lowercase_	:   Any  				=     attention_probs_dropout_prob
									lowercase_	:   Union[str, Any]  				=     initializer_range
									lowercase_	:   Optional[Any]  				=     type_vocab_size
									lowercase_	:   Dict  				=     layer_norm_eps

									# Character config:
									lowercase_	:   Optional[int]  				=     downsampling_rate
									lowercase_	:   List[str]  				=     upsampling_kernel_size
									lowercase_	:   int  				=     num_hash_functions
									lowercase_	:   List[str]  				=     num_hash_buckets
									lowercase_	:   List[str]  				=     local_transformer_stride