




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     lowerCamelCase__(					unittest.TestCase):
      UpperCAmelCase__      :	str    		=   MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
      UpperCAmelCase__      :	int    		=   TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
      def 			lowerCAmelCase__						(				self: Union[str, Any]    ,       UpperCamelCase_: Tuple    ,       UpperCamelCase_: List[Any]    ,       UpperCamelCase_: List[str]   ):
             __lowerCamelCase   =						AudioClassificationPipeline(model=UpperCamelCase_    ,       feature_extractor=UpperCamelCase_   )

             # test with a raw waveform
             __lowerCamelCase   =						np.zeros((3_40_00,)   )
             __lowerCamelCase   =						np.zeros((1_40_00,)   )
             return audio_classifier, [audioa, audio]
      def 			lowerCAmelCase__						(				self: int    ,       UpperCamelCase_: Any    ,       UpperCamelCase_: Any   ):
             __lowerCamelCase,   __lowerCamelCase   =						examples
             __lowerCamelCase   =						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_   )},
                 ]    ,       )
             __lowerCamelCase   =						audio_classifier(UpperCamelCase_    ,       top_k=1   )
             self.assertEqual(
                 UpperCamelCase_    ,       [
                     {"""score""": ANY(UpperCamelCase_   ), """label""": ANY(UpperCamelCase_   )},
                 ]    ,       )

             self.run_torchaudio(UpperCamelCase_   )
      @require_torchaudio
      def 			lowerCAmelCase__						(				self: Union[str, Any]    ,       UpperCamelCase_: Union[str, Any]   ):
             import datasets

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

             __lowerCamelCase   =						pipeline("""audio-classification"""    ,       model=UpperCamelCase_   )

             __lowerCamelCase   =						np.ones((80_00,)   )
             __lowerCamelCase   =						audio_classifier(UpperCamelCase_    ,       top_k=4   )

             __lowerCamelCase   =						[
                 {"""score""": 0.0842, """label""": """no"""},
                 {"""score""": 0.0838, """label""": """up"""},
                 {"""score""": 0.0837, """label""": """go"""},
                 {"""score""": 0.0834, """label""": """right"""},
             ]
             __lowerCamelCase   =						[
                 {"""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]   )

             __lowerCamelCase   =						{"""array""": np.ones((80_00,)   ), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate}
             __lowerCamelCase   =						audio_classifier(UpperCamelCase_    ,       top_k=4   )
             self.assertIn(nested_simplify(UpperCamelCase_    ,       decimals=4   )    ,       [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]   )
      @require_torch
      @slow
      def 			lowerCAmelCase__						(				self: List[Any]   ):
             import datasets

             __lowerCamelCase   =						"""superb/wav2vec2-base-superb-ks"""

             __lowerCamelCase   =						pipeline("""audio-classification"""    ,       model=UpperCamelCase_   )
             __lowerCamelCase   =						datasets.load_dataset("""anton-l/superb_dummy"""    ,       """ks"""    ,       split="""test"""   )

             __lowerCamelCase   =						np.array(dataset[3]["""speech"""]    ,       dtype=np.floataa   )
             __lowerCamelCase   =						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 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             pass









import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin





class __lowerCamelCase (      snake_case__     ,       unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__								=      TransfoXLTokenizer
	UpperCamelCase__								=      False
	UpperCamelCase__								=      False

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							super().setUp()

							_UpperCAmelCase									=			[
							    '<unk>',
							    '[CLS]',
							    '[SEP]',
							    'want',
							    'unwanted',
							    'wa',
							    'un',
							    'running',
							    ',',
							    'low',
							    'l',
							]
							_UpperCAmelCase									=			os.path.join(self.tmpdirname       ,    VOCAB_FILES_NAMES['vocab_file']    )
							with open(self.vocab_file       ,    'w'       ,    encoding='utf-8'    ) as vocab_writer:
													vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]    )    )

	def        UpperCamelCase    (							self       ,    **UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			True
							return TransfoXLTokenizer.from_pretrained(self.tmpdirname       ,    **UpperCAmelCase    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase    ):
							"""simple docstring"""







							_UpperCAmelCase									=			'<unk> UNwanted , running'
							_UpperCAmelCase									=			'<unk> unwanted, running'
							return input_text, output_text

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			TransfoXLTokenizer(vocab_file=self.vocab_file       ,    lower_case=UpperCAmelCase    )

							_UpperCAmelCase									=			tokenizer.tokenize('<unk> UNwanted , running'    )
							self.assertListEqual(UpperCAmelCase       ,    ['<unk>', 'unwanted', ',', 'running']    )

							self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase    )       ,    [0, 4, 8, 7]    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			TransfoXLTokenizer(lower_case=UpperCAmelCase    )

							self.assertListEqual(
							    tokenizer.tokenize(' \tHeLLo ! how  \n Are yoU ?  '    )       ,    ['hello', '!', 'how', 'are', 'you', '?']    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			TransfoXLTokenizer(lower_case=UpperCAmelCase    )

							self.assertListEqual(
							    tokenizer.tokenize(' \tHeLLo ! how  \n Are yoU ?  '    )       ,    ['HeLLo', '!', 'how', 'Are', 'yoU', '?']    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			TransfoXLTokenizer(lower_case=UpperCAmelCase    )
							_UpperCAmelCase									=			'Hello (bracket) and side-scrolled [and] Henry\'s $5,000 with 3.34 m. What\'s up!?'
							_UpperCAmelCase									=			[
							    'Hello',
							    '(',
							    'bracket',
							    ')',
							    'and',
							    'side',
							    '@-@',
							    'scrolled',
							    '[',
							    'and',
							    ']',
							    'Henry',
							    '\'s',
							    '$',
							    '5',
							    '@,@',
							    '000',
							    'with',
							    '3',
							    '@.@',
							    '34',
							    'm',
							    '.',
							    'What',
							    '\'s',
							    'up',
							    '!',
							    '?',
							]

							self.assertListEqual(tokenizer.tokenize(UpperCAmelCase    )       ,    UpperCAmelCase    )

							self.assertEqual(tokenizer.convert_tokens_to_string(UpperCAmelCase    )       ,    UpperCAmelCase    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.get_tokenizer()
							_UpperCAmelCase									=			len(UpperCAmelCase    )

							tokenizer.add_tokens(['new1', 'new2']    )
							tokenizer.move_added_token('new1'       ,    1    )

							# Check that moved token is not copied (duplicate)
							self.assertEqual(len(UpperCAmelCase    )       ,    original_len + 2    )
							# Check that token is moved to specified id
							self.assertEqual(tokenizer.encode('new1'    )       ,    [1]    )
							self.assertEqual(tokenizer.decode([1]    )       ,    'new1'    )
