
"""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 		a__						(      unittest.TestCase						):
   __lowerCAmelCase            =				MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
   __lowerCAmelCase            =				TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
   def 			__magic_name__			(			self	,    _a	,    _a	,    _a				):
          lowercase    :				Any       		=							AudioClassificationPipeline(model=_a	,    feature_extractor=_a				)

          # test with a raw waveform
          lowercase    :				List[str]       		=							np.zeros((34_000,)				)
          lowercase    :				str       		=							np.zeros((14_000,)				)
          return audio_classifier, [audioa, audio]
   def 			__magic_name__			(			self	,    _a	,    _a				):
          lowercase		,    lowercase    :				Optional[Any]       		=							examples
          lowercase    :				List[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				)},
              ]	,    )
          lowercase    :				List[str]       		=							audio_classifier(_a	,    top_k=1				)
          self.assertEqual(
              _a	,    [
                  {"score": ANY(_a				), "label": ANY(_a				)},
              ]	,    )

          self.run_torchaudio(_a				)
   @require_torchaudio
   def 			__magic_name__			(			self	,    _a				):
          import datasets

          # test with a local file
          lowercase    :				Union[str, Any]       		=							datasets.load_dataset("hf-internal-testing/librispeech_asr_dummy"	,    "clean"	,    split="validation"				)
          lowercase    :				Dict       		=							dataset[0]["audio"]["array"]
          lowercase    :				int       		=							audio_classifier(_a				)
          self.assertEqual(
              _a	,    [
                  {"score": ANY(_a				), "label": ANY(_a				)},
                  {"score": ANY(_a				), "label": ANY(_a				)},
              ]	,    )
   @require_torch
   def 			__magic_name__			(			self				):
          lowercase    :				Tuple       		=							"anton-l/wav2vec2-random-tiny-classifier"

          lowercase    :				Union[str, Any]       		=							pipeline("audio-classification"	,    model=_a				)

          lowercase    :				Optional[int]       		=							np.ones((8_000,)				)
          lowercase    :				Optional[int]       		=							audio_classifier(_a	,    top_k=4				)

          lowercase    :				Any       		=							[
              {"score": 0.0_8_4_2, "label": "no"},
              {"score": 0.0_8_3_8, "label": "up"},
              {"score": 0.0_8_3_7, "label": "go"},
              {"score": 0.0_8_3_4, "label": "right"},
          ]
          lowercase    :				List[Any]       		=							[
              {"score": 0.0_8_4_5, "label": "stop"},
              {"score": 0.0_8_4_4, "label": "on"},
              {"score": 0.0_8_4_1, "label": "right"},
              {"score": 0.0_8_3_4, "label": "left"},
          ]
          self.assertIn(nested_simplify(_a	,    decimals=4				)	,    [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]				)

          lowercase    :				str       		=							{"array": np.ones((8_000,)				), "sampling_rate": audio_classifier.feature_extractor.sampling_rate}
          lowercase    :				Tuple       		=							audio_classifier(_a	,    top_k=4				)
          self.assertIn(nested_simplify(_a	,    decimals=4				)	,    [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]				)
   @require_torch
   @slow
   def 			__magic_name__			(			self				):
          import datasets

          lowercase    :				str       		=							"superb/wav2vec2-base-superb-ks"

          lowercase    :				str       		=							pipeline("audio-classification"	,    model=_a				)
          lowercase    :				Optional[int]       		=							datasets.load_dataset("anton-l/superb_dummy"	,    "ks"	,    split="test"				)

          lowercase    :				str       		=							np.array(dataset[3]["speech"]	,    dtype=np.floataa				)
          lowercase    :				Optional[int]       		=							audio_classifier(_a	,    top_k=4				)
          self.assertEqual(
              nested_simplify(_a	,    decimals=3				)	,    [
                  {"score": 0.9_8_1, "label": "go"},
                  {"score": 0.0_0_7, "label": "up"},
                  {"score": 0.0_0_6, "label": "_unknown_"},
                  {"score": 0.0_0_1, "label": "down"},
              ]	,    )

   @require_tf
   @unittest.skip("Audio classification is not implemented for TF"				)
   def 			__magic_name__			(			self				):
          pass




"""simple docstring"""




import shutil
import tempfile
import unittest

from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torchaudio

from .test_feature_extraction_clap import floats_list

@require_torchaudio
@require_sentencepiece
class 		a__						(      unittest.TestCase						):
   def 			__magic_name__			(			self				):
          lowercase    :				Optional[int]       		=							"laion/clap-htsat-unfused"
          lowercase    :				Optional[int]       		=							tempfile.mkdtemp()
   def 			__magic_name__			(			self	,    **_a				):
          return RobertaTokenizer.from_pretrained(self.checkpoint	,    **_a				)
   def 			__magic_name__			(			self	,    **_a				):
          return ClapFeatureExtractor.from_pretrained(self.checkpoint	,    **_a				)
   def 			__magic_name__			(			self				):
          shutil.rmtree(self.tmpdirname				)
   def 			__magic_name__			(			self				):
          lowercase    :				Optional[int]       		=							self.get_tokenizer()
          lowercase    :				List[Any]       		=							self.get_feature_extractor()

          lowercase    :				Dict       		=							ClapProcessor(tokenizer=_a	,    feature_extractor=_a				)

          processor.save_pretrained(self.tmpdirname				)
          lowercase    :				int       		=							ClapProcessor.from_pretrained(self.tmpdirname				)

          self.assertEqual(processor.tokenizer.get_vocab()	,    tokenizer.get_vocab()				)
          self.assertIsInstance(processor.tokenizer	,    _a				)

          self.assertEqual(processor.feature_extractor.to_json_string()	,    feature_extractor.to_json_string()				)
          self.assertIsInstance(processor.feature_extractor	,    _a				)
   def 			__magic_name__			(			self				):
          lowercase    :				Tuple       		=							ClapProcessor(tokenizer=self.get_tokenizer()	,    feature_extractor=self.get_feature_extractor()				)
          processor.save_pretrained(self.tmpdirname				)

          lowercase    :				Union[str, Any]       		=							self.get_tokenizer(bos_token="(BOS)"	,    eos_token="(EOS)"				)
          lowercase    :				Optional[int]       		=							self.get_feature_extractor(do_normalize=_a	,    padding_value=1.0				)

          lowercase    :				Dict       		=							ClapProcessor.from_pretrained(
              self.tmpdirname	,    bos_token="(BOS)"	,    eos_token="(EOS)"	,    do_normalize=_a	,    padding_value=1.0				)

          self.assertEqual(processor.tokenizer.get_vocab()	,    tokenizer_add_kwargs.get_vocab()				)
          self.assertIsInstance(processor.tokenizer	,    _a				)

          self.assertEqual(processor.feature_extractor.to_json_string()	,    feature_extractor_add_kwargs.to_json_string()				)
          self.assertIsInstance(processor.feature_extractor	,    _a				)
   def 			__magic_name__			(			self				):
          lowercase    :				List[Any]       		=							self.get_feature_extractor()
          lowercase    :				List[str]       		=							self.get_tokenizer()

          lowercase    :				int       		=							ClapProcessor(tokenizer=_a	,    feature_extractor=_a				)

          lowercase    :				Dict       		=							floats_list((3, 1_000)				)

          lowercase    :				str       		=							feature_extractor(_a	,    return_tensors="np"				)
          lowercase    :				Dict       		=							processor(audios=_a	,    return_tensors="np"				)

          for key in input_feat_extract.keys():
                 self.assertAlmostEqual(input_feat_extract[key].sum()	,    input_processor[key].sum()	,    delta=1E-2				)
   def 			__magic_name__			(			self				):
          lowercase    :				Dict       		=							self.get_feature_extractor()
          lowercase    :				int       		=							self.get_tokenizer()

          lowercase    :				Dict       		=							ClapProcessor(tokenizer=_a	,    feature_extractor=_a				)

          lowercase    :				Optional[Any]       		=							"This is a test string"

          lowercase    :				Any       		=							processor(text=_a				)

          lowercase    :				List[Any]       		=							tokenizer(_a				)

          for key in encoded_tok.keys():
                 self.assertListEqual(encoded_tok[key]	,    encoded_processor[key]				)
   def 			__magic_name__			(			self				):
          lowercase    :				Optional[int]       		=							self.get_feature_extractor()
          lowercase    :				Any       		=							self.get_tokenizer()

          lowercase    :				Union[str, Any]       		=							ClapProcessor(tokenizer=_a	,    feature_extractor=_a				)

          lowercase    :				Union[str, Any]       		=							[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

          lowercase    :				str       		=							processor.batch_decode(_a				)
          lowercase    :				Optional[int]       		=							tokenizer.batch_decode(_a				)

          self.assertListEqual(_a	,    _a				)

   def 			__magic_name__			(			self				):
          lowercase    :				List[Any]       		=							self.get_feature_extractor()
          lowercase    :				Union[str, Any]       		=							self.get_tokenizer()

          lowercase    :				Any       		=							ClapProcessor(tokenizer=_a	,    feature_extractor=_a				)

          self.assertListEqual(
              processor.model_input_names[2:]	,    feature_extractor.model_input_names	,    msg="`processor` and `feature_extractor` model input names do not match"	,    )


