


'''simple docstring'''
import logging
import os
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

from tqdm import auto as tqdm_lib


UpperCamelCase__          = {
    '''debug''': logging.DEBUG,
    '''info''': logging.INFO,
    '''warning''': logging.WARNING,
    '''error''': logging.ERROR,
    '''critical''': logging.CRITICAL,
}

UpperCamelCase__          = logging.WARNING





def    a__			(   )       ->      Optional[int]:
    UpperCAmelCase__     :						int						=							os.getenv('''DATASETS_VERBOSITY'''    ,   lowerCAmelCase__		)
    if env_level_str:
        if env_level_str in log_levels:
            return log_levels[env_level_str]
        else:
            logging.getLogger().warning(
                F"""Unknown option DATASETS_VERBOSITY={env_level_str}, """
                F"""has to be one of: { ", ".join(log_levels.keys()		) }"""		)
    return _default_log_level





def    a__			(   )       ->      str:
    return __name__.split('''.'''		)[0]





def    a__			(   )       ->      logging.Logger:
    return logging.getLogger(_get_library_name()		)





def    a__			(   )       ->      None:
    # Apply our default configuration to the library root logger.
    UpperCAmelCase__     :						Optional[Any]						=							_get_library_root_logger()
    library_root_logger.setLevel(_get_default_logging_level()		)





def    a__			(   )       ->      None:
    UpperCAmelCase__     :						str						=							_get_library_root_logger()
    library_root_logger.setLevel(logging.NOTSET		)





def    a__			(   lowerCAmelCase__ = None		)       ->      logging.Logger:
    if name is None:
        UpperCAmelCase__     :						int						=							_get_library_name()
    return logging.getLogger(lowerCAmelCase__		)





def    a__			(   )       ->      int:
    return _get_library_root_logger().getEffectiveLevel()





def    a__			(   lowerCAmelCase__		)       ->      None:
    _get_library_root_logger().setLevel(lowerCAmelCase__		)





def    a__			(   )       ->      Dict:
    return set_verbosity(lowerCAmelCase__		)





def    a__			(   )       ->      Any:
    return set_verbosity(lowerCAmelCase__		)





def    a__			(   )       ->      Union[str, Any]:
    return set_verbosity(lowerCAmelCase__		)





def    a__			(   )       ->      Dict:
    return set_verbosity(lowerCAmelCase__		)





def    a__			(   )       ->      None:
    UpperCAmelCase__     :						Dict						=							False





def    a__			(   )       ->      None:
    UpperCAmelCase__     :						Tuple						=							True


# Configure the library root logger at the module level (singleton-like)
_configure_library_root_logger()




class      lowerCamelCase_  :






      def __init__(   self							:       str		,		*_A							:       Dict		,		**_A							:       List[str]					):  # pylint: disable=unused-argument


          '''simple docstring'''


          UpperCAmelCase__     :						Any						=							args[0] if args else None






      def __iter__(   self							:       Union[str, Any]					):


          '''simple docstring'''


          return iter(self._iterator					)






      def __getattr__(   self							:       Optional[Any]		,		_A							:       List[Any]					):


          '''simple docstring'''


          def empty_fn(*_A							:       List[Any]		,		**_A							:       Dict					):  # pylint: disable=unused-argument
              return

          return empty_fn






      def __enter__(   self							:       Dict					):


          '''simple docstring'''


          return self






      def __exit__(   self							:       int		,		_A							:       Dict		,		_A							:       Tuple		,		_A							:       List[Any]					):


          '''simple docstring'''


          return

UpperCamelCase__          = True




class      lowerCamelCase_  :






      def __call__(   self							:       str		,		*_A							:       List[str]		,		_A							:       Tuple=False		,		**_A							:       int					):


          '''simple docstring'''


          if _tqdm_active and not disable:
              return tqdm_lib.tqdm(*_A		,		**_A					)
          else:
              return EmptyTqdm(*_A		,		**_A					)






      def lowercase_				(   self							:       Optional[Any]		,		*_A							:       List[str]		,		**_A							:       Optional[int]					):


          '''simple docstring'''


          UpperCAmelCase__     :						Optional[Any]						=							None
          if _tqdm_active:
              return tqdm_lib.tqdm.set_lock(*_A		,		**_A					)






      def lowercase_				(   self							:       Tuple					):


          '''simple docstring'''


          if _tqdm_active:
              return tqdm_lib.tqdm.get_lock()

UpperCamelCase__          = _tqdm_cls()





def    a__			(   )       ->      bool:
    global _tqdm_active
    return bool(_tqdm_active		)





def    a__			(   )       ->      str:
    global _tqdm_active
    UpperCAmelCase__     :						Union[str, Any]						=							True







def    a__			(   )       ->      Optional[int]:
    global _tqdm_active
    UpperCAmelCase__     :						Dict						=							False








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



	'''simple docstring'''




	lowerCAmelCase						=					MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
	lowerCAmelCase						=					TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




	def 				_UpperCamelCase     (			self			,     a			,     a			,     a   )			->       Union[str, Any]:
								snake_case_           =				AudioClassificationPipeline(model=a			,     feature_extractor=a   )

								# test with a raw waveform
								snake_case_           =				np.zeros((3_40_00,)   )
								snake_case_           =				np.zeros((1_40_00,)   )
								return audio_classifier, [audioa, audio]




	def 				_UpperCamelCase     (			self			,     a			,     a   )			->       Tuple:
								snake_case_     ,						snake_case_           =				examples
								snake_case_           =				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   )},
								    ]			,     )
								snake_case_           =				audio_classifier(a			,     top_k=1   )
								self.assertEqual(
								    a			,     [
								        {'score': ANY(a   ), 'label': ANY(a   )},
								    ]			,     )

								self.run_torchaudio(a   )




	@require_torchaudio
	def 				_UpperCamelCase     (			self			,     a   )			->       List[str]:
								import datasets

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




	@require_torch
	def 				_UpperCamelCase     (			self   )			->       Dict:
								snake_case_           =				'anton-l/wav2vec2-random-tiny-classifier'

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

								snake_case_           =				np.ones((80_00,)   )
								snake_case_           =				audio_classifier(a			,     top_k=4   )

								snake_case_           =				[
								    {'score': 0.0_842, 'label': 'no'},
								    {'score': 0.0_838, 'label': 'up'},
								    {'score': 0.0_837, 'label': 'go'},
								    {'score': 0.0_834, 'label': 'right'},
								]
								snake_case_           =				[
								    {'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]   )

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




	@require_torch
	@slow
	def 				_UpperCamelCase     (			self   )			->       str:
								import datasets

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

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

								snake_case_           =				np.array(dataset[3]['speech']			,     dtype=np.floataa   )
								snake_case_           =				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 				_UpperCamelCase     (			self   )			->       Optional[int]:
								pass





