


'''simple docstring'''







from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
       import tensorflow as tf

       from transformers import TFViTForImageClassification, TFViTModel


if is_vision_available():
       from PIL import Image

       from transformers import ViTImageProcessor
class 	UpperCamelCase__					:



     """simple docstring"""






     def __init__( self      ,	snake_case__      ,	snake_case__=13      ,	snake_case__=30      ,	snake_case__=2      ,	snake_case__=3      ,	snake_case__=True      ,	snake_case__=True      ,	snake_case__=32      ,	snake_case__=2      ,	snake_case__=4      ,	snake_case__=37      ,	snake_case__="gelu"      ,	snake_case__=0.1      ,	snake_case__=0.1      ,	snake_case__=10      ,	snake_case__=0.02      ,	snake_case__=3      ,	snake_case__=None      ,	):

            '''simple docstring'''

            _lowerCAmelCase    :							Optional[Any]   			=       parent
            _lowerCAmelCase    :							str   			=       batch_size
            _lowerCAmelCase    :							Optional[int]   			=       image_size
            _lowerCAmelCase    :							Optional[int]   			=       patch_size
            _lowerCAmelCase    :							Optional[int]   			=       num_channels
            _lowerCAmelCase    :							int   			=       is_training
            _lowerCAmelCase    :							Tuple   			=       use_labels
            _lowerCAmelCase    :							Optional[Any]   			=       hidden_size
            _lowerCAmelCase    :							List[str]   			=       num_hidden_layers
            _lowerCAmelCase    :							Any   			=       num_attention_heads
            _lowerCAmelCase    :							Optional[Any]   			=       intermediate_size
            _lowerCAmelCase    :							Tuple   			=       hidden_act
            _lowerCAmelCase    :							List[str]   			=       hidden_dropout_prob
            _lowerCAmelCase    :							Tuple   			=       attention_probs_dropout_prob
            _lowerCAmelCase    :							Union[str, Any]   			=       type_sequence_label_size
            _lowerCAmelCase    :							Dict   			=       initializer_range
            _lowerCAmelCase    :							Union[str, Any]   			=       scope

            # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
            _lowerCAmelCase    :							Tuple   			=       (image_size // patch_size) ** 2
            _lowerCAmelCase    :							Any   			=       num_patches + 1







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Union[str, Any]   			=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

            _lowerCAmelCase    :							int   			=       None
            if self.use_labels:
                   _lowerCAmelCase    :							List[str]   			=       ids_tensor([self.batch_size]      ,	self.type_sequence_label_size   )

            _lowerCAmelCase    :							int   			=       self.get_config()

            return config, pixel_values, labels







     def a	( self   ):

            '''simple docstring'''

            return ViTConfig(
                image_size=self.image_size      ,	patch_size=self.patch_size      ,	num_channels=self.num_channels      ,	hidden_size=self.hidden_size      ,	num_hidden_layers=self.num_hidden_layers      ,	num_attention_heads=self.num_attention_heads      ,	intermediate_size=self.intermediate_size      ,	hidden_act=self.hidden_act      ,	hidden_dropout_prob=self.hidden_dropout_prob      ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,	is_decoder=snake_case__      ,	initializer_range=self.initializer_range      ,	)







     def a	( self      ,	snake_case__      ,	snake_case__      ,	snake_case__   ):

            '''simple docstring'''

            _lowerCAmelCase    :							List[str]   			=       TFViTModel(config=snake_case__   )
            _lowerCAmelCase    :							List[Any]   			=       model(snake_case__      ,	training=snake_case__   )
            self.parent.assertEqual(result.last_hidden_state.shape      ,	(self.batch_size, self.seq_length, self.hidden_size)   )

            # Test with an image with different size than the one specified in config.
            _lowerCAmelCase    :							Tuple   			=       self.image_size // 2
            _lowerCAmelCase    :							str   			=       pixel_values[:, :, :image_size, :image_size]
            _lowerCAmelCase    :							Optional[int]   			=       model(snake_case__      ,	interpolate_pos_encoding=snake_case__      ,	training=snake_case__   )
            _lowerCAmelCase    :							Tuple   			=       (image_size // self.patch_size) ** 2 + 1
            self.parent.assertEqual(result.last_hidden_state.shape      ,	(self.batch_size, seq_length, self.hidden_size)   )







     def a	( self      ,	snake_case__      ,	snake_case__      ,	snake_case__   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Dict   			=       self.type_sequence_label_size
            _lowerCAmelCase    :							Optional[Any]   			=       TFViTForImageClassification(snake_case__   )
            _lowerCAmelCase    :							Dict   			=       model(snake_case__      ,	labels=snake_case__      ,	training=snake_case__   )
            self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.type_sequence_label_size)   )

            # Test with an image with different size than the one specified in config.
            _lowerCAmelCase    :							List[Any]   			=       self.image_size // 2
            _lowerCAmelCase    :							str   			=       pixel_values[:, :, :image_size, :image_size]
            _lowerCAmelCase    :							Union[str, Any]   			=       model(snake_case__      ,	interpolate_pos_encoding=snake_case__      ,	training=snake_case__   )
            self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.type_sequence_label_size)   )

            # test greyscale images
            _lowerCAmelCase    :							int   			=       1
            _lowerCAmelCase    :							Any   			=       TFViTForImageClassification(snake_case__   )
            _lowerCAmelCase    :							Tuple   			=       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
            _lowerCAmelCase    :							Union[str, Any]   			=       model(snake_case__   )
            self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.type_sequence_label_size)   )







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							int   			=       self.prepare_config_and_inputs()
            _lowerCAmelCase    :							Tuple   			=       config_and_inputs
            _lowerCAmelCase    :							int   			=       {'pixel_values': pixel_values}
            return config, inputs_dict
