



"""simple docstring"""






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




		def __init__(			self					:						Optional[int]						, __lowerCAmelCase					:						str						, __lowerCAmelCase					:						Union[str, Any]=1_2						, __lowerCAmelCase					:						Optional[int]=7						, __lowerCAmelCase					:						Any=True						, __lowerCAmelCase					:						List[str]=True						, __lowerCAmelCase					:						Optional[Any]=True						, __lowerCAmelCase					:						str=9_9						, __lowerCAmelCase					:						List[Any]=3_2						, __lowerCAmelCase					:						List[Any]=3_2						, __lowerCAmelCase					:						Union[str, Any]=2						, __lowerCAmelCase					:						Any=4						, __lowerCAmelCase					:						List[str]=3_7						, __lowerCAmelCase					:						List[Any]=0.1						, __lowerCAmelCase					:						List[Any]=0.1						, __lowerCAmelCase					:						int=5_1_2						, __lowerCAmelCase					:						str=0.02						, __lowerCAmelCase					:						List[Any]=0						, __lowerCAmelCase					:						int=None						, ):






									"""simple docstring"""






									_lowerCamelCase						:    Tuple         =			parent
									_lowerCamelCase						:    Optional[Any]         =			batch_size
									_lowerCamelCase						:    Union[str, Any]         =			seq_length
									_lowerCamelCase						:    Optional[int]         =			is_training
									_lowerCamelCase						:    int         =			use_input_mask
									_lowerCamelCase						:    List[str]         =			use_labels
									_lowerCamelCase						:    Any         =			vocab_size
									_lowerCamelCase						:    Dict         =			hidden_size
									_lowerCamelCase						:    List[str]         =			projection_dim
									_lowerCamelCase						:    str         =			num_hidden_layers
									_lowerCamelCase						:    List[str]         =			num_attention_heads
									_lowerCamelCase						:    Union[str, Any]         =			intermediate_size
									_lowerCamelCase						:    Union[str, Any]         =			dropout
									_lowerCamelCase						:    Optional[int]         =			attention_dropout
									_lowerCamelCase						:    Optional[Any]         =			max_position_embeddings
									_lowerCamelCase						:    List[Any]         =			initializer_range
									_lowerCamelCase						:    Any         =			scope
									_lowerCamelCase						:    Optional[Any]         =			bos_token_id




		def 						SCREAMING_SNAKE_CASE    (			self					:						Dict		):






									"""simple docstring"""






									_lowerCamelCase						:    List[str]         =			ids_tensor([self.batch_size, self.seq_length]						, self.vocab_size		)

									_lowerCamelCase						:    Optional[int]         =			None
									if self.use_input_mask:
																_lowerCamelCase						:    List[str]         =			random_attention_mask([self.batch_size, self.seq_length]		)

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

									_lowerCamelCase						:    Optional[int]         =			self.get_config()

									return config, input_ids, tf.convert_to_tensor(_UpperCAmelCase		)




		def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]		):






									"""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 						SCREAMING_SNAKE_CASE    (			self					:						Tuple						, __lowerCAmelCase					:						int						, __lowerCAmelCase					:						str						, __lowerCAmelCase					:						Union[str, Any]		):






									"""simple docstring"""






									_lowerCamelCase						:    Dict         =			TFBlipTextModel(config=_UpperCAmelCase		)
									_lowerCamelCase						:    Tuple         =			model(_UpperCAmelCase						, attention_mask=_UpperCAmelCase						, training=_UpperCAmelCase		)
									_lowerCamelCase						:    Tuple         =			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 						SCREAMING_SNAKE_CASE    (			self					:						int		):






									"""simple docstring"""






									_lowerCamelCase						:    Tuple         =			self.prepare_config_and_inputs()
									_lowerCamelCase						:    List[str]         =			config_and_inputs
									_lowerCamelCase						:    str         =			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
									return config, inputs_dict






@require_tf
class 	__snake_case	(     _UpperCAmelCase  ,					unittest.TestCase):
		snake_case__    :       Tuple        =						(TFBlipTextModel,) if is_tf_available() else ()
		snake_case__    :       Optional[Any]        =						False
		snake_case__    :       str        =						False
		snake_case__    :       List[str]        =						False




		def 						SCREAMING_SNAKE_CASE    (			self					:						str		):






									"""simple docstring"""






									_lowerCamelCase						:    int         =			BlipTextModelTester(self		)
									_lowerCamelCase						:    Optional[int]         =			ConfigTester(self						, config_class=_UpperCAmelCase						, hidden_size=3_7		)




		def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






									"""simple docstring"""






									self.config_tester.run_common_tests()




		def 						SCREAMING_SNAKE_CASE    (			self					:						int		):






									"""simple docstring"""






									_lowerCamelCase						:    Optional[int]         =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCAmelCase		)




		def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[int]		):






									"""simple docstring"""






									pass




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






									"""simple docstring"""






									pass




		@unittest.skip(reason='''Blip does not use inputs_embeds'''		)
		def 						SCREAMING_SNAKE_CASE    (			self					:						Tuple		):






									"""simple docstring"""






									pass




		@unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''		)
		def 						SCREAMING_SNAKE_CASE    (			self					:						str		):






									"""simple docstring"""






									pass




		@unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''		)
		def 						SCREAMING_SNAKE_CASE    (			self					:						Dict		):






									"""simple docstring"""






									pass




		@slow
		def 						SCREAMING_SNAKE_CASE    (			self					:						Any		):






									"""simple docstring"""






									for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_lowerCamelCase						:    Dict         =			TFBlipTextModel.from_pretrained(_UpperCAmelCase		)
																self.assertIsNotNone(_UpperCAmelCase		)




		def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[Any]						, __lowerCAmelCase					:						List[str]=True		):






									"""simple docstring"""






									super().test_pt_tf_model_equivalence(allow_missing_keys=_UpperCAmelCase		)







"""simple docstring"""






from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, ChunkPipeline


if is_vision_available():
   from PIL import Image

   from ..image_utils import load_image

if is_torch_available():
   import torch

   from transformers.modeling_outputs import BaseModelOutput

   from ..models.auto.modeling_auto import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

lowerCAmelCase__      =    logging.get_logger(__name__)






@add_end_docstrings(_lowercase)
class 	__snake_case	(     _lowercase):




  def __init__(			self					:						Any						, **__lowerCAmelCase					:						Union[str, Any]		):






         """simple docstring"""






         super().__init__(**__lowerCAmelCase		)

         if self.framework == "tf":
                raise ValueError(f'''The {self.__class__} is only available in PyTorch.'''		)

         requires_backends(self						, '''vision'''		)
         self.check_model_type(__lowerCAmelCase		)




  def __call__(			self					:						Dict						, __lowerCAmelCase					:						Union[str, "Image.Image", List[Dict[str, Any]]]						, __lowerCAmelCase					:						Union[str, List[str]] = None						, **__lowerCAmelCase					:						int						, ):






         """simple docstring"""






         if "text_queries" in kwargs:
                _lowerCamelCase						:    List[Any]         =			kwargs.pop('''text_queries'''		)

         if isinstance(__lowerCAmelCase						, (str, Image.Image)		):
                _lowerCamelCase						:    Optional[int]         =			{'''image''': image, '''candidate_labels''': candidate_labels}
         else:
                _lowerCamelCase						:    List[Any]         =			image
         _lowerCamelCase						:    List[str]         =			super().__call__(__lowerCAmelCase						, **__lowerCAmelCase		)
         return results




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]						, **__lowerCAmelCase					:						int		):






         """simple docstring"""






         _lowerCamelCase						:    int         =			{}
         if "threshold" in kwargs:
                _lowerCamelCase						:    Optional[Any]         =			kwargs['''threshold''']
         if "top_k" in kwargs:
                _lowerCamelCase						:    int         =			kwargs['''top_k''']
         return {}, {}, postprocess_params




  def 						SCREAMING_SNAKE_CASE    (			self					:						List[Any]						, __lowerCAmelCase					:						Union[str, Any]		):






         """simple docstring"""






         _lowerCamelCase						:    int         =			load_image(inputs['''image''']		)
         _lowerCamelCase						:    Optional[Any]         =			inputs['''candidate_labels''']
         if isinstance(__lowerCAmelCase						, __lowerCAmelCase		):
                _lowerCamelCase						:    int         =			candidate_labels.split(''','''		)

         _lowerCamelCase						:    Tuple         =			torch.tensor([[image.height, image.width]]						, dtype=torch.intaa		)
         for i, candidate_label in enumerate(__lowerCAmelCase		):
                _lowerCamelCase						:    Any         =			self.tokenizer(__lowerCAmelCase						, return_tensors=self.framework		)
                _lowerCamelCase						:    Optional[Any]         =			self.image_processor(__lowerCAmelCase						, return_tensors=self.framework		)
                yield {
                    "is_last": i == len(__lowerCAmelCase		) - 1,
                    "target_size": target_size,
                    "candidate_label": candidate_label,
                    **text_inputs,
                    **image_features,
                }




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any						, __lowerCAmelCase					:						List[Any]		):






         """simple docstring"""






         _lowerCamelCase						:    Optional[Any]         =			model_inputs.pop('''target_size'''		)
         _lowerCamelCase						:    List[Any]         =			model_inputs.pop('''candidate_label'''		)
         _lowerCamelCase						:    Dict         =			model_inputs.pop('''is_last'''		)

         _lowerCamelCase						:    str         =			self.model(**__lowerCAmelCase		)

         _lowerCamelCase						:    Optional[Any]         =			{'''target_size''': target_size, '''candidate_label''': candidate_label, '''is_last''': is_last, **outputs}
         return model_outputs




  def 						SCREAMING_SNAKE_CASE    (			self					:						Optional[Any]						, __lowerCAmelCase					:						Any						, __lowerCAmelCase					:						Union[str, Any]=0.1						, __lowerCAmelCase					:						Optional[Any]=None		):






         """simple docstring"""






         _lowerCamelCase						:    str         =			[]
         for model_output in model_outputs:
                _lowerCamelCase						:    Any         =			model_output['''candidate_label''']
                _lowerCamelCase						:    Union[str, Any]         =			BaseModelOutput(__lowerCAmelCase		)
                _lowerCamelCase						:    Tuple         =			self.image_processor.post_process_object_detection(
                    outputs=__lowerCAmelCase						, threshold=__lowerCAmelCase						, target_sizes=model_output['''target_size''']		)[0]

                for index in outputs["scores"].nonzero():
                       _lowerCamelCase						:    Tuple         =			outputs['''scores'''][index].item()
                       _lowerCamelCase						:    Optional[Any]         =			self._get_bounding_box(outputs['''boxes'''][index][0]		)

                       _lowerCamelCase						:    Optional[Any]         =			{'''score''': score, '''label''': label, '''box''': box}
                       results.append(__lowerCAmelCase		)

         _lowerCamelCase						:    int         =			sorted(__lowerCAmelCase						, key=lambda __lowerCAmelCase		: x["score"]						, reverse=__lowerCAmelCase		)
         if top_k:
                _lowerCamelCase						:    Dict         =			results[:top_k]

         return results




  def 						SCREAMING_SNAKE_CASE    (			self					:						Any						, __lowerCAmelCase					:						"torch.Tensor"		):






         """simple docstring"""






         if self.framework != "pt":
                raise ValueError('''The ZeroShotObjectDetectionPipeline is only available in PyTorch.'''		)
         _lowerCamelCase      ,   _lowerCamelCase      ,   _lowerCamelCase      ,   _lowerCamelCase						:    Dict         =			box.int().tolist()
         _lowerCamelCase						:    Union[str, Any]         =			{
             '''xmin''': xmin,
             '''ymin''': ymin,
             '''xmax''': xmax,
             '''ymax''': ymax,
         }
         return bbox


