





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


       @staticmethod
       def 							SCREAMING_SNAKE_CASE  (						*_UpperCAmelCase						,     **_UpperCAmelCase):

            '''simple docstring'''






            pass


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


     @require_torch
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			str								=	pipeline(
              model='hf-internal-testing/tiny-random-clip-zero-shot-image-classification'						,     )
          __A						:			str								=	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
          __A						:			Optional[int]								=	image_classifier(_UpperCAmelCase						,     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(_UpperCAmelCase)						,     [
                  [{'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'}],
              ]						,     )

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


     @require_tf
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






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

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

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


     @slow
     @require_torch
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						:			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
          __A						:			int								=	Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
          __A						:			Dict								=	image_classifier(_UpperCAmelCase						,     candidate_labels=['cat', 'plane', 'remote'])

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

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


     @slow
     @require_tf
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






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

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






'''simple docstring'''




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowercase__   :				Optional[Any]      =   logging.get_logger(__name__)

lowercase__   :				str      =   {
    '''andreasmadsen/efficient_mlm_m0.40''': (
        '''https://huggingface.co/andreasmadsen/efficient_mlm_m0.40/resolve/main/config.json'''
    ),
}


class 			SCREAMING_SNAKE_CASE   (a__							):
     lowerCAmelCase		      =	'''roberta-prelayernorm'''


     def __init__(						self						,     _UpperCAmelCase=5_0265						,     _UpperCAmelCase=768						,     _UpperCAmelCase=12						,     _UpperCAmelCase=12						,     _UpperCAmelCase=3072						,     _UpperCAmelCase="gelu"						,     _UpperCAmelCase=0.1						,     _UpperCAmelCase=0.1						,     _UpperCAmelCase=512						,     _UpperCAmelCase=2						,     _UpperCAmelCase=0.02						,     _UpperCAmelCase=1e-1_2						,     _UpperCAmelCase=1						,     _UpperCAmelCase=0						,     _UpperCAmelCase=2						,     _UpperCAmelCase="absolute"						,     _UpperCAmelCase=True						,     _UpperCAmelCase=None						,     **_UpperCAmelCase						,     ):

          '''simple docstring'''






          super().__init__(pad_token_id=_UpperCAmelCase						,     bos_token_id=_UpperCAmelCase						,     eos_token_id=_UpperCAmelCase						,     **_UpperCAmelCase)

          __A						:			Union[str, Any]								=	vocab_size
          __A						:			List[str]								=	hidden_size
          __A						:			Tuple								=	num_hidden_layers
          __A						:			List[str]								=	num_attention_heads
          __A						:			Tuple								=	hidden_act
          __A						:			Optional[Any]								=	intermediate_size
          __A						:			List[Any]								=	hidden_dropout_prob
          __A						:			Union[str, Any]								=	attention_probs_dropout_prob
          __A						:			Dict								=	max_position_embeddings
          __A						:			Any								=	type_vocab_size
          __A						:			Optional[int]								=	initializer_range
          __A						:			List[str]								=	layer_norm_eps
          __A						:			Optional[int]								=	position_embedding_type
          __A						:			Dict								=	use_cache
          __A						:			List[str]								=	classifier_dropout




class 			SCREAMING_SNAKE_CASE   (a__							):


     @property
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          if self.task == "multiple-choice":
               __A						:			List[str]								=	{0: 'batch', 1: 'choice', 2: 'sequence'}
          else:
               __A						:			Tuple								=	{0: 'batch', 1: 'sequence'}
          return OrderedDict(
              [
                  ('input_ids', dynamic_axis),
                  ('attention_mask', dynamic_axis),
              ])