


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 						A__							(		unittest.TestCase  ):
						UpperCAmelCase             =						MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
						UpperCAmelCase             =						TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




						def 							__UpperCamelCase				(       self					:      str				,    _a					:      str				,    _a					:      Dict				,    _a					:      str  )							->     int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =AudioClassificationPipeline(model=_a				,    feature_extractor=_a  )

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




						def 							__UpperCamelCase				(       self					:      Tuple				,    _a					:      Any				,    _a					:      Dict  )							->     Optional[Any]:


										"""simple docstring"""





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

										self.run_torchaudio(_a  )




						@require_torchaudio
						def 							__UpperCamelCase				(       self					:      Union[str, Any]				,    _a					:      Any  )							->     Tuple:


										"""simple docstring"""





										import datasets

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




						@require_torch
						def 							__UpperCamelCase				(       self					:      Optional[int]  )							->     int:


										"""simple docstring"""





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

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

										_SCREAMING_SNAKE_CASE			  =np.ones((8000,)  )
										_SCREAMING_SNAKE_CASE			  =audio_classifier(_a				,    top_k=4  )

										_SCREAMING_SNAKE_CASE			  =[
										    {'''score''': 0.08_42, '''label''': '''no'''},
										    {'''score''': 0.08_38, '''label''': '''up'''},
										    {'''score''': 0.08_37, '''label''': '''go'''},
										    {'''score''': 0.08_34, '''label''': '''right'''},
										]
										_SCREAMING_SNAKE_CASE			  =[
										    {'''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(_a				,    decimals=4  )				,    [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]  )

										_SCREAMING_SNAKE_CASE			  ={'''array''': np.ones((8000,)  ), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
										_SCREAMING_SNAKE_CASE			  =audio_classifier(_a				,    top_k=4  )
										self.assertIn(nested_simplify(_a				,    decimals=4  )				,    [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]  )




						@require_torch
						@slow
						def 							__UpperCamelCase				(       self					:      Optional[Any]  )							->     int:


										"""simple docstring"""





										import datasets

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

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

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


										"""simple docstring"""





										pass



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


snake_case_						:			str													=							logging.get_logger(__name__)

snake_case_						:			str													=							{
    '''facebook/data2vec-text-base''': '''https://huggingface.co/data2vec/resolve/main/config.json''',
}

class 						A__							(		UpperCamelCase__  ):
						UpperCAmelCase             =						"data2vec-text"




						def __init__(       self					:      Optional[int]				,    _a					:      Union[str, Any]=3_0522				,    _a					:      Any=768				,    _a					:      List[Any]=12				,    _a					:      Any=12				,    _a					:      List[str]=3072				,    _a					:      Optional[Any]="gelu"				,    _a					:      List[str]=0.1				,    _a					:      Optional[Any]=0.1				,    _a					:      Optional[Any]=512				,    _a					:      Any=2				,    _a					:      Optional[Any]=0.02				,    _a					:      Dict=1E-12				,    _a					:      int=1				,    _a					:      Any=0				,    _a					:      List[Any]=2				,    _a					:      Dict="absolute"				,    _a					:      Optional[Any]=True				,    _a					:      Dict=None				,    **_a					:      Optional[Any]				,    )							->     Union[str, Any]:


										"""simple docstring"""





										super().__init__(pad_token_id=_a				,    bos_token_id=_a				,    eos_token_id=_a				,    **_a  )

										_SCREAMING_SNAKE_CASE			  =vocab_size
										_SCREAMING_SNAKE_CASE			  =hidden_size
										_SCREAMING_SNAKE_CASE			  =num_hidden_layers
										_SCREAMING_SNAKE_CASE			  =num_attention_heads
										_SCREAMING_SNAKE_CASE			  =hidden_act
										_SCREAMING_SNAKE_CASE			  =intermediate_size
										_SCREAMING_SNAKE_CASE			  =hidden_dropout_prob
										_SCREAMING_SNAKE_CASE			  =attention_probs_dropout_prob
										_SCREAMING_SNAKE_CASE			  =max_position_embeddings
										_SCREAMING_SNAKE_CASE			  =type_vocab_size
										_SCREAMING_SNAKE_CASE			  =initializer_range
										_SCREAMING_SNAKE_CASE			  =layer_norm_eps
										_SCREAMING_SNAKE_CASE			  =position_embedding_type
										_SCREAMING_SNAKE_CASE			  =use_cache
										_SCREAMING_SNAKE_CASE			  =classifier_dropout

class 						A__							(		UpperCamelCase__  ):




						@property
						def 							__UpperCamelCase				(       self					:      List[str]  )							->     Mapping[str, Mapping[int, str]]:


										"""simple docstring"""





										if self.task == "multiple-choice":
														_SCREAMING_SNAKE_CASE			  ={0: '''batch''', 1: '''choice''', 2: '''sequence'''}
										else:
														_SCREAMING_SNAKE_CASE			  ={0: '''batch''', 1: '''sequence'''}
										return OrderedDict(
										    [
										        ('''input_ids''', dynamic_axis),
										        ('''attention_mask''', dynamic_axis),
										    ]  )