






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







     '''simple docstring'''




     def __init__(					self							:      List[str]			,						UpperCamelCase_							:      Optional[Any]			,						UpperCamelCase_							:      Optional[Any]=12			,						UpperCamelCase_							:      Union[str, Any]=7			,						UpperCamelCase_							:      str=True			,						UpperCamelCase_							:      Dict=True			,						UpperCamelCase_							:      Optional[Any]=True			,						UpperCamelCase_							:      Optional[int]=99			,						UpperCamelCase_							:      Tuple=32			,						UpperCamelCase_							:      Optional[int]=32			,						UpperCamelCase_							:      Any=2			,						UpperCamelCase_							:      List[Any]=4			,						UpperCamelCase_							:      int=37			,						UpperCamelCase_							:      str=0.1			,						UpperCamelCase_							:      Tuple=0.1			,						UpperCamelCase_							:      Tuple=512			,						UpperCamelCase_							:      List[Any]=0.02			,						UpperCamelCase_							:      Dict=0			,						UpperCamelCase_							:      Optional[int]=None			,						):


         """simple docstring"""



         __A       =						parent
         __A       =						batch_size
         __A       =						seq_length
         __A       =						is_training
         __A       =						use_input_mask
         __A       =						use_labels
         __A       =						vocab_size
         __A       =						hidden_size
         __A       =						projection_dim
         __A       =						num_hidden_layers
         __A       =						num_attention_heads
         __A       =						intermediate_size
         __A       =						dropout
         __A       =						attention_dropout
         __A       =						max_position_embeddings
         __A       =						initializer_range
         __A       =						scope
         __A       =						bos_token_id




     def 		lowerCAmelCase_  (					self							:      List[str]					):


         """simple docstring"""



         __A       =						ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size					)

         __A       =						None
         if self.use_input_mask:
             __A       =						random_attention_mask([self.batch_size, self.seq_length]					)

         if input_mask is not None:
             __A       =						input_mask.numpy()
             __A     ,						__A       =						input_mask.shape
             __A       =						np.random.randint(1			,						seq_length - 1			,						size=(batch_size,)					)
             for batch_idx, start_index in enumerate(UpperCamelCase_					):
                 __A       =						1
                 __A       =						0

         __A       =						self.get_config()

         return config, input_ids, tf.convert_to_tensor(UpperCamelCase_					)




     def 		lowerCAmelCase_  (					self							:      Union[str, Any]					):


         """simple docstring"""



         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 		lowerCAmelCase_  (					self							:      Tuple			,						UpperCamelCase_							:      Tuple			,						UpperCamelCase_							:      List[Any]			,						UpperCamelCase_							:      List[Any]					):


         """simple docstring"""



         __A       =						TFBlipTextModel(config=UpperCamelCase_					)
         __A       =						model(UpperCamelCase_			,						attention_mask=UpperCamelCase_			,						training=UpperCamelCase_					)
         __A       =						model(UpperCamelCase_			,						training=UpperCamelCase_					)
         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 		lowerCAmelCase_  (					self							:      Union[str, Any]					):


         """simple docstring"""



         __A       =						self.prepare_config_and_inputs()
         __A     ,						__A     ,						__A       =						config_and_inputs
         __A       =						{"""input_ids""": input_ids, """attention_mask""": input_mask}
         return config, inputs_dict


@require_tf
class    __lowercase   (						lowercase_      ,   unittest.TestCase	):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE     =					(TFBlipTextModel,) if is_tf_available() else ()
     SCREAMING_SNAKE_CASE     =					False
     SCREAMING_SNAKE_CASE     =					False
     SCREAMING_SNAKE_CASE     =					False




     def 		lowerCAmelCase_  (					self							:      str					):


         """simple docstring"""



         __A       =						BlipTextModelTester(self					)
         __A       =						ConfigTester(self			,						config_class=UpperCamelCase_			,						hidden_size=37					)




     def 		lowerCAmelCase_  (					self							:      str					):


         """simple docstring"""



         self.config_tester.run_common_tests()




     def 		lowerCAmelCase_  (					self							:      List[Any]					):


         """simple docstring"""



         __A       =						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*UpperCamelCase_					)




     def 		lowerCAmelCase_  (					self							:      Union[str, Any]					):


         """simple docstring"""



         pass




     def 		lowerCAmelCase_  (					self							:      Optional[int]					):


         """simple docstring"""



         pass




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


         """simple docstring"""



         pass




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


         """simple docstring"""



         pass




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


         """simple docstring"""



         pass




     @slow
     def 		lowerCAmelCase_  (					self							:      Optional[Any]					):


         """simple docstring"""



         for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __A       =						TFBlipTextModel.from_pretrained(UpperCamelCase_					)
             self.assertIsNotNone(UpperCamelCase_					)





     def 		lowerCAmelCase_  (					self							:      List[str]			,						UpperCamelCase_							:      int=True					):


         """simple docstring"""



         super().test_pt_tf_model_equivalence(allow_missing_keys=UpperCamelCase_					)












# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import re

from ..utils import cached_file


# docstyle-ignore
__a   :							Tuple						= "\nHuman: <<task>>\n\nAssistant: "


__a   :							Union[str, Any]						= "huggingface-tools/default-prompts"
__a   :							Optional[int]						= {"chat": "chat_prompt_template.txt", "run": "run_prompt_template.txt"}



def       _SCREAMING_SNAKE_CASE    (			__lowercase :      Tuple      ,					__lowercase :      Optional[Any]      ,					__lowercase :      List[Any]="run"     )    ->   Optional[int]:





    """simple docstring"""

    if prompt_or_repo_id is None:
        __A       =						DEFAULT_PROMPTS_REPO

    # prompt is considered a repo ID when it does not contain any kind of space
    if re.search("""\\s"""      ,					__lowercase     ) is not None:
        return prompt_or_repo_id

    __A       =						cached_file(
        __lowercase      ,					PROMPT_FILES[mode]      ,					repo_type="""dataset"""      ,					user_agent={"""agent""": agent_name}     )
    with open(__lowercase      ,					"""r"""      ,					encoding="""utf-8"""     ) as f:
        return f.read()




