





"""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 		__snake_case					(		unittest.TestCase     ):





 """simple docstring"""


 _lowerCamelCase        =     MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
 _lowerCamelCase        =     TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase					,						__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						str						 =     AudioClassificationPipeline(model=__SCREAMING_SNAKE_CASE					,						feature_extractor=__SCREAMING_SNAKE_CASE  )

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

 def 					UpperCamelCase__(    self					,						__lowerCamelCase					,						__lowerCamelCase  ):



     '''simple docstring'''




     __A    :						List[str]						 =     examples
     __A    :						Tuple						 =     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    :						int						 =     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 					UpperCamelCase__(    self					,						__lowerCamelCase  ):



     '''simple docstring'''




     import datasets

     # test with a local file
     __A    :						Optional[int]						 =     datasets.load_dataset('''hf-internal-testing/librispeech_asr_dummy'''					,						'''clean'''					,						split='''validation'''  )
     __A    :						Any						 =     dataset[0]['''audio''']['''array''']
     __A    :						Tuple						 =     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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     __A    :						str						 =     '''anton-l/wav2vec2-random-tiny-classifier'''

     __A    :						str						 =     pipeline('''audio-classification'''					,						model=__SCREAMING_SNAKE_CASE  )

     __A    :						Union[str, Any]						 =     np.ones((8000,)  )
     __A    :						List[Any]						 =     audio_classifier(__SCREAMING_SNAKE_CASE					,						top_k=4  )

     __A    :						Dict						 =     [
         {'''score''': 0.0_8_4_2, '''label''': '''no'''},
         {'''score''': 0.0_8_3_8, '''label''': '''up'''},
         {'''score''': 0.0_8_3_7, '''label''': '''go'''},
         {'''score''': 0.0_8_3_4, '''label''': '''right'''},
     ]
     __A    :						Optional[int]						 =     [
         {'''score''': 0.0_8_4_5, '''label''': '''stop'''},
         {'''score''': 0.0_8_4_4, '''label''': '''on'''},
         {'''score''': 0.0_8_4_1, '''label''': '''right'''},
         {'''score''': 0.0_8_3_4, '''label''': '''left'''},
     ]
     self.assertIn(nested_simplify(__SCREAMING_SNAKE_CASE					,						decimals=4  )					,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]  )

     __A    :						List[str]						 =     {'''array''': np.ones((8000,)  ), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
     __A    :						Dict						 =     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 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     import datasets

     __A    :						Any						 =     '''superb/wav2vec2-base-superb-ks'''

     __A    :						Optional[Any]						 =     pipeline('''audio-classification'''					,						model=__SCREAMING_SNAKE_CASE  )
     __A    :						Optional[Any]						 =     datasets.load_dataset('''anton-l/superb_dummy'''					,						'''ks'''					,						split='''test'''  )

     __A    :						int						 =     np.array(dataset[3]['''speech''']					,						dtype=np.floataa  )
     __A    :						Optional[Any]						 =     audio_classifier(__SCREAMING_SNAKE_CASE					,						top_k=4  )
     self.assertEqual(
         nested_simplify(__SCREAMING_SNAKE_CASE					,						decimals=3  )					,						[
             {'''score''': 0.9_8_1, '''label''': '''go'''},
             {'''score''': 0.0_0_7, '''label''': '''up'''},
             {'''score''': 0.0_0_6, '''label''': '''_unknown_'''},
             {'''score''': 0.0_0_1, '''label''': '''down'''},
         ]					,						)







 @require_tf
 @unittest.skip('''Audio classification is not implemented for TF'''  )
 def 					UpperCamelCase__(    self  ):



     '''simple docstring'''




     pass






'''simple docstring'''







import copy
import os
from typing import Union

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


_lowercase :  Optional[Any]    							=       logging.get_logger(__name__)

_lowercase :  List[str]    							=       {
    "google/pix2struct-textcaps-base": (
        "https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"
    ),
}






class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_text_model'''
					lowerCAmelCase_        =     ['''past_key_values''']
					lowerCAmelCase_        =     {
					    '''hidden_size''': '''hidden_size''',
					    '''num_attention_heads''': '''num_heads''',
					    '''num_hidden_layers''': '''num_layers''',
					}






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=5_02_44				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							__SCREAMING_SNAKE_CASE=0.1				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=0				,							__SCREAMING_SNAKE_CASE=1				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											lowercase_	:     Any													=       vocab_size
											lowercase_	:     Tuple													=       hidden_size
											lowercase_	:     Optional[Any]													=       d_kv
											lowercase_	:     List[str]													=       d_ff
											lowercase_	:     List[str]													=       num_layers
											lowercase_	:     Optional[Any]													=       num_heads
											lowercase_	:     Union[str, Any]													=       relative_attention_num_buckets
											lowercase_	:     Optional[int]													=       relative_attention_max_distance
											lowercase_	:     Union[str, Any]													=       dropout_rate
											lowercase_	:     Dict													=       layer_norm_epsilon
											lowercase_	:     Dict													=       initializer_factor
											lowercase_	:     List[Any]													=       use_cache

											lowercase_	:     Optional[int]													=       eos_token_id
											lowercase_	:     Optional[int]													=       decoder_start_token_id

											# for backwards compatibility
											lowercase_	:     Any													=       dense_act_fn

											super().__init__(
											    pad_token_id=__SCREAMING_SNAKE_CASE				,							eos_token_id=__SCREAMING_SNAKE_CASE				,							decoder_start_token_id=__SCREAMING_SNAKE_CASE				,							tie_word_embeddings=__SCREAMING_SNAKE_CASE				,							is_decoder=__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE				,							)






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     Optional[int]													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											# get the text config dict if we are loading from Pix2StructConfig
											if config_dict.get('''model_type'''		) == "pix2struct":
																	lowercase_	:     List[Any]													=       config_dict['''text_config''']

											if "model_type" in config_dict and hasattr(cls				,							'''model_type'''		) and config_dict["model_type"] != cls.model_type:
																	logger.warning(
																	    F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
																	    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''		)

											return cls.from_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct_vision_model'''






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=7_68				,							__SCREAMING_SNAKE_CASE=20_48				,							__SCREAMING_SNAKE_CASE=64				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE=12				,							__SCREAMING_SNAKE_CASE="gelu_new"				,							__SCREAMING_SNAKE_CASE=1E-6				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=0.0				,							__SCREAMING_SNAKE_CASE=1E-1_0				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=40_96				,							__SCREAMING_SNAKE_CASE=32				,							__SCREAMING_SNAKE_CASE=1_28				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											super().__init__(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Union[str, Any]													=       hidden_size
											lowercase_	:     Any													=       patch_embed_hidden_size
											lowercase_	:     List[Any]													=       d_ff
											lowercase_	:     Dict													=       dropout_rate
											lowercase_	:     Any													=       num_hidden_layers
											lowercase_	:     Any													=       num_attention_heads
											lowercase_	:     int													=       initializer_range
											lowercase_	:     Dict													=       initializer_factor
											lowercase_	:     Dict													=       attention_dropout
											lowercase_	:     Optional[Any]													=       layer_norm_eps
											lowercase_	:     str													=       dense_act_fn
											lowercase_	:     Dict													=       seq_len
											lowercase_	:     List[Any]													=       relative_attention_num_buckets
											lowercase_	:     int													=       relative_attention_max_distance
											lowercase_	:     Optional[int]													=       d_kv






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											cls._set_token_in_kwargs(__SCREAMING_SNAKE_CASE		)

											lowercase_						, lowercase_	:     str													=       cls.get_config_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											# get the vision config dict if we are loading from Pix2StructConfig
											if config_dict.get('''model_type'''		) == "pix2struct":
																	lowercase_	:     Optional[int]													=       config_dict['''vision_config''']

											if "model_type" in config_dict and hasattr(cls				,							'''model_type'''		) and config_dict["model_type"] != cls.model_type:
																	logger.warning(
																	    F'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
																	    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''		)

											return cls.from_dict(__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)







class  lowerCAmelCase__   (  lowerCamelCase_     ):
					lowerCAmelCase_        =     '''pix2struct'''
					lowerCAmelCase_        =     True






					def __init__(   self				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=None				,							__SCREAMING_SNAKE_CASE=1.0				,							__SCREAMING_SNAKE_CASE=0.02				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=False				,							__SCREAMING_SNAKE_CASE=True				,							**__SCREAMING_SNAKE_CASE				,							):


											"""simple docstring"""





											super().__init__(tie_word_embeddings=__SCREAMING_SNAKE_CASE				,							is_encoder_decoder=__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		)

											if text_config is None:
																	lowercase_	:     Optional[Any]													=       {}
																	logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.'''		)

											if vision_config is None:
																	lowercase_	:     Dict													=       {}
																	logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.'''		)

											lowercase_	:     str													=       PixaStructTextConfig(**__SCREAMING_SNAKE_CASE		)
											lowercase_	:     Tuple													=       PixaStructVisionConfig(**__SCREAMING_SNAKE_CASE		)

											lowercase_	:     Optional[Any]													=       self.text_config.decoder_start_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.pad_token_id
											lowercase_	:     Union[str, Any]													=       self.text_config.eos_token_id

											lowercase_	:     int													=       initializer_factor
											lowercase_	:     Any													=       initializer_range

											lowercase_	:     str													=       self.initializer_range
											lowercase_	:     str													=       self.initializer_range

											lowercase_	:     int													=       is_vqa






					@classmethod
					def 	_snake_case    (   cls				,							__SCREAMING_SNAKE_CASE				,							__SCREAMING_SNAKE_CASE				,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											return cls(text_config=text_config.to_dict()				,							vision_config=vision_config.to_dict()				,							**__SCREAMING_SNAKE_CASE		)






					def 	_snake_case    (   self		):


											"""simple docstring"""





											lowercase_	:     Tuple													=       copy.deepcopy(self.__dict__		)
											lowercase_	:     Any													=       self.text_config.to_dict()
											lowercase_	:     Optional[Any]													=       self.vision_config.to_dict()
											lowercase_	:     Optional[int]													=       self.__class__.model_type
											return output