@require_tf
class 	UpperCamelCase__					( SCREAMING_SNAKE_CASE_						,					SCREAMING_SNAKE_CASE_						,					unittest.TestCase	):



     """simple docstring"""






     __magic_name__    			=    (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
     __magic_name__    			=    (
         {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
         if is_tf_available()
         else {}
     )

     __magic_name__    			=    False
     __magic_name__    			=    False
     __magic_name__    			=    False







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Dict   			=       TFViTModelTester(self   )
            _lowerCAmelCase    :							Any   			=       ConfigTester(self      ,	config_class=snake_case__      ,	has_text_modality=snake_case__      ,	hidden_size=37   )







     def a	( self   ):

            '''simple docstring'''

            self.config_tester.run_common_tests()







     @unittest.skip(reason='ViT does not use inputs_embeds'   )
     def a	( self   ):

            '''simple docstring'''

            pass







     @unittest.skip(reason='ViT does not use inputs_embeds'   )
     def a	( self   ):

            '''simple docstring'''

            pass







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Any   			=       self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   _lowerCAmelCase    :							List[str]   			=       model_class(snake_case__   )
                   self.assertIsInstance(model.get_input_embeddings()      ,	(tf.keras.layers.Layer)   )
                   _lowerCAmelCase    :							List[str]   			=       model.get_output_embeddings()
                   self.assertTrue(x is None or isinstance(snake_case__      ,	tf.keras.layers.Layer   )   )







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Optional[int]   			=       self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   _lowerCAmelCase    :							Dict   			=       model_class(snake_case__   )
                   _lowerCAmelCase    :							str   			=       inspect.signature(model.call   )
                   # signature.parameters is an OrderedDict => so arg_names order is deterministic
                   _lowerCAmelCase    :							Any   			=       [*signature.parameters.keys()]

                   _lowerCAmelCase    :							Tuple   			=       ['pixel_values']
                   self.assertListEqual(arg_names[:1]      ,	snake_case__   )







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							List[Any]   			=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*snake_case__   )







     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							List[Any]   			=       self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*snake_case__   )







     @slow
     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Any   			=       TFViTModel.from_pretrained('google/vit-base-patch16-224'   )
            self.assertIsNotNone(snake_case__   )






