



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'''

							A				:int =       MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
							A				:Any =       TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING



							def 		_A	(							self ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase				):







											"""simple docstring"""



											a__	:							Optional[Any]      						= AudioClassificationPipeline(model=__UpperCAmelCase ,					feature_extractor=__UpperCAmelCase				)

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



							def 		_A	(							self ,					__UpperCAmelCase ,					__UpperCAmelCase				):







											"""simple docstring"""



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

											self.run_torchaudio(__UpperCAmelCase				)



							@require_torchaudio
							def 		_A	(							self ,					__UpperCAmelCase				):







											"""simple docstring"""



											import datasets

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



							@require_torch
							def 		_A	(							self				):







											"""simple docstring"""



											a__	:							Union[str, Any]      						= "anton-l/wav2vec2-random-tiny-classifier"

											a__	:							Dict      						= pipeline("audio-classification" ,					model=__UpperCAmelCase				)

											a__	:							Any      						= np.ones((8000,)				)
											a__	:							str      						= audio_classifier(__UpperCAmelCase ,					top_k=4				)

											a__	:							Union[str, Any]      						= [
											    {"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__	:							List[str]      						= [
											    {"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(__UpperCAmelCase ,					decimals=4				) ,					[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]				)

											a__	:							Union[str, Any]      						= {"array": np.ones((8000,)				), "sampling_rate": audio_classifier.feature_extractor.sampling_rate}
											a__	:							Any      						= audio_classifier(__UpperCAmelCase ,					top_k=4				)
											self.assertIn(nested_simplify(__UpperCAmelCase ,					decimals=4				) ,					[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]				)



							@require_torch
							@slow
							def 		_A	(							self				):







											"""simple docstring"""



											import datasets

											a__	:							Union[str, Any]      						= "superb/wav2vec2-base-superb-ks"

											a__	:							Union[str, Any]      						= pipeline("audio-classification" ,					model=__UpperCAmelCase				)
											a__	:							str      						= datasets.load_dataset("anton-l/superb_dummy" ,					"ks" ,					split="test"				)

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







# this script reports modified .py files under the desired list of top-level sub-dirs passed as a list of arguments, e.g.:
#   python ./utils/get_modified_files.py utils src tests examples
#
# it uses git to find the forking point and which files were modified - i.e. files not under git won't be considered
# since the output of this script is fed into Makefile commands it doesn't print a newline after the results

import re
import subprocess
import sys


lowerCamelCase		       =      subprocess.check_output("""git merge-base main HEAD""".split()).decode("""utf-8""")
lowerCamelCase		       =      subprocess.check_output(F'git diff --name-only {fork_point_sha}'.split()).decode("""utf-8""").split()

lowerCamelCase		       =      """|""".join(sys.argv[1:])
lowerCamelCase		       =      re.compile(RF'^({joined_dirs}).*?\.py$')

lowerCamelCase		       =      [x for x in modified_files if regex.match(x)]
print(""" """.join(relevant_modified_files), end="""""")


