




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import ConvNextImageProcessor
class 				__snake_case     (unittest.TestCase						):



				def __init__(					self					:						Optional[int]						,		_UpperCAmelCase					:						Optional[Any]						,		_UpperCAmelCase					:						List[Any]=7						,		_UpperCAmelCase					:						Union[str, Any]=3						,		_UpperCAmelCase					:						Union[str, Any]=18						,		_UpperCAmelCase					:						Optional[Any]=30						,		_UpperCAmelCase					:						Tuple=400						,		_UpperCAmelCase					:						Any=True						,		_UpperCAmelCase					:						Dict=None						,		_UpperCAmelCase					:						Optional[Any]=0.875						,		_UpperCAmelCase					:						str=True						,		_UpperCAmelCase					:						Any=[0.5, 0.5, 0.5]						,		_UpperCAmelCase					:						Union[str, Any]=[0.5, 0.5, 0.5]						,		)     ->			Any:





								'''simple docstring'''




								_lowerCAmelCase			:			Tuple        =					size if size is not None else {"""shortest_edge""": 20}
								_lowerCAmelCase			:			Any        =					parent
								_lowerCAmelCase			:			Optional[int]        =					batch_size
								_lowerCAmelCase			:			int        =					num_channels
								_lowerCAmelCase			:			Dict        =					image_size
								_lowerCAmelCase			:			Dict        =					min_resolution
								_lowerCAmelCase			:			Any        =					max_resolution
								_lowerCAmelCase			:			List[Any]        =					do_resize
								_lowerCAmelCase			:			Dict        =					size
								_lowerCAmelCase			:			int        =					crop_pct
								_lowerCAmelCase			:			List[str]        =					do_normalize
								_lowerCAmelCase			:			Optional[int]        =					image_mean
								_lowerCAmelCase			:			str        =					image_std



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





								'''simple docstring'''




								return {
								    "image_mean": self.image_mean,
								    "image_std": self.image_std,
								    "do_normalize": self.do_normalize,
								    "do_resize": self.do_resize,
								    "size": self.size,
								    "crop_pct": self.crop_pct,
								}






