






'''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  snake_case__      (					unittest.TestCase):
					a_        =					MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
					a_        =					TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




					def 		A      (  self  :		Any				, _A  :		Optional[Any]				, _A  :		List[Any]				, _A  :		str      )    ->							List[Any]:
								UpperCAmelCase_      :       str          =       AudioClassificationPipeline(model=_A				, feature_extractor=_A      )

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




					def 		A      (  self  :		Union[str, Any]				, _A  :		List[Any]				, _A  :		Dict      )    ->							List[str]:
								UpperCAmelCase_    ,      UpperCAmelCase_      :       Dict          =       examples
								UpperCAmelCase_      :       Optional[Any]          =       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      )},
								    ]				, )
								UpperCAmelCase_      :       Union[str, Any]          =       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  :		int      )    ->							List[Any]:
								import datasets

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




					@require_torch
					def 		A      (  self  :		int      )    ->							Optional[int]:
								UpperCAmelCase_      :       int          =       '''anton-l/wav2vec2-random-tiny-classifier'''

								UpperCAmelCase_      :       Any          =       pipeline('''audio-classification'''				, model=_A      )

								UpperCAmelCase_      :       List[str]          =       np.ones((80_00,)      )
								UpperCAmelCase_      :       Tuple          =       audio_classifier(_A				, top_k=4      )

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

								UpperCAmelCase_      :       str          =       {'''array''': np.ones((80_00,)      ), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
								UpperCAmelCase_      :       Dict          =       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  :		List[Any]      )    ->							List[Any]:
								import datasets

								UpperCAmelCase_      :       Optional[Any]          =       '''superb/wav2vec2-base-superb-ks'''

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

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








'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


UpperCAmelCase_					=   {"configuration_swin": ["SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP", "SwinConfig", "SwinOnnxConfig"]}


try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_					=   [
      "SWIN_PRETRAINED_MODEL_ARCHIVE_LIST",
      "SwinForImageClassification",
      "SwinForMaskedImageModeling",
      "SwinModel",
      "SwinPreTrainedModel",
      "SwinBackbone",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase_					=   [
      "TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST",
      "TFSwinForImageClassification",
      "TFSwinForMaskedImageModeling",
      "TFSwinModel",
      "TFSwinPreTrainedModel",
  ]

if TYPE_CHECKING:
  from .configuration_swin import SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinConfig, SwinOnnxConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_swin import (
        SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
        SwinBackbone,
        SwinForImageClassification,
        SwinForMaskedImageModeling,
        SwinModel,
        SwinPreTrainedModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_swin import (
        TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFSwinForImageClassification,
        TFSwinForMaskedImageModeling,
        TFSwinModel,
        TFSwinPreTrainedModel,
    )

else:
  import sys

  UpperCAmelCase_					=   _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)



