


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




 '''simple docstring'''
 __lowercase					:		List[str]     				=    MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
 __lowercase					:		Any     				=    TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		List[str]	  =      AudioClassificationPipeline(model=__lowercase   ,   feature_extractor=__lowercase    )

  # test with a raw waveform
  __A	:		int	  =      np.zeros((34_000,)    )
  __A	:		str	  =      np.zeros((14_000,)    )
  return audio_classifier, [audioa, audio]


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A  ,__A	:		Tuple	  =      examples
  __A	:		Optional[int]	  =      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    )},
      ]   ,   )
  __A	:		List[Any]	  =      audio_classifier(__lowercase   ,   top_k=1    )
  self.assertEqual(
      __lowercase   ,   [
          {'score': ANY(__lowercase    ), 'label': ANY(__lowercase    )},
      ]   ,   )

  self.run_torchaudio(__lowercase    )


 @require_torchaudio
 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  import datasets

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


 @require_torch
 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Union[str, Any]	  =      'anton-l/wav2vec2-random-tiny-classifier'

  __A	:		Dict	  =      pipeline('audio-classification'   ,   model=__lowercase    )

  __A	:		Union[str, Any]	  =      np.ones((8_000,)    )
  __A	:		Union[str, Any]	  =      audio_classifier(__lowercase   ,   top_k=4    )

  __A	:		Tuple	  =      [
      {'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(__lowercase   ,   decimals=4    )   ,   [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]    )

  __A	:		List[Any]	  =      {'array': np.ones((8_000,)    ), 'sampling_rate': audio_classifier.feature_extractor.sampling_rate}
  __A	:		Tuple	  =      audio_classifier(__lowercase   ,   top_k=4    )
  self.assertIn(nested_simplify(__lowercase   ,   decimals=4    )   ,   [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]    )


 @require_torch
 @slow
 def 			snake_case__ (   self    ):

  """simple docstring"""







  import datasets

  __A	:		Optional[int]	  =      'superb/wav2vec2-base-superb-ks'

  __A	:		Dict	  =      pipeline('audio-classification'   ,   model=__lowercase    )
  __A	:		str	  =      datasets.load_dataset('anton-l/superb_dummy'   ,   'ks'   ,   split='test'    )

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

  """simple docstring"""







  pass










'''simple docstring'''
from ...configuration_utils import PretrainedConfig
from ...utils import logging


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

UpperCAmelCase_			:  List[str]										=   {
    'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/config.json',
    'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/config.json'
    # See all FNet models at https://huggingface.co/models?filter=fnet
}

class     _lowerCamelCase       (     snake_case_					):




 '''simple docstring'''
 __lowercase					:		List[Any]     				=    '''fnet'''


 def __init__(   self   ,   __lowercase=32_000   ,   __lowercase=768   ,   __lowercase=12   ,   __lowercase=3_072   ,   __lowercase="gelu_new"   ,   __lowercase=0.1   ,   __lowercase=512   ,   __lowercase=4   ,   __lowercase=0.0_2   ,   __lowercase=1E-12   ,   __lowercase=False   ,   __lowercase=512   ,   __lowercase=3   ,   __lowercase=1   ,   __lowercase=2   ,   **__lowercase   ,   ):

  """simple docstring"""







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

  __A	:		Union[str, Any]	  =      vocab_size
  __A	:		Dict	  =      max_position_embeddings
  __A	:		List[str]	  =      hidden_size
  __A	:		Tuple	  =      num_hidden_layers
  __A	:		Optional[int]	  =      intermediate_size
  __A	:		Dict	  =      hidden_act
  __A	:		List[str]	  =      hidden_dropout_prob
  __A	:		str	  =      initializer_range
  __A	:		Dict	  =      type_vocab_size
  __A	:		int	  =      layer_norm_eps
  __A	:		Tuple	  =      use_tpu_fourier_optimizations
  __A	:		Optional[Any]	  =      tpu_short_seq_length






