"""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	):
      snake_case__			:   Optional[int]							  =	MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
      snake_case__			:   str							  =	TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



            lowercase														=	AudioClassificationPipeline(model=__lowerCAmelCase		,       feature_extractor=__lowerCAmelCase						)

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

      def 				A__       (						self		,       __lowerCAmelCase		,       __lowerCAmelCase						):







            """simple docstring"""



            lowercase							,							lowercase														=	examples
            lowercase														=	audio_classifier(__lowerCAmelCase						)
            # by default a model is initialized with num_labels=2
            self.assertEqual(
                __lowerCAmelCase		,       [
                    {"""score""": ANY(__lowerCAmelCase						), """label""": ANY(__lowerCAmelCase						)},
                    {"""score""": ANY(__lowerCAmelCase						), """label""": ANY(__lowerCAmelCase						)},
                ]		,       )
            lowercase														=	audio_classifier(__lowerCAmelCase		,       top_k=1						)
            self.assertEqual(
                __lowerCAmelCase		,       [
                    {"""score""": ANY(__lowerCAmelCase						), """label""": ANY(__lowerCAmelCase						)},
                ]		,       )

            self.run_torchaudio(__lowerCAmelCase						)

      @require_torchaudio
      def 				A__       (						self		,       __lowerCAmelCase						):







            """simple docstring"""



            import datasets

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

      @require_torch
      def 				A__       (						self						):







            """simple docstring"""



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

            lowercase														=	pipeline("""audio-classification"""		,       model=__lowerCAmelCase						)

            lowercase														=	np.ones((8000,)						)
            lowercase														=	audio_classifier(__lowerCAmelCase		,       top_k=4						)

            lowercase														=	[
                {"""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"""},
            ]
            lowercase														=	[
                {"""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(__lowerCAmelCase		,       decimals=4						)		,       [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]						)

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

      @require_torch
      @slow
      def 				A__       (						self						):







            """simple docstring"""



            import datasets

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

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

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







            """simple docstring"""



            pass




"""simple docstring"""






from ..utils import DummyObject, requires_backends





class  _A						(       metaclass=lowerCAmelCase	):
      snake_case__			:   List[str]							  =	['onnx']

      def __init__(						self		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(self		,       ["""onnx"""]						)

      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""onnx"""]						)







      @classmethod
      def 				A__       (						cls		,       *__lowerCAmelCase		,       **__lowerCAmelCase						):







            """simple docstring"""



            requires_backends(cls		,       ["""onnx"""]						)



