


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







				"""simple docstring"""



				__UpperCamelCase    :		int	=	MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
				__UpperCamelCase    :		Dict	=	TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




				def 					_snake_case  (self       ,			__lowercase       ,			__lowercase       ,			__lowercase		):
										__lowerCAmelCase							       =				AudioClassificationPipeline(model=a_       ,			feature_extractor=a_		)

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




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

										self.run_torchaudio(a_		)




				@require_torchaudio
				def 					_snake_case  (self       ,			__lowercase		):
										import datasets

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




				@require_torch
				def 					_snake_case  (self		):
										__lowerCAmelCase							       =				"anton-l/wav2vec2-random-tiny-classifier"

										__lowerCAmelCase							       =				pipeline('''audio-classification'''       ,			model=a_		)

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

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

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




				@require_torch
				@slow
				def 					_snake_case  (self		):
										import datasets

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

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

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





'''simple docstring'''

from math import asin, atan, cos, radians, sin, sqrt, tan

_UpperCAmelCase				: str							    =				6_37_81_37.0
_UpperCAmelCase				: Tuple							    =				6_35_67_52.31_42_45
_UpperCAmelCase				: Optional[Any]							    =				6_3_7_8_1_3_7

def 							__magic_name__(						lowerCamelCase,				lowerCamelCase,				lowerCamelCase,				lowerCamelCase):
						__lowerCAmelCase							       =				(AXIS_A - AXIS_B) / AXIS_A
						__lowerCAmelCase							       =				atan((1 - flattening) * tan(radians(lowerCamelCase)))
						__lowerCAmelCase							       =				atan((1 - flattening) * tan(radians(lowerCamelCase)))
						__lowerCAmelCase							       =				radians(lowerCamelCase)
						__lowerCAmelCase							       =				radians(lowerCamelCase)
						# Equation
						__lowerCAmelCase							       =				sin((phi_a - phi_a) / 2)
						__lowerCAmelCase							       =				sin((lambda_a - lambda_a) / 2)
						# Square both values
						sin_sq_phi *= sin_sq_phi
						sin_sq_lambda *= sin_sq_lambda
						__lowerCAmelCase							       =				sqrt(sin_sq_phi + (cos(lowerCamelCase) * cos(lowerCamelCase) * sin_sq_lambda))
						return 2 * RADIUS * asin(lowerCamelCase)


if __name__ == "__main__":
	import doctest

	doctest.testmod()

