


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  ):
						UpperCAmelCase             =						MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
						UpperCAmelCase             =						TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING




						def 							__UpperCamelCase				(       self					:      str				,    _a					:      str				,    _a					:      Dict				,    _a					:      str  )							->     int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =AudioClassificationPipeline(model=_a				,    feature_extractor=_a  )

										# test with a raw waveform
										_SCREAMING_SNAKE_CASE			  =np.zeros((3_4000,)  )
										_SCREAMING_SNAKE_CASE			  =np.zeros((1_4000,)  )
										return audio_classifier, [audioa, audio]




						def 							__UpperCamelCase				(       self					:      Tuple				,    _a					:      Any				,    _a					:      Dict  )							->     Optional[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE			  =examples
										_SCREAMING_SNAKE_CASE			  =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  )},
										    ]				,    )
										_SCREAMING_SNAKE_CASE			  =audio_classifier(_a				,    top_k=1  )
										self.assertEqual(
										    _a				,    [
										        {'''score''': ANY(_a  ), '''label''': ANY(_a  )},
										    ]				,    )

										self.run_torchaudio(_a  )




						@require_torchaudio
						def 							__UpperCamelCase				(       self					:      Union[str, Any]				,    _a					:      Any  )							->     Tuple:


										"""simple docstring"""





										import datasets

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




						@require_torch
						def 							__UpperCamelCase				(       self					:      Optional[int]  )							->     int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  ='''anton-l/wav2vec2-random-tiny-classifier'''

										_SCREAMING_SNAKE_CASE			  =pipeline('''audio-classification'''				,    model=_a  )

										_SCREAMING_SNAKE_CASE			  =np.ones((8000,)  )
										_SCREAMING_SNAKE_CASE			  =audio_classifier(_a				,    top_k=4  )

										_SCREAMING_SNAKE_CASE			  =[
										    {'''score''': 0.08_42, '''label''': '''no'''},
										    {'''score''': 0.08_38, '''label''': '''up'''},
										    {'''score''': 0.08_37, '''label''': '''go'''},
										    {'''score''': 0.08_34, '''label''': '''right'''},
										]
										_SCREAMING_SNAKE_CASE			  =[
										    {'''score''': 0.08_45, '''label''': '''stop'''},
										    {'''score''': 0.08_44, '''label''': '''on'''},
										    {'''score''': 0.08_41, '''label''': '''right'''},
										    {'''score''': 0.08_34, '''label''': '''left'''},
										]
										self.assertIn(nested_simplify(_a				,    decimals=4  )				,    [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]  )

										_SCREAMING_SNAKE_CASE			  ={'''array''': np.ones((8000,)  ), '''sampling_rate''': audio_classifier.feature_extractor.sampling_rate}
										_SCREAMING_SNAKE_CASE			  =audio_classifier(_a				,    top_k=4  )
										self.assertIn(nested_simplify(_a				,    decimals=4  )				,    [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]  )




						@require_torch
						@slow
						def 							__UpperCamelCase				(       self					:      Optional[Any]  )							->     int:


										"""simple docstring"""





										import datasets

										_SCREAMING_SNAKE_CASE			  ='''superb/wav2vec2-base-superb-ks'''

										_SCREAMING_SNAKE_CASE			  =pipeline('''audio-classification'''				,    model=_a  )
										_SCREAMING_SNAKE_CASE			  =datasets.load_dataset('''anton-l/superb_dummy'''				,    '''ks'''				,    split='''test'''  )

										_SCREAMING_SNAKE_CASE			  =np.array(dataset[3]['''speech''']				,    dtype=np.floataa  )
										_SCREAMING_SNAKE_CASE			  =audio_classifier(_a				,    top_k=4  )
										self.assertEqual(
										    nested_simplify(_a				,    decimals=3  )				,    [
										        {'''score''': 0.9_81, '''label''': '''go'''},
										        {'''score''': 0.0_07, '''label''': '''up'''},
										        {'''score''': 0.0_06, '''label''': '''_unknown_'''},
										        {'''score''': 0.0_01, '''label''': '''down'''},
										    ]				,    )




						@require_tf
						@unittest.skip('''Audio classification is not implemented for TF'''  )
						def 							__UpperCamelCase				(       self					:      List[str]  )							->     str:


										"""simple docstring"""





										pass



import os
import unittest

