






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   ):






      '''simple docstring'''




      __snake_case       =  MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
      __snake_case       =  TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING







      def       __lowerCAmelCase					(      self      :						Union[str, Any] ,					__UpperCAmelCase      :						Optional[int] ,					__UpperCAmelCase      :						Optional[Any] ,					__UpperCAmelCase      :						Optional[int]  )    ->List[str]:




       """simple docstring"""


       a		  =      AudioClassificationPipeline(model=__UpperCAmelCase ,					feature_extractor=__UpperCAmelCase  )

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







      def       __lowerCAmelCase					(      self      :						Union[str, Any] ,					__UpperCAmelCase      :						Optional[int] ,					__UpperCAmelCase      :						Dict  )    ->Any:




       """simple docstring"""


       a  ,     a		  =      examples
       a		  =      audio_classifier(__UpperCAmelCase  )
       # by default a model is initialized with num_labels=2
       self.assertEqual(
           __UpperCAmelCase ,					[
               {'''score''': ANY(__UpperCAmelCase  ), '''label''': ANY(__UpperCAmelCase  )},
               {'''score''': ANY(__UpperCAmelCase  ), '''label''': ANY(__UpperCAmelCase  )},
           ] ,					)
       a		  =      audio_classifier(__UpperCAmelCase ,					top_k=1  )
       self.assertEqual(
           __UpperCAmelCase ,					[
               {'''score''': ANY(__UpperCAmelCase  ), '''label''': ANY(__UpperCAmelCase  )},
           ] ,					)

       self.run_torchaudio(__UpperCAmelCase  )







      @require_torchaudio
      def       __lowerCAmelCase					(      self      :						Dict ,					__UpperCAmelCase      :						Dict  )    ->Dict:




       """simple docstring"""


       import datasets

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







      @require_torch
      def       __lowerCAmelCase					(      self      :						Dict  )    ->Tuple:




       """simple docstring"""


       a		  =      '''anton-l/wav2vec2-random-tiny-classifier'''

       a		  =      pipeline('''audio-classification''' ,					model=__UpperCAmelCase  )

       a		  =      np.ones((8_000,)  )
       a		  =      audio_classifier(__UpperCAmelCase ,					top_k=4  )

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

       a		  =      {'''array''': np.ones((8_000,)  ), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
       a		  =      audio_classifier(__UpperCAmelCase ,					top_k=4  )
       self.assertIn(nested_simplify(__UpperCAmelCase ,					decimals=4  ) ,					[EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]  )







      @require_torch
      @slow
      def       __lowerCAmelCase					(      self      :						Tuple  )    ->Dict:




       """simple docstring"""


       import datasets

       a		  =      '''superb/wav2vec2-base-superb-ks'''

       a		  =      pipeline('''audio-classification''' ,					model=__UpperCAmelCase  )
       a		  =      datasets.load_dataset('''anton-l/superb_dummy''' ,					'''ks''' ,					split='''test'''  )

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




       """simple docstring"""


       pass










from math import factorial

UpperCAmelCase__				   =		{str(digit): factorial(digit) for digit in range(10)}
def 						_a    (      a :int )							->			int:
 if not isinstance(a	,							a ):
  raise TypeError('''Parameter number must be int''' )

 if number < 0:
  raise ValueError('''Parameter number must be greater than or equal to 0''' )

 # Converts number in string to iterate on its digits and adds its factorial.
 return sum(DIGIT_FACTORIAL[digit] for digit in str(a ) )
def 						_a    (      a :int = 60	,							a :int = 1_000_000 )							->			int:

 if not isinstance(a	,							a ) or not isinstance(a	,							a ):
  raise TypeError('''Parameters chain_length and number_limit must be int''' )

 if chain_length <= 0 or number_limit <= 0:
  raise ValueError(
      '''Parameters chain_length and number_limit must be greater than 0''' )

 # the counter for the chains with the exact desired length
 a		  =      0
 # the cached sizes of the previous chains
 a		  =      {}

 for start_chain_element in range(1	,							a ):
  # The temporary set will contain the elements of the chain
  a		  =      set()
  a		  =      0

  # Stop computing the chain when you find a cached size, a repeating item or the
  # length is greater then the desired one.
  a		  =      start_chain_element
  while (
      chain_element not in chain_sets_lengths
      and chain_element not in chain_set
      and chain_set_length <= chain_length
  ):
   chain_set.add(a )
   chain_set_length += 1
   a		  =      digit_factorial_sum(a )

  if chain_element in chain_sets_lengths:
   chain_set_length += chain_sets_lengths[chain_element]

  a		  =      chain_set_length

  # If chain contains the exact amount of elements increase the counter
  if chain_set_length == chain_length:
   chains_counter += 1

 return chains_counter


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     print(f"""{solution()}""")


