


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 							lowercase  (						unittest.TestCase			):
     lowerCamelCase			:			Any      = MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
     lowerCamelCase			:			int      = TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING







     def 							lowercase__       (						self :						str   ,   _lowercase :						Union[str, Any]   ,   _lowercase :						str   ,   _lowercase :						int      ):
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		AudioClassificationPipeline(model=_lowercase   ,   feature_extractor=_lowercase      )

           # test with a raw waveform
           SCREAMING_SNAKE_CASE__       :  List[Any]						     =		np.zeros((3_40_00,)      )
           SCREAMING_SNAKE_CASE__       :  Dict						     =		np.zeros((1_40_00,)      )
           return audio_classifier, [audioa, audio]







     def 							lowercase__       (						self :						List[str]   ,   _lowercase :						Optional[int]   ,   _lowercase :						Dict      ):
           SCREAMING_SNAKE_CASE__     ,       SCREAMING_SNAKE_CASE__       :  Dict						     =		examples
           SCREAMING_SNAKE_CASE__       :  Dict						     =		audio_classifier(_lowercase      )
           # by default a model is initialized with num_labels=2
           self.assertEqual(
               _lowercase   ,   [
                   {'''score''': ANY(_lowercase      ), '''label''': ANY(_lowercase      )},
                   {'''score''': ANY(_lowercase      ), '''label''': ANY(_lowercase      )},
               ]   ,   )
           SCREAMING_SNAKE_CASE__       :  str						     =		audio_classifier(_lowercase   ,   top_k=1      )
           self.assertEqual(
               _lowercase   ,   [
                   {'''score''': ANY(_lowercase      ), '''label''': ANY(_lowercase      )},
               ]   ,   )

           self.run_torchaudio(_lowercase      )







     @require_torchaudio
     def 							lowercase__       (						self :						Union[str, Any]   ,   _lowercase :						List[str]      ):
           import datasets

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







     @require_torch
     def 							lowercase__       (						self :						Union[str, Any]      ):
           SCREAMING_SNAKE_CASE__       :  int						     =		'''anton-l/wav2vec2-random-tiny-classifier'''

           SCREAMING_SNAKE_CASE__       :  str						     =		pipeline('''audio-classification'''   ,   model=_lowercase      )

           SCREAMING_SNAKE_CASE__       :  List[str]						     =		np.ones((80_00,)      )
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		audio_classifier(_lowercase   ,   top_k=4      )

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

           SCREAMING_SNAKE_CASE__       :  Tuple						     =		{'''array''': np.ones((80_00,)      ), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
           SCREAMING_SNAKE_CASE__       :  List[str]						     =		audio_classifier(_lowercase   ,   top_k=4      )
           self.assertIn(nested_simplify(_lowercase   ,   decimals=4      )   ,   [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]      )







     @require_torch
     @slow
     def 							lowercase__       (						self :						List[str]      ):
           import datasets

           SCREAMING_SNAKE_CASE__       :  Union[str, Any]						     =		'''superb/wav2vec2-base-superb-ks'''

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

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








'''simple docstring'''

import torch
from torch import nn
from transformers import CLIPPreTrainedModel, CLIPVisionModel

from ...models.attention import BasicTransformerBlock
from ...utils import logging


snake_case									=    logging.get_logger(__name__)  # pylint: disable=invalid-name




class    lowerCAmelCase       (     UpperCamelCase_  ):


 def __init__(    self  : Union[str, Any]			,							a__  : int			,							a__  : List[Any]=768    ):
  '''simple docstring'''

  super().__init__(a__    )
  lowerCAmelCase__     :   int     		=	proj_size

  lowerCAmelCase__     :   str     		=	CLIPVisionModel(a__    )
  lowerCAmelCase__     :   int     		=	PaintByExampleMapper(a__    )
  lowerCAmelCase__     :   Optional[int]     		=	nn.LayerNorm(config.hidden_size    )
  lowerCAmelCase__     :   Optional[Any]     		=	nn.Linear(config.hidden_size			,							self.proj_size    )

  # uncondition for scaling
  lowerCAmelCase__     :   Dict     		=	nn.Parameter(torch.randn((1, 1, self.proj_size)    )    )


 def    _A	(    self  : Tuple			,							a__  : int			,							a__  : Optional[Any]=False    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[Any]     		=	self.model(pixel_values=a__    )
  lowerCAmelCase__     :   Any     		=	clip_output.pooler_output
  lowerCAmelCase__     :   Union[str, Any]     		=	self.mapper(latent_states[:, None]    )
  lowerCAmelCase__     :   List[Any]     		=	self.final_layer_norm(a__    )
  lowerCAmelCase__     :   Optional[int]     		=	self.proj_out(a__    )
  if return_uncond_vector:
   return latent_states, self.uncond_vector

  return latent_states




class    lowerCAmelCase       (     nn.Module  ):


 def __init__(    self  : Optional[Any]			,							a__  : Optional[Any]    ):
  '''simple docstring'''

  super().__init__()
  lowerCAmelCase__     :   Optional[Any]     		=	(config.num_hidden_layers + 1) // 5
  lowerCAmelCase__     :   str     		=	config.hidden_size
  lowerCAmelCase__     :   Dict     		=	1
  lowerCAmelCase__     :   int     		=	nn.ModuleList(
      [
          BasicTransformerBlock(a__			,							a__			,							a__			,							activation_fn="gelu"			,							attention_bias=a__    )
          for _ in range(a__    )
      ]    )


 def    _A	(    self  : int			,							a__  : Union[str, Any]    ):
  '''simple docstring'''

  for block in self.blocks:
   lowerCAmelCase__     :   List[Any]     		=	block(a__    )

  return hidden_states





