


from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
 import tensorflow as tf

 from transformers import TFBlipTextModel
 from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST


class 		_SCREAMING_SNAKE_CASE     :


     def __init__(  self							,					A_							,					A_=12							,					A_=7							,					A_=True							,					A_=True							,					A_=True							,					A_=99							,					A_=32							,					A_=32							,					A_=2							,					A_=4							,					A_=37							,					A_=0.1							,					A_=0.1							,					A_=5_12							,					A_=0.0_2							,					A_=0							,					A_=None							,					):
       _UpperCAmelCase		:							Tuple							=     parent
       _UpperCAmelCase		:							Optional[Any]							=     batch_size
       _UpperCAmelCase		:							List[Any]							=     seq_length
       _UpperCAmelCase		:							List[str]							=     is_training
       _UpperCAmelCase		:							Optional[int]							=     use_input_mask
       _UpperCAmelCase		:							Any							=     use_labels
       _UpperCAmelCase		:							Union[str, Any]							=     vocab_size
       _UpperCAmelCase		:							Optional[int]							=     hidden_size
       _UpperCAmelCase		:							int							=     projection_dim
       _UpperCAmelCase		:							Union[str, Any]							=     num_hidden_layers
       _UpperCAmelCase		:							List[Any]							=     num_attention_heads
       _UpperCAmelCase		:							Optional[Any]							=     intermediate_size
       _UpperCAmelCase		:							int							=     dropout
       _UpperCAmelCase		:							Optional[Any]							=     attention_dropout
       _UpperCAmelCase		:							Optional[int]							=     max_position_embeddings
       _UpperCAmelCase		:							int							=     initializer_range
       _UpperCAmelCase		:							int							=     scope
       _UpperCAmelCase		:							List[Any]							=     bos_token_id


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							List[Any]							=     ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size    )

       _UpperCAmelCase		:							Tuple							=     None
       if self.use_input_mask:
         _UpperCAmelCase		:							Dict							=     random_attention_mask([self.batch_size, self.seq_length]    )

       if input_mask is not None:
         _UpperCAmelCase		:							Any							=     input_mask.numpy()
         _UpperCAmelCase,_UpperCAmelCase		:							Any							=     input_mask.shape
         _UpperCAmelCase		:							List[Any]							=     np.random.randint(1							,					seq_length - 1							,					size=(batch_size,)    )
         for batch_idx, start_index in enumerate(A_    ):
           _UpperCAmelCase		:							str							=     1
           _UpperCAmelCase		:							List[str]							=     0

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

       return config, input_ids, tf.convert_to_tensor(A_    )


     def        __snake_case(  self    ):
       return BlipTextConfig(
           vocab_size=self.vocab_size							,					hidden_size=self.hidden_size							,					projection_dim=self.projection_dim							,					num_hidden_layers=self.num_hidden_layers							,					num_attention_heads=self.num_attention_heads							,					intermediate_size=self.intermediate_size							,					dropout=self.dropout							,					attention_dropout=self.attention_dropout							,					max_position_embeddings=self.max_position_embeddings							,					initializer_range=self.initializer_range							,					bos_token_id=self.bos_token_id							,					)


     def        __snake_case(  self							,					A_							,					A_							,					A_    ):
       _UpperCAmelCase		:							List[str]							=     TFBlipTextModel(config=A_    )
       _UpperCAmelCase		:							Union[str, Any]							=     model(A_							,					attention_mask=A_							,					training=A_    )
       _UpperCAmelCase		:							Tuple							=     model(A_							,					training=A_    )
       self.parent.assertEqual(result.last_hidden_state.shape							,					(self.batch_size, self.seq_length, self.hidden_size)    )
       self.parent.assertEqual(result.pooler_output.shape							,					(self.batch_size, self.hidden_size)    )




     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Dict							=     self.prepare_config_and_inputs()
       _UpperCAmelCase,_UpperCAmelCase,_UpperCAmelCase		:							List[str]							=     config_and_inputs
       _UpperCAmelCase		:							str							=     {"""input_ids""": input_ids, """attention_mask""": input_mask}
       return config, inputs_dict


@require_tf
class 		_SCREAMING_SNAKE_CASE     (						A							,	unittest.TestCase     ):
     __SCREAMING_SNAKE_CASE     					=    (TFBlipTextModel,) if is_tf_available() else ()
     __SCREAMING_SNAKE_CASE     					=    False
     __SCREAMING_SNAKE_CASE     					=    False
     __SCREAMING_SNAKE_CASE     					=    False


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Optional[int]							=     BlipTextModelTester(self    )
       _UpperCAmelCase		:							Optional[int]							=     ConfigTester(self							,					config_class=A_							,					hidden_size=37    )


     def        __snake_case(  self    ):
       self.config_tester.run_common_tests()


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Union[str, Any]							=     self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*A_    )


     def        __snake_case(  self    ):
       pass


     def        __snake_case(  self    ):
       pass


     @unittest.skip(reason="""Blip does not use inputs_embeds"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING"""    )
     def        __snake_case(  self    ):
       pass


     @slow
     def        __snake_case(  self    ):
       for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         _UpperCAmelCase		:							Dict							=     TFBlipTextModel.from_pretrained(A_    )
         self.assertIsNotNone(A_    )




     def        __snake_case(  self							,					A_=True    ):
       super().test_pt_tf_model_equivalence(allow_missing_keys=A_    )









from PIL import Image




def    a__    (		snake_case__					:							Image						,   snake_case__					:							int     ):
  _UpperCAmelCase		:							Optional[Any]							=     (259 * (level + 255)) / (255 * (259 - level))

  def contrast(snake_case__					:							int     ) -> int:
    return int(128 + factor * (c - 128)     )

  return img.point(snake_case__     )


if __name__ == "__main__":
 # Load image
 with Image.open('image_data/lena.jpg') as img:
  # Change contrast to 170
  SCREAMING_SNAKE_CASE__					:     int       =     change_contrast(img, 170)
  cont_img.save('image_data/lena_high_contrast.png', format='png')





