




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






   '''simple docstring'''





   UpperCamelCase__								=MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
   UpperCamelCase__								=TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING

   def 							UpperCAmelCase__       ( self			:   Union[str, Any]       ,						lowerCamelCase_			:   Any       ,						lowerCamelCase_			:   Any       ,						lowerCamelCase_			:   Tuple  )						->   int:
        __magic_name__							:	str    							=							AudioClassificationPipeline(model=lowerCamelCase_       ,						feature_extractor=lowerCamelCase_  )

        # test with a raw waveform
        __magic_name__							:	Tuple    							=							np.zeros((34000,)  )
        __magic_name__							:	Union[str, Any]    							=							np.zeros((14000,)  )
        return audio_classifier, [audioa, audio]

   def 							UpperCAmelCase__       ( self			:   str       ,						lowerCamelCase_			:   Tuple       ,						lowerCamelCase_			:   List[str]  )						->   Tuple:
        __magic_name__							:	Union[str, Any]    							=							examples
        __magic_name__							:	Tuple    							=							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_  )},
            ]       ,						)
        __magic_name__							:	Any    							=							audio_classifier(lowerCamelCase_       ,						top_k=1  )
        self.assertEqual(
            lowerCamelCase_       ,						[
                {'''score''': ANY(lowerCamelCase_  ), '''label''': ANY(lowerCamelCase_  )},
            ]       ,						)

        self.run_torchaudio(lowerCamelCase_  )

   @require_torchaudio
   def 							UpperCAmelCase__       ( self			:   int       ,						lowerCamelCase_			:   Optional[int]  )						->   int:
        import datasets

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

   @require_torch
   def 							UpperCAmelCase__       ( self			:   Tuple  )						->   int:
        __magic_name__							:	int    							=							'''anton-l/wav2vec2-random-tiny-classifier'''

        __magic_name__							:	List[Any]    							=							pipeline('''audio-classification'''       ,						model=lowerCamelCase_  )

        __magic_name__							:	List[Any]    							=							np.ones((8000,)  )
        __magic_name__							:	Tuple    							=							audio_classifier(lowerCamelCase_       ,						top_k=4  )

        __magic_name__							:	List[Any]    							=							[
            {'''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'''},
        ]
        __magic_name__							:	Any    							=							[
            {'''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(lowerCamelCase_       ,						decimals=4  )       ,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]  )

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

   @require_torch
   @slow
   def 							UpperCAmelCase__       ( self			:   Any  )						->   List[Any]:
        import datasets

        __magic_name__							:	List[str]    							=							'''superb/wav2vec2-base-superb-ks'''

        __magic_name__							:	List[str]    							=							pipeline('''audio-classification'''       ,						model=lowerCamelCase_  )
        __magic_name__							:	List[Any]    							=							datasets.load_dataset('''anton-l/superb_dummy'''       ,						'''ks'''       ,						split='''test'''  )

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









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


__lowerCamelCase   :						Optional[int]				=			logging.get_logger(__name__)

__lowerCamelCase   :						Dict				=			{}


class    lowerCamelCase    (		_lowerCamelCase						):






			'''simple docstring'''





			UpperCamelCase__								='''llama'''
			UpperCamelCase__								=['''past_key_values''']

			def __init__( self			:   Dict       ,						lowerCamelCase_			:   List[str]=32000       ,						lowerCamelCase_			:   List[Any]=4096       ,						lowerCamelCase_			:   Any=11008       ,						lowerCamelCase_			:   Optional[Any]=32       ,						lowerCamelCase_			:   Dict=32       ,						lowerCamelCase_			:   Optional[Any]=None       ,						lowerCamelCase_			:   Dict="silu"       ,						lowerCamelCase_			:   Optional[int]=2048       ,						lowerCamelCase_			:   int=0.0_2       ,						lowerCamelCase_			:   Tuple=1E-6       ,						lowerCamelCase_			:   Tuple=True       ,						lowerCamelCase_			:   List[str]=0       ,						lowerCamelCase_			:   List[str]=1       ,						lowerCamelCase_			:   Optional[int]=2       ,						lowerCamelCase_			:   List[str]=1       ,						lowerCamelCase_			:   Dict=False       ,						lowerCamelCase_			:   int=None       ,						**lowerCamelCase_			:   Tuple       ,						)						->   Tuple:
								__magic_name__							:	Any    							=							vocab_size
								__magic_name__							:	Optional[int]    							=							max_position_embeddings
								__magic_name__							:	Union[str, Any]    							=							hidden_size
								__magic_name__							:	Dict    							=							intermediate_size
								__magic_name__							:	Union[str, Any]    							=							num_hidden_layers
								__magic_name__							:	Tuple    							=							num_attention_heads

								# for backward compatibility
								if num_key_value_heads is None:
													__magic_name__							:	Optional[int]    							=							num_attention_heads

								__magic_name__							:	Optional[int]    							=							num_key_value_heads
								__magic_name__							:	List[str]    							=							hidden_act
								__magic_name__							:	List[str]    							=							initializer_range
								__magic_name__							:	Tuple    							=							rms_norm_eps
								__magic_name__							:	Optional[int]    							=							pretraining_tp
								__magic_name__							:	Any    							=							use_cache
								__magic_name__							:	int    							=							rope_scaling
								self._rope_scaling_validation()

								super().__init__(
								    pad_token_id=lowerCamelCase_       ,						bos_token_id=lowerCamelCase_       ,						eos_token_id=lowerCamelCase_       ,						tie_word_embeddings=lowerCamelCase_       ,						**lowerCamelCase_       ,						)


			def 							UpperCAmelCase__       ( self			:   str  )						->   Tuple:
								if self.rope_scaling is None:
													return

								if not isinstance(self.rope_scaling       ,						lowerCamelCase_  ) or len(self.rope_scaling  ) != 2:
													raise ValueError(
													    '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
													    F'''got {self.rope_scaling}'''  )
								__magic_name__							:	List[str]    							=							self.rope_scaling.get('''type'''       ,						lowerCamelCase_  )
								__magic_name__							:	str    							=							self.rope_scaling.get('''factor'''       ,						lowerCamelCase_  )
								if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
													raise ValueError(
													    F'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}'''  )
								if rope_scaling_factor is None or not isinstance(lowerCamelCase_       ,						lowerCamelCase_  ) or rope_scaling_factor <= 1.0:
													raise ValueError(F'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}'''  )



