




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'''






    UpperCAmelCase__													=   MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
    UpperCAmelCase__													=   TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING


    def  SCREAMING_SNAKE_CASE							(    self   :			str      ,				UpperCAmelCase__   :			int      ,				UpperCAmelCase__   :			Optional[Any]      ,				UpperCAmelCase__   :			int)		->List[Any]:





           '''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  SCREAMING_SNAKE_CASE							(    self   :			List[Any]      ,				UpperCAmelCase__   :			Optional[Any]      ,				UpperCAmelCase__   :			Optional[int])		->str:





           '''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  SCREAMING_SNAKE_CASE							(    self   :			List[str]      ,				UpperCAmelCase__   :			List[str])		->List[Any]:





           '''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  SCREAMING_SNAKE_CASE							(    self   :			str)		->int:





           '''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  SCREAMING_SNAKE_CASE							(    self   :			List[str])		->Optional[int]:





           '''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  SCREAMING_SNAKE_CASE							(    self   :			Optional[int])		->List[Any]:





           '''simple docstring'''

           pass








from typing import TYPE_CHECKING

from ...file_utils import _LazyModule, is_tokenizers_available, is_torch_available, is_vision_available
from ...utils import OptionalDependencyNotAvailable


_lowerCamelCase			:	Union[str, Any]							 =     {"""configuration_dpt""": ["""DPT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DPTConfig"""]}

try:
  if not is_vision_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCamelCase			:	Optional[Any]							 =     ["""DPTFeatureExtractor"""]
  _lowerCamelCase			:	int							 =     ["""DPTImageProcessor"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _lowerCamelCase			:	Optional[int]							 =     [
      """DPT_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """DPTForDepthEstimation""",
      """DPTForSemanticSegmentation""",
      """DPTModel""",
      """DPTPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_dpt import DPT_PRETRAINED_CONFIG_ARCHIVE_MAP, DPTConfig

  try:
    if not is_vision_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .feature_extraction_dpt import DPTFeatureExtractor
    from .image_processing_dpt import DPTImageProcessor

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_dpt import (
        DPT_PRETRAINED_MODEL_ARCHIVE_LIST,
        DPTForDepthEstimation,
        DPTForSemanticSegmentation,
        DPTModel,
        DPTPreTrainedModel,
    )


else:
  import sys

  _lowerCamelCase			:	Any							 =     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


