

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







			_UpperCAmelCase   :							Optional[int]										=     MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
			_UpperCAmelCase   :							int										=     TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING



			def        _SCREAMING_SNAKE_CASE			( self  :     List[Any]   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     Dict):
									SCREAMING_SNAKE_CASE_: Any  		=       AudioClassificationPipeline(model=lowerCAmelCase__   ,						feature_extractor=lowerCAmelCase__)

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



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[int]   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Optional[Any]):
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: Any  		=       examples
									SCREAMING_SNAKE_CASE_: List[str]  		=       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__)},
									    ]   ,						)
									SCREAMING_SNAKE_CASE_: Dict  		=       audio_classifier(lowerCAmelCase__   ,						top_k=1)
									self.assertEqual(
									    lowerCAmelCase__   ,						[
									        {"score": ANY(lowerCAmelCase__), "label": ANY(lowerCAmelCase__)},
									    ]   ,						)

									self.run_torchaudio(lowerCAmelCase__)



			@require_torchaudio
			def        _SCREAMING_SNAKE_CASE			( self  :     Union[str, Any]   ,						lowerCAmelCase__  :     Dict):
									import datasets

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



			@require_torch
			def        _SCREAMING_SNAKE_CASE			( self  :     int):
									SCREAMING_SNAKE_CASE_: List[str]  		=       "anton-l/wav2vec2-random-tiny-classifier"

									SCREAMING_SNAKE_CASE_: Tuple  		=       pipeline("audio-classification"   ,						model=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: str  		=       np.ones((8000,))
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       audio_classifier(lowerCAmelCase__   ,						top_k=4)

									SCREAMING_SNAKE_CASE_: List[Any]  		=       [
									    {"score": 0.0842, "label": "no"},
									    {"score": 0.0838, "label": "up"},
									    {"score": 0.0837, "label": "go"},
									    {"score": 0.0834, "label": "right"},
									]
									SCREAMING_SNAKE_CASE_: 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(lowerCAmelCase__   ,						decimals=4)   ,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2])

									SCREAMING_SNAKE_CASE_: int  		=       {"array": np.ones((8000,)), "sampling_rate": audio_classifier.feature_extractor.sampling_rate}
									SCREAMING_SNAKE_CASE_: str  		=       audio_classifier(lowerCAmelCase__   ,						top_k=4)
									self.assertIn(nested_simplify(lowerCAmelCase__   ,						decimals=4)   ,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2])



			@require_torch
			@slow
			def        _SCREAMING_SNAKE_CASE			( self  :     Any):
									import datasets

									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       "superb/wav2vec2-base-superb-ks"

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

									SCREAMING_SNAKE_CASE_: List[Any]  		=       np.array(dataset[3]["speech"]   ,						dtype=np.floataa)
									SCREAMING_SNAKE_CASE_: str  		=       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        _SCREAMING_SNAKE_CASE			( self  :     List[str]):
									pass














from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__UpperCAmelCase								=						{
    'configuration_time_series_transformer': [
        'TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'TimeSeriesTransformerConfig',
    ],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__UpperCAmelCase								=						[
		    'TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'TimeSeriesTransformerForPrediction',
		    'TimeSeriesTransformerModel',
		    'TimeSeriesTransformerPreTrainedModel',
		]


if TYPE_CHECKING:
		from .configuration_time_series_transformer import (
		    TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    TimeSeriesTransformerConfig,
		)

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_time_series_transformer import (
				    TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TimeSeriesTransformerForPrediction,
				    TimeSeriesTransformerModel,
				    TimeSeriesTransformerPreTrainedModel,
				)

else:
		import sys

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






