

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 _UpperCamelCase							(							unittest.TestCase   ):
			'''simple docstring'''







			lowerCamelCase__           =MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
			lowerCamelCase__           =TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
			def 							__UpperCamelCase (			self							: Optional[Any]      ,      a							: Tuple      ,      a							: Union[str, Any]      ,      a							: Optional[Any]	)							->       Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[str]				     =       AudioClassificationPipeline(model=a      ,      feature_extractor=a	)

				# test with a raw waveform
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       np.zeros((3_4000,)	)
				SCREAMING_SNAKE_CASE   :      List[str]				     =       np.zeros((1_4000,)	)
				return audio_classifier, [audioa, audio]
			def 							__UpperCamelCase (			self							: Dict      ,      a							: str      ,      a							: Union[str, Any]	)							->       int:

				"""simple docstring"""

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

				self.run_torchaudio(a	)
			@require_torchaudio
			def 							__UpperCamelCase (			self							: str      ,      a							: Optional[int]	)							->       Union[str, Any]:

				"""simple docstring"""

				import datasets

				# test with a local file
				SCREAMING_SNAKE_CASE   :      Tuple				     =       datasets.load_dataset("hf-internal-testing/librispeech_asr_dummy"      ,      "clean"      ,      split="validation"	)
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       dataset[0]["audio"]["array"]
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       audio_classifier(a	)
				self.assertEqual(
				    a      ,      [
				        {"score": ANY(a	), "label": ANY(a	)},
				        {"score": ANY(a	), "label": ANY(a	)},
				    ]      ,      )
			@require_torch
			def 							__UpperCamelCase (			self							: Union[str, Any]	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       "anton-l/wav2vec2-random-tiny-classifier"

				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       pipeline("audio-classification"      ,      model=a	)

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       np.ones((8000,)	)
				SCREAMING_SNAKE_CASE   :      Dict				     =       audio_classifier(a      ,      top_k=4	)

				SCREAMING_SNAKE_CASE   :      List[str]				     =       [
				    {"score": 0.0842, "label": "no"},
				    {"score": 0.0838, "label": "up"},
				    {"score": 0.0837, "label": "go"},
				    {"score": 0.0834, "label": "right"},
				]
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       [
				    {"score": 0.0845, "label": "stop"},
				    {"score": 0.0844, "label": "on"},
				    {"score": 0.0841, "label": "right"},
				    {"score": 0.0834, "label": "left"},
				]
				self.assertIn(nested_simplify(a      ,      decimals=4	)      ,      [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]	)

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       {"array": np.ones((8000,)	), "sampling_rate": audio_classifier.feature_extractor.sampling_rate}
				SCREAMING_SNAKE_CASE   :      Any				     =       audio_classifier(a      ,      top_k=4	)
				self.assertIn(nested_simplify(a      ,      decimals=4	)      ,      [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]	)
			@require_torch
			@slow
			def 							__UpperCamelCase (			self							: int	)							->       List[str]:

				"""simple docstring"""

				import datasets

				SCREAMING_SNAKE_CASE   :      Dict				     =       "superb/wav2vec2-base-superb-ks"

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       pipeline("audio-classification"      ,      model=a	)
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       datasets.load_dataset("anton-l/superb_dummy"      ,      "ks"      ,      split="test"	)

				SCREAMING_SNAKE_CASE   :      Dict				     =       np.array(dataset[3]["speech"]      ,      dtype=np.floataa	)
				SCREAMING_SNAKE_CASE   :      List[str]				     =       audio_classifier(a      ,      top_k=4	)
				self.assertEqual(
				    nested_simplify(a      ,      decimals=3	)      ,      [
				        {"score": 0.981, "label": "go"},
				        {"score": 0.007, "label": "up"},
				        {"score": 0.006, "label": "_unknown_"},
				        {"score": 0.001, "label": "down"},
				    ]      ,      )






			@require_tf
			@unittest.skip("Audio classification is not implemented for TF"	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Dict:

				"""simple docstring"""

				pass



from __future__ import annotations

def  lowerCamelCase__ (					_a							,			_a):
	if b == 0:
		return (1, 0)
	((SCREAMING_SNAKE_CASE)     ,(SCREAMING_SNAKE_CASE))   :      Tuple				     =       extended_euclid(_a							,			a % b)
	SCREAMING_SNAKE_CASE   :      Dict				     =       a // b
	return (y, x - k * y)

def  lowerCamelCase__ (					_a							,			_a							,			_a							,			_a):
	((SCREAMING_SNAKE_CASE)     ,(SCREAMING_SNAKE_CASE))   :      str				     =       extended_euclid(_a							,			_a)
	SCREAMING_SNAKE_CASE   :      str				     =       na * na
	SCREAMING_SNAKE_CASE   :      int				     =       ra * x * na + ra * y * na
	return (n % m + m) % m

def  lowerCamelCase__ (					_a							,			_a):
	((SCREAMING_SNAKE_CASE)     ,(SCREAMING_SNAKE_CASE))   :      List[str]				     =       extended_euclid(_a							,			_a)
	if b < 0:
		SCREAMING_SNAKE_CASE   :      int				     =       (b % n + n) % n
	return b

def  lowerCamelCase__ (					_a							,			_a							,			_a							,			_a):
	SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Dict				     =       invert_modulo(_a							,			_a), invert_modulo(_a							,			_a)
	SCREAMING_SNAKE_CASE   :      Optional[int]				     =       na * na
	SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       ra * x * na + ra * y * na
	return (n % m + m) % m


if __name__ == "__main__":
			from doctest import testmod

			testmod(name='chinese_remainder_theorem', verbose=True)
			testmod(name='chinese_remainder_theorem2', verbose=True)
			testmod(name='invert_modulo', verbose=True)
			testmod(name='extended_euclid', verbose=True)