




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__      :			List[Any]      =  MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
						UpperCamelCase__      :			Tuple      =  TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING






						def 	_A     (   self				,							_A				,							_A				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= AudioClassificationPipeline(model=_A				,							feature_extractor=_A		)

													# test with a raw waveform
													__SCREAMING_SNAKE_CASE									= np.zeros((34_000,)		)
													__SCREAMING_SNAKE_CASE									= np.zeros((14_000,)		)
													return audio_classifier, [audioa, audio]






						def 	_A     (   self				,							_A				,							_A		):







													'''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 	_A     (   self				,							_A		):







													'''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 	_A     (   self		):







													'''simple docstring'''

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

													__SCREAMING_SNAKE_CASE									= pipeline('audio-classification'				,							model=_A		)

													__SCREAMING_SNAKE_CASE									= np.ones((8_000,)		)
													__SCREAMING_SNAKE_CASE									= audio_classifier(_A				,							top_k=4		)

													__SCREAMING_SNAKE_CASE									= [
													    {'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'},
													]
													__SCREAMING_SNAKE_CASE									= [
													    {'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]		)

													__SCREAMING_SNAKE_CASE									= {'array': np.ones((8_000,)		), '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 	_A     (   self		):







													'''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_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 	_A     (   self		):







													'''simple docstring'''

													pass











import os
import unittest

from transformers import LayoutLMTokenizer, LayoutLMTokenizerFast
from transformers.models.layoutlm.tokenization_layoutlm import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin

@require_tokenizers
class        UpperCAmelCase_  (       UpperCamelCase_   ,   unittest.TestCase	):

						'''simple docstring'''




						UpperCamelCase__      :			Optional[int]      =  LayoutLMTokenizer
						UpperCamelCase__      :			Any      =  LayoutLMTokenizerFast
						UpperCamelCase__      :			Optional[int]      =  True
						UpperCamelCase__      :			int      =  True






						def 	_A     (   self		):







													'''simple docstring'''

													super().setUp()

													__SCREAMING_SNAKE_CASE									= [
													    '[UNK]',
													    '[CLS]',
													    '[SEP]',
													    '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 	_A     (   self				,							**_A		):







													'''simple docstring'''

													return LayoutLMTokenizer.from_pretrained(self.tmpdirname				,							**_A		)






						def 	_A     (   self				,							_A		):







													'''simple docstring'''

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






						def 	_A     (   self		):







													'''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		)				,							[7, 4, 5, 10, 8, 9]		)






						def 	_A     (   self		):







													'''simple docstring'''

													pass