@require_torch
@require_vision
class 				__snake_case     (_a  ,		unittest.TestCase						):
				lowerCAmelCase__			=   ConvNextImageProcessor if is_vision_available() else None



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





								'''simple docstring'''




								_lowerCAmelCase			:			Any        =					ConvNextImageProcessingTester(self					)



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





								'''simple docstring'''




								return self.image_processor_tester.prepare_image_processor_dict()



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





								'''simple docstring'''




								_lowerCAmelCase			:			List[Any]        =					self.image_processing_class(**self.image_processor_dict					)
								self.assertTrue(hasattr(_UpperCAmelCase						,		"""do_resize"""					)					)
								self.assertTrue(hasattr(_UpperCAmelCase						,		"""size"""					)					)
								self.assertTrue(hasattr(_UpperCAmelCase						,		"""crop_pct"""					)					)
								self.assertTrue(hasattr(_UpperCAmelCase						,		"""do_normalize"""					)					)
								self.assertTrue(hasattr(_UpperCAmelCase						,		"""image_mean"""					)					)
								self.assertTrue(hasattr(_UpperCAmelCase						,		"""image_std"""					)					)



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





								'''simple docstring'''




								_lowerCAmelCase			:			Union[str, Any]        =					self.image_processing_class.from_dict(self.image_processor_dict					)
								self.assertEqual(image_processor.size						,		{"""shortest_edge""": 20}					)

								_lowerCAmelCase			:			Optional[int]        =					self.image_processing_class.from_dict(self.image_processor_dict						,		size=42					)
								self.assertEqual(image_processor.size						,		{"""shortest_edge""": 42}					)



				def 					SCREAMING_SNAKE_CASE				(					self					:						int					)     ->			Tuple:





								'''simple docstring'''




								pass



				def 					SCREAMING_SNAKE_CASE				(					self					:						Tuple					)     ->			Dict:





								'''simple docstring'''




								_lowerCAmelCase			:			Union[str, Any]        =					self.image_processing_class(**self.image_processor_dict					)
								# create random PIL images
								_lowerCAmelCase			:			Tuple        =					prepare_image_inputs(self.image_processor_tester						,		equal_resolution=_UpperCAmelCase					)
								for image in image_inputs:
												self.assertIsInstance(_UpperCAmelCase						,		Image.Image					)

								# Test not batched input
								_lowerCAmelCase			:			Dict        =					image_processing(image_inputs[0]						,		return_tensors="""pt"""					).pixel_values
								self.assertEqual(
								    encoded_images.shape						,		(
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.size["""shortest_edge"""],
								        self.image_processor_tester.size["""shortest_edge"""],
								    )						,		)

								# Test batched
								_lowerCAmelCase			:			List[str]        =					image_processing(_UpperCAmelCase						,		return_tensors="""pt"""					).pixel_values
								self.assertEqual(
								    encoded_images.shape						,		(
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.size["""shortest_edge"""],
								        self.image_processor_tester.size["""shortest_edge"""],
								    )						,		)



				def 					SCREAMING_SNAKE_CASE				(					self					:						int					)     ->			Optional[int]:





								'''simple docstring'''




								_lowerCAmelCase			:			Any        =					self.image_processing_class(**self.image_processor_dict					)
								# create random numpy tensors
								_lowerCAmelCase			:			Union[str, Any]        =					prepare_image_inputs(self.image_processor_tester						,		equal_resolution=_UpperCAmelCase						,		numpify=_UpperCAmelCase					)
								for image in image_inputs:
												self.assertIsInstance(_UpperCAmelCase						,		np.ndarray					)

								# Test not batched input
								_lowerCAmelCase			:			List[Any]        =					image_processing(image_inputs[0]						,		return_tensors="""pt"""					).pixel_values
								self.assertEqual(
								    encoded_images.shape						,		(
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.size["""shortest_edge"""],
								        self.image_processor_tester.size["""shortest_edge"""],
								    )						,		)

								# Test batched
								_lowerCAmelCase			:			List[Any]        =					image_processing(_UpperCAmelCase						,		return_tensors="""pt"""					).pixel_values
								self.assertEqual(
								    encoded_images.shape						,		(
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.size["""shortest_edge"""],
								        self.image_processor_tester.size["""shortest_edge"""],
								    )						,		)



				def 					SCREAMING_SNAKE_CASE				(					self					:						Dict					)     ->			Any:





								'''simple docstring'''




								_lowerCAmelCase			:			int        =					self.image_processing_class(**self.image_processor_dict					)
								# create random PyTorch tensors
								_lowerCAmelCase			:			int        =					prepare_image_inputs(self.image_processor_tester						,		equal_resolution=_UpperCAmelCase						,		torchify=_UpperCAmelCase					)
								for image in image_inputs:
												self.assertIsInstance(_UpperCAmelCase						,		torch.Tensor					)

								# Test not batched input
								_lowerCAmelCase			:			Optional[Any]        =					image_processing(image_inputs[0]						,		return_tensors="""pt"""					).pixel_values
								self.assertEqual(
								    encoded_images.shape						,		(
								        1,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.size["""shortest_edge"""],
								        self.image_processor_tester.size["""shortest_edge"""],
								    )						,		)

								# Test batched
								_lowerCAmelCase			:			Optional[int]        =					image_processing(_UpperCAmelCase						,		return_tensors="""pt"""					).pixel_values
								self.assertEqual(
								    encoded_images.shape						,		(
								        self.image_processor_tester.batch_size,
								        self.image_processor_tester.num_channels,
								        self.image_processor_tester.size["""shortest_edge"""],
								        self.image_processor_tester.size["""shortest_edge"""],
								    )						,		)







import inspect
import json
import os
import tempfile
import unittest

from transformers.models.speech_to_text_a import SpeechaTextaTokenizer
from transformers.models.speech_to_text_a.tokenization_speech_to_text_a import VOCAB_FILES_NAMES

