





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   ):
			'''simple docstring'''




			__lowerCamelCase     :	List[Any]          =   MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
			__lowerCamelCase     :	List[str]          =   TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING






			def        a_	(      self       :   Optional[int]							,     __lowerCAmelCase       :   int							,     __lowerCAmelCase       :   List[Any]							,     __lowerCAmelCase       :   Optional[Any]       )     ->     Dict:







					"""simple docstring"""






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

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






			def        a_	(      self       :   Tuple							,     __lowerCAmelCase       :   Any							,     __lowerCAmelCase       :   Dict       )     ->     Optional[Any]:







					"""simple docstring"""






					A__				,      A__  				=       examples
					A__  				=       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       )},
					    ]							,     )
					A__  				=       audio_classifier(__lowerCAmelCase							,     top_k=1       )
					self.assertEqual(
					    __lowerCAmelCase							,     [
					        {"""score""": ANY(__lowerCAmelCase       ), """label""": ANY(__lowerCAmelCase       )},
					    ]							,     )

					self.run_torchaudio(__lowerCAmelCase       )






			@require_torchaudio
			def        a_	(      self       :   Dict							,     __lowerCAmelCase       :   int       )     ->     List[str]:







					"""simple docstring"""






					import datasets

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






			@require_torch
			def        a_	(      self       :   int       )     ->     List[Any]:







					"""simple docstring"""






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

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

					A__  				=       np.ones((80_00,)       )
					A__  				=       audio_classifier(__lowerCAmelCase							,     top_k=4       )

					A__  				=       [
					    {"""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"""},
					]
					A__  				=       [
					    {"""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]       )

					A__  				=       {"""array""": np.ones((80_00,)       ), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate}
					A__  				=       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       :   List[str]       )     ->     Tuple:







					"""simple docstring"""






					import datasets

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

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

					A__  				=       np.array(dataset[3]["""speech"""]							,     dtype=np.floataa       )
					A__  				=       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       :   Any       )     ->     int:







					"""simple docstring"""






					pass










from ...utils import is_note_seq_available, is_transformers_available, is_torch_available
from ...utils import OptionalDependencyNotAvailable


try:
					if not (is_transformers_available() and is_torch_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from ...utils.dummy_torch_and_transformers_objects import *  # noqa F403
else:
					from .notes_encoder import SpectrogramNotesEncoder
					from .continous_encoder import SpectrogramContEncoder
					from .pipeline_spectrogram_diffusion import (
					    SpectrogramContEncoder,
					    SpectrogramDiffusionPipeline,
					    TaFilmDecoder,
					)

try:
					if not (is_transformers_available() and is_torch_available() and is_note_seq_available()):
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					from ...utils.dummy_transformers_and_torch_and_note_seq_objects import *  # noqa F403
else:
					from .midi_utils import MidiProcessor



