




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

A									=logging.get_logger(__name__)


@add_end_docstrings(__a				)
class     _a						(			__a				):
       def __init__(			self		:			List[Any]     ,       **lowercase		:			Optional[Any]       ):



           '''simple docstring'''

           super().__init__(**lowercase       )

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

           requires_backends(self     ,       '''vision'''       )
           self.check_model_type(lowercase       )
       def __call__(			self		:			Any     ,       lowercase		:			Union[str, "Image.Image", List[Dict[str, Any]]]     ,       lowercase		:			Union[str, List[str]] = None     ,       **lowercase		:			Any     ,       ):



           '''simple docstring'''

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

           if isinstance(lowercase     ,       (str, Image.Image)       ):
               UpperCAmelCase     =	{'''image''': image, '''candidate_labels''': candidate_labels}
           else:
               UpperCAmelCase     =	image
           UpperCAmelCase     =	super().__call__(lowercase     ,       **lowercase       )
           return results
       def 					A   (			self		:			Union[str, Any]     ,       **lowercase		:			Union[str, Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	{}
           if "threshold" in kwargs:
               UpperCAmelCase     =	kwargs['''threshold''']
           if "top_k" in kwargs:
               UpperCAmelCase     =	kwargs['''top_k''']
           return {}, {}, postprocess_params
       def 					A   (			self		:			Tuple     ,       lowercase		:			Any       ):



           '''simple docstring'''

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

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



           '''simple docstring'''

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

           UpperCAmelCase     =	self.model(**lowercase       )

           UpperCAmelCase     =	{'''target_size''': target_size, '''candidate_label''': candidate_label, '''is_last''': is_last, **outputs}
           return model_outputs
       def 					A   (			self		:			List[str]     ,       lowercase		:			List[str]     ,       lowercase		:			List[str]=0.1     ,       lowercase		:			int=None       ):



           '''simple docstring'''

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

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

                   UpperCAmelCase     =	{'''score''': score, '''label''': label, '''box''': box}
                   results.append(lowercase       )

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

           return results







       def 					A   (			self		:			Optional[int]     ,       lowercase		:			"torch.Tensor"       ):



           '''simple docstring'''

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










'''simple docstring'''







import json
import os
import unittest

from transformers import MgpstrTokenizer
from transformers.models.mgp_str.tokenization_mgp_str import VOCAB_FILES_NAMES
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class     _a						(			__a							,							unittest.TestCase				):
       __a       :  Any            =      MgpstrTokenizer
       __a       :  Optional[Any]            =      False
       __a       :  str            =      {}
       __a       :  Optional[int]            =      False
       def 					A   (			self		:			Dict       ):



           '''simple docstring'''

           super().setUp()

           # fmt: off
           UpperCAmelCase     =	['''[GO]''', '''[s]''', '''0''', '''1''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''a''', '''b''', '''c''', '''d''', '''e''', '''f''', '''g''', '''h''', '''i''', '''j''', '''k''', '''l''', '''m''', '''n''', '''o''', '''p''', '''q''', '''r''', '''s''', '''t''', '''u''', '''v''', '''w''', '''x''', '''y''', '''z''']
           # fmt: on
           UpperCAmelCase     =	dict(zip(lowercase     ,       range(len(lowercase       )       )       )       )

           UpperCAmelCase     =	os.path.join(self.tmpdirname     ,       VOCAB_FILES_NAMES['''vocab_file''']       )
           with open(self.vocab_file     ,       '''w'''     ,       encoding='''utf-8'''       ) as fp:
               fp.write(json.dumps(lowercase       ) + '''\n'''       )
       def 					A   (			self		:			int     ,       **lowercase		:			Optional[Any]       ):



           '''simple docstring'''

           return MgpstrTokenizer.from_pretrained(self.tmpdirname     ,       **lowercase       )
       def 					A   (			self		:			int     ,       lowercase		:			str       ):



           '''simple docstring'''

           UpperCAmelCase     =	'''tester'''
           UpperCAmelCase     =	'''tester'''
           return input_text, output_text
       @unittest.skip('''MGP-STR always lower cases letters.'''       )
       def 					A   (			self		:			Optional[int]       ):



           '''simple docstring'''

           pass
       def 					A   (			self		:			int       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.get_tokenizers(do_lower_case=lowercase       )
           for tokenizer in tokenizers:
               with self.subTest(f"{tokenizer.__class__.__name__}"       ):
                   UpperCAmelCase     =	'''[SPECIAL_TOKEN]'''

                   tokenizer.add_special_tokens({'''cls_token''': special_token}       )
                   UpperCAmelCase     =	tokenizer.encode([special_token]     ,       add_special_tokens=lowercase       )
                   self.assertEqual(len(lowercase       )     ,       1       )

                   UpperCAmelCase     =	tokenizer.decode(lowercase     ,       skip_special_tokens=lowercase       )
                   self.assertTrue(special_token not in decoded       )
       def 					A   (			self		:			List[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.get_tokenizers()
           for tokenizer in tokenizers:
               with self.subTest(f"{tokenizer.__class__.__name__}"       ):
                   UpperCAmelCase ,  UpperCAmelCase     =	self.get_input_output_texts(lowercase       )

                   UpperCAmelCase     =	tokenizer.tokenize(lowercase       )
                   UpperCAmelCase     =	tokenizer.convert_tokens_to_ids(lowercase       )
                   UpperCAmelCase     =	tokenizer.encode(lowercase     ,       add_special_tokens=lowercase       )
                   self.assertListEqual(lowercase     ,       lowercase       )

                   UpperCAmelCase     =	tokenizer.convert_ids_to_tokens(lowercase       )
                   self.assertNotEqual(len(lowercase       )     ,       0       )
                   UpperCAmelCase     =	tokenizer.decode(lowercase       )
                   self.assertIsInstance(lowercase     ,       lowercase       )

                   self.assertEqual(text_a.replace(''' '''     ,       ''''''       )     ,       lowercase       )
       @unittest.skip('''MGP-STR tokenizer only handles one sequence.'''       )
       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           pass







       @unittest.skip('''inputs cannot be pretokenized in MgpstrTokenizer'''       )
       def 					A   (			self		:			str       ):



           '''simple docstring'''

           pass




