



from __future__ import annotations

import copy
import inspect
import unittest

import numpy as np

from transformers import is_tf_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_tf, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import tensorflow as tf

   from transformers import (
       TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
       TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
       TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
       TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
       TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
       LayoutLMvaConfig,
       TFLayoutLMvaForQuestionAnswering,
       TFLayoutLMvaForSequenceClassification,
       TFLayoutLMvaForTokenClassification,
       TFLayoutLMvaModel,
   )

if is_vision_available():
   from PIL import Image

   from transformers import LayoutLMvaImageProcessor


class 	lowerCamelCase__	:



 """simple docstring"""

 def __init__(    self					:					str			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Union[str, Any]=2			,						__lowerCAmelCase					:					int=3			,						__lowerCAmelCase					:					Tuple=4			,						__lowerCAmelCase					:					Union[str, Any]=2			,						__lowerCAmelCase					:					List[str]=7			,						__lowerCAmelCase					:					List[Any]=True			,						__lowerCAmelCase					:					str=True			,						__lowerCAmelCase					:					Optional[Any]=True			,						__lowerCAmelCase					:					Any=True			,						__lowerCAmelCase					:					str=99			,						__lowerCAmelCase					:					int=36			,						__lowerCAmelCase					:					List[Any]=2			,						__lowerCAmelCase					:					List[Any]=4			,						__lowerCAmelCase					:					Dict=37			,						__lowerCAmelCase					:					Dict="gelu"			,						__lowerCAmelCase					:					Optional[int]=0.1			,						__lowerCAmelCase					:					Tuple=0.1			,						__lowerCAmelCase					:					Optional[Any]=5_12			,						__lowerCAmelCase					:					Any=16			,						__lowerCAmelCase					:					Optional[Any]=2			,						__lowerCAmelCase					:					Optional[int]=0.02			,						__lowerCAmelCase					:					int=6			,						__lowerCAmelCase					:					Tuple=6			,						__lowerCAmelCase					:					Dict=3			,						__lowerCAmelCase					:					Dict=4			,						__lowerCAmelCase					:					Optional[Any]=None			,						__lowerCAmelCase					:					Tuple=10_00			,						)   ->	List[str]:
    _A      =       parent
    _A      =       batch_size
    _A      =       num_channels
    _A      =       image_size
    _A      =       patch_size
    _A      =       is_training
    _A      =       use_input_mask
    _A      =       use_token_type_ids
    _A      =       use_labels
    _A      =       vocab_size
    _A      =       hidden_size
    _A      =       num_hidden_layers
    _A      =       num_attention_heads
    _A      =       intermediate_size
    _A      =       hidden_act
    _A      =       hidden_dropout_prob
    _A      =       attention_probs_dropout_prob
    _A      =       max_position_embeddings
    _A      =       type_vocab_size
    _A      =       type_sequence_label_size
    _A      =       initializer_range
    _A      =       coordinate_size
    _A      =       shape_size
    _A      =       num_labels
    _A      =       num_choices
    _A      =       scope
    _A      =       range_bbox

    # LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
    _A      =       text_seq_length
    _A      =       (image_size // patch_size) ** 2 + 1
    _A      =       self.text_seq_length + self.image_seq_length

 def     snake_case_      (    self					:					List[Any]      )   ->	Dict:
    _A      =       ids_tensor([self.batch_size, self.text_seq_length]			,						self.vocab_size      )

    _A      =       ids_tensor([self.batch_size, self.text_seq_length, 4]			,						self.range_bbox      )
    _A      =       bbox.numpy()
    # Ensure that bbox is legal
    for i in range(bbox.shape[0]      ):
       for j in range(bbox.shape[1]      ):
          if bbox[i, j, 3] < bbox[i, j, 1]:
             _A      =       bbox[i, j, 3]
             _A      =       bbox[i, j, 1]
             _A      =       tmp_coordinate
          if bbox[i, j, 2] < bbox[i, j, 0]:
             _A      =       bbox[i, j, 2]
             _A      =       bbox[i, j, 0]
             _A      =       tmp_coordinate
    _A      =       tf.constant(__lowerCAmelCase      )

    _A      =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

    _A      =       None
    if self.use_input_mask:
       _A      =       random_attention_mask([self.batch_size, self.text_seq_length]      )

    _A      =       None
    if self.use_token_type_ids:
       _A      =       ids_tensor([self.batch_size, self.text_seq_length]			,						self.type_vocab_size      )

    _A      =       None
    _A      =       None
    if self.use_labels:
       _A      =       ids_tensor([self.batch_size]			,						self.type_sequence_label_size      )
       _A      =       ids_tensor([self.batch_size, self.text_seq_length]			,						self.num_labels      )

    _A      =       LayoutLMvaConfig(
        vocab_size=self.vocab_size			,						hidden_size=self.hidden_size			,						num_hidden_layers=self.num_hidden_layers			,						num_attention_heads=self.num_attention_heads			,						intermediate_size=self.intermediate_size			,						hidden_act=self.hidden_act			,						hidden_dropout_prob=self.hidden_dropout_prob			,						attention_probs_dropout_prob=self.attention_probs_dropout_prob			,						max_position_embeddings=self.max_position_embeddings			,						type_vocab_size=self.type_vocab_size			,						initializer_range=self.initializer_range			,						coordinate_size=self.coordinate_size			,						shape_size=self.shape_size			,						input_size=self.image_size			,						patch_size=self.patch_size			,						)

    return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels

 def     snake_case_      (    self					:					Union[str, Any]			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					Tuple			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					Union[str, Any]      )   ->	Union[str, Any]:
    _A      =       TFLayoutLMvaModel(config=__lowerCAmelCase      )

    # text + image
    _A      =       model(__lowerCAmelCase			,						pixel_values=__lowerCAmelCase			,						training=__lowerCAmelCase      )
    _A      =       model(
        __lowerCAmelCase			,						bbox=__lowerCAmelCase			,						pixel_values=__lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						token_type_ids=__lowerCAmelCase			,						training=__lowerCAmelCase			,						)
    _A      =       model(__lowerCAmelCase			,						bbox=__lowerCAmelCase			,						pixel_values=__lowerCAmelCase			,						training=__lowerCAmelCase      )

    self.parent.assertEqual(result.last_hidden_state.shape			,						(self.batch_size, self.seq_length, self.hidden_size)      )

    # text only
    _A      =       model(__lowerCAmelCase			,						training=__lowerCAmelCase      )
    self.parent.assertEqual(
        result.last_hidden_state.shape			,						(self.batch_size, self.text_seq_length, self.hidden_size)      )

    # image only
    _A      =       model({'''pixel_values''': pixel_values}			,						training=__lowerCAmelCase      )
    self.parent.assertEqual(
        result.last_hidden_state.shape			,						(self.batch_size, self.image_seq_length, self.hidden_size)      )

 def     snake_case_      (    self					:					Tuple			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					List[Any]      )   ->	List[str]:
    _A      =       self.num_labels
    _A      =       TFLayoutLMvaForSequenceClassification(config=__lowerCAmelCase      )
    _A      =       model(
        __lowerCAmelCase			,						bbox=__lowerCAmelCase			,						pixel_values=__lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						token_type_ids=__lowerCAmelCase			,						labels=__lowerCAmelCase			,						training=__lowerCAmelCase			,						)
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_labels)      )

 def     snake_case_      (    self					:					Optional[Any]			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Optional[int]			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					Union[str, Any]			,						__lowerCAmelCase					:					Dict      )   ->	List[Any]:
    _A      =       self.num_labels
    _A      =       TFLayoutLMvaForTokenClassification(config=__lowerCAmelCase      )
    _A      =       model(
        __lowerCAmelCase			,						bbox=__lowerCAmelCase			,						pixel_values=__lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						token_type_ids=__lowerCAmelCase			,						labels=__lowerCAmelCase			,						training=__lowerCAmelCase			,						)
    self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.text_seq_length, self.num_labels)      )

 def     snake_case_      (    self					:					List[str]			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					Dict			,						__lowerCAmelCase					:					str			,						__lowerCAmelCase					:					Tuple			,						__lowerCAmelCase					:					Optional[int]			,						__lowerCAmelCase					:					Optional[int]      )   ->	Optional[int]:
    _A      =       2
    _A      =       TFLayoutLMvaForQuestionAnswering(config=__lowerCAmelCase      )
    _A      =       model(
        __lowerCAmelCase			,						bbox=__lowerCAmelCase			,						pixel_values=__lowerCAmelCase			,						attention_mask=__lowerCAmelCase			,						token_type_ids=__lowerCAmelCase			,						start_positions=__lowerCAmelCase			,						end_positions=__lowerCAmelCase			,						training=__lowerCAmelCase			,						)
    self.parent.assertEqual(result.start_logits.shape			,						(self.batch_size, self.seq_length)      )
    self.parent.assertEqual(result.end_logits.shape			,						(self.batch_size, self.seq_length)      )

 def     snake_case_      (    self					:					Tuple      )   ->	Union[str, Any]:
    _A      =       self.prepare_config_and_inputs()
    ((_A)  ,    (_A)  ,    (_A)  ,    (_A)  ,    (_A)  ,    (_A)  ,    (_A)  ,    (_A))      =       config_and_inputs
    _A      =       {
        '''input_ids''': input_ids,
        '''bbox''': bbox,
        '''pixel_values''': pixel_values,
        '''token_type_ids''': token_type_ids,
        '''attention_mask''': input_mask,
    }
    return config, inputs_dict


