






import functools
import operator

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


lowerCAmelCase_           = logging.get_logger(__name__)

lowerCAmelCase_           = {
    'asapp/sew-tiny-100k': 'https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json',
    # See all SEW models at https://huggingface.co/models?filter=sew
}
class      UpperCamelCase (				UpperCAmelCase__ ):







		"""simple docstring"""



		snake_case    =   'sew'
		def __init__(  self		:       Any						,_SCREAMING_SNAKE_CASE		:       Tuple=3_2						,_SCREAMING_SNAKE_CASE		:       Any=7_6_8						,_SCREAMING_SNAKE_CASE		:       str=1_2						,_SCREAMING_SNAKE_CASE		:       str=1_2						,_SCREAMING_SNAKE_CASE		:       List[Any]=3_0_7_2						,_SCREAMING_SNAKE_CASE		:       int=2						,_SCREAMING_SNAKE_CASE		:       str="gelu"						,_SCREAMING_SNAKE_CASE		:       int=0.1						,_SCREAMING_SNAKE_CASE		:       Dict=0.1						,_SCREAMING_SNAKE_CASE		:       Union[str, Any]=0.1						,_SCREAMING_SNAKE_CASE		:       str=0.0						,_SCREAMING_SNAKE_CASE		:       str=0.1						,_SCREAMING_SNAKE_CASE		:       Optional[Any]=0.1						,_SCREAMING_SNAKE_CASE		:       Optional[Any]=0.02						,_SCREAMING_SNAKE_CASE		:       Optional[Any]=1E-5						,_SCREAMING_SNAKE_CASE		:       Dict="group"						,_SCREAMING_SNAKE_CASE		:       List[str]="gelu"						,_SCREAMING_SNAKE_CASE		:       Any=(6_4, 1_2_8, 1_2_8, 1_2_8, 1_2_8, 2_5_6, 2_5_6, 2_5_6, 2_5_6, 5_1_2, 5_1_2, 5_1_2, 5_1_2)						,_SCREAMING_SNAKE_CASE		:       str=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)						,_SCREAMING_SNAKE_CASE		:       List[str]=(1_0, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)						,_SCREAMING_SNAKE_CASE		:       Optional[int]=False						,_SCREAMING_SNAKE_CASE		:       List[Any]=1_2_8						,_SCREAMING_SNAKE_CASE		:       int=1_6						,_SCREAMING_SNAKE_CASE		:       List[str]=True						,_SCREAMING_SNAKE_CASE		:       Optional[Any]=0.05						,_SCREAMING_SNAKE_CASE		:       Optional[Any]=1_0						,_SCREAMING_SNAKE_CASE		:       Union[str, Any]=2						,_SCREAMING_SNAKE_CASE		:       List[Any]=0.0						,_SCREAMING_SNAKE_CASE		:       str=1_0						,_SCREAMING_SNAKE_CASE		:       int=0						,_SCREAMING_SNAKE_CASE		:       Dict="mean"						,_SCREAMING_SNAKE_CASE		:       Any=False						,_SCREAMING_SNAKE_CASE		:       Dict=False						,_SCREAMING_SNAKE_CASE		:       Optional[Any]=2_5_6						,_SCREAMING_SNAKE_CASE		:       Dict=0						,_SCREAMING_SNAKE_CASE		:       Union[str, Any]=1						,_SCREAMING_SNAKE_CASE		:       Tuple=2						,**_SCREAMING_SNAKE_CASE		:       Dict						,)    ->   str:


						'''simple docstring'''
						super().__init__(**_SCREAMING_SNAKE_CASE						,pad_token_id=_SCREAMING_SNAKE_CASE						,bos_token_id=_SCREAMING_SNAKE_CASE						,eos_token_id=_SCREAMING_SNAKE_CASE		)
						A	 =      hidden_size
						A	 =      feat_extract_norm
						A	 =      feat_extract_activation
						A	 =      list(_SCREAMING_SNAKE_CASE		)
						A	 =      list(_SCREAMING_SNAKE_CASE		)
						A	 =      list(_SCREAMING_SNAKE_CASE		)
						A	 =      conv_bias
						A	 =      num_conv_pos_embeddings
						A	 =      num_conv_pos_embedding_groups
						A	 =      len(self.conv_dim		)
						A	 =      num_hidden_layers
						A	 =      intermediate_size
						A	 =      squeeze_factor
						A	 =      hidden_act
						A	 =      num_attention_heads
						A	 =      hidden_dropout
						A	 =      attention_dropout
						A	 =      activation_dropout
						A	 =      feat_proj_dropout
						A	 =      final_dropout
						A	 =      layerdrop
						A	 =      layer_norm_eps
						A	 =      initializer_range
						A	 =      vocab_size

						if (
						    (len(self.conv_stride		) != self.num_feat_extract_layers)
						    or (len(self.conv_kernel		) != self.num_feat_extract_layers)
						    or (len(self.conv_dim		) != self.num_feat_extract_layers)
						):
										raise ValueError(
										    'Configuration for convolutional layers is incorrect.'
										    'It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,'
										    f'but is `len(config.conv_dim) = {len(self.conv_dim		)}`, `len(config.conv_stride)'
										    f'= {len(self.conv_stride		)}`, `len(config.conv_kernel) = {len(self.conv_kernel		)}`.'		)

						# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
						A	 =      apply_spec_augment
						A	 =      mask_time_prob
						A	 =      mask_time_length
						A	 =      mask_time_min_masks
						A	 =      mask_feature_prob
						A	 =      mask_feature_length
						A	 =      mask_feature_min_masks

						# ctc loss
						A	 =      ctc_loss_reduction
						A	 =      ctc_zero_infinity

						# sequence classification
						A	 =      use_weighted_layer_sum
						A	 =      classifier_proj_size







		@property
		def       A(  self		:       Optional[int]		)    ->   Union[str, Any]:


						'''simple docstring'''
						return functools.reduce(operator.mul						,self.conv_stride						,1		)








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



		snake_case    =   MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
		snake_case    =   TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
		def       A(  self		:       int						,_SCREAMING_SNAKE_CASE		:       Any						,_SCREAMING_SNAKE_CASE		:       List[Any]						,_SCREAMING_SNAKE_CASE		:       Tuple		)    ->   Dict:


						'''simple docstring'''
						A	 =      AudioClassificationPipeline(model=_SCREAMING_SNAKE_CASE						,feature_extractor=_SCREAMING_SNAKE_CASE		)

						# test with a raw waveform
						A	 =      np.zeros((3_4_0_0_0,)		)
						A	 =      np.zeros((1_4_0_0_0,)		)
						return audio_classifier, [audioa, audio]
		def       A(  self		:       int						,_SCREAMING_SNAKE_CASE		:       Tuple						,_SCREAMING_SNAKE_CASE		:       List[Any]		)    ->   Union[str, Any]:


						'''simple docstring'''
						A ,		A	 =      examples
						A	 =      audio_classifier(_SCREAMING_SNAKE_CASE		)
						# by default a model is initialized with num_labels=2
						self.assertEqual(
						    _SCREAMING_SNAKE_CASE						,[
						        {'score': ANY(_SCREAMING_SNAKE_CASE		), 'label': ANY(_SCREAMING_SNAKE_CASE		)},
						        {'score': ANY(_SCREAMING_SNAKE_CASE		), 'label': ANY(_SCREAMING_SNAKE_CASE		)},
						    ]						,)
						A	 =      audio_classifier(_SCREAMING_SNAKE_CASE						,top_k=1		)
						self.assertEqual(
						    _SCREAMING_SNAKE_CASE						,[
						        {'score': ANY(_SCREAMING_SNAKE_CASE		), 'label': ANY(_SCREAMING_SNAKE_CASE		)},
						    ]						,)

						self.run_torchaudio(_SCREAMING_SNAKE_CASE		)
		@require_torchaudio
		def       A(  self		:       Optional[int]						,_SCREAMING_SNAKE_CASE		:       int		)    ->   Optional[Any]:


						'''simple docstring'''
						import datasets

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


						'''simple docstring'''
						A	 =      'anton-l/wav2vec2-random-tiny-classifier'

						A	 =      pipeline('audio-classification'						,model=_SCREAMING_SNAKE_CASE		)

						A	 =      np.ones((8_0_0_0,)		)
						A	 =      audio_classifier(_SCREAMING_SNAKE_CASE						,top_k=4		)

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

						A	 =      {'array': np.ones((8_0_0_0,)		), 'sampling_rate': audio_classifier.feature_extractor.sampling_rate}
						A	 =      audio_classifier(_SCREAMING_SNAKE_CASE						,top_k=4		)
						self.assertIn(nested_simplify(_SCREAMING_SNAKE_CASE						,decimals=4		)						,[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]		)
		@require_torch
		@slow
		def       A(  self		:       Optional[int]		)    ->   Union[str, Any]:


						'''simple docstring'''
						import datasets

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

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

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


						'''simple docstring'''
						pass





