code: 
  
  
  
  """simple docstring"""
  import unittest
  
  from transformers import (
      MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
      TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
      PreTrainedTokenizer,
      is_vision_available,
  )
  from transformers.pipelines import ImageClassificationPipeline, pipeline
  from transformers.testing_utils import (
      is_pipeline_test,
      nested_simplify,
      require_tf,
      require_torch,
      require_torch_or_tf,
      require_vision,
      slow,
  )
  
  from .test_pipelines_common import ANY
  
  
  if is_vision_available():
    from PIL import Image
  else:
    class 							_UpperCAmelCase							:
  
  
  
  
  
         '''simple docstring'''
  
  
  
         @staticmethod
         def 				__lowerCAmelCase			(      *A ,			**A	)							->      Tuple:
             pass
  @is_pipeline_test
  @require_torch_or_tf
  @require_vision
  class 							_UpperCAmelCase							(				unittest.TestCase   ):
  
  
  
  
  
       '''simple docstring'''
  
  
  
       a__             =MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
       a__             =TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A ,			A ,			A	)							->      Optional[int]:
           _UpperCAmelCase							:						List[Any]       							=					ImageClassificationPipeline(model=UpperCamelCase_ ,			image_processor=UpperCamelCase_ ,			top_k=2	)
           _UpperCAmelCase							:						List[Any]       							=					[
               Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	),
               '''http://images.cocodataset.org/val2017/000000039769.jpg''',
           ]
           return image_classifier, examples
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A ,			A	)							->      Tuple:
           _UpperCAmelCase							:						Union[str, Any]       							=					image_classifier('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
  
           self.assertEqual(
               UpperCamelCase_ ,			[
                   {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
                   {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
               ] ,			)
  
           import datasets
  
           _UpperCAmelCase							:						Dict       							=					datasets.load_dataset('''hf-internal-testing/fixtures_image_utils''' ,			'''image''' ,			split='''test'''	)
  
           # Accepts URL + PIL.Image + lists
           _UpperCAmelCase							:						List[Any]       							=					image_classifier(
               [
                   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	),
                   '''http://images.cocodataset.org/val2017/000000039769.jpg''',
                   # RGBA
                   dataset[0]['''file'''],
                   # LA
                   dataset[1]['''file'''],
                   # L
                   dataset[2]['''file'''],
               ]	)
           self.assertEqual(
               UpperCamelCase_ ,			[
                   [
                       {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
                       {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
                   ],
                   [
                       {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
                       {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
                   ],
                   [
                       {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
                       {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
                   ],
                   [
                       {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
                       {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
                   ],
                   [
                       {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
                       {'''score''': ANY(UpperCamelCase_	), '''label''': ANY(UpperCamelCase_	)},
                   ],
               ] ,			)
  
  
  
  
  
       @require_torch
       def 				__lowerCAmelCase			(      self	)							->      Optional[Any]:
           _UpperCAmelCase							:						List[str]       							=					'''hf-internal-testing/tiny-random-vit'''
           _UpperCAmelCase							:						List[str]       							=					pipeline('''image-classification''' ,			model=UpperCamelCase_	)
  
           _UpperCAmelCase							:						List[str]       							=					image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''	)
           self.assertEqual(
               nested_simplify(UpperCamelCase_ ,			decimals=4	) ,			[{'''label''': '''LABEL_1''', '''score''': 0.574}, {'''label''': '''LABEL_0''', '''score''': 0.426}] ,			)
  
           _UpperCAmelCase							:						Optional[Any]       							=					image_classifier(
               [
                   '''http://images.cocodataset.org/val2017/000000039769.jpg''',
                   '''http://images.cocodataset.org/val2017/000000039769.jpg''',
               ] ,			top_k=2 ,			)
           self.assertEqual(
               nested_simplify(UpperCamelCase_ ,			decimals=4	) ,			[
                   [{'''label''': '''LABEL_1''', '''score''': 0.574}, {'''label''': '''LABEL_0''', '''score''': 0.426}],
                   [{'''label''': '''LABEL_1''', '''score''': 0.574}, {'''label''': '''LABEL_0''', '''score''': 0.426}],
               ] ,			)
  
  
  
  
  
       @require_tf
       def 				__lowerCAmelCase			(      self	)							->      Dict:
           _UpperCAmelCase							:						Dict       							=					'''hf-internal-testing/tiny-random-vit'''
           _UpperCAmelCase							:						List[Any]       							=					pipeline('''image-classification''' ,			model=UpperCamelCase_ ,			framework='''tf'''	)
  
           _UpperCAmelCase							:						Any       							=					image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''	)
           self.assertEqual(
               nested_simplify(UpperCamelCase_ ,			decimals=4	) ,			[{'''label''': '''LABEL_1''', '''score''': 0.574}, {'''label''': '''LABEL_0''', '''score''': 0.426}] ,			)
  
           _UpperCAmelCase							:						str       							=					image_classifier(
               [
                   '''http://images.cocodataset.org/val2017/000000039769.jpg''',
                   '''http://images.cocodataset.org/val2017/000000039769.jpg''',
               ] ,			top_k=2 ,			)
           self.assertEqual(
               nested_simplify(UpperCamelCase_ ,			decimals=4	) ,			[
                   [{'''label''': '''LABEL_1''', '''score''': 0.574}, {'''label''': '''LABEL_0''', '''score''': 0.426}],
                   [{'''label''': '''LABEL_1''', '''score''': 0.574}, {'''label''': '''LABEL_0''', '''score''': 0.426}],
               ] ,			)
  
  
  
  
  
       def 				__lowerCAmelCase			(      self	)							->      List[Any]:
           _UpperCAmelCase							:						List[str]       							=					PreTrainedTokenizer()
  
           # Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
           _UpperCAmelCase							:						Tuple       							=					pipeline(
               '''image-classification''' ,			model='''hf-internal-testing/tiny-random-vit''' ,			tokenizer=UpperCamelCase_	)
  
           self.assertIs(image_classifier.tokenizer ,			UpperCamelCase_	)
  
  
  
  
  
       @slow
       @require_torch
       def 				__lowerCAmelCase			(      self	)							->      Dict:
           # Perceiver is not tested by `run_pipeline_test` properly.
           # That is because the type of feature_extractor and model preprocessor need to be kept
           # in sync, which is not the case in the current design
           _UpperCAmelCase							:						Union[str, Any]       							=					pipeline('''image-classification''' ,			model='''deepmind/vision-perceiver-conv'''	)
           _UpperCAmelCase							:						str       							=					image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''	)
           self.assertEqual(
               nested_simplify(UpperCamelCase_ ,			decimals=4	) ,			[
                   {'''score''': 0.4_385, '''label''': '''tabby, tabby cat'''},
                   {'''score''': 0.321, '''label''': '''tiger cat'''},
                   {'''score''': 0.0_502, '''label''': '''Egyptian cat'''},
                   {'''score''': 0.0_137, '''label''': '''crib, cot'''},
                   {'''score''': 0.007, '''label''': '''radiator'''},
               ] ,			)
  
           _UpperCAmelCase							:						Any       							=					pipeline('''image-classification''' ,			model='''deepmind/vision-perceiver-fourier'''	)
           _UpperCAmelCase							:						Optional[Any]       							=					image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''	)
           self.assertEqual(
               nested_simplify(UpperCamelCase_ ,			decimals=4	) ,			[
                   {'''score''': 0.5_658, '''label''': '''tabby, tabby cat'''},
                   {'''score''': 0.1_309, '''label''': '''tiger cat'''},
                   {'''score''': 0.0_722, '''label''': '''Egyptian cat'''},
                   {'''score''': 0.0_707, '''label''': '''remote control, remote'''},
                   {'''score''': 0.0_082, '''label''': '''computer keyboard, keypad'''},
               ] ,			)
  
           _UpperCAmelCase							:						Tuple       							=					pipeline('''image-classification''' ,			model='''deepmind/vision-perceiver-learned'''	)
           _UpperCAmelCase							:						List[Any]       							=					image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''	)
           self.assertEqual(
               nested_simplify(UpperCamelCase_ ,			decimals=4	) ,			[
                   {'''score''': 0.3_022, '''label''': '''tabby, tabby cat'''},
                   {'''score''': 0.2_362, '''label''': '''Egyptian cat'''},
                   {'''score''': 0.1_856, '''label''': '''tiger cat'''},
                   {'''score''': 0.0_324, '''label''': '''remote control, remote'''},
                   {'''score''': 0.0_096, '''label''': '''quilt, comforter, comfort, puff'''},
               ] ,			)
  
  
  
style_context: 
  
  
  
  """simple docstring"""
  # Lint as: python3
  import sys
  from collections.abc import Mapping
  from typing import TYPE_CHECKING
  
  import numpy as np
  import pyarrow as pa
  
  from .. import config
  from ..utils.py_utils import map_nested
  from .formatting import TensorFormatter
  
  
  if TYPE_CHECKING:
    import tensorflow as tf
  class 							_UpperCAmelCase							(				TensorFormatter[Mapping, '''tf.Tensor''', Mapping]   ):
  
  
  
  
  
       '''simple docstring'''
  
  
  
       def __init__(      self ,			A=None ,			**A	)							->      List[str]:
           super().__init__(features=A	)
           _UpperCAmelCase							:						Any       							=					tf_tensor_kwargs
           import tensorflow as tf  # noqa: import tf at initialization
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A	)							->      Optional[Any]:
           import tensorflow as tf
  
           if isinstance(A ,			A	) and column:
               if all(
                   isinstance(A ,			tf.Tensor	) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column	):
                   return tf.stack(A	)
               elif all(
                   isinstance(A ,			(tf.Tensor, tf.RaggedTensor)	) and x.ndim == 1 and x.dtype == column[0].dtype
                   for x in column	):
                   # only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
                   return tf.ragged.stack(A	)
  
           return column
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A	)							->      int:
           import tensorflow as tf
  
           if value is None:
               return value
  
           _UpperCAmelCase							:						Any       							=					{}
  
           if isinstance(A ,			(np.number, np.ndarray)	) and np.issubdtype(value.dtype ,			np.integer	):
               _UpperCAmelCase							:						Union[str, Any]       							=					{'''dtype''': tf.intaa}
           elif isinstance(A ,			(np.number, np.ndarray)	) and np.issubdtype(value.dtype ,			np.floating	):
               _UpperCAmelCase							:						Dict       							=					{'''dtype''': tf.floataa}
           elif config.PIL_AVAILABLE and "PIL" in sys.modules:
               import PIL.Image
  
               if isinstance(A ,			PIL.Image.Image	):
                   _UpperCAmelCase							:						Optional[int]       							=					np.asarray(A	)
  
           return tf.convert_to_tensor(A ,			**{**default_dtype, **self.tf_tensor_kwargs}	)
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A	)							->      Tuple:
           import tensorflow as tf
  
           # support for torch, tf, jax etc.
           if config.TORCH_AVAILABLE and "torch" in sys.modules:
               import torch
  
               if isinstance(A ,			torch.Tensor	):
                   return self._tensorize(data_struct.detach().cpu().numpy()[()]	)
           if hasattr(A ,			'''__array__'''	) and not isinstance(A ,			tf.Tensor	):
               _UpperCAmelCase							:						List[str]       							=					data_struct.__array__()
           # support for nested types like struct of list of struct
           if isinstance(A ,			np.ndarray	):
               if data_struct.dtype == object:  # tf tensors cannot be instantied from an array of objects
                   return self._consolidate([self.recursive_tensorize(A	) for substruct in data_struct]	)
           elif isinstance(A ,			(list, tuple)	):
               return self._consolidate([self.recursive_tensorize(A	) for substruct in data_struct]	)
           return self._tensorize(A	)
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A	)							->      Optional[int]:
           return map_nested(self._recursive_tensorize ,			A ,			map_list=A	)
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A	)							->      Mapping:
           _UpperCAmelCase							:						str       							=					self.numpy_arrow_extractor().extract_row(A	)
           _UpperCAmelCase							:						List[Any]       							=					self.python_features_decoder.decode_row(A	)
           return self.recursive_tensorize(A	)
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A	)							->      "tf.Tensor":
           _UpperCAmelCase							:						Union[str, Any]       							=					self.numpy_arrow_extractor().extract_column(A	)
           _UpperCAmelCase							:						Tuple       							=					self.python_features_decoder.decode_column(A ,			pa_table.column_names[0]	)
           _UpperCAmelCase							:						str       							=					self.recursive_tensorize(A	)
           _UpperCAmelCase							:						Optional[Any]       							=					self._consolidate(A	)
           return column
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A	)							->      Mapping:
           _UpperCAmelCase							:						Dict       							=					self.numpy_arrow_extractor().extract_batch(A	)
           _UpperCAmelCase							:						Tuple       							=					self.python_features_decoder.decode_batch(A	)
           _UpperCAmelCase							:						Union[str, Any]       							=					self.recursive_tensorize(A	)
           for column_name in batch:
               _UpperCAmelCase							:						str       							=					self._consolidate(batch[column_name]	)
           return batch
  
  
  