



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       UpperCamelCase  (					unittest.TestCase				):





     """simple docstring"""






     def __init__(					self :							Optional[int]	,      _lowerCamelCase :							int	,      _lowerCamelCase :							Optional[Any]=7	,      _lowerCamelCase :							Union[str, Any]=3	,      _lowerCamelCase :							int=1_8	,      _lowerCamelCase :							Any=3_0	,      _lowerCamelCase :							str=4_0_0	,      _lowerCamelCase :							Optional[Any]=True	,      _lowerCamelCase :							Any=None	,      _lowerCamelCase :							Optional[Any]=0.875	,      _lowerCamelCase :							Any=True	,      _lowerCamelCase :							List[Any]=[0.5, 0.5, 0.5]	,      _lowerCamelCase :							Tuple=[0.5, 0.5, 0.5]	,      ):
          A__				=   size if size is not None else {'''shortest_edge''': 2_0}
          A__				=   parent
          A__				=   batch_size
          A__				=   num_channels
          A__				=   image_size
          A__				=   min_resolution
          A__				=   max_resolution
          A__				=   do_resize
          A__				=   size
          A__				=   crop_pct
          A__				=   do_normalize
          A__				=   image_mean
          A__				=   image_std





     def 						A__  (					self :							Any ):
          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       UpperCamelCase  (					lowerCAmelCase_      , unittest.TestCase				):





     """simple docstring"""






     _lowerCamelCase					: str				   =ConvNextImageProcessor if is_vision_available() else None





     def 						A__  (					self :							Optional[int] ):
          A__				=   ConvNextImageProcessingTester(self )





     @property
     def 						A__  (					self :							Dict ):
          return self.image_processor_tester.prepare_image_processor_dict()





     def 						A__  (					self :							Union[str, Any] ):
          A__				=   self.image_processing_class(**self.image_processor_dict )
          self.assertTrue(hasattr(lowerCamelCase_	,      '''do_resize''' ) )
          self.assertTrue(hasattr(lowerCamelCase_	,      '''size''' ) )
          self.assertTrue(hasattr(lowerCamelCase_	,      '''crop_pct''' ) )
          self.assertTrue(hasattr(lowerCamelCase_	,      '''do_normalize''' ) )
          self.assertTrue(hasattr(lowerCamelCase_	,      '''image_mean''' ) )
          self.assertTrue(hasattr(lowerCamelCase_	,      '''image_std''' ) )





     def 						A__  (					self :							List[str] ):
          A__				=   self.image_processing_class.from_dict(self.image_processor_dict )
          self.assertEqual(image_processor.size	,      {'''shortest_edge''': 2_0} )

          A__				=   self.image_processing_class.from_dict(self.image_processor_dict	,      size=4_2 )
          self.assertEqual(image_processor.size	,      {'''shortest_edge''': 4_2} )





     def 						A__  (					self :							Dict ):
          pass





     def 						A__  (					self :							str ):
          A__				=   self.image_processing_class(**self.image_processor_dict )
          # create random PIL images
          A__				=   prepare_image_inputs(self.image_processor_tester	,      equal_resolution=lowerCamelCase_ )
          for image in image_inputs:
               self.assertIsInstance(lowerCamelCase_	,      Image.Image )

          # Test not batched input
          A__				=   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
          A__				=   image_processing(lowerCamelCase_	,      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 						A__  (					self :							List[str] ):
          A__				=   self.image_processing_class(**self.image_processor_dict )
          # create random numpy tensors
          A__				=   prepare_image_inputs(self.image_processor_tester	,      equal_resolution=lowerCamelCase_	,      numpify=lowerCamelCase_ )
          for image in image_inputs:
               self.assertIsInstance(lowerCamelCase_	,      np.ndarray )

          # Test not batched input
          A__				=   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
          A__				=   image_processing(lowerCamelCase_	,      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 						A__  (					self :							int ):
          A__				=   self.image_processing_class(**self.image_processor_dict )
          # create random PyTorch tensors
          A__				=   prepare_image_inputs(self.image_processor_tester	,      equal_resolution=lowerCamelCase_	,      torchify=lowerCamelCase_ )
          for image in image_inputs:
               self.assertIsInstance(lowerCamelCase_	,      torch.Tensor )

          # Test not batched input
          A__				=   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
          A__				=   image_processing(lowerCamelCase_	,      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'''],
              )	,      )








"""simple docstring"""







import json
import os
import unittest

from transformers import HerbertTokenizer, HerbertTokenizerFast
from transformers.models.herbert.tokenization_herbert import VOCAB_FILES_NAMES
from transformers.testing_utils import get_tests_dir, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin

@require_tokenizers
class       UpperCamelCase  (					a      , unittest.TestCase				):





     """simple docstring"""






     _lowerCamelCase					: Dict				   =HerbertTokenizer
     _lowerCamelCase					: Optional[int]				   =HerbertTokenizerFast
     _lowerCamelCase					: Optional[Any]				   =True





     def 						A__  (					self :							Dict ):
          super().setUp()

          # Use a simpler test file without japanese/chinese characters
          with open(F'''{get_tests_dir()}/fixtures/sample_text_no_unicode.txt'''	,      encoding='''utf-8''' ) as f_data:
               A__				=   f_data.read().replace('''\n\n'''	,      '''\n''' ).strip()

          A__				=   [
              '''<s>''',
              '''</s>''',
              '''l''',
              '''o''',
              '''w''',
              '''e''',
              '''r''',
              '''s''',
              '''t''',
              '''i''',
              '''d''',
              '''n''',
              '''w</w>''',
              '''r</w>''',
              '''t</w>''',
              '''lo''',
              '''low''',
              '''er</w>''',
              '''low</w>''',
              '''lowest</w>''',
              '''newer</w>''',
              '''wider</w>''',
              ''',</w>''',
              '''<unk>''',
          ]
          A__				=   dict(zip(_lowerCamelCase	,      range(len(_lowerCamelCase ) ) ) )
          A__				=   ['''l o 123''', '''lo w 1456''', '''e r</w> 1789''', '''''']

          A__				=   os.path.join(self.tmpdirname	,      VOCAB_FILES_NAMES['''vocab_file'''] )
          A__				=   os.path.join(self.tmpdirname	,      VOCAB_FILES_NAMES['''merges_file'''] )
          with open(self.vocab_file	,      '''w''' ) as fp:
               fp.write(json.dumps(_lowerCamelCase ) )
          with open(self.merges_file	,      '''w''' ) as fp:
               fp.write('''\n'''.join(_lowerCamelCase ) )





     def 						A__  (					self :							List[Any]	,      _lowerCamelCase :							Union[str, Any] ):
          A__				=   '''lower newer'''
          A__				=   '''lower newer'''
          return input_text, output_text





     def 						A__  (					self :							int ):
          A__				=   self.tokenizer_class(vocab_file=self.vocab_file	,      merges_file=self.merges_file )

          A__				=   '''lower'''
          A__				=   ['''low''', '''er</w>''']
          A__				=   tokenizer.tokenize(_lowerCamelCase )
          self.assertListEqual(_lowerCamelCase	,      _lowerCamelCase )

          A__				=   tokens + ['''<unk>''']
          A__				=   [1_6, 1_7, 2_3]
          self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCamelCase )	,      _lowerCamelCase )





     def 						A__  (					self :							Optional[int] ):
          if not self.test_rust_tokenizer:
               return

          A__				=   self.get_tokenizer()
          A__				=   self.get_rust_tokenizer()

          A__				=   '''lower,newer'''

          A__				=   tokenizer.tokenize(_lowerCamelCase )
          A__				=   rust_tokenizer.tokenize(_lowerCamelCase )
          self.assertListEqual(_lowerCamelCase	,      _lowerCamelCase )

          A__				=   tokenizer.encode(_lowerCamelCase	,      add_special_tokens=_lowerCamelCase )
          A__				=   rust_tokenizer.encode(_lowerCamelCase	,      add_special_tokens=_lowerCamelCase )
          self.assertListEqual(_lowerCamelCase	,      _lowerCamelCase )

          A__				=   self.get_rust_tokenizer()
          A__				=   tokenizer.encode(_lowerCamelCase )
          A__				=   rust_tokenizer.encode(_lowerCamelCase )
          self.assertListEqual(_lowerCamelCase	,      _lowerCamelCase )





     @slow
     def 						A__  (					self :							Any ):
          A__				=   self.tokenizer_class.from_pretrained('''allegro/herbert-base-cased''' )

          A__				=   tokenizer.encode('''konstruowanie sekwencji'''	,      add_special_tokens=_lowerCamelCase )
          A__				=   tokenizer.encode('''konstruowanie wielu sekwencji'''	,      add_special_tokens=_lowerCamelCase )

          A__				=   tokenizer.build_inputs_with_special_tokens(_lowerCamelCase )
          A__				=   tokenizer.build_inputs_with_special_tokens(_lowerCamelCase	,      _lowerCamelCase )

          assert encoded_sentence == [0] + text + [2]
          assert encoded_pair == [0] + text + [2] + text_a + [2]