@require_tf
class 	lowerCamelCase__	(       _A							,  _A							,  unittest.TestCase):



 """simple docstring"""

 a__					: Any =  (
     (
         TFLayoutLMvaModel,
         TFLayoutLMvaForQuestionAnswering,
         TFLayoutLMvaForSequenceClassification,
         TFLayoutLMvaForTokenClassification,
     )
     if is_tf_available()
     else ()
 )
 a__					: str =  (
     {"document-question-answering": TFLayoutLMvaForQuestionAnswering, "feature-extraction": TFLayoutLMvaModel}
     if is_tf_available()
     else {}
 )

 a__					: Tuple =  False
 a__					: List[Any] =  False
 a__					: int =  False

 def     snake_case_      (    self					:					Dict			,						__lowerCAmelCase					:					List[str]			,						__lowerCAmelCase					:					Any			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					List[Any]			,						__lowerCAmelCase					:					Any      )   ->	Union[str, Any]:
    return True

 def     snake_case_      (    self					:					Optional[Any]			,						__lowerCAmelCase					:					Tuple			,						__lowerCAmelCase					:					Optional[Any]			,						__lowerCAmelCase					:					List[Any]=False      )   ->	dict:
    _A      =       copy.deepcopy(__lowerCAmelCase      )

    if model_class in get_values(__lowerCAmelCase      ):
       _A      =       {
           k: tf.tile(tf.expand_dims(__lowerCAmelCase			,						1      )			,						(1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)      )
           if isinstance(__lowerCAmelCase			,						tf.Tensor      ) and v.ndim > 0
           else v
           for k, v in inputs_dict.items()
       }

    if return_labels:
       if model_class in get_values(__lowerCAmelCase      ):
          _A      =       tf.ones(self.model_tester.batch_size			,						dtype=tf.intaa      )
       elif model_class in get_values(__lowerCAmelCase      ):
          _A      =       tf.zeros(self.model_tester.batch_size			,						dtype=tf.intaa      )
          _A      =       tf.zeros(self.model_tester.batch_size			,						dtype=tf.intaa      )
       elif model_class in get_values(__lowerCAmelCase      ):
          _A      =       tf.zeros(self.model_tester.batch_size			,						dtype=tf.intaa      )
       elif model_class in get_values(__lowerCAmelCase      ):
          _A      =       tf.zeros(
              (self.model_tester.batch_size, self.model_tester.text_seq_length)			,						dtype=tf.intaa      )

    return inputs_dict

 def     snake_case_      (    self					:					Any      )   ->	List[str]:
    _A      =       TFLayoutLMvaModelTester(self      )
    _A      =       ConfigTester(self			,						config_class=__lowerCAmelCase			,						hidden_size=37      )

 def     snake_case_      (    self					:					Dict      )   ->	Dict:
    self.config_tester.run_common_tests()

 def     snake_case_      (    self					:					str      )   ->	int:
    _A  ,    _A      =       self.model_tester.prepare_config_and_inputs_for_common()
    for model_class in self.all_model_classes:
       _A      =       model_class(__lowerCAmelCase      )
       if getattr(__lowerCAmelCase			,						'''hf_compute_loss'''			,						__lowerCAmelCase      ):
          # The number of elements in the loss should be the same as the number of elements in the label
          _A      =       self._prepare_for_class(inputs_dict.copy()			,						__lowerCAmelCase			,						return_labels=__lowerCAmelCase      )
          _A      =       prepared_for_class[
              sorted(prepared_for_class.keys() - inputs_dict.keys()			,						reverse=__lowerCAmelCase      )[0]
          ]
          _A      =       added_label.shape.as_list()[:1]

          # Test that model correctly compute the loss with kwargs
          _A      =       self._prepare_for_class(inputs_dict.copy()			,						__lowerCAmelCase			,						return_labels=__lowerCAmelCase      )
          _A      =       prepared_for_class.pop('''input_ids'''      )

          _A      =       model(__lowerCAmelCase			,						**__lowerCAmelCase      )[0]
          self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )

          # Test that model correctly compute the loss when we mask some positions
          _A      =       self._prepare_for_class(inputs_dict.copy()			,						__lowerCAmelCase			,						return_labels=__lowerCAmelCase      )
          _A      =       prepared_for_class.pop('''input_ids'''      )
          if "labels" in prepared_for_class:
             _A      =       prepared_for_class['''labels'''].numpy()
             if len(labels.shape      ) > 1 and labels.shape[1] != 1:
                _A      =       -1_00
                _A      =       tf.convert_to_tensor(__lowerCAmelCase      )
                _A      =       model(__lowerCAmelCase			,						**__lowerCAmelCase      )[0]
                self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )
                self.assertTrue(not np.any(np.isnan(loss.numpy()      )      )      )

                # Test that model correctly compute the loss with a dict
          _A      =       self._prepare_for_class(inputs_dict.copy()			,						__lowerCAmelCase			,						return_labels=__lowerCAmelCase      )
          _A      =       model(__lowerCAmelCase      )[0]
          self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )

          # Test that model correctly compute the loss with a tuple
          _A      =       self._prepare_for_class(inputs_dict.copy()			,						__lowerCAmelCase			,						return_labels=__lowerCAmelCase      )

          # Get keys that were added with the _prepare_for_class function
          _A      =       prepared_for_class.keys() - inputs_dict.keys()
          _A      =       inspect.signature(model.call      ).parameters
          _A      =       list(signature.keys()      )

          # Create a dictionary holding the location of the tensors in the tuple
          _A      =       {0: '''input_ids'''}
          for label_key in label_keys:
             _A      =       signature_names.index(__lowerCAmelCase      )
             _A      =       label_key
          _A      =       sorted(tuple_index_mapping.items()      )
          # Initialize a list with their default values, update the values and convert to a tuple
          _A      =       []

          for name in signature_names:
             if name != "kwargs":
                list_input.append(signature[name].default      )

          for index, value in sorted_tuple_index_mapping:
             _A      =       prepared_for_class[value]

          _A      =       tuple(__lowerCAmelCase      )

          # Send to model
          _A      =       model(tuple_input[:-1]      )[0]

          self.assertTrue(loss.shape.as_list() == expected_loss_size or loss.shape.as_list() == [1]      )

 def     snake_case_      (    self					:					Tuple      )   ->	Optional[int]:
    (
        (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    
    )      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase      )

 def     snake_case_      (    self					:					Dict      )   ->	int:
    (
        (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    
    )      =       self.model_tester.prepare_config_and_inputs()
    for type in ["absolute", "relative_key", "relative_key_query"]:
       _A      =       type
       self.model_tester.create_and_check_model(__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase      )

 def     snake_case_      (    self					:					List[Any]      )   ->	Dict:
    (
        (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    
    )      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(
        __lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase      )

 def     snake_case_      (    self					:					Dict      )   ->	List[str]:
    (
        (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    
    )      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(
        __lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase      )

 def     snake_case_      (    self					:					List[Any]      )   ->	Optional[Any]:
    (
        (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    (
        _A
    )  ,    
    )      =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(
        __lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase			,						__lowerCAmelCase      )

 @slow
 def     snake_case_      (    self					:					Dict      )   ->	Union[str, Any]:
    for model_name in TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       _A      =       TFLayoutLMvaModel.from_pretrained(__lowerCAmelCase      )
       self.assertIsNotNone(__lowerCAmelCase      )

def 	SCREAMING_SNAKE_CASE_							(	)  ->     Optional[Any]:
   _A      =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
   return image


@require_tf
class 	lowerCamelCase__	(       unittest.TestCase):



 """simple docstring"""

 @cached_property
 def     snake_case_      (    self					:					List[Any]      )   ->	Union[str, Any]:
    return LayoutLMvaImageProcessor(apply_ocr=__lowerCAmelCase      ) if is_vision_available() else None

 @slow
 def     snake_case_      (    self					:					Union[str, Any]      )   ->	Any:
    _A      =       TFLayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''      )

    _A      =       self.default_image_processor
    _A      =       prepare_img()
    _A      =       image_processor(images=__lowerCAmelCase			,						return_tensors='''tf'''      ).pixel_values

    _A      =       tf.constant([[1, 2]]      )
    _A      =       tf.expand_dims(tf.constant([[1, 2, 3, 4], [5, 6, 7, 8]]      )			,						axis=0      )

    # forward pass
    _A      =       model(input_ids=__lowerCAmelCase			,						bbox=__lowerCAmelCase			,						pixel_values=__lowerCAmelCase			,						training=__lowerCAmelCase      )

    # verify the logits
    _A      =       (1, 1_99, 7_68)
    self.assertEqual(outputs.last_hidden_state.shape			,						__lowerCAmelCase      )

    _A      =       tf.constant(
        [[-0.0529, 0.3618, 0.1632], [-0.1587, -0.1667, -0.0400], [-0.1557, -0.1671, -0.0505]]      )

    self.assertTrue(np.allclose(outputs.last_hidden_state[0, :3, :3]			,						__lowerCAmelCase			,						atol=1E-4      )      )








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     snake_case_      (    *__lowerCAmelCase					:					Optional[Any]			,						**__lowerCAmelCase					:					Any      )   ->	Any:
       pass


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



 """simple docstring"""

 @require_torch
 def     snake_case_      (    self					:					Tuple      )   ->	Tuple:
    _A      =       pipeline(
        model='''hf-internal-testing/tiny-random-clip-zero-shot-image-classification'''			,						)
    _A      =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
    _A      =       image_classifier(__lowerCAmelCase			,						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(__lowerCAmelCase      )			,						[
            [{'''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      =       image_classifier([image] * 5			,						candidate_labels=['''A''', '''B''', '''C''']			,						batch_size=2      )
    self.assertEqual(
        nested_simplify(__lowerCAmelCase      )			,						[
            [
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
            ],
            [
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
            ],
            [
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
            ],
            [
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
            ],
            [
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
                {'''score''': 0.333, '''label''': ANY(__lowerCAmelCase      )},
            ],
        ]			,						)

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

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

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

 @slow
 @require_torch
 def     snake_case_      (    self					:					Optional[int]      )   ->	int:
    _A      =       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      =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
    _A      =       image_classifier(__lowerCAmelCase			,						candidate_labels=['''cat''', '''plane''', '''remote''']      )

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

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

 @slow
 @require_tf
 def     snake_case_      (    self					:					Optional[int]      )   ->	Dict:
    _A      =       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      =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
    _A      =       image_classifier(__lowerCAmelCase			,						candidate_labels=['''cat''', '''plane''', '''remote''']      )
    self.assertEqual(
        nested_simplify(__lowerCAmelCase      )			,						[
            {'''score''': 0.511, '''label''': '''remote'''},
            {'''score''': 0.485, '''label''': '''cat'''},
            {'''score''': 0.004, '''label''': '''plane'''},
        ]			,						)

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



