
"""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      _UpperCAmelCase							(		unittest.TestCase							):
      __SCREAMING_SNAKE_CASE   :							Tuple							      =	MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
      __SCREAMING_SNAKE_CASE   :							List[str]							      =	TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




      def     a_      (							self   ,       lowercase_   ,       lowercase_   ,       lowercase_							)						->					Optional[Any]:
          UpperCAmelCase									=					AudioClassificationPipeline(model=lowercase_   ,       feature_extractor=lowercase_							)

          # test with a raw waveform
          UpperCAmelCase									=					np.zeros((3_4_0_0_0,)							)
          UpperCAmelCase									=					np.zeros((1_4_0_0_0,)							)
          return audio_classifier, [audioa, audio]




      def     a_      (							self   ,       lowercase_   ,       lowercase_							)						->					int:
          UpperCAmelCase			,							UpperCAmelCase									=					examples
          UpperCAmelCase									=					audio_classifier(lowercase_							)
          # by default a model is initialized with num_labels=2
          self.assertEqual(
              lowercase_   ,       [
                  {'score': ANY(lowercase_							), 'label': ANY(lowercase_							)},
                  {'score': ANY(lowercase_							), 'label': ANY(lowercase_							)},
              ]   ,       )
          UpperCAmelCase									=					audio_classifier(lowercase_   ,       top_k=1							)
          self.assertEqual(
              lowercase_   ,       [
                  {'score': ANY(lowercase_							), 'label': ANY(lowercase_							)},
              ]   ,       )

          self.run_torchaudio(lowercase_							)




      @require_torchaudio
      def     a_      (							self   ,       lowercase_							)						->					List[str]:
          import datasets

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




      @require_torch
      def     a_      (							self							)						->					Optional[int]:
          UpperCAmelCase									=					'anton-l/wav2vec2-random-tiny-classifier'

          UpperCAmelCase									=					pipeline('audio-classification'   ,       model=lowercase_							)

          UpperCAmelCase									=					np.ones((8_0_0_0,)							)
          UpperCAmelCase									=					audio_classifier(lowercase_   ,       top_k=4							)

          UpperCAmelCase									=					[
              {'score': 0.0_8_4_2, 'label': 'no'},
              {'score': 0.0_8_3_8, 'label': 'up'},
              {'score': 0.0_8_3_7, 'label': 'go'},
              {'score': 0.0_8_3_4, 'label': 'right'},
          ]
          UpperCAmelCase									=					[
              {'score': 0.0_8_4_5, 'label': 'stop'},
              {'score': 0.0_8_4_4, 'label': 'on'},
              {'score': 0.0_8_4_1, 'label': 'right'},
              {'score': 0.0_8_3_4, 'label': 'left'},
          ]
          self.assertIn(nested_simplify(lowercase_   ,       decimals=4							)   ,       [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]							)

          UpperCAmelCase									=					{'array': np.ones((8_0_0_0,)							), 'sampling_rate': audio_classifier.feature_extractor.sampling_rate}
          UpperCAmelCase									=					audio_classifier(lowercase_   ,       top_k=4							)
          self.assertIn(nested_simplify(lowercase_   ,       decimals=4							)   ,       [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]							)




      @require_torch
      @slow
      def     a_      (							self							)						->					Tuple:
          import datasets

          UpperCAmelCase									=					'superb/wav2vec2-base-superb-ks'

          UpperCAmelCase									=					pipeline('audio-classification'   ,       model=lowercase_							)
          UpperCAmelCase									=					datasets.load_dataset('anton-l/superb_dummy'   ,       'ks'   ,       split='test'							)

          UpperCAmelCase									=					np.array(dataset[3]['speech']   ,       dtype=np.floataa							)
          UpperCAmelCase									=					audio_classifier(lowercase_   ,       top_k=4							)
          self.assertEqual(
              nested_simplify(lowercase_   ,       decimals=3							)   ,       [
                  {'score': 0.9_8_1, 'label': 'go'},
                  {'score': 0.0_0_7, 'label': 'up'},
                  {'score': 0.0_0_6, 'label': '_unknown_'},
                  {'score': 0.0_0_1, 'label': 'down'},
              ]   ,       )




      @require_tf
      @unittest.skip('Audio classification is not implemented for TF'							)
      def     a_      (							self							)						->					Optional[Any]:
          pass





"""simple docstring"""

from abc import ABC, abstractmethod
from argparse import ArgumentParser
class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_							):




      @staticmethod
      @abstractmethod
      def     a_      (							lowercase_							)						->					Optional[Any]:
          raise NotImplementedError()




      @abstractmethod
      def     a_      (							self							)						->					Any:
          raise NotImplementedError()



