




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 argparse import ArgumentParser

from . import BaseTransformersCLICommand







def SCREAMING_SNAKE_CASE       ( lowercase_		)     -> str:



       """simple docstring"""

       return DownloadCommand(args.model				,       args.cache_dir				,       args.force				,       args.trust_remote_code		)


class        UpperCamelCase_					(				UpperCAmelCase__  ):

    '''simple docstring'''






    @staticmethod
    def  SCREAMING_SNAKE_CASE							(    UpperCAmelCase__   :			ArgumentParser)		->Any:





           '''simple docstring'''

           A__   				=					parser.add_parser('''download''')
           download_parser.add_argument(
               '''--cache-dir'''      ,				type=UpperCAmelCase__      ,				default=UpperCAmelCase__      ,				help='''Path to location to store the models''')
           download_parser.add_argument(
               '''--force'''      ,				action='''store_true'''      ,				help='''Force the model to be download even if already in cache-dir''')
           download_parser.add_argument(
               '''--trust-remote-code'''      ,				action='''store_true'''      ,				help='''Whether or not to allow for custom models defined on the Hub in their own modeling files. Use only if you\'ve reviewed the code as it will execute on your local machine'''      ,				)
           download_parser.add_argument('''model'''      ,				type=UpperCAmelCase__      ,				help='''Name of the model to download''')
           download_parser.set_defaults(func=UpperCAmelCase__)


    def __init__(    self   :			Dict      ,				UpperCAmelCase__   :			str      ,				UpperCAmelCase__   :			str      ,				UpperCAmelCase__   :			bool      ,				UpperCAmelCase__   :			bool)		->int:





           '''simple docstring'''

           A__   				=					model
           A__   				=					cache
           A__   				=					force
           A__   				=					trust_remote_code





    def  SCREAMING_SNAKE_CASE							(    self   :			str)		->Union[str, Any]:





           '''simple docstring'''

           from ..models.auto import AutoModel, AutoTokenizer

           AutoModel.from_pretrained(
               self._model      ,				cache_dir=self._cache      ,				force_download=self._force      ,				trust_remote_code=self._trust_remote_code)
           AutoTokenizer.from_pretrained(
               self._model      ,				cache_dir=self._cache      ,				force_download=self._force      ,				trust_remote_code=self._trust_remote_code)


