code: 
  
  
  
  from typing import List, Optional, Union
  
  import numpy as np
  
  from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
  from ...feature_extraction_utils import BatchFeature
  from ...utils import PaddingStrategy, TensorType, logging
  
  
  lowerCamelCase			:							Tuple       =logging.get_logger(__name__)
  
  
  
  
  
  class     __a						(					A__ ):
   _lowerCAmelCase : Any 				=				['''input_values''', '''attention_mask''']
  
  
  
  
  
  
  
   def __init__(	self      :     List[Any]							,     SCREAMING_SNAKE_CASE      :     Tuple=1							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=1_60_00							,     SCREAMING_SNAKE_CASE      :     Dict=0.0							,     SCREAMING_SNAKE_CASE      :     Optional[int]=False							,     SCREAMING_SNAKE_CASE      :     Optional[int]=True							,     **SCREAMING_SNAKE_CASE      :     Optional[int]							,     ):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        super().__init__(feature_size=SCREAMING_SNAKE_CASE							,     sampling_rate=SCREAMING_SNAKE_CASE							,     padding_value=SCREAMING_SNAKE_CASE							,     **SCREAMING_SNAKE_CASE		)
        UpperCamelCase__    :     List[str]        =						return_attention_mask
        UpperCamelCase__    :     Any        =						do_normalize
  
  
  
  
  
  
  
   @staticmethod
   def       __lowercase      (	SCREAMING_SNAKE_CASE      :     List[np.ndarray]							,     SCREAMING_SNAKE_CASE      :     List[np.ndarray]							,     SCREAMING_SNAKE_CASE      :     float = 0.0		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        if attention_mask is not None:
             UpperCamelCase__    :     List[str]        =						np.array(SCREAMING_SNAKE_CASE							,     np.intaa		)
             UpperCamelCase__    :     Optional[Any]        =						[]
  
             for vector, length in zip(SCREAMING_SNAKE_CASE							,     attention_mask.sum(-1		)		):
                  UpperCamelCase__    :     Tuple        =						(vector - vector[:length].mean()) / np.sqrt(vector[:length].var() + 1e-7		)
                  if length < normed_slice.shape[0]:
                       UpperCamelCase__    :     Any        =						padding_value
  
                  normed_input_values.append(SCREAMING_SNAKE_CASE		)
        else:
             UpperCamelCase__    :     Dict        =						[(x - x.mean()) / np.sqrt(x.var() + 1e-7		) for x in input_values]
  
        return normed_input_values
  
  
  
  
  
  
  
   def __call__(	self      :     Any							,     SCREAMING_SNAKE_CASE      :     Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]							,     SCREAMING_SNAKE_CASE      :     Union[bool, str, PaddingStrategy] = False							,     SCREAMING_SNAKE_CASE      :     Optional[int] = None							,     SCREAMING_SNAKE_CASE      :     bool = False							,     SCREAMING_SNAKE_CASE      :     Optional[int] = None							,     SCREAMING_SNAKE_CASE      :     Optional[bool] = None							,     SCREAMING_SNAKE_CASE      :     Optional[Union[str, TensorType]] = None							,     SCREAMING_SNAKE_CASE      :     Optional[int] = None							,     **SCREAMING_SNAKE_CASE      :     Tuple							,     ):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        if sampling_rate is not None:
             if sampling_rate != self.sampling_rate:
                  raise ValueError(
                      F'The model corresponding to this feature extractor: {self} was trained using a sampling rate of'
                      F' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'
                      F' {self.sampling_rate} and not {sampling_rate}.'		)
        else:
             logger.warning(
                 "It is strongly recommended to pass the ``sampling_rate`` argument to this function. "
                 "Failing to do so can result in silent errors that might be hard to debug."		)
  
        UpperCamelCase__    :     Any        =						isinstance(SCREAMING_SNAKE_CASE							,     np.ndarray		) and len(raw_speech.shape		) > 1
        if is_batched_numpy and len(raw_speech.shape		) > 2:
             raise ValueError(F'Only mono-channel audio is supported for input to {self}'		)
        UpperCamelCase__    :     Union[str, Any]        =						is_batched_numpy or (
            isinstance(SCREAMING_SNAKE_CASE							,     (list, tuple)		) and (isinstance(raw_speech[0]							,     (np.ndarray, tuple, list)		))
        )
  
        # always return batch
        if not is_batched:
             UpperCamelCase__    :     Dict        =						[raw_speech]
  
        # convert into correct format for padding
        UpperCamelCase__    :     Dict        =						BatchFeature({"input_values": raw_speech}		)
  
        UpperCamelCase__    :     int        =						self.pad(
            SCREAMING_SNAKE_CASE							,     padding=SCREAMING_SNAKE_CASE							,     max_length=SCREAMING_SNAKE_CASE							,     truncation=SCREAMING_SNAKE_CASE							,     pad_to_multiple_of=SCREAMING_SNAKE_CASE							,     return_attention_mask=SCREAMING_SNAKE_CASE							,     )
  
        # convert input values to correct format
        UpperCamelCase__    :     Optional[int]        =						padded_inputs["input_values"]
        if not isinstance(input_values[0]							,     np.ndarray		):
             UpperCamelCase__    :     Optional[Any]        =						[np.asarray(SCREAMING_SNAKE_CASE							,     dtype=np.floataa		) for array in input_values]
        elif (
            not isinstance(SCREAMING_SNAKE_CASE							,     np.ndarray		)
            and isinstance(input_values[0]							,     np.ndarray		)
            and input_values[0].dtype is np.dtype(np.floataa		)
        ):
             UpperCamelCase__    :     List[str]        =						[array.astype(np.floataa		) for array in input_values]
        elif isinstance(SCREAMING_SNAKE_CASE							,     np.ndarray		) and input_values.dtype is np.dtype(np.floataa		):
             UpperCamelCase__    :     Optional[Any]        =						input_values.astype(np.floataa		)
  
        # convert attention_mask to correct format
        UpperCamelCase__    :     Optional[int]        =						padded_inputs.get("attention_mask"		)
        if attention_mask is not None:
             UpperCamelCase__    :     int        =						[np.asarray(SCREAMING_SNAKE_CASE							,     dtype=np.intaa		) for array in attention_mask]
  
        # zero-mean and unit-variance normalization
        if self.do_normalize:
             UpperCamelCase__    :     str        =						(
                 attention_mask
                 if self._get_padding_strategies(SCREAMING_SNAKE_CASE							,     max_length=SCREAMING_SNAKE_CASE		) is not PaddingStrategy.DO_NOT_PAD
                 else None
             )
             UpperCamelCase__    :     int        =						self.zero_mean_unit_var_norm(
                 padded_inputs["input_values"]							,     attention_mask=SCREAMING_SNAKE_CASE							,     padding_value=self.padding_value		)
  
        if return_tensors is not None:
             UpperCamelCase__    :     Union[str, Any]        =						padded_inputs.convert_to_tensors(SCREAMING_SNAKE_CASE		)
  
        return padded_inputs
