



"""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 		_A       (			unittest.TestCase					):
      __a								=	MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
      __a								=	TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING





      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		)   ->					List[str]:
         lowerCamelCase__						=			AudioClassificationPipeline(model=SCREAMING_SNAKE_CASE__    ,						feature_extractor=SCREAMING_SNAKE_CASE__		)

         # test with a raw waveform
         lowerCamelCase__						=			np.zeros((34000,)		)
         lowerCamelCase__						=			np.zeros((14000,)		)
         return audio_classifier, [audioa, audio]





      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		)   ->					Optional[int]:
         lowerCamelCase__					,       lowerCamelCase__						=			examples
         lowerCamelCase__						=			audio_classifier(SCREAMING_SNAKE_CASE__		)
         # by default a model is initialized with num_labels=2
         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__		)},
             ]    ,						)
         lowerCamelCase__						=			audio_classifier(SCREAMING_SNAKE_CASE__    ,						top_k=1		)
         self.assertEqual(
             SCREAMING_SNAKE_CASE__    ,						[
                 {"score": ANY(SCREAMING_SNAKE_CASE__		), "label": ANY(SCREAMING_SNAKE_CASE__		)},
             ]    ,						)

         self.run_torchaudio(SCREAMING_SNAKE_CASE__		)





      @require_torchaudio
      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__		)   ->					Union[str, Any]:
         import datasets

         # test with a local file
         lowerCamelCase__						=			datasets.load_dataset("hf-internal-testing/librispeech_asr_dummy"    ,						"clean"    ,						split="validation"		)
         lowerCamelCase__						=			dataset[0]["audio"]["array"]
         lowerCamelCase__						=			audio_classifier(SCREAMING_SNAKE_CASE__		)
         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__		)},
             ]    ,						)





      @require_torch
      def 					_lowerCamelCase  (  self		)   ->					Tuple:
         lowerCamelCase__						=			"anton-l/wav2vec2-random-tiny-classifier"

         lowerCamelCase__						=			pipeline("audio-classification"    ,						model=SCREAMING_SNAKE_CASE__		)

         lowerCamelCase__						=			np.ones((8000,)		)
         lowerCamelCase__						=			audio_classifier(SCREAMING_SNAKE_CASE__    ,						top_k=4		)

         lowerCamelCase__						=			[
             {"score": 0.08_42, "label": "no"},
             {"score": 0.08_38, "label": "up"},
             {"score": 0.08_37, "label": "go"},
             {"score": 0.08_34, "label": "right"},
         ]
         lowerCamelCase__						=			[
             {"score": 0.08_45, "label": "stop"},
             {"score": 0.08_44, "label": "on"},
             {"score": 0.08_41, "label": "right"},
             {"score": 0.08_34, "label": "left"},
         ]
         self.assertIn(nested_simplify(SCREAMING_SNAKE_CASE__    ,						decimals=4		)    ,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]		)

         lowerCamelCase__						=			{"array": np.ones((8000,)		), "sampling_rate": audio_classifier.feature_extractor.sampling_rate}
         lowerCamelCase__						=			audio_classifier(SCREAMING_SNAKE_CASE__    ,						top_k=4		)
         self.assertIn(nested_simplify(SCREAMING_SNAKE_CASE__    ,						decimals=4		)    ,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]		)





      @require_torch
      @slow
      def 					_lowerCamelCase  (  self		)   ->					str:
         import datasets

         lowerCamelCase__						=			"superb/wav2vec2-base-superb-ks"

         lowerCamelCase__						=			pipeline("audio-classification"    ,						model=SCREAMING_SNAKE_CASE__		)
         lowerCamelCase__						=			datasets.load_dataset("anton-l/superb_dummy"    ,						"ks"    ,						split="test"		)

         lowerCamelCase__						=			np.array(dataset[3]["speech"]    ,						dtype=np.floataa		)
         lowerCamelCase__						=			audio_classifier(SCREAMING_SNAKE_CASE__    ,						top_k=4		)
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE__    ,						decimals=3		)    ,						[
                 {"score": 0.9_81, "label": "go"},
                 {"score": 0.0_07, "label": "up"},
                 {"score": 0.0_06, "label": "_unknown_"},
                 {"score": 0.0_01, "label": "down"},
             ]    ,						)





      @require_tf
      @unittest.skip("Audio classification is not implemented for TF"		)
      def 					_lowerCamelCase  (  self		)   ->					Union[str, Any]:
         pass







"""simple docstring"""




def 	UpperCAmelCase__   (				A__   )      ->      list[int]:




   """simple docstring"""

   if length <= 0 or not isinstance(A__	,    A__   ):
      raise ValueError("Length must be a positive integer."   )
   return [n * (2 * n - 1) for n in range(A__   )]


if __name__ == "__main__":
     print(hexagonal_numbers(length=5))
     print(hexagonal_numbers(length=10))




