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



							"""simple docstring"""




							@staticmethod
							def 						_lowercase  (       *SCREAMING_SNAKE_CASE_  :			Optional[int]       ,							**SCREAMING_SNAKE_CASE_  :			List[str]       )       ->     List[str]:
											pass
@is_pipeline_test
@require_vision
class  lowercase__(				unittest.TestCase							):



	"""simple docstring"""




	@require_torch
	def 						_lowercase  (       self  :			List[Any]       )       ->     List[str]:
					lowercase_       =					pipeline(
					    model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''       ,							)
					lowercase_       =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
					lowercase_       =					image_classifier(SCREAMING_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(SCREAMING_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'''}],
					    ]       ,							)

					lowercase_       =					image_classifier([image] * 5       ,							candidate_labels=['''A''', '''B''', '''C''']       ,							batch_size=2       )
					self.assertEqual(
					    nested_simplify(SCREAMING_SNAKE_CASE_       )       ,							[
					        [
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					        ],
					        [
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					        ],
					        [
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					        ],
					        [
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					        ],
					        [
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					        ],
					    ]       ,							)
	@require_tf
	def 						_lowercase  (       self  :			str       )       ->     Union[str, Any]:
					lowercase_       =					pipeline(
					    model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''       ,							framework='''tf'''       )
					lowercase_       =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
					lowercase_       =					image_classifier(SCREAMING_SNAKE_CASE_       ,							candidate_labels=['''a''', '''b''', '''c''']       )

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

					lowercase_       =					image_classifier([image] * 5       ,							candidate_labels=['''A''', '''B''', '''C''']       ,							batch_size=2       )
					self.assertEqual(
					    nested_simplify(SCREAMING_SNAKE_CASE_       )       ,							[
					        [
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					        ],
					        [
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					        ],
					        [
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					        ],
					        [
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					        ],
					        [
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					            {'''score''': 0.3_33, '''label''': ANY(SCREAMING_SNAKE_CASE_       )},
					        ],
					    ]       ,							)
	@slow
	@require_torch
	def 						_lowercase  (       self  :			int       )       ->     Dict:
					lowercase_       =					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_       =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
					lowercase_       =					image_classifier(SCREAMING_SNAKE_CASE_       ,							candidate_labels=['''cat''', '''plane''', '''remote''']       )

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

					lowercase_       =					image_classifier([image] * 5       ,							candidate_labels=['''cat''', '''plane''', '''remote''']       ,							batch_size=2       )
					self.assertEqual(
					    nested_simplify(SCREAMING_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 						_lowercase  (       self  :			Dict       )       ->     Union[str, Any]:
					lowercase_       =					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_       =					Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
					lowercase_       =					image_classifier(SCREAMING_SNAKE_CASE_       ,							candidate_labels=['''cat''', '''plane''', '''remote''']       )
					self.assertEqual(
					    nested_simplify(SCREAMING_SNAKE_CASE_       )       ,							[
					        {'''score''': 0.5_11, '''label''': '''remote'''},
					        {'''score''': 0.4_85, '''label''': '''cat'''},
					        {'''score''': 0.0_04, '''label''': '''plane'''},
					    ]       ,							)

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






import logging

from transformers.configuration_utils import PretrainedConfig


__a  			=					logging.getLogger(__name__)
class  lowercase__(				UpperCAmelCase							):



	"""simple docstring"""




	a			:Optional[int]         =	'masked_bert'
	def __init__(       self  :			Optional[int]       ,							SCREAMING_SNAKE_CASE_  :			List[Any]=3_0_5_2_2       ,							SCREAMING_SNAKE_CASE_  :			List[str]=7_6_8       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]=1_2       ,							SCREAMING_SNAKE_CASE_  :			Any=1_2       ,							SCREAMING_SNAKE_CASE_  :			str=3_0_7_2       ,							SCREAMING_SNAKE_CASE_  :			Union[str, Any]="gelu"       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]=0.1       ,							SCREAMING_SNAKE_CASE_  :			List[str]=0.1       ,							SCREAMING_SNAKE_CASE_  :			Tuple=5_1_2       ,							SCREAMING_SNAKE_CASE_  :			str=2       ,							SCREAMING_SNAKE_CASE_  :			Dict=0.02       ,							SCREAMING_SNAKE_CASE_  :			Any=1e-12       ,							SCREAMING_SNAKE_CASE_  :			Any=0       ,							SCREAMING_SNAKE_CASE_  :			Optional[int]="topK"       ,							SCREAMING_SNAKE_CASE_  :			Dict="constant"       ,							SCREAMING_SNAKE_CASE_  :			Optional[Any]=0.0       ,							**SCREAMING_SNAKE_CASE_  :			Optional[int]       ,							)       ->     Optional[Any]:
					super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_       ,							**SCREAMING_SNAKE_CASE_       )

					lowercase_       =					vocab_size
					lowercase_       =					hidden_size
					lowercase_       =					num_hidden_layers
					lowercase_       =					num_attention_heads
					lowercase_       =					hidden_act
					lowercase_       =					intermediate_size
					lowercase_       =					hidden_dropout_prob
					lowercase_       =					attention_probs_dropout_prob
					lowercase_       =					max_position_embeddings
					lowercase_       =					type_vocab_size
					lowercase_       =					initializer_range
					lowercase_       =					layer_norm_eps
					lowercase_       =					pruning_method
					lowercase_       =					mask_init
					lowercase_       =					mask_scale




