





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       lowerCAmelCase__:






       '''simple docstring'''


       def __init__(   self     ,    __lowerCamelCase     ,    __lowerCamelCase=1_3     ,    __lowerCamelCase=3_0     ,    __lowerCamelCase=2     ,    __lowerCamelCase=3     ,    __lowerCamelCase=True     ,    __lowerCamelCase=True     ,    __lowerCamelCase=3_2     ,    __lowerCamelCase=2     ,    __lowerCamelCase=4     ,    __lowerCamelCase=3_7     ,    __lowerCamelCase="gelu"     ,    __lowerCamelCase=0.1     ,    __lowerCamelCase=0.1     ,    __lowerCamelCase=1_0     ,    __lowerCamelCase=0.02     ,    __lowerCamelCase=3     ,    __lowerCamelCase=None     ,    )      ->  int:
            _SCREAMING_SNAKE_CASE							:						Tuple         =				parent
            _SCREAMING_SNAKE_CASE							:						List[str]         =				batch_size
            _SCREAMING_SNAKE_CASE							:						Dict         =				image_size
            _SCREAMING_SNAKE_CASE							:						str         =				patch_size
            _SCREAMING_SNAKE_CASE							:						List[Any]         =				num_channels
            _SCREAMING_SNAKE_CASE							:						Any         =				is_training
            _SCREAMING_SNAKE_CASE							:						int         =				use_labels
            _SCREAMING_SNAKE_CASE							:						Dict         =				hidden_size
            _SCREAMING_SNAKE_CASE							:						List[str]         =				num_hidden_layers
            _SCREAMING_SNAKE_CASE							:						List[Any]         =				num_attention_heads
            _SCREAMING_SNAKE_CASE							:						List[str]         =				intermediate_size
            _SCREAMING_SNAKE_CASE							:						List[Any]         =				hidden_act
            _SCREAMING_SNAKE_CASE							:						List[Any]         =				hidden_dropout_prob
            _SCREAMING_SNAKE_CASE							:						int         =				attention_probs_dropout_prob
            _SCREAMING_SNAKE_CASE							:						Tuple         =				type_sequence_label_size
            _SCREAMING_SNAKE_CASE							:						Dict         =				initializer_range
            _SCREAMING_SNAKE_CASE							:						Any         =				scope

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




       def      UpperCamelCase_      (   self				)      ->  Optional[int]:
            _SCREAMING_SNAKE_CASE							:						Optional[Any]         =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

            _SCREAMING_SNAKE_CASE							:						List[Any]         =				None
            if self.use_labels:
                 _SCREAMING_SNAKE_CASE							:						Union[str, Any]         =				ids_tensor([self.batch_size]     ,    self.type_sequence_label_size				)

            _SCREAMING_SNAKE_CASE							:						Optional[Any]         =				self.get_config()

            return config, pixel_values, labels




       def      UpperCamelCase_      (   self				)      ->  Any:
            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      UpperCamelCase_      (   self     ,    __lowerCamelCase     ,    __lowerCamelCase     ,    __lowerCamelCase				)      ->  List[Any]:
            _SCREAMING_SNAKE_CASE							:						Optional[int]         =				TFViTModel(config=__snake_case				)
            _SCREAMING_SNAKE_CASE							:						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.
            _SCREAMING_SNAKE_CASE							:						Optional[Any]         =				self.image_size // 2
            _SCREAMING_SNAKE_CASE							:						Tuple         =				pixel_values[:, :, :image_size, :image_size]
            _SCREAMING_SNAKE_CASE							:						Optional[Any]         =				model(__snake_case     ,    interpolate_pos_encoding=__snake_case     ,    training=__snake_case				)
            _SCREAMING_SNAKE_CASE							:						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      UpperCamelCase_      (   self     ,    __lowerCamelCase     ,    __lowerCamelCase     ,    __lowerCamelCase				)      ->  str:
            _SCREAMING_SNAKE_CASE							:						Optional[int]         =				self.type_sequence_label_size
            _SCREAMING_SNAKE_CASE							:						Dict         =				TFViTForImageClassification(__snake_case				)
            _SCREAMING_SNAKE_CASE							:						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.
            _SCREAMING_SNAKE_CASE							:						Tuple         =				self.image_size // 2
            _SCREAMING_SNAKE_CASE							:						Optional[Any]         =				pixel_values[:, :, :image_size, :image_size]
            _SCREAMING_SNAKE_CASE							:						Dict         =				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
            _SCREAMING_SNAKE_CASE							:						str         =				1
            _SCREAMING_SNAKE_CASE							:						Optional[Any]         =				TFViTForImageClassification(__snake_case				)
            _SCREAMING_SNAKE_CASE							:						Optional[Any]         =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
            _SCREAMING_SNAKE_CASE							:						Dict         =				model(__snake_case				)
            self.parent.assertEqual(result.logits.shape     ,    (self.batch_size, self.type_sequence_label_size)				)







       def      UpperCamelCase_      (   self				)      ->  Any:
            _SCREAMING_SNAKE_CASE							:						str         =				self.prepare_config_and_inputs()
            _SCREAMING_SNAKE_CASE     ,      _SCREAMING_SNAKE_CASE     ,      _SCREAMING_SNAKE_CASE							:						Optional[int]         =				config_and_inputs
            _SCREAMING_SNAKE_CASE							:						Dict         =				{"pixel_values": pixel_values}
            return config, inputs_dict