def 			lowercase				():







       """simple docstring"""







       _lowerCAmelCase    :							Dict   			=       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					)
       return image
@require_tf
@require_vision
class 	UpperCamelCase__					( unittest.TestCase	):



     """simple docstring"""






     @cached_property
     def a	( self   ):

            '''simple docstring'''

            return ViTImageProcessor.from_pretrained('google/vit-base-patch16-224'   ) if is_vision_available() else None







     @slow
     def a	( self   ):

            '''simple docstring'''

            _lowerCAmelCase    :							Optional[Any]   			=       TFViTForImageClassification.from_pretrained('google/vit-base-patch16-224'   )

            _lowerCAmelCase    :							int   			=       self.default_image_processor
            _lowerCAmelCase    :							Dict   			=       prepare_img()
            _lowerCAmelCase    :							Optional[int]   			=       image_processor(images=snake_case__      ,	return_tensors='tf'   )

            # forward pass
            _lowerCAmelCase    :							Any   			=       model(**snake_case__   )

            # verify the logits
            _lowerCAmelCase    :							Tuple   			=       tf.TensorShape((1, 1000)   )
            self.assertEqual(outputs.logits.shape      ,	snake_case__   )

            _lowerCAmelCase    :							Union[str, Any]   			=       tf.constant([-0.2744, 0.8215, -0.0836]   )

            tf.debugging.assert_near(outputs.logits[0, :3]      ,	snake_case__      ,	atol=1E-4   )





'''simple docstring'''







from collections import Counter
from timeit import timeit






def 			lowercase				(_A = ""   ,						):







       """simple docstring"""







       return sum(c % 2 for c in Counter(input_str.replace(' '   ,						''					).lower()					).values()					) < 2






def 			lowercase				(_A = ""					):







       """simple docstring"""







       if len(_A					) == 0:
              return True
       _lowerCAmelCase    :							Union[str, Any]   			=       input_str.replace(' '   ,						''					).lower()
       # character_freq_dict: Stores the frequency of every character in the input string
       _lowerCAmelCase    :							dict[str, int]   			=       {}

       for character in lower_case_input_str:
              _lowerCAmelCase    :							Union[str, Any]   			=       character_freq_dict.get(_A   ,						0					) + 1
       _lowerCAmelCase    :							List[Any]   			=       0

       for character_count in character_freq_dict.values():
              if character_count % 2:
                     odd_char += 1
       if odd_char > 1:
              return False
       return True






def 			lowercase				(_A = ""					):







       """simple docstring"""







       print('\nFor string = '   ,						_A   ,						':'					)
       print(
           '> can_string_be_rearranged_as_palindrome_counter()'   ,						'\tans ='   ,						can_string_be_rearranged_as_palindrome_counter(_A					)   ,						'\ttime ='   ,						timeit(
               'z.can_string_be_rearranged_as_palindrome_counter(z.check_str)'   ,						setup='import __main__ as z'   ,						)   ,						'seconds'   ,						)
       print(
           '> can_string_be_rearranged_as_palindrome()'   ,						'\tans ='   ,						can_string_be_rearranged_as_palindrome(_A					)   ,						'\ttime ='   ,						timeit(
               'z.can_string_be_rearranged_as_palindrome(z.check_str)'   ,						setup='import __main__ as z'   ,						)   ,						'seconds'   ,						)


if __name__ == "__main__":
       lowerCAmelCase    :     Tuple					=						input(
           """Enter string to determine if it can be rearranged as a palindrome or not: """
       ).strip()
       benchmark(check_str)
       lowerCAmelCase    :     Optional[Any]					=						can_string_be_rearranged_as_palindrome_counter(check_str)
       print(F'''{check_str} can {"" if status else "not "}be rearranged as a palindrome''')

