



"""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  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
      _SCREAMING_SNAKE_CASE      =					TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]   ,      SCREAMING_SNAKE_CASE_    :				int   ,      SCREAMING_SNAKE_CASE_    :				Dict   ,      SCREAMING_SNAKE_CASE_    :				str	):
        lowerCAmelCase_       :    Union[str, Any]								=			AudioClassificationPipeline(model=UpperCamelCase_   ,      feature_extractor=UpperCamelCase_	)

        # test with a raw waveform
        lowerCAmelCase_       :    List[Any]								=			np.zeros((3_4_0_0_0,)	)
        lowerCAmelCase_       :    int								=			np.zeros((1_4_0_0_0,)	)
        return audio_classifier, [audioa, audio]



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[Any]   ,      SCREAMING_SNAKE_CASE_    :				Dict   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]	):
        lowerCAmelCase_						,lowerCAmelCase_       :    Any								=			examples
        lowerCAmelCase_       :    int								=			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_	)},
            ]   ,      )
        lowerCAmelCase_       :    Any								=			audio_classifier(UpperCamelCase_   ,      top_k=1	)
        self.assertEqual(
            UpperCamelCase_   ,      [
                {'score': ANY(UpperCamelCase_	), 'label': ANY(UpperCamelCase_	)},
            ]   ,      )

        self.run_torchaudio(UpperCamelCase_	)



      @require_torchaudio
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				List[Any]	):
        import datasets

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



      @require_torch
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[int]	):
        lowerCAmelCase_       :    Any								=			'anton-l/wav2vec2-random-tiny-classifier'

        lowerCAmelCase_       :    Optional[int]								=			pipeline('audio-classification'   ,      model=UpperCamelCase_	)

        lowerCAmelCase_       :    Optional[Any]								=			np.ones((8_0_0_0,)	)
        lowerCAmelCase_       :    Dict								=			audio_classifier(UpperCamelCase_   ,      top_k=4	)

        lowerCAmelCase_       :    List[Any]								=			[
            {'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_       :    List[Any]								=			[
            {'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(UpperCamelCase_   ,      decimals=4	)   ,      [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]	)

        lowerCAmelCase_       :    Union[str, Any]								=			{'array': np.ones((8_0_0_0,)	), 'sampling_rate': audio_classifier.feature_extractor.sampling_rate}
        lowerCAmelCase_       :    Optional[Any]								=			audio_classifier(UpperCamelCase_   ,      top_k=4	)
        self.assertIn(nested_simplify(UpperCamelCase_   ,      decimals=4	)   ,      [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]	)



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

        lowerCAmelCase_       :    int								=			'superb/wav2vec2-base-superb-ks'

        lowerCAmelCase_       :    Optional[Any]								=			pipeline('audio-classification'   ,      model=UpperCamelCase_	)
        lowerCAmelCase_       :    str								=			datasets.load_dataset('anton-l/superb_dummy'   ,      'ks'   ,      split='test'	)

        lowerCAmelCase_       :    Optional[Any]								=			np.array(dataset[3]['speech']   ,      dtype=np.floataa	)
        lowerCAmelCase_       :    str								=			audio_classifier(UpperCamelCase_   ,      top_k=4	)
        self.assertEqual(
            nested_simplify(UpperCamelCase_   ,      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 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]	):
        pass










"""simple docstring"""







import baseaa

def     UpperCamelCase_   (   lowerCAmelCase__				:    str						)						->       bytes:

  """simple docstring"""




  return baseaa.aaaencode(string.encode('utf-8'						)						)

def     UpperCamelCase_   (   lowerCAmelCase__				:    bytes						)						->       str:

  """simple docstring"""




  return baseaa.aaadecode(lowerCAmelCase__						).decode('utf-8'						)


if __name__ == "__main__":
     import doctest

     doctest.testmod()





