



from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
 import tensorflow as tf

 from transformers import (
     TFConvBertForMaskedLM,
     TFConvBertForMultipleChoice,
     TFConvBertForQuestionAnswering,
     TFConvBertForSequenceClassification,
     TFConvBertForTokenClassification,
     TFConvBertModel,
 )

class    _A	:



       def __init__(		self							:					int					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					List[str]=13					,		__SCREAMING_SNAKE_CASE							:					List[str]=7					,		__SCREAMING_SNAKE_CASE							:					List[str]=True					,		__SCREAMING_SNAKE_CASE							:					Dict=True					,		__SCREAMING_SNAKE_CASE							:					str=True					,		__SCREAMING_SNAKE_CASE							:					Dict=True					,		__SCREAMING_SNAKE_CASE							:					Any=99					,		__SCREAMING_SNAKE_CASE							:					Optional[int]=32					,		__SCREAMING_SNAKE_CASE							:					int=2					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]=4					,		__SCREAMING_SNAKE_CASE							:					Any=37					,		__SCREAMING_SNAKE_CASE							:					str="gelu"					,		__SCREAMING_SNAKE_CASE							:					List[Any]=0.1					,		__SCREAMING_SNAKE_CASE							:					Tuple=0.1					,		__SCREAMING_SNAKE_CASE							:					str=512					,		__SCREAMING_SNAKE_CASE							:					Tuple=16					,		__SCREAMING_SNAKE_CASE							:					Optional[int]=2					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=0.02					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=3					,		__SCREAMING_SNAKE_CASE							:					Optional[int]=4					,		__SCREAMING_SNAKE_CASE							:					str=None					,		):


           '''simple docstring'''

           __a							=    parent
           __a							=    13
           __a							=    7
           __a							=    True
           __a							=    True
           __a							=    True
           __a							=    True
           __a							=    99
           __a							=    384
           __a							=    2
           __a							=    4
           __a							=    37
           __a							=    '''gelu'''
           __a							=    0.1
           __a							=    0.1
           __a							=    512
           __a							=    16
           __a							=    2
           __a							=    0.02
           __a							=    3
           __a							=    4
           __a							=    128
           __a							=    2
           __a							=    9
           __a							=    1
           __a							=    None



       def 		_lowerCamelCase      (		self							:					Any):


           '''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])

           __a							=    None
           if self.use_token_type_ids:
               __a							=    ids_tensor([self.batch_size, self.seq_length]					,		self.type_vocab_size)

           __a							=    None
           __a							=    None
           __a							=    None
           if self.use_labels:
               __a							=    ids_tensor([self.batch_size]					,		self.type_sequence_label_size)
               __a							=    ids_tensor([self.batch_size, self.seq_length]					,		self.num_labels)
               __a							=    ids_tensor([self.batch_size]					,		self.num_choices)

           __a							=    ConvBertConfig(
               vocab_size=self.vocab_size					,		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					,		max_position_embeddings=self.max_position_embeddings					,		type_vocab_size=self.type_vocab_size					,		initializer_range=self.initializer_range					,		return_dict=__SCREAMING_SNAKE_CASE					,		)

           return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



       def 		_lowerCamelCase      (		self							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					Tuple					,		__SCREAMING_SNAKE_CASE							:					List[Any]					,		__SCREAMING_SNAKE_CASE							:					Dict					,		__SCREAMING_SNAKE_CASE							:					List[str]					,		__SCREAMING_SNAKE_CASE							:					str					,		__SCREAMING_SNAKE_CASE							:					List[str]					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]):


           '''simple docstring'''

           __a							=    TFConvBertModel(config=__SCREAMING_SNAKE_CASE)
           __a							=    {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

           __a							=    [input_ids, input_mask]
           __a							=    model(__SCREAMING_SNAKE_CASE)

           __a							=    model(__SCREAMING_SNAKE_CASE)

           self.parent.assertEqual(result.last_hidden_state.shape					,		(self.batch_size, self.seq_length, self.hidden_size))



       def 		_lowerCamelCase      (		self							:					Any					,		__SCREAMING_SNAKE_CASE							:					List[Any]					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]					,		__SCREAMING_SNAKE_CASE							:					str					,		__SCREAMING_SNAKE_CASE							:					int					,		__SCREAMING_SNAKE_CASE							:					Any					,		__SCREAMING_SNAKE_CASE							:					List[str]					,		__SCREAMING_SNAKE_CASE							:					Optional[int]):


           '''simple docstring'''

           __a							=    TFConvBertForMaskedLM(config=__SCREAMING_SNAKE_CASE)
           __a							=    {
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }
           __a							=    model(__SCREAMING_SNAKE_CASE)
           self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.seq_length, self.vocab_size))



       def 		_lowerCamelCase      (		self							:					str					,		__SCREAMING_SNAKE_CASE							:					Dict					,		__SCREAMING_SNAKE_CASE							:					Tuple					,		__SCREAMING_SNAKE_CASE							:					Optional[int]					,		__SCREAMING_SNAKE_CASE							:					Tuple					,		__SCREAMING_SNAKE_CASE							:					str					,		__SCREAMING_SNAKE_CASE							:					int					,		__SCREAMING_SNAKE_CASE							:					List[str]):


           '''simple docstring'''

           __a							=    self.num_labels
           __a							=    TFConvBertForSequenceClassification(config=__SCREAMING_SNAKE_CASE)
           __a							=    {
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }

           __a							=    model(__SCREAMING_SNAKE_CASE)
           self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.num_labels))



       def 		_lowerCamelCase      (		self							:					Optional[int]					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					str					,		__SCREAMING_SNAKE_CASE							:					Any					,		__SCREAMING_SNAKE_CASE							:					List[Any]					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					int					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]):


           '''simple docstring'''

           __a							=    self.num_choices
           __a							=    TFConvBertForMultipleChoice(config=__SCREAMING_SNAKE_CASE)
           __a							=    tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE					,		1)					,		(1, self.num_choices, 1))
           __a							=    tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE					,		1)					,		(1, self.num_choices, 1))
           __a							=    tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE					,		1)					,		(1, self.num_choices, 1))
           __a							=    {
               '''input_ids''': multiple_choice_inputs_ids,
               '''attention_mask''': multiple_choice_input_mask,
               '''token_type_ids''': multiple_choice_token_type_ids,
           }
           __a							=    model(__SCREAMING_SNAKE_CASE)
           self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.num_choices))



       def 		_lowerCamelCase      (		self							:					Union[str, Any]					,		__SCREAMING_SNAKE_CASE							:					int					,		__SCREAMING_SNAKE_CASE							:					Any					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					str					,		__SCREAMING_SNAKE_CASE							:					str					,		__SCREAMING_SNAKE_CASE							:					Dict					,		__SCREAMING_SNAKE_CASE							:					str):


           '''simple docstring'''

           __a							=    self.num_labels
           __a							=    TFConvBertForTokenClassification(config=__SCREAMING_SNAKE_CASE)
           __a							=    {
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }
           __a							=    model(__SCREAMING_SNAKE_CASE)
           self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.seq_length, self.num_labels))



       def 		_lowerCamelCase      (		self							:					str					,		__SCREAMING_SNAKE_CASE							:					Optional[int]					,		__SCREAMING_SNAKE_CASE							:					str					,		__SCREAMING_SNAKE_CASE							:					Optional[int]					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					Optional[int]					,		__SCREAMING_SNAKE_CASE							:					Any					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]):


           '''simple docstring'''

           __a							=    TFConvBertForQuestionAnswering(config=__SCREAMING_SNAKE_CASE)
           __a							=    {
               '''input_ids''': input_ids,
               '''attention_mask''': input_mask,
               '''token_type_ids''': token_type_ids,
           }

           __a							=    model(__SCREAMING_SNAKE_CASE)
           self.parent.assertEqual(result.start_logits.shape					,		(self.batch_size, self.seq_length))
           self.parent.assertEqual(result.end_logits.shape					,		(self.batch_size, self.seq_length))





       def 		_lowerCamelCase      (		self							:					Tuple):


           '''simple docstring'''

           __a							=    self.prepare_config_and_inputs()
           (
               (
               __a
           )	,       (
               __a
           )	,       (
               __a
           )	,       (
               __a
           )	,       (
               __a
           )	,       (
               __a
           )	,       (
               __a
           )	,       
           )							=    config_and_inputs
           __a							=    {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
           return config, inputs_dict


@require_tf
class    _A	(     __UpperCAmelCase				,__UpperCAmelCase				,unittest.TestCase   ):
       UpperCamelCase__  :   str						 =						(
           (
               TFConvBertModel,
               TFConvBertForMaskedLM,
               TFConvBertForQuestionAnswering,
               TFConvBertForSequenceClassification,
               TFConvBertForTokenClassification,
               TFConvBertForMultipleChoice,
           )
           if is_tf_available()
           else ()
       )
       UpperCamelCase__  :   Dict						 =						(
           {
               '''feature-extraction''': TFConvBertModel,
               '''fill-mask''': TFConvBertForMaskedLM,
               '''question-answering''': TFConvBertForQuestionAnswering,
               '''text-classification''': TFConvBertForSequenceClassification,
               '''token-classification''': TFConvBertForTokenClassification,
               '''zero-shot''': TFConvBertForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       UpperCamelCase__  :   Optional[Any]						 =						False
       UpperCamelCase__  :   List[str]						 =						False
       UpperCamelCase__  :   Optional[Any]						 =						False



       def 		_lowerCamelCase      (		self							:					int):


           '''simple docstring'''

           __a							=    TFConvBertModelTester(self)
           __a							=    ConfigTester(self					,		config_class=__SCREAMING_SNAKE_CASE					,		hidden_size=37)



       def 		_lowerCamelCase      (		self							:					Optional[Any]):


           '''simple docstring'''

           self.config_tester.run_common_tests()



       def 		_lowerCamelCase      (		self							:					List[str]):


           '''simple docstring'''

           __a							=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase      (		self							:					List[Any]):


           '''simple docstring'''

           __a							=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase      (		self							:					int):


           '''simple docstring'''

           __a							=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase      (		self							:					List[Any]):


           '''simple docstring'''

           __a							=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase      (		self							:					List[Any]):


           '''simple docstring'''

           __a							=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE)



       def 		_lowerCamelCase      (		self							:					List[Any]):


           '''simple docstring'''

           __a							=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE)



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


           '''simple docstring'''

           __a	,       __a							=    self.model_tester.prepare_config_and_inputs_for_common()
           __a							=    True
           __a							=    True

           if hasattr(__SCREAMING_SNAKE_CASE					,		'''use_cache'''):
               __a							=    True

           __a							=    getattr(self.model_tester					,		'''encoder_seq_length'''					,		self.model_tester.seq_length)
           __a							=    getattr(self.model_tester					,		'''key_length'''					,		__SCREAMING_SNAKE_CASE)

           for model_class in self.all_model_classes:
               __a							=    self._prepare_for_class(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE)
               __a							=    model_class(__SCREAMING_SNAKE_CASE)
               __a							=    len(model(__SCREAMING_SNAKE_CASE))

               with tempfile.TemporaryDirectory() as tmpdirname:
                   model.save_pretrained(__SCREAMING_SNAKE_CASE					,		saved_model=__SCREAMING_SNAKE_CASE)
                   __a							=    os.path.join(__SCREAMING_SNAKE_CASE					,		'''saved_model'''					,		'''1''')
                   __a							=    tf.keras.models.load_model(__SCREAMING_SNAKE_CASE)
                   __a							=    model(__SCREAMING_SNAKE_CASE)

                   if self.is_encoder_decoder:
                       __a							=    outputs['''encoder_hidden_states''']
                       __a							=    outputs['''encoder_attentions''']
                   else:
                       __a							=    outputs['''hidden_states''']
                       __a							=    outputs['''attentions''']

                   self.assertEqual(len(__SCREAMING_SNAKE_CASE)					,		__SCREAMING_SNAKE_CASE)

                   __a							=    getattr(
                       self.model_tester					,		'''expected_num_hidden_layers'''					,		self.model_tester.num_hidden_layers + 1)

                   self.assertEqual(len(__SCREAMING_SNAKE_CASE)					,		__SCREAMING_SNAKE_CASE)
                   self.assertListEqual(
                       list(output_hidden_states[0].shape[-2:])					,		[self.model_tester.seq_length, self.model_tester.hidden_size]					,		)

                   self.assertEqual(len(__SCREAMING_SNAKE_CASE)					,		self.model_tester.num_hidden_layers)
                   self.assertListEqual(
                       list(output_attentions[0].shape[-3:])					,		[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]					,		)



       @slow
       def 		_lowerCamelCase      (		self							:					Any):


           '''simple docstring'''

           __a							=    TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''')
           self.assertIsNotNone(__SCREAMING_SNAKE_CASE)





       def 		_lowerCamelCase      (		self							:					Any):


           '''simple docstring'''

           __a	,       __a							=    self.model_tester.prepare_config_and_inputs_for_common()
           __a							=    True
           __a							=    getattr(self.model_tester					,		'''decoder_seq_length'''					,		self.model_tester.seq_length)
           __a							=    getattr(self.model_tester					,		'''encoder_seq_length'''					,		self.model_tester.seq_length)
           __a							=    getattr(self.model_tester					,		'''key_length'''					,		__SCREAMING_SNAKE_CASE)
           __a							=    getattr(self.model_tester					,		'''key_length'''					,		__SCREAMING_SNAKE_CASE)

           def check_decoder_attentions_output(__SCREAMING_SNAKE_CASE							:					List[Any]):
               __a							=    len(__SCREAMING_SNAKE_CASE)
               self.assertEqual(out_len % 2					,		0)
               __a							=    outputs.decoder_attentions
               self.assertEqual(len(__SCREAMING_SNAKE_CASE)					,		self.model_tester.num_hidden_layers)
               self.assertListEqual(
                   list(decoder_attentions[0].shape[-3:])					,		[self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]					,		)

           def check_encoder_attentions_output(__SCREAMING_SNAKE_CASE							:					int):
               __a							=    [
                   t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
               ]
               self.assertEqual(len(__SCREAMING_SNAKE_CASE)					,		self.model_tester.num_hidden_layers)
               self.assertListEqual(
                   list(attentions[0].shape[-3:])					,		[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]					,		)

           for model_class in self.all_model_classes:
               __a							=    True
               __a							=    False
               __a							=    model_class(__SCREAMING_SNAKE_CASE)
               __a							=    model(self._prepare_for_class(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE))
               __a							=    len(__SCREAMING_SNAKE_CASE)
               self.assertEqual(config.output_hidden_states					,		__SCREAMING_SNAKE_CASE)
               check_encoder_attentions_output(__SCREAMING_SNAKE_CASE)

               if self.is_encoder_decoder:
                   __a							=    model_class(__SCREAMING_SNAKE_CASE)
                   __a							=    model(self._prepare_for_class(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE))
                   self.assertEqual(config.output_hidden_states					,		__SCREAMING_SNAKE_CASE)
                   check_decoder_attentions_output(__SCREAMING_SNAKE_CASE)

               # Check that output attentions can also be changed via the config
               del inputs_dict["output_attentions"]
               __a							=    True
               __a							=    model_class(__SCREAMING_SNAKE_CASE)
               __a							=    model(self._prepare_for_class(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE))
               self.assertEqual(config.output_hidden_states					,		__SCREAMING_SNAKE_CASE)
               check_encoder_attentions_output(__SCREAMING_SNAKE_CASE)

               # Check attention is always last and order is fine
               __a							=    True
               __a							=    True
               __a							=    model_class(__SCREAMING_SNAKE_CASE)
               __a							=    model(self._prepare_for_class(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE))

               self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)					,		len(__SCREAMING_SNAKE_CASE))
               self.assertEqual(model.config.output_hidden_states					,		__SCREAMING_SNAKE_CASE)
               check_encoder_attentions_output(__SCREAMING_SNAKE_CASE)