from transformers import BatchEncoding
from transformers.models.bert.tokenization_bert import (
    BasicTokenizer,
    WordpieceTokenizer,
    _is_control,
    _is_punctuation,
    _is_whitespace,
)
from transformers.models.prophetnet.tokenization_prophetnet import VOCAB_FILES_NAMES, ProphetNetTokenizer
from transformers.testing_utils import require_torch, slow

from ...test_tokenization_common import TokenizerTesterMixin

class 						A__							(		UpperCamelCase__   ,     unittest.TestCase  ):
						UpperCAmelCase             =						ProphetNetTokenizer
						UpperCAmelCase             =						False




						def 							__UpperCamelCase				(       self					:      List[Any]  )							->     int:


										"""simple docstring"""





										super().setUp()

										_SCREAMING_SNAKE_CASE			  =[
										    '''[UNK]''',
										    '''[CLS]''',
										    '''[SEP]''',
										    '''[PAD]''',
										    '''[MASK]''',
										    '''want''',
										    '''##want''',
										    '''##ed''',
										    '''wa''',
										    '''un''',
										    '''runn''',
										    '''##ing''',
										    ''',''',
										    '''low''',
										    '''lowest''',
										]
										_SCREAMING_SNAKE_CASE			  =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					:      str				,    _a					:      Union[str, Any]  )							->     List[str]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  ='''UNwant\u00E9d,running'''
										_SCREAMING_SNAKE_CASE			  ='''unwanted, running'''
										return input_text, output_text




						def 							__UpperCamelCase				(       self					:      Optional[int]  )							->     str:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.tokenizer_class(self.vocab_file  )

										_SCREAMING_SNAKE_CASE			  =tokenizer.tokenize('''UNwant\u00E9d,running'''  )
										self.assertListEqual(_a				,    ['''un''', '''##want''', '''##ed''', ''',''', '''runn''', '''##ing''']  )
										self.assertListEqual(tokenizer.convert_tokens_to_ids(_a  )				,    [9, 6, 7, 12, 10, 11]  )




						def 							__UpperCamelCase				(       self					:      Tuple  )							->     Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =BasicTokenizer()

										self.assertListEqual(tokenizer.tokenize('''ah\u535A\u63A8zz'''  )				,    ['''ah''', '''\u535A''', '''\u63A8''', '''zz''']  )




						def 							__UpperCamelCase				(       self					:      Dict  )							->     List[Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =BasicTokenizer(do_lower_case=_a  )

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




						def 							__UpperCamelCase				(       self					:      Dict  )							->     Dict:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =BasicTokenizer(do_lower_case=_a				,    strip_accents=_a  )

										self.assertListEqual(
										    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''  )				,    ['''hällo''', '''!''', '''how''', '''are''', '''you''', '''?''']  )
										self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''  )				,    ['''h\u00E9llo''']  )




						def 							__UpperCamelCase				(       self					:      List[str]  )							->     int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =BasicTokenizer(do_lower_case=_a				,    strip_accents=_a  )

										self.assertListEqual(
										    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''  )				,    ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?''']  )
										self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''  )				,    ['''hello''']  )




						def 							__UpperCamelCase				(       self					:      Optional[int]  )							->     Tuple:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =BasicTokenizer(do_lower_case=_a  )

										self.assertListEqual(
										    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''  )				,    ['''hallo''', '''!''', '''how''', '''are''', '''you''', '''?''']  )
										self.assertListEqual(tokenizer.tokenize('''H\u00E9llo'''  )				,    ['''hello''']  )




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


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =BasicTokenizer(do_lower_case=_a  )

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




						def 							__UpperCamelCase				(       self					:      Dict  )							->     int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =BasicTokenizer(do_lower_case=_a				,    strip_accents=_a  )

										self.assertListEqual(
										    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''  )				,    ['''HäLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''']  )




						def 							__UpperCamelCase				(       self					:      Optional[Any]  )							->     int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =BasicTokenizer(do_lower_case=_a				,    strip_accents=_a  )

										self.assertListEqual(
										    tokenizer.tokenize(''' \tHäLLo!how  \n Are yoU?  '''  )				,    ['''HaLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''']  )




						def 							__UpperCamelCase				(       self					:      List[str]  )							->     Dict:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =BasicTokenizer(do_lower_case=_a				,    never_split=['''[UNK]''']  )

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




						def 							__UpperCamelCase				(       self					:      List[Any]  )							->     List[str]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =['''[UNK]''', '''[CLS]''', '''[SEP]''', '''want''', '''##want''', '''##ed''', '''wa''', '''un''', '''runn''', '''##ing''']

										_SCREAMING_SNAKE_CASE			  ={}
										for i, token in enumerate(_a  ):
														_SCREAMING_SNAKE_CASE			  =i
										_SCREAMING_SNAKE_CASE			  =WordpieceTokenizer(vocab=_a				,    unk_token='''[UNK]'''  )

										self.assertListEqual(tokenizer.tokenize(''''''  )				,    []  )

										self.assertListEqual(tokenizer.tokenize('''unwanted running'''  )				,    ['''un''', '''##want''', '''##ed''', '''runn''', '''##ing''']  )

										self.assertListEqual(tokenizer.tokenize('''unwantedX running'''  )				,    ['''[UNK]''', '''runn''', '''##ing''']  )




						@require_torch
						def 							__UpperCamelCase				(       self					:      Any  )							->     Union[str, Any]:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.tokenizer_class.from_pretrained('''microsoft/prophetnet-large-uncased'''  )

										_SCREAMING_SNAKE_CASE			  =['''A long paragraph for summarization.''', '''Another paragraph for summarization.''']
										_SCREAMING_SNAKE_CASE			  =[1037, 2146, 2_0423, 2005, 7680, 7849, 3989, 1012, 102]
										_SCREAMING_SNAKE_CASE			  =tokenizer(_a				,    padding=_a				,    return_tensors='''pt'''  )
										self.assertIsInstance(_a				,    _a  )
										_SCREAMING_SNAKE_CASE			  =list(batch.input_ids.numpy()[0]  )
										self.assertListEqual(_a				,    _a  )

										self.assertEqual((2, 9)				,    batch.input_ids.shape  )
										self.assertEqual((2, 9)				,    batch.attention_mask.shape  )




						def 							__UpperCamelCase				(       self					:      List[str]  )							->     Optional[int]:


										"""simple docstring"""





										self.assertTrue(_is_whitespace(''' '''  )  )
										self.assertTrue(_is_whitespace('''\t'''  )  )
										self.assertTrue(_is_whitespace('''\r'''  )  )
										self.assertTrue(_is_whitespace('''\n'''  )  )
										self.assertTrue(_is_whitespace('''\u00A0'''  )  )

										self.assertFalse(_is_whitespace('''A'''  )  )
										self.assertFalse(_is_whitespace('''-'''  )  )




						def 							__UpperCamelCase				(       self					:      int  )							->     Dict:


										"""simple docstring"""





										self.assertTrue(_is_control('''\u0005'''  )  )

										self.assertFalse(_is_control('''A'''  )  )
										self.assertFalse(_is_control(''' '''  )  )
										self.assertFalse(_is_control('''\t'''  )  )
										self.assertFalse(_is_control('''\r'''  )  )




						def 							__UpperCamelCase				(       self					:      List[str]  )							->     List[str]:


										"""simple docstring"""





										self.assertTrue(_is_punctuation('''-'''  )  )
										self.assertTrue(_is_punctuation('''$'''  )  )
										self.assertTrue(_is_punctuation('''`'''  )  )
										self.assertTrue(_is_punctuation('''.'''  )  )

										self.assertFalse(_is_punctuation('''A'''  )  )
										self.assertFalse(_is_punctuation(''' '''  )  )




						@slow
						def 							__UpperCamelCase				(       self					:      Tuple  )							->     int:


										"""simple docstring"""





										_SCREAMING_SNAKE_CASE			  =self.tokenizer_class.from_pretrained('''microsoft/prophetnet-large-uncased'''  )

										_SCREAMING_SNAKE_CASE			  =tokenizer.encode('''sequence builders'''				,    add_special_tokens=_a  )
										_SCREAMING_SNAKE_CASE			  =tokenizer.encode('''multi-sequence build'''				,    add_special_tokens=_a  )

										_SCREAMING_SNAKE_CASE			  =tokenizer.build_inputs_with_special_tokens(_a  )
										_SCREAMING_SNAKE_CASE			  =tokenizer.build_inputs_with_special_tokens(_a				,    _a  )

										assert encoded_sentence == text + [102]
										assert encoded_pair == text + [102] + text_a + [102]