style_context: 
  
  
  
  
  
  
  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     __a						:
  
  
  
  
  
  
  
    @staticmethod
    def       __lowercase      (	*SCREAMING_SNAKE_CASE      :     Optional[Any]							,     **SCREAMING_SNAKE_CASE      :     Union[str, Any]		):
  
  
         '''simple docstring'''
  
  
  
  
  
  
         pass
  
  
  
  
  
  @is_pipeline_test
  @require_torch_or_tf
  @require_vision
  class     __a						(					unittest.TestCase ):
   _lowerCAmelCase : Tuple 				=				MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
   _lowerCAmelCase : str 				=				TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Union[str, Any]							,     SCREAMING_SNAKE_CASE      :     str							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]							,     SCREAMING_SNAKE_CASE      :     Optional[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Dict        =						ImageClassificationPipeline(model=SCREAMING_SNAKE_CASE							,     image_processor=SCREAMING_SNAKE_CASE							,     top_k=2		)
        UpperCamelCase__    :     Union[str, Any]        =						[
            Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"		),
            "http://images.cocodataset.org/val2017/000000039769.jpg",
        ]
        return image_classifier, examples
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Tuple							,     SCREAMING_SNAKE_CASE      :     Tuple							,     SCREAMING_SNAKE_CASE      :     Tuple		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Optional[Any]        =						image_classifier("./tests/fixtures/tests_samples/COCO/000000039769.png"		)
  
        self.assertEqual(
            SCREAMING_SNAKE_CASE							,     [
                {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
            ]							,     )
  
        import datasets
  
        UpperCamelCase__    :     int        =						datasets.load_dataset("hf-internal-testing/fixtures_image_utils"							,     "image"							,     split="test"		)
  
        # Accepts URL + PIL.Image + lists
        UpperCamelCase__    :     Tuple        =						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(
            SCREAMING_SNAKE_CASE							,     [
                [
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                ],
                [
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                ],
                [
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                ],
                [
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                ],
                [
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                    {"score": ANY(SCREAMING_SNAKE_CASE		), "label": ANY(SCREAMING_SNAKE_CASE		)},
                ],
            ]							,     )
  
  
  
  
  
  
  
   @require_torch
   def       __lowercase      (	self      :     List[str]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     List[Any]        =						"hf-internal-testing/tiny-random-vit"
        UpperCamelCase__    :     List[str]        =						pipeline("image-classification"							,     model=SCREAMING_SNAKE_CASE		)
  
        UpperCamelCase__    :     List[str]        =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"		)
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}]							,     )
  
        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(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [
                [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
                [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
            ]							,     )
  
  
  
  
  
  
  
   @require_tf
   def       __lowercase      (	self      :     int		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     str        =						"hf-internal-testing/tiny-random-vit"
        UpperCamelCase__    :     int        =						pipeline("image-classification"							,     model=SCREAMING_SNAKE_CASE							,     framework="tf"		)
  
        UpperCamelCase__    :     List[Any]        =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"		)
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}]							,     )
  
        UpperCamelCase__    :     int        =						image_classifier(
            [
                "http://images.cocodataset.org/val2017/000000039769.jpg",
                "http://images.cocodataset.org/val2017/000000039769.jpg",
            ]							,     top_k=2							,     )
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [
                [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
                [{"label": "LABEL_1", "score": 0.5_7_4}, {"label": "LABEL_0", "score": 0.4_2_6}],
            ]							,     )
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Optional[int]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Any        =						PreTrainedTokenizer()
  
        # Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
        UpperCamelCase__    :     str        =						pipeline(
            "image-classification"							,     model="hf-internal-testing/tiny-random-vit"							,     tokenizer=SCREAMING_SNAKE_CASE		)
  
        self.assertIs(image_classifier.tokenizer							,     SCREAMING_SNAKE_CASE		)
  
  
  
  
  
  
  
   @slow
   @require_torch
   def       __lowercase      (	self      :     int		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Optional[Any]        =						pipeline("image-classification"							,     model="deepmind/vision-perceiver-conv"		)
        UpperCamelCase__    :     Any        =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"		)
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [
                {"score": 0.4_3_8_5, "label": "tabby, tabby cat"},
                {"score": 0.3_2_1, "label": "tiger cat"},
                {"score": 0.0_5_0_2, "label": "Egyptian cat"},
                {"score": 0.0_1_3_7, "label": "crib, cot"},
                {"score": 0.0_0_7, "label": "radiator"},
            ]							,     )
  
        UpperCamelCase__    :     Optional[Any]        =						pipeline("image-classification"							,     model="deepmind/vision-perceiver-fourier"		)
        UpperCamelCase__    :     List[str]        =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"		)
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [
                {"score": 0.5_6_5_8, "label": "tabby, tabby cat"},
                {"score": 0.1_3_0_9, "label": "tiger cat"},
                {"score": 0.0_7_2_2, "label": "Egyptian cat"},
                {"score": 0.0_7_0_7, "label": "remote control, remote"},
                {"score": 0.0_0_8_2, "label": "computer keyboard, keypad"},
            ]							,     )
  
        UpperCamelCase__    :     List[str]        =						pipeline("image-classification"							,     model="deepmind/vision-perceiver-learned"		)
        UpperCamelCase__    :     Optional[int]        =						image_classifier("http://images.cocodataset.org/val2017/000000039769.jpg"		)
        self.assertEqual(
            nested_simplify(SCREAMING_SNAKE_CASE							,     decimals=4		)							,     [
                {"score": 0.3_0_2_2, "label": "tabby, tabby cat"},
                {"score": 0.2_3_6_2, "label": "Egyptian cat"},
                {"score": 0.1_8_5_6, "label": "tiger cat"},
                {"score": 0.0_3_2_4, "label": "remote control, remote"},
                {"score": 0.0_0_9_6, "label": "quilt, comforter, comfort, puff"},
            ]							,     )