



import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
 from PIL import Image

 from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast

@require_vision
class    _A	(     unittest.TestCase   ):



       def 		_lowerCamelCase      (		self							:					int):


           '''simple docstring'''

           __a							=    tempfile.mkdtemp()

           __a							=    BlipImageProcessor()
           __a							=    BertTokenizer.from_pretrained('''hf-internal-testing/tiny-random-BertModel''')

           __a							=    BlipProcessor(snake_case__					,		snake_case__)

           processor.save_pretrained(self.tmpdirname)



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


           '''simple docstring'''

           return AutoProcessor.from_pretrained(self.tmpdirname					,		**snake_case__).tokenizer



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


           '''simple docstring'''

           return AutoProcessor.from_pretrained(self.tmpdirname					,		**snake_case__).image_processor



       def 		_lowerCamelCase      (		self							:					int):


           '''simple docstring'''

           shutil.rmtree(self.tmpdirname)



       def 		_lowerCamelCase      (		self							:					List[Any]):


           '''simple docstring'''

           __a							=    [np.random.randint(255					,		size=(3, 30, 400)					,		dtype=np.uinta)]

           __a							=    [Image.fromarray(np.moveaxis(snake_case__					,		0					,		-1)) for x in image_inputs]

           return image_inputs



       def 		_lowerCamelCase      (		self							:					Any):


           '''simple docstring'''

           __a							=    BlipProcessor(tokenizer=self.get_tokenizer()					,		image_processor=self.get_image_processor())
           processor.save_pretrained(self.tmpdirname)

           __a							=    self.get_tokenizer(bos_token='''(BOS)'''					,		eos_token='''(EOS)''')
           __a							=    self.get_image_processor(do_normalize=snake_case__					,		padding_value=1.0)

           __a							=    BlipProcessor.from_pretrained(
               self.tmpdirname					,		bos_token='''(BOS)'''					,		eos_token='''(EOS)'''					,		do_normalize=snake_case__					,		padding_value=1.0)

           self.assertEqual(processor.tokenizer.get_vocab()					,		tokenizer_add_kwargs.get_vocab())
           self.assertIsInstance(processor.tokenizer					,		snake_case__)

           self.assertEqual(processor.image_processor.to_json_string()					,		image_processor_add_kwargs.to_json_string())
           self.assertIsInstance(processor.image_processor					,		snake_case__)



       def 		_lowerCamelCase      (		self							:					List[str]):


           '''simple docstring'''

           __a							=    self.get_image_processor()
           __a							=    self.get_tokenizer()

           __a							=    BlipProcessor(tokenizer=snake_case__					,		image_processor=snake_case__)

           __a							=    self.prepare_image_inputs()

           __a							=    image_processor(snake_case__					,		return_tensors='''np''')
           __a							=    processor(images=snake_case__					,		return_tensors='''np''')

           for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum()					,		input_processor[key].sum()					,		delta=1E-2)



       def 		_lowerCamelCase      (		self							:					Tuple):


           '''simple docstring'''

           __a							=    self.get_image_processor()
           __a							=    self.get_tokenizer()

           __a							=    BlipProcessor(tokenizer=snake_case__					,		image_processor=snake_case__)

           __a							=    'lower newer'

           __a							=    processor(text=snake_case__)

           __a							=    tokenizer(snake_case__					,		return_token_type_ids=snake_case__)

           for key in encoded_tok.keys():
               self.assertListEqual(encoded_tok[key]					,		encoded_processor[key])



       def 		_lowerCamelCase      (		self							:					Tuple):


           '''simple docstring'''

           __a							=    self.get_image_processor()
           __a							=    self.get_tokenizer()

           __a							=    BlipProcessor(tokenizer=snake_case__					,		image_processor=snake_case__)

           __a							=    'lower newer'
           __a							=    self.prepare_image_inputs()

           __a							=    processor(text=snake_case__					,		images=snake_case__)

           self.assertListEqual(list(inputs.keys())					,		['''pixel_values''', '''input_ids''', '''attention_mask'''])

           # test if it raises when no input is passed
           with pytest.raises(snake_case__):
               processor()



       def 		_lowerCamelCase      (		self							:					Tuple):


           '''simple docstring'''

           __a							=    self.get_image_processor()
           __a							=    self.get_tokenizer()

           __a							=    BlipProcessor(tokenizer=snake_case__					,		image_processor=snake_case__)

           __a							=    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

           __a							=    processor.batch_decode(snake_case__)
           __a							=    tokenizer.batch_decode(snake_case__)

           self.assertListEqual(snake_case__					,		snake_case__)





       def 		_lowerCamelCase      (		self							:					Tuple):


           '''simple docstring'''

           __a							=    self.get_image_processor()
           __a							=    self.get_tokenizer()

           __a							=    BlipProcessor(tokenizer=snake_case__					,		image_processor=snake_case__)

           __a							=    'lower newer'
           __a							=    self.prepare_image_inputs()

           __a							=    processor(text=snake_case__					,		images=snake_case__)

           # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask']
           self.assertListEqual(list(inputs.keys())					,		['''pixel_values''', '''input_ids''', '''attention_mask'''])








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





