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


if is_torch_available():
     import torch

if is_vision_available():
     from PIL import Image

     from transformers import BlipImageProcessor







class       __snake_case (  unittest.TestCase ):
 def __init__(	self		, snake_case__		, snake_case__=7		, snake_case__=3		, snake_case__=18		, snake_case__=30		, snake_case__=400		, snake_case__=True		, snake_case__=None		, snake_case__=True		, snake_case__=False		, snake_case__=[0.4814_5466, 0.457_8275, 0.4082_1073]		, snake_case__=[0.2686_2954, 0.2613_0258, 0.2757_7711]		, snake_case__=True		, )						->				Tuple:
       '''simple docstring'''





       UpperCAmelCase     :			str     	=size if size is not None else {'''height''': 20, '''width''': 20}
       UpperCAmelCase     :			Any     	=parent
       UpperCAmelCase     :			int     	=batch_size
       UpperCAmelCase     :			int     	=num_channels
       UpperCAmelCase     :			Dict     	=image_size
       UpperCAmelCase     :			Union[str, Any]     	=min_resolution
       UpperCAmelCase     :			Any     	=max_resolution
       UpperCAmelCase     :			Optional[int]     	=do_resize
       UpperCAmelCase     :			Union[str, Any]     	=size
       UpperCAmelCase     :			Optional[int]     	=do_normalize
       UpperCAmelCase     :			Union[str, Any]     	=image_mean
       UpperCAmelCase     :			Dict     	=image_std
       UpperCAmelCase     :			Union[str, Any]     	=do_pad
       UpperCAmelCase     :			str     	=do_convert_rgb
 def 						UpperCAmelCase__							(	self    )						->				Tuple:
       '''simple docstring'''





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






 def 						UpperCAmelCase__							(	self		, snake_case__=False		, snake_case__=False		, snake_case__=False    )						->				str:
       '''simple docstring'''





       assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

       if equal_resolution:
             UpperCAmelCase     :			Optional[int]     	=[]
             for i in range(self.batch_size    ):
                   image_inputs.append(
                       np.random.randint(
                           255		, size=(self.num_channels, self.max_resolution, self.max_resolution)		, dtype=np.uinta    )    )
       else:
             UpperCAmelCase     :			Optional[Any]     	=[]
             for i in range(self.batch_size    ):
                   UpperCAmelCase	,    UpperCAmelCase     :			Tuple     	=np.random.choice(np.arange(self.min_resolution		, self.max_resolution    )		, 2    )
                   image_inputs.append(np.random.randint(255		, size=(self.num_channels, width, height)		, dtype=np.uinta    )    )

       if not numpify and not torchify:
             # PIL expects the channel dimension as last dimension
             UpperCAmelCase     :			List[str]     	=[Image.fromarray(np.moveaxis(snake_case__		, 0		, -1    )    ) for x in image_inputs]

       if torchify:
             UpperCAmelCase     :			List[Any]     	=[torch.from_numpy(snake_case__    ) for x in image_inputs]

       return image_inputs