from ...test_tokenization_common import TokenizerTesterMixin
class 				__snake_case     (_a  ,		unittest.TestCase						):
				lowerCAmelCase__			=   SpeechaTextaTokenizer
				lowerCAmelCase__			=   False



				def 					SCREAMING_SNAKE_CASE				(					self					:						Optional[int]					)     ->			Optional[int]:





								'''simple docstring'''




								super().setUp()

								_lowerCAmelCase			:			Optional[int]        =					"""<s> <pad> </s> <unk> here@@ a couple of@@ words for the he@@ re@@ vocab""".split(""" """					)
								_lowerCAmelCase			:			Optional[int]        =					["""he re</w> 123""", """here a 1456"""]
								_lowerCAmelCase			:			Union[str, Any]        =					dict(zip(_UpperCAmelCase						,		range(len(_UpperCAmelCase					)					)					)					)

								_lowerCAmelCase			:			Any        =					{"""pad_token""": """<pad>""", """unk_token""": """<unk>""", """bos_token""": """<s>""", """eos_token""": """</s>"""}

								_lowerCAmelCase			:			Union[str, Any]        =					tempfile.mkdtemp()
								_lowerCAmelCase			:			Tuple        =					os.path.join(self.tmpdirname						,		VOCAB_FILES_NAMES["""vocab_file"""]					)
								_lowerCAmelCase			:			Any        =					os.path.join(self.tmpdirname						,		VOCAB_FILES_NAMES["""merges_file"""]					)

								with open(self.vocab_file						,		"""w"""						,		encoding="""utf-8"""					) as fp:
												fp.write(json.dumps(_UpperCAmelCase					) + """\n"""					)

								with open(self.merges_file						,		"""w"""					) as fp:
												fp.write("""\n""".join(_UpperCAmelCase					)					)



				def 					SCREAMING_SNAKE_CASE				(					self					:						int					)     ->			Any:





								'''simple docstring'''




								_lowerCAmelCase			:			List[str]        =					list(self.get_tokenizer().get_vocab().keys()					)

								self.assertEqual(vocab_keys[0]						,		"""<s>"""					)
								self.assertEqual(vocab_keys[1]						,		"""<pad>"""					)
								self.assertEqual(vocab_keys[-1]						,		"""vocab"""					)
								self.assertEqual(len(_UpperCAmelCase					)						,		14					)



				def 					SCREAMING_SNAKE_CASE				(					self					:						Any					)     ->			int:





								'''simple docstring'''




								self.assertEqual(self.get_tokenizer().vocab_size						,		14					)



				def 					SCREAMING_SNAKE_CASE				(					self					:						str					)     ->			Tuple:





								'''simple docstring'''




								_lowerCAmelCase			:			List[Any]        =					SpeechaTextaTokenizer.from_pretrained(self.tmpdirname					)

								# make sure @@ is correctly concatenated
								_lowerCAmelCase			:			int        =					[4, 6, 8, 7, 10]  # ["here@@", "couple", "words", "of@@", "the"]
								_lowerCAmelCase			:			List[Any]        =					tokenizer.decode(_UpperCAmelCase					)

								self.assertTrue(output_string == """herecouple words ofthe"""					)



				def 					SCREAMING_SNAKE_CASE				(					self					:						Dict					)     ->			Any:





								'''simple docstring'''




								_lowerCAmelCase			:			str        =					SpeechaTextaTokenizer.from_pretrained(self.tmpdirname					)

								with tempfile.TemporaryDirectory() as tmp_dirname:
												tokenizer.save_pretrained(_UpperCAmelCase					)
												os.remove(os.path.join(_UpperCAmelCase						,		"""merges.txt"""					)					)

												# load tokenizer without merges file should not throw an error
												_lowerCAmelCase			:			Any        =					SpeechaTextaTokenizer.from_pretrained(_UpperCAmelCase					)

								with tempfile.TemporaryDirectory() as tmp_dirname:
												# save tokenizer and load again
												tokenizer.save_pretrained(_UpperCAmelCase					)
												_lowerCAmelCase			:			int        =					SpeechaTextaTokenizer.from_pretrained(_UpperCAmelCase					)

								self.assertIsNotNone(_UpperCAmelCase					)



				def 					SCREAMING_SNAKE_CASE				(					self					:						Optional[Any]					)     ->			Optional[Any]:





								'''simple docstring'''




								if not self.test_slow_tokenizer:
												return

								_lowerCAmelCase			:			Dict        =					inspect.signature(self.tokenizer_class.__init__					)
								_lowerCAmelCase			:			Union[str, Any]        =					self.get_tokenizer()

								for parameter_name, parameter in signature.parameters.items():
												if parameter.default != inspect.Parameter.empty and parameter_name != "merges_file":
																self.assertIn(_UpperCAmelCase						,		tokenizer.init_kwargs					)