@require_tf
class    _A	(     unittest.TestCase   ):



       @slow
       def 		_lowerCamelCase      (		self							:					List[Any]):


           '''simple docstring'''

           __a							=    TFConvBertModel.from_pretrained('''YituTech/conv-bert-base''')
           __a							=    tf.constant([[0, 1, 2, 3, 4, 5]])
           __a							=    model(__SCREAMING_SNAKE_CASE)[0]

           __a							=    [1, 6, 768]
           self.assertEqual(output.shape					,		__SCREAMING_SNAKE_CASE)

           __a							=    tf.constant(
               [
                   [
                       [-0.03_47_54_93, -0.4_68_60_34, -0.30_63_88_32],
                       [0.22_63_72_48, -0.26_98_86_46, -0.7_42_34_24],
                       [0.10_32_48_68, -0.45_01_35_08, -0.58_28_07_84],
                   ]
               ])
           tf.debugging.assert_near(output[:, :3, :3]					,		__SCREAMING_SNAKE_CASE					,		atol=1E-4)










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

__snake_case						:Tuple						=					logging.get_logger(__name__)

@add_end_docstrings(__UpperCAmelCase   )
class    _A	(     __UpperCAmelCase   ):



       def __init__(		self							:					Optional[int]					,		**__SCREAMING_SNAKE_CASE							:					Optional[Any]):


           '''simple docstring'''

           super().__init__(**__SCREAMING_SNAKE_CASE)

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

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



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


           '''simple docstring'''

           if "text_queries" in kwargs:
               __a							=    kwargs.pop('''text_queries''')

           if isinstance(__SCREAMING_SNAKE_CASE					,		(str, Image.Image)):
               __a							=    {'''image''': image, '''candidate_labels''': candidate_labels}
           else:
               __a							=    image
           __a							=    super().__call__(__SCREAMING_SNAKE_CASE					,		**__SCREAMING_SNAKE_CASE)
           return results



       def 		_lowerCamelCase      (		self							:					str					,		**__SCREAMING_SNAKE_CASE							:					Union[str, Any]):


           '''simple docstring'''

           __a							=    {}
           if "threshold" in kwargs:
               __a							=    kwargs['''threshold''']
           if "top_k" in kwargs:
               __a							=    kwargs['''top_k''']
           return {}, {}, postprocess_params



       def 		_lowerCamelCase      (		self							:					List[str]					,		__SCREAMING_SNAKE_CASE							:					Tuple):


           '''simple docstring'''

           __a							=    load_image(inputs['''image'''])
           __a							=    inputs['''candidate_labels''']
           if isinstance(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE):
               __a							=    candidate_labels.split(''',''')

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



       def 		_lowerCamelCase      (		self							:					int					,		__SCREAMING_SNAKE_CASE							:					Tuple):


           '''simple docstring'''

           __a							=    model_inputs.pop('''target_size''')
           __a							=    model_inputs.pop('''candidate_label''')
           __a							=    model_inputs.pop('''is_last''')

           __a							=    self.model(**__SCREAMING_SNAKE_CASE)

           __a							=    {'''target_size''': target_size, '''candidate_label''': candidate_label, '''is_last''': is_last, **outputs}
           return model_outputs



       def 		_lowerCamelCase      (		self							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					Optional[int]					,		__SCREAMING_SNAKE_CASE							:					Tuple=0.1					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]=None):


           '''simple docstring'''

           __a							=    []
           for model_output in model_outputs:
               __a							=    model_output['''candidate_label''']
               __a							=    BaseModelOutput(__SCREAMING_SNAKE_CASE)
               __a							=    self.image_processor.post_process_object_detection(
                   outputs=__SCREAMING_SNAKE_CASE					,		threshold=__SCREAMING_SNAKE_CASE					,		target_sizes=model_output['''target_size'''])[0]

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

                   __a							=    {'''score''': score, '''label''': label, '''box''': box}
                   results.append(__SCREAMING_SNAKE_CASE)

           __a							=    sorted(__SCREAMING_SNAKE_CASE					,		key=lambda __SCREAMING_SNAKE_CASE: x["score"]					,		reverse=__SCREAMING_SNAKE_CASE)
           if top_k:
               __a							=    results[:top_k]

           return results





       def 		_lowerCamelCase      (		self							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					"torch.Tensor"):


           '''simple docstring'''

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





