
"""simple docstring"""




import unittest

import numpy as np

from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
from transformers.pipelines import AudioClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torchaudio,
    slow,
)

from .test_pipelines_common import ANY







@is_pipeline_test
class   _lowercase						(       unittest.TestCase				):


     '''simple docstring'''





     _A								=							MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
     _A								=							TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase  )->					Dict:
          UpperCAmelCase__							: Optional[int] 							=						AudioClassificationPipeline(model=__UpperCamelCase ,			feature_extractor=__UpperCamelCase  )

          # test with a raw waveform
          UpperCAmelCase__							: Dict 							=						np.zeros((3_40_00,)  )
          UpperCAmelCase__							: Optional[Any] 							=						np.zeros((1_40_00,)  )
          return audio_classifier, [audioa, audio]



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase  )->					Optional[Any]:
          UpperCAmelCase__							,	UpperCAmelCase__							: List[str] 							=						examples
          UpperCAmelCase__							: List[str] 							=						audio_classifier(__UpperCamelCase  )
          # by default a model is initialized with num_labels=2
          self.assertEqual(
              __UpperCamelCase ,			[
                  {"score": ANY(__UpperCamelCase  ), "label": ANY(__UpperCamelCase  )},
                  {"score": ANY(__UpperCamelCase  ), "label": ANY(__UpperCamelCase  )},
              ] ,			)
          UpperCAmelCase__							: List[Any] 							=						audio_classifier(__UpperCamelCase ,			top_k=1  )
          self.assertEqual(
              __UpperCamelCase ,			[
                  {"score": ANY(__UpperCamelCase  ), "label": ANY(__UpperCamelCase  )},
              ] ,			)

          self.run_torchaudio(__UpperCamelCase  )



     @require_torchaudio
     def  lowerCAmelCase__       (      self ,			__UpperCamelCase  )->					List[Any]:
          import datasets

          # test with a local file
          UpperCAmelCase__							: Optional[Any] 							=						datasets.load_dataset("hf-internal-testing/librispeech_asr_dummy" ,			"clean" ,			split="validation"  )
          UpperCAmelCase__							: Dict 							=						dataset[0]["audio"]["array"]
          UpperCAmelCase__							: Optional[Any] 							=						audio_classifier(__UpperCamelCase  )
          self.assertEqual(
              __UpperCamelCase ,			[
                  {"score": ANY(__UpperCamelCase  ), "label": ANY(__UpperCamelCase  )},
                  {"score": ANY(__UpperCamelCase  ), "label": ANY(__UpperCamelCase  )},
              ] ,			)



     @require_torch
     def  lowerCAmelCase__       (      self  )->					Dict:
          UpperCAmelCase__							: List[Any] 							=						"anton-l/wav2vec2-random-tiny-classifier"

          UpperCAmelCase__							: Union[str, Any] 							=						pipeline("audio-classification" ,			model=__UpperCamelCase  )

          UpperCAmelCase__							: Dict 							=						np.ones((80_00,)  )
          UpperCAmelCase__							: Tuple 							=						audio_classifier(__UpperCamelCase ,			top_k=4  )

          UpperCAmelCase__							: Any 							=						[
              {"score": 0.0842, "label": "no"},
              {"score": 0.0838, "label": "up"},
              {"score": 0.0837, "label": "go"},
              {"score": 0.0834, "label": "right"},
          ]
          UpperCAmelCase__							: Dict 							=						[
              {"score": 0.0845, "label": "stop"},
              {"score": 0.0844, "label": "on"},
              {"score": 0.0841, "label": "right"},
              {"score": 0.0834, "label": "left"},
          ]
          self.assertIn(nested_simplify(__UpperCamelCase ,			decimals=4  ) ,			[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]  )

          UpperCAmelCase__							: List[str] 							=						{"array": np.ones((80_00,)  ), "sampling_rate": audio_classifier.feature_extractor.sampling_rate}
          UpperCAmelCase__							: Tuple 							=						audio_classifier(__UpperCamelCase ,			top_k=4  )
          self.assertIn(nested_simplify(__UpperCamelCase ,			decimals=4  ) ,			[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]  )



     @require_torch
     @slow
     def  lowerCAmelCase__       (      self  )->					Union[str, Any]:
          import datasets

          UpperCAmelCase__							: Dict 							=						"superb/wav2vec2-base-superb-ks"

          UpperCAmelCase__							: Dict 							=						pipeline("audio-classification" ,			model=__UpperCamelCase  )
          UpperCAmelCase__							: Optional[int] 							=						datasets.load_dataset("anton-l/superb_dummy" ,			"ks" ,			split="test"  )

          UpperCAmelCase__							: List[str] 							=						np.array(dataset[3]["speech"] ,			dtype=np.floataa  )
          UpperCAmelCase__							: Optional[Any] 							=						audio_classifier(__UpperCamelCase ,			top_k=4  )
          self.assertEqual(
              nested_simplify(__UpperCamelCase ,			decimals=3  ) ,			[
                  {"score": 0.981, "label": "go"},
                  {"score": 0.007, "label": "up"},
                  {"score": 0.006, "label": "_unknown_"},
                  {"score": 0.001, "label": "down"},
              ] ,			)



     @require_tf
     @unittest.skip("Audio classification is not implemented for TF"  )
     def  lowerCAmelCase__       (      self  )->					Dict:
          pass





"""simple docstring"""




from typing import Optional, Union

import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models.modeling_utils import ModelMixin







class   _lowercase						(       lowerCAmelCase_	,   lowerCAmelCase_				):


     '''simple docstring'''





     @register_to_config
     def __init__(      self ,			__UpperCamelCase = 7_68 ,			)->					Union[str, Any]:
          super().__init__()

          UpperCAmelCase__							: str 							=						nn.Parameter(torch.zeros(1 ,			__UpperCamelCase  )  )
          UpperCAmelCase__							: Optional[int] 							=						nn.Parameter(torch.ones(1 ,			__UpperCamelCase  )  )



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase = None ,			__UpperCamelCase = None ,			)->					Any:
          UpperCAmelCase__							: Dict 							=						nn.Parameter(self.mean.to(__UpperCamelCase  ).to(__UpperCamelCase  )  )
          UpperCAmelCase__							: Any 							=						nn.Parameter(self.std.to(__UpperCamelCase  ).to(__UpperCamelCase  )  )
          return self



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase  )->					Union[str, Any]:
          UpperCAmelCase__							: Dict 							=						(embeds - self.mean) * 1.0 / self.std
          return embeds



     def  lowerCAmelCase__       (      self ,			__UpperCamelCase  )->					List[Any]:
          UpperCAmelCase__							: Any 							=						(embeds * self.std) + self.mean
          return embeds



