



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





				'''simple docstring'''





				lowerCAmelCase__					    =    MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
				lowerCAmelCase__					    =    TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

				def 				__lowerCamelCase     (							self  :  str  ,       _lowerCAmelCase  :  int  ,       _lowerCAmelCase  :  Tuple  ,       _lowerCAmelCase  :  Optional[int]):







											'''simple docstring'''

											__lowercase       	=AudioClassificationPipeline(model=_lowerCAmelCase  ,       feature_extractor=_lowerCAmelCase)

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

				def 				__lowerCamelCase     (							self  :  Optional[Any]  ,       _lowerCAmelCase  :  Optional[Any]  ,       _lowerCAmelCase  :  str):







											'''simple docstring'''

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

											self.run_torchaudio(_lowerCAmelCase)

				@require_torchaudio
				def 				__lowerCamelCase     (							self  :  Optional[int]  ,       _lowerCAmelCase  :  int):







											'''simple docstring'''

											import datasets

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

				@require_torch
				def 				__lowerCamelCase     (							self  :  str):







											'''simple docstring'''

											__lowercase       	='anton-l/wav2vec2-random-tiny-classifier'

											__lowercase       	=pipeline('audio-classification'  ,       model=_lowerCAmelCase)

											__lowercase       	=np.ones((8_0_0_0,))
											__lowercase       	=audio_classifier(_lowerCAmelCase  ,       top_k=4)

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

											__lowercase       	={'array': np.ones((8_0_0_0,)), 'sampling_rate': audio_classifier.feature_extractor.sampling_rate}
											__lowercase       	=audio_classifier(_lowerCAmelCase  ,       top_k=4)
											self.assertIn(nested_simplify(_lowerCAmelCase  ,       decimals=4)  ,       [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2])

				@require_torch
				@slow
				def 				__lowerCamelCase     (							self  :  Tuple):







											'''simple docstring'''

											import datasets

											__lowercase       	='superb/wav2vec2-base-superb-ks'

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

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







											'''simple docstring'''

											pass








'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


lowerCamelCase	 =       {
    """configuration_mask2former""": [
        """MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """Mask2FormerConfig""",
    ],
}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowerCamelCase	 =       ["""Mask2FormerImageProcessor"""]

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		lowerCamelCase	 =       [
		    """MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
		    """Mask2FormerForUniversalSegmentation""",
		    """Mask2FormerModel""",
		    """Mask2FormerPreTrainedModel""",
		]

if TYPE_CHECKING:
		from .configuration_maskaformer import MASK2FORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskaFormerConfig

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .image_processing_maskaformer import MaskaFormerImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_maskaformer import (
				    MASK2FORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    MaskaFormerForUniversalSegmentation,
				    MaskaFormerModel,
				    MaskaFormerPreTrainedModel,
				)


else:
		import sys

		lowerCamelCase	 =       _LazyModule(__name__, globals()["""__file__"""], _import_structure)



