




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       :      Optional[int]			=      MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
  _UpperCamelCase       :      Union[str, Any]			=      TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING






  def  __a      (  self		:     List[Any]				,						_A		:     str				,						_A		:     Optional[Any]				,						_A		:     Optional[int]				)		->   Optional[int]:






       """simple docstring"""







       lowercase :       Optional[int]       =			AudioClassificationPipeline(model=_A				,						feature_extractor=_A				)

       # test with a raw waveform
       lowercase :       Tuple       =			np.zeros((34_000,)				)
       lowercase :       Dict       =			np.zeros((14_000,)				)
       return audio_classifier, [audioa, audio]






  def  __a      (  self		:     List[Any]				,						_A		:     List[Any]				,						_A		:     List[str]				)		->   List[str]:






       """simple docstring"""







       lowercase      ,	lowercase :       List[str]       =			examples
       lowercase :       int       =			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				)},
           ]				,						)
       lowercase :       Dict       =			audio_classifier(_A				,						top_k=1				)
       self.assertEqual(
           _A				,						[
               {'''score''': ANY(_A				), '''label''': ANY(_A				)},
           ]				,						)

       self.run_torchaudio(_A				)






  @require_torchaudio
  def  __a      (  self		:     List[str]				,						_A		:     Optional[Any]				)		->   int:






       """simple docstring"""







       import datasets

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






  @require_torch
  def  __a      (  self		:     str				)		->   Union[str, Any]:






       """simple docstring"""







       lowercase :       Any       =			'''anton-l/wav2vec2-random-tiny-classifier'''

       lowercase :       str       =			pipeline('''audio-classification'''				,						model=_A				)

       lowercase :       Optional[Any]       =			np.ones((8_000,)				)
       lowercase :       Tuple       =			audio_classifier(_A				,						top_k=4				)

       lowercase :       Dict       =			[
           {'''score''': 0.0_842, '''label''': '''no'''},
           {'''score''': 0.0_838, '''label''': '''up'''},
           {'''score''': 0.0_837, '''label''': '''go'''},
           {'''score''': 0.0_834, '''label''': '''right'''},
       ]
       lowercase :       int       =			[
           {'''score''': 0.0_845, '''label''': '''stop'''},
           {'''score''': 0.0_844, '''label''': '''on'''},
           {'''score''': 0.0_841, '''label''': '''right'''},
           {'''score''': 0.0_834, '''label''': '''left'''},
       ]
       self.assertIn(nested_simplify(_A				,						decimals=4				)				,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]				)

       lowercase :       Optional[int]       =			{'''array''': np.ones((8_000,)				), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
       lowercase :       Any       =			audio_classifier(_A				,						top_k=4				)
       self.assertIn(nested_simplify(_A				,						decimals=4				)				,						[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]				)






  @require_torch
  @slow
  def  __a      (  self		:     str				)		->   str:






       """simple docstring"""







       import datasets

       lowercase :       Tuple       =			'''superb/wav2vec2-base-superb-ks'''

       lowercase :       List[str]       =			pipeline('''audio-classification'''				,						model=_A				)
       lowercase :       Optional[int]       =			datasets.load_dataset('''anton-l/superb_dummy'''				,						'''ks'''				,						split='''test'''				)

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






       """simple docstring"""







       pass





class 						_A :  # Public class to implement a graph






  def __init__(  self		:     List[Any]				,						_A		:     int				,						_A		:     int				,						_A		:     list[list[bool]]				)		->   None:






       """simple docstring"""







       lowercase :       Tuple       =			row
       lowercase :       Union[str, Any]       =			col
       lowercase :       int       =			graph






  def  __a      (  self		:     List[Any]				,						_A		:     int				,						_A		:     int				,						_A		:     list[list[bool]]				)		->   bool:






       """simple docstring"""







       return (
           0 <= i < self.ROW
           and 0 <= j < self.COL
           and not visited[i][j]
           and self.graph[i][j]
       )






  def  __a      (  self		:     int				,						_A		:     int				,						_A		:     int				,						_A		:     list[list[bool]]				)		->   None:






       """simple docstring"""







       lowercase :       List[str]       =			[-1, -1, -1, 0, 0, 1, 1, 1]  # Coordinate order
       lowercase :       Dict       =			[-1, 0, 1, -1, 1, -1, 0, 1]
       lowercase :       Dict       =			True  # Make those cells visited
       for k in range(8				):
            if self.is_safe(i + row_nbr[k]				,						j + col_nbr[k]				,						_A				):
                 self.diffs(i + row_nbr[k]				,						j + col_nbr[k]				,						_A				)






  def  __a      (  self		:     List[str]				)		->   int:  # And finally, count all islands.






       """simple docstring"""







       lowercase :       List[str]       =			[[False for j in range(self.COL				)] for i in range(self.ROW				)]
       lowercase :       Optional[Any]       =			0
       for i in range(self.ROW				):
            for j in range(self.COL				):
                 if visited[i][j] is False and self.graph[i][j] == 1:
                      self.diffs(_A				,						_A				,						_A				)
                      count += 1
       return count