

'''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 		__SCREAMING_SNAKE_CASE							(			unittest.TestCase      ):
				lowerCamelCase_		       =    MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
				lowerCamelCase_		       =    TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING







				def  lowerCamelCase_	(       self     :		Optional[int]		,					UpperCAmelCase__     :		List[Any]		,					UpperCAmelCase__     :		Optional[int]		,					UpperCAmelCase__     :		Any   ):
										'''simple docstring'''
										lowercase :							str    		=AudioClassificationPipeline(model=UpperCAmelCase__		,					feature_extractor=UpperCAmelCase__   )

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







				def  lowerCamelCase_	(       self     :		str		,					UpperCAmelCase__     :		str		,					UpperCAmelCase__     :		Tuple   ):
										'''simple docstring'''
										lowercase		,	lowercase :							Union[str, Any]    		=examples
										lowercase :							Union[str, 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__   )},
										    ]		,					)
										lowercase :							List[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     :		str		,					UpperCAmelCase__     :		Optional[int]   ):
										'''simple docstring'''
										import datasets

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







				@require_torch
				def  lowerCamelCase_	(       self     :		str   ):
										'''simple docstring'''
										lowercase :							List[Any]    		='''anton-l/wav2vec2-random-tiny-classifier'''

										lowercase :							Any    		=pipeline('''audio-classification'''		,					model=UpperCAmelCase__   )

										lowercase :							Any    		=np.ones((8000,)   )
										lowercase :							Tuple    		=audio_classifier(UpperCAmelCase__		,					top_k=4   )

										lowercase :							Any    		=[
										    {'''score''': 0.08_42, '''label''': '''no'''},
										    {'''score''': 0.08_38, '''label''': '''up'''},
										    {'''score''': 0.08_37, '''label''': '''go'''},
										    {'''score''': 0.08_34, '''label''': '''right'''},
										]
										lowercase :							str    		=[
										    {'''score''': 0.08_45, '''label''': '''stop'''},
										    {'''score''': 0.08_44, '''label''': '''on'''},
										    {'''score''': 0.08_41, '''label''': '''right'''},
										    {'''score''': 0.08_34, '''label''': '''left'''},
										]
										self.assertIn(nested_simplify(UpperCAmelCase__		,					decimals=4   )		,					[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]   )

										lowercase :							Optional[int]    		={'''array''': np.ones((8000,)   ), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
										lowercase :							Union[str, 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     :		Any   ):
										'''simple docstring'''
										import datasets

										lowercase :							List[Any]    		='''superb/wav2vec2-base-superb-ks'''

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

										lowercase :							Optional[int]    		=np.array(dataset[3]['''speech''']		,					dtype=np.floataa   )
										lowercase :							Optional[Any]    		=audio_classifier(UpperCAmelCase__		,					top_k=4   )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,					decimals=3   )		,					[
										        {'''score''': 0.9_81, '''label''': '''go'''},
										        {'''score''': 0.0_07, '''label''': '''up'''},
										        {'''score''': 0.0_06, '''label''': '''_unknown_'''},
										        {'''score''': 0.0_01, '''label''': '''down'''},
										    ]		,					)







				@require_tf
				@unittest.skip('''Audio classification is not implemented for TF'''   )
				def  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										pass













"""simple docstring"""
from typing import List, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__    						=			logging.get_logger(__name__)

lowerCAmelCase__    						=			{
    'huggingface/time-series-transformer-tourism-monthly': (
        'https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json'
    ),
    # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer
}


class _lowerCAmelCase						(    __UpperCAmelCase       ):
						SCREAMING_SNAKE_CASE_:					Any 					=				'time_series_transformer'
						SCREAMING_SNAKE_CASE_:					List[str] 					=				{
						    'hidden_size': 'd_model',
						    'num_attention_heads': 'encoder_attention_heads',
						    'num_hidden_layers': 'encoder_layers',
						}







						def __init__(    self   ,				lowerCAmelCase_ = None   ,				lowerCAmelCase_ = None   ,				lowerCAmelCase_ = "student_t"   ,				lowerCAmelCase_ = "nll"   ,				lowerCAmelCase_ = 1   ,				lowerCAmelCase_ = [1, 2, 3, 4, 5, 6, 7]   ,				lowerCAmelCase_ = "mean"   ,				lowerCAmelCase_ = 0   ,				lowerCAmelCase_ = 0   ,				lowerCAmelCase_ = 0   ,				lowerCAmelCase_ = 0   ,				lowerCAmelCase_ = None   ,				lowerCAmelCase_ = None   ,				lowerCAmelCase_ = 3_2   ,				lowerCAmelCase_ = 3_2   ,				lowerCAmelCase_ = 2   ,				lowerCAmelCase_ = 2   ,				lowerCAmelCase_ = 2   ,				lowerCAmelCase_ = 2   ,				lowerCAmelCase_ = True   ,				lowerCAmelCase_ = "gelu"   ,				lowerCAmelCase_ = 6_4   ,				lowerCAmelCase_ = 0.1   ,				lowerCAmelCase_ = 0.1   ,				lowerCAmelCase_ = 0.1   ,				lowerCAmelCase_ = 0.1   ,				lowerCAmelCase_ = 0.1   ,				lowerCAmelCase_ = 1_0_0   ,				lowerCAmelCase_ = 0.02   ,				lowerCAmelCase_=True   ,				**lowerCAmelCase_   ,				)		->							str:
										# time series specific configuration
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    prediction_length
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    context_length or prediction_length
										_SCREAMING_SNAKE_CASE   :		Any       		=    distribution_output
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    loss
										_SCREAMING_SNAKE_CASE   :		Any       		=    input_size
										_SCREAMING_SNAKE_CASE   :		Any       		=    num_time_features
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    lags_sequence
										_SCREAMING_SNAKE_CASE   :		Dict       		=    scaling
										_SCREAMING_SNAKE_CASE   :		Any       		=    num_dynamic_real_features
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    num_static_real_features
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    num_static_categorical_features
										if cardinality and num_static_categorical_features > 0:
														if len(lowerCAmelCase_							) != num_static_categorical_features:
																		raise ValueError(
																		    'The cardinality should be a list of the same length as `num_static_categorical_features`'							)
														_SCREAMING_SNAKE_CASE   :		List[str]       		=    cardinality
										else:
														_SCREAMING_SNAKE_CASE   :		Dict       		=    [0]
										if embedding_dimension and num_static_categorical_features > 0:
														if len(lowerCAmelCase_							) != num_static_categorical_features:
																		raise ValueError(
																		    'The embedding dimension should be a list of the same length as `num_static_categorical_features`'							)
														_SCREAMING_SNAKE_CASE   :		int       		=    embedding_dimension
										else:
														_SCREAMING_SNAKE_CASE   :		List[str]       		=    [min(5_0   ,				(cat + 1) // 2							) for cat in self.cardinality]
										_SCREAMING_SNAKE_CASE   :		str       		=    num_parallel_samples

										# Transformer architecture configuration
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    input_size * len(lowerCAmelCase_							) + self._number_of_features
										_SCREAMING_SNAKE_CASE   :		int       		=    d_model
										_SCREAMING_SNAKE_CASE   :		str       		=    encoder_attention_heads
										_SCREAMING_SNAKE_CASE   :		Optional[Any]       		=    decoder_attention_heads
										_SCREAMING_SNAKE_CASE   :		List[Any]       		=    encoder_ffn_dim
										_SCREAMING_SNAKE_CASE   :		Dict       		=    decoder_ffn_dim
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    encoder_layers
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    decoder_layers

										_SCREAMING_SNAKE_CASE   :		Dict       		=    dropout
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    attention_dropout
										_SCREAMING_SNAKE_CASE   :		Tuple       		=    activation_dropout
										_SCREAMING_SNAKE_CASE   :		Union[str, Any]       		=    encoder_layerdrop
										_SCREAMING_SNAKE_CASE   :		List[str]       		=    decoder_layerdrop

										_SCREAMING_SNAKE_CASE   :		Dict       		=    activation_function
										_SCREAMING_SNAKE_CASE   :		str       		=    init_std

										_SCREAMING_SNAKE_CASE   :		Tuple       		=    use_cache

										super().__init__(is_encoder_decoder=lowerCAmelCase_   ,				**lowerCAmelCase_							)







						@property
						def 			A      (    self							)		->							int:
										return (
										    sum(self.embedding_dimension							)
										    + self.num_dynamic_real_features
										    + self.num_time_features
										    + self.num_static_real_features
										    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
										)