@require_tf
class       lowerCAmelCase__(			snake_case_	,  snake_case_	,  unittest.TestCase     ):






       '''simple docstring'''


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

       __snake_case			      =     False
       __snake_case			      =     False
       __snake_case			      =     False




       def      UpperCamelCase_      (   self				)      ->  str:
            _SCREAMING_SNAKE_CASE							:						Optional[int]         =				TFViTModelTester(self				)
            _SCREAMING_SNAKE_CASE							:						Optional[Any]         =				ConfigTester(self     ,    config_class=__snake_case     ,    has_text_modality=__snake_case     ,    hidden_size=3_7				)




       def      UpperCamelCase_      (   self				)      ->  Optional[Any]:
            self.config_tester.run_common_tests()




       @unittest.skip(reason="ViT does not use inputs_embeds"				)
       def      UpperCamelCase_      (   self				)      ->  Dict:
            pass




       @unittest.skip(reason="ViT does not use inputs_embeds"				)
       def      UpperCamelCase_      (   self				)      ->  Any:
            pass




       def      UpperCamelCase_      (   self				)      ->  List[str]:
            _SCREAMING_SNAKE_CASE     ,      _SCREAMING_SNAKE_CASE							:						Union[str, Any]         =				self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 _SCREAMING_SNAKE_CASE							:						Dict         =				model_class(__snake_case				)
                 self.assertIsInstance(model.get_input_embeddings()     ,    (tf.keras.layers.Layer)				)
                 _SCREAMING_SNAKE_CASE							:						int         =				model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(__snake_case     ,    tf.keras.layers.Layer				)				)




       def      UpperCamelCase_      (   self				)      ->  List[str]:
            _SCREAMING_SNAKE_CASE     ,      _SCREAMING_SNAKE_CASE							:						Optional[Any]         =				self.model_tester.prepare_config_and_inputs_for_common()

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

                 _SCREAMING_SNAKE_CASE							:						Any         =				["pixel_values"]
                 self.assertListEqual(arg_names[:1]     ,    __snake_case				)




       def      UpperCamelCase_      (   self				)      ->  int:
            _SCREAMING_SNAKE_CASE							:						List[str]         =				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__snake_case				)




       def      UpperCamelCase_      (   self				)      ->  List[str]:
            _SCREAMING_SNAKE_CASE							:						List[str]         =				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*__snake_case				)







       @slow
       def      UpperCamelCase_      (   self				)      ->  int:
            _SCREAMING_SNAKE_CASE							:						Union[str, Any]         =				TFViTModel.from_pretrained("google/vit-base-patch16-224"				)
            self.assertIsNotNone(__snake_case				)






def      lowerCamelCase__      ():
     _SCREAMING_SNAKE_CASE							:						Tuple         =				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
     return image





@require_tf
@require_vision
class       lowerCAmelCase__(			unittest.TestCase     ):






       '''simple docstring'''


       @cached_property
       def      UpperCamelCase_      (   self				)      ->  List[str]:
            return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224"				) if is_vision_available() else None







       @slow
       def      UpperCamelCase_      (   self				)      ->  Optional[int]:
            _SCREAMING_SNAKE_CASE							:						Union[str, Any]         =				TFViTForImageClassification.from_pretrained("google/vit-base-patch16-224"				)

            _SCREAMING_SNAKE_CASE							:						int         =				self.default_image_processor
            _SCREAMING_SNAKE_CASE							:						Optional[int]         =				prepare_img()
            _SCREAMING_SNAKE_CASE							:						List[str]         =				image_processor(images=__snake_case     ,    return_tensors="tf"				)

            # forward pass
            _SCREAMING_SNAKE_CASE							:						List[Any]         =				model(**__snake_case				)

            # verify the logits
            _SCREAMING_SNAKE_CASE							:						Optional[int]         =				tf.TensorShape((1, 1_0_0_0)				)
            self.assertEqual(outputs.logits.shape     ,    __snake_case				)

            _SCREAMING_SNAKE_CASE							:						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 datetime import datetime as dt
import os

from github import Github


A__							:  str    =[
    '''good first issue''',
    '''good second issue''',
    '''good difficult issue''',
    '''feature request''',
    '''new model''',
    '''wip''',
]
def   UpperCamelCase__ (						):





      """simple docstring"""


      _lowerCAmelCase					=				Github(os.environ["""GITHUB_TOKEN"""]    )
      _lowerCAmelCase					=				g.get_repo("""huggingface/transformers"""    )
      _lowerCAmelCase					=				repo.get_issues(state="""open"""    )

      for issue in open_issues:
            _lowerCAmelCase					=				sorted([comment for comment in issue.get_comments()]				,      key=lambda lowerCAmelCase    : i.created_at				,      reverse=lowerCAmelCase    )
            _lowerCAmelCase					=				comments[0] if len(lowerCAmelCase    ) > 0 else None
            if (
                last_comment is not None
                and last_comment.user.login == "github-actions[bot]"
                and (dt.utcnow() - issue.updated_at).days > 7
                and (dt.utcnow() - issue.created_at).days >= 30
                and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()    )
            ):
                  # print(f"Would close issue {issue.number} since it has been 7 days of inactivity since bot mention.")
                  issue.edit(state="""closed"""    )
            elif (
                (dt.utcnow() - issue.updated_at).days > 23
                and (dt.utcnow() - issue.created_at).days >= 30
                and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()    )
            ):
                  # print(f"Would add stale comment to {issue.number}")
                  issue.create_comment(
                      """This issue has been automatically marked as stale because it has not had """
                      """recent activity. If you think this still needs to be addressed """
                      """please comment on this thread.\n\nPlease note that issues that do not follow the """
                      """[contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) """
                      """are likely to be ignored."""    )


if __name__ == "__main__":
      main()






