


'''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   lowercase      (				unittest.TestCase   ):

		"""simple docstring"""

		_a       					=							MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
		_a       					=							TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__  :str				   =					AudioClassificationPipeline(model=UpperCamelCase_       ,  feature_extractor=UpperCamelCase_				)

							# test with a raw waveform
							UpperCamelCase__  :Optional[Any]				   =					np.zeros((34000,)				)
							UpperCamelCase__  :Dict				   =					np.zeros((14000,)				)
							return audio_classifier, [audioa, audio]




		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_       ,  UpperCamelCase_				):

							'''simple docstring'''


							UpperCamelCase__   ,							UpperCamelCase__  :str				   =					examples
							UpperCamelCase__  :List[Any]				   =					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_				)},
							    ]       ,  )
							UpperCamelCase__  :str				   =					audio_classifier(UpperCamelCase_       ,  top_k=1				)
							self.assertEqual(
							    UpperCamelCase_       ,  [
							        {'''score''': ANY(UpperCamelCase_				), '''label''': ANY(UpperCamelCase_				)},
							    ]       ,  )

							self.run_torchaudio(UpperCamelCase_				)




		@require_torchaudio
		def        lowerCAmelCase__							(						self       ,  UpperCamelCase_				):

							'''simple docstring'''


							import datasets

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




		@require_torch
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							UpperCamelCase__  :List[Any]				   =					'''anton-l/wav2vec2-random-tiny-classifier'''

							UpperCamelCase__  :int				   =					pipeline('''audio-classification'''       ,  model=UpperCamelCase_				)

							UpperCamelCase__  :Optional[int]				   =					np.ones((8000,)				)
							UpperCamelCase__  :Optional[int]				   =					audio_classifier(UpperCamelCase_       ,  top_k=4				)

							UpperCamelCase__  :str				   =					[
							    {'''score''': 0.0842, '''label''': '''no'''},
							    {'''score''': 0.0838, '''label''': '''up'''},
							    {'''score''': 0.0837, '''label''': '''go'''},
							    {'''score''': 0.0834, '''label''': '''right'''},
							]
							UpperCamelCase__  :List[Any]				   =					[
							    {'''score''': 0.0845, '''label''': '''stop'''},
							    {'''score''': 0.0844, '''label''': '''on'''},
							    {'''score''': 0.0841, '''label''': '''right'''},
							    {'''score''': 0.0834, '''label''': '''left'''},
							]
							self.assertIn(nested_simplify(UpperCamelCase_       ,  decimals=4				)       ,  [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]				)

							UpperCamelCase__  :Optional[int]				   =					{'''array''': np.ones((8000,)				), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
							UpperCamelCase__  :List[Any]				   =					audio_classifier(UpperCamelCase_       ,  top_k=4				)
							self.assertIn(nested_simplify(UpperCamelCase_       ,  decimals=4				)       ,  [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]				)




		@require_torch
		@slow
		def        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							import datasets

							UpperCamelCase__  :List[str]				   =					'''superb/wav2vec2-base-superb-ks'''

							UpperCamelCase__  :Optional[Any]				   =					pipeline('''audio-classification'''       ,  model=UpperCamelCase_				)
							UpperCamelCase__  :Optional[int]				   =					datasets.load_dataset('''anton-l/superb_dummy'''       ,  '''ks'''       ,  split='''test'''				)

							UpperCamelCase__  :List[Any]				   =					np.array(dataset[3]['''speech''']       ,  dtype=np.floataa				)
							UpperCamelCase__  :Dict				   =					audio_classifier(UpperCamelCase_       ,  top_k=4				)
							self.assertEqual(
							    nested_simplify(UpperCamelCase_       ,  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        lowerCAmelCase__							(						self				):

							'''simple docstring'''


							pass



'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


__snake_case				 =	{
    '''configuration_clip''': [
        '''CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''CLIPConfig''',
        '''CLIPOnnxConfig''',
        '''CLIPTextConfig''',
        '''CLIPVisionConfig''',
    ],
    '''processing_clip''': ['''CLIPProcessor'''],
    '''tokenization_clip''': ['''CLIPTokenizer'''],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case				 =	['''CLIPTokenizerFast''']

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case				 =	['''CLIPFeatureExtractor''']
		__snake_case				 =	['''CLIPImageProcessor''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case				 =	[
		    '''CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''CLIPModel''',
		    '''CLIPPreTrainedModel''',
		    '''CLIPTextModel''',
		    '''CLIPTextModelWithProjection''',
		    '''CLIPVisionModel''',
		    '''CLIPVisionModelWithProjection''',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case				 =	[
		    '''TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''TFCLIPModel''',
		    '''TFCLIPPreTrainedModel''',
		    '''TFCLIPTextModel''',
		    '''TFCLIPVisionModel''',
		]

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__snake_case				 =	[
		    '''FlaxCLIPModel''',
		    '''FlaxCLIPPreTrainedModel''',
		    '''FlaxCLIPTextModel''',
		    '''FlaxCLIPTextPreTrainedModel''',
		    '''FlaxCLIPVisionModel''',
		    '''FlaxCLIPVisionPreTrainedModel''',
		]


if TYPE_CHECKING:
		from .configuration_clip import (
		    CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    CLIPConfig,
		    CLIPOnnxConfig,
		    CLIPTextConfig,
		    CLIPVisionConfig,
		)
		from .processing_clip import CLIPProcessor
		from .tokenization_clip import CLIPTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_clip_fast import CLIPTokenizerFast

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_clip import CLIPFeatureExtractor
				from .image_processing_clip import CLIPImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_clip import (
				    CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    CLIPModel,
				    CLIPPreTrainedModel,
				    CLIPTextModel,
				    CLIPTextModelWithProjection,
				    CLIPVisionModel,
				    CLIPVisionModelWithProjection,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_clip import (
				    TF_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFCLIPModel,
				    TFCLIPPreTrainedModel,
				    TFCLIPTextModel,
				    TFCLIPVisionModel,
				)

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_flax_clip import (
				    FlaxCLIPModel,
				    FlaxCLIPPreTrainedModel,
				    FlaxCLIPTextModel,
				    FlaxCLIPTextPreTrainedModel,
				    FlaxCLIPVisionModel,
				    FlaxCLIPVisionPreTrainedModel,
				)


else:
		import sys

		__snake_case				 =	_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)