






'''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       ):
  lowercase  	=   MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
  lowercase  	=   TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE   )						->   int:



        """simple docstring"""






        UpperCamelCase       						=      AudioClassificationPipeline(model=_SCREAMING_SNAKE_CASE    ,	feature_extractor=_SCREAMING_SNAKE_CASE   )

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

  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE   )						->   Optional[int]:



        """simple docstring"""






        UpperCamelCase						,UpperCamelCase       						=      examples
        UpperCamelCase       						=      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   )},
            ]    ,	)
        UpperCamelCase       						=      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 						A__		(   self    ,	_SCREAMING_SNAKE_CASE   )						->   Union[str, Any]:



        """simple docstring"""






        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(_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 						A__		(   self   )						->   int:



        """simple docstring"""






        UpperCamelCase       						=      """anton-l/wav2vec2-random-tiny-classifier"""

        UpperCamelCase       						=      pipeline("""audio-classification"""    ,	model=_SCREAMING_SNAKE_CASE   )

        UpperCamelCase       						=      np.ones((8000,)   )
        UpperCamelCase       						=      audio_classifier(_SCREAMING_SNAKE_CASE    ,	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(_SCREAMING_SNAKE_CASE    ,	decimals=4   )    ,	[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]   )

        UpperCamelCase       						=      {"""array""": np.ones((8000,)   ), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate}
        UpperCamelCase       						=      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 						A__		(   self   )						->   int:



        """simple docstring"""






        import datasets

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

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

        UpperCamelCase       						=      np.array(dataset[3]["""speech"""]    ,	dtype=np.floataa   )
        UpperCamelCase       						=      audio_classifier(_SCREAMING_SNAKE_CASE    ,	top_k=4   )
        self.assertEqual(
            nested_simplify(_SCREAMING_SNAKE_CASE    ,	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   )						->   Dict:



        """simple docstring"""






        pass








'''simple docstring'''

from typing import TYPE_CHECKING

from ...utils import _LazyModule


SCREAMING_SNAKE_CASE__											=     {'tokenization_wav2vec2_phoneme': ['Wav2Vec2PhonemeCTCTokenizer']}


if TYPE_CHECKING:
     from .tokenization_wavaveca_phoneme import WavaVecaPhonemeCTCTokenizer
else:
     import sys

     SCREAMING_SNAKE_CASE__											=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