@require_torch
@require_vision
class       __snake_case (  lowerCamelCase__       , unittest.TestCase ):
 __lowerCamelCase     : List[str] 	=   BlipImageProcessor if is_vision_available() else None
 def 						UpperCAmelCase__							(	self    )						->				Dict:
       '''simple docstring'''





       UpperCAmelCase     :			Optional[Any]     	=BlipImageProcessingTester(self    )
 @property
 def 						UpperCAmelCase__							(	self    )						->				Optional[Any]:
       '''simple docstring'''





       return self.image_processor_tester.prepare_image_processor_dict()
 def 						UpperCAmelCase__							(	self    )						->				List[str]:
       '''simple docstring'''





       UpperCAmelCase     :			Union[str, Any]     	=self.image_processing_class(**self.image_processor_dict    )
       self.assertTrue(hasattr(snake_case__		, '''do_resize'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''size'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''do_normalize'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''image_mean'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''image_std'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''do_convert_rgb'''    )    )
 def 						UpperCAmelCase__							(	self    )						->				Union[str, Any]:
       '''simple docstring'''





       pass
 def 						UpperCAmelCase__							(	self    )						->				Optional[int]:
       '''simple docstring'''





       UpperCAmelCase     :			int     	=self.image_processing_class(**self.image_processor_dict    )
       # create random PIL images
       UpperCAmelCase     :			List[str]     	=self.image_processor_tester.prepare_inputs(equal_resolution=snake_case__    )
       for image in image_inputs:
             self.assertIsInstance(snake_case__		, Image.Image    )

       # Test not batched input
       UpperCAmelCase     :			Optional[Any]     	=image_processor(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['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )

       # Test batched
       UpperCAmelCase     :			Tuple     	=image_processor(snake_case__		, 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['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )
 def 						UpperCAmelCase__							(	self    )						->				Union[str, Any]:
       '''simple docstring'''





       UpperCAmelCase     :			str     	=self.image_processing_class(**self.image_processor_dict    )
       # create random numpy tensors
       UpperCAmelCase     :			int     	=self.image_processor_tester.prepare_inputs(equal_resolution=snake_case__		, numpify=snake_case__    )
       for image in image_inputs:
             self.assertIsInstance(snake_case__		, np.ndarray    )

       # Test not batched input
       UpperCAmelCase     :			Optional[Any]     	=image_processor(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['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )

       # Test batched
       UpperCAmelCase     :			Tuple     	=image_processor(snake_case__		, 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['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )






 def 						UpperCAmelCase__							(	self    )						->				List[Any]:
       '''simple docstring'''





       UpperCAmelCase     :			Optional[Any]     	=self.image_processing_class(**self.image_processor_dict    )
       # create random PyTorch tensors
       UpperCAmelCase     :			List[Any]     	=self.image_processor_tester.prepare_inputs(equal_resolution=snake_case__		, torchify=snake_case__    )
       for image in image_inputs:
             self.assertIsInstance(snake_case__		, torch.Tensor    )

       # Test not batched input
       UpperCAmelCase     :			Any     	=image_processor(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['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )

       # Test batched
       UpperCAmelCase     :			List[str]     	=image_processor(snake_case__		, 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['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )







@require_torch
@require_vision
class       __snake_case (  lowerCamelCase__       , unittest.TestCase ):
 __lowerCamelCase     : Optional[Any] 	=   BlipImageProcessor if is_vision_available() else None
 def 						UpperCAmelCase__							(	self    )						->				int:
       '''simple docstring'''





       UpperCAmelCase     :			Optional[int]     	=BlipImageProcessingTester(self		, num_channels=4    )
       UpperCAmelCase     :			int     	=3
 @property
 def 						UpperCAmelCase__							(	self    )						->				Optional[Any]:
       '''simple docstring'''





       return self.image_processor_tester.prepare_image_processor_dict()
 def 						UpperCAmelCase__							(	self    )						->				Tuple:
       '''simple docstring'''





       UpperCAmelCase     :			Dict     	=self.image_processing_class(**self.image_processor_dict    )
       self.assertTrue(hasattr(snake_case__		, '''do_resize'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''size'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''do_normalize'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''image_mean'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''image_std'''    )    )
       self.assertTrue(hasattr(snake_case__		, '''do_convert_rgb'''    )    )
 def 						UpperCAmelCase__							(	self    )						->				List[str]:
       '''simple docstring'''





       pass






 def 						UpperCAmelCase__							(	self    )						->				str:
       '''simple docstring'''





       UpperCAmelCase     :			int     	=self.image_processing_class(**self.image_processor_dict    )
       # create random PIL images
       UpperCAmelCase     :			List[str]     	=self.image_processor_tester.prepare_inputs(equal_resolution=snake_case__    )
       for image in image_inputs:
             self.assertIsInstance(snake_case__		, Image.Image    )

       # Test not batched input
       UpperCAmelCase     :			Dict     	=image_processor(image_inputs[0]		, return_tensors='''pt'''    ).pixel_values
       self.assertEqual(
           encoded_images.shape		, (
               1,
               self.expected_encoded_image_num_channels,
               self.image_processor_tester.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )

       # Test batched
       UpperCAmelCase     :			Union[str, Any]     	=image_processor(snake_case__		, return_tensors='''pt'''    ).pixel_values
       self.assertEqual(
           encoded_images.shape		, (
               self.image_processor_tester.batch_size,
               self.expected_encoded_image_num_channels,
               self.image_processor_tester.size['''height'''],
               self.image_processor_tester.size['''width'''],
           )		, )

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 (  lowerCamelCase__       , unittest.TestCase ):
 __lowerCamelCase     : Any 	=   SpeechaTextaTokenizer
 __lowerCamelCase     : Tuple 	=   False
 def 						UpperCAmelCase__							(	self    )						->				List[Any]:
       '''simple docstring'''





       super().setUp()

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

       UpperCAmelCase     :			Tuple     	={'''pad_token''': '''<pad>''', '''unk_token''': '''<unk>''', '''bos_token''': '''<s>''', '''eos_token''': '''</s>'''}

       UpperCAmelCase     :			Tuple     	=tempfile.mkdtemp()
       UpperCAmelCase     :			Optional[Any]     	=os.path.join(self.tmpdirname		, VOCAB_FILES_NAMES['''vocab_file''']    )
       UpperCAmelCase     :			Dict     	=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(snake_case__    ) + '''\n'''    )

       with open(self.merges_file		, '''w'''    ) as fp:
             fp.write('''\n'''.join(snake_case__    )    )
 def 						UpperCAmelCase__							(	self    )						->				Union[str, Any]:
       '''simple docstring'''





       UpperCAmelCase     :			Union[str, Any]     	=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(snake_case__    )		, 14    )
 def 						UpperCAmelCase__							(	self    )						->				Union[str, Any]:
       '''simple docstring'''





       self.assertEqual(self.get_tokenizer().vocab_size		, 14    )
 def 						UpperCAmelCase__							(	self    )						->				List[Any]:
       '''simple docstring'''





       UpperCAmelCase     :			Union[str, Any]     	=SpeechaTextaTokenizer.from_pretrained(self.tmpdirname    )

       # make sure @@ is correctly concatenated
       UpperCAmelCase     :			str     	=[4, 6, 8, 7, 10]  # ["here@@", "couple", "words", "of@@", "the"]
       UpperCAmelCase     :			Optional[Any]     	=tokenizer.decode(snake_case__    )

       self.assertTrue(output_string == '''herecouple words ofthe'''    )
 def 						UpperCAmelCase__							(	self    )						->				Tuple:
       '''simple docstring'''





       UpperCAmelCase     :			Optional[Any]     	=SpeechaTextaTokenizer.from_pretrained(self.tmpdirname    )

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

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

       with tempfile.TemporaryDirectory() as tmp_dirname:
             # save tokenizer and load again
             tokenizer.save_pretrained(snake_case__    )
             UpperCAmelCase     :			List[str]     	=SpeechaTextaTokenizer.from_pretrained(snake_case__    )

       self.assertIsNotNone(snake_case__    )






 def 						UpperCAmelCase__							(	self    )						->				int:
       '''simple docstring'''





       if not self.test_slow_tokenizer:
             return

       UpperCAmelCase     :			List[str]     	=inspect.signature(self.tokenizer_class.__init__    )
       UpperCAmelCase     :			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(snake_case__		, tokenizer.init_kwargs    )
