



'''simple docstring'''




import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_base import BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_gpta import GPTaTokenizer


if TYPE_CHECKING:
						from transformers.pipelines.conversational import Conversation


UpperCamelCase__       :	List[Any]    = logging.get_logger(__name__)

UpperCamelCase__       :	Dict    = {'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

UpperCamelCase__       :	Optional[Any]    = {
    '''vocab_file''': {
        '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/vocab.json''',
        '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/vocab.json''',
        '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/vocab.json''',
        '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/vocab.json''',
        '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/vocab.json''',
    },
    '''merges_file''': {
        '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/merges.txt''',
        '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/merges.txt''',
        '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/merges.txt''',
        '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/merges.txt''',
        '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/merges.txt''',
    },
    '''tokenizer_file''': {
        '''gpt2''': '''https://huggingface.co/gpt2/resolve/main/tokenizer.json''',
        '''gpt2-medium''': '''https://huggingface.co/gpt2-medium/resolve/main/tokenizer.json''',
        '''gpt2-large''': '''https://huggingface.co/gpt2-large/resolve/main/tokenizer.json''',
        '''gpt2-xl''': '''https://huggingface.co/gpt2-xl/resolve/main/tokenizer.json''',
        '''distilgpt2''': '''https://huggingface.co/distilgpt2/resolve/main/tokenizer.json''',
    },
}

UpperCamelCase__       :	List[str]    = {
    '''gpt2''': 10_24,
    '''gpt2-medium''': 10_24,
    '''gpt2-large''': 10_24,
    '''gpt2-xl''': 10_24,
    '''distilgpt2''': 10_24,
}






class 						_UpperCamelCase  (							lowerCamelCase__    ):


			'''simple docstring'''

			_A      :		Union[str, Any]			    =		VOCAB_FILES_NAMES
			_A      :		Union[str, Any]			    =		PRETRAINED_VOCAB_FILES_MAP
			_A      :		Dict			    =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			_A      :		int			    =		['''input_ids''', '''attention_mask''']
			_A      :		int			    =		GPTaTokenizer

			def __init__(							self     : Union[str, Any]							,     lowerCAmelCase__     : Union[str, Any]=None							,     lowerCAmelCase__     : List[str]=None							,     lowerCAmelCase__     : Optional[int]=None							,     lowerCAmelCase__     : Tuple="<|endoftext|>"							,     lowerCAmelCase__     : str="<|endoftext|>"							,     lowerCAmelCase__     : Optional[Any]="<|endoftext|>"							,     lowerCAmelCase__     : int=False							,     **lowerCAmelCase__     : Any							,     ):





					"""simple docstring"""






					super().__init__(
					    lowerCAmelCase__							,     lowerCAmelCase__							,     tokenizer_file=lowerCAmelCase__							,     unk_token=lowerCAmelCase__							,     bos_token=lowerCAmelCase__							,     eos_token=lowerCAmelCase__							,     add_prefix_space=lowerCAmelCase__							,     **lowerCAmelCase__							,     )

					__SCREAMING_SNAKE_CASE       :		int           =		kwargs.pop("""add_bos_token"""							,     lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE       :		int           =		json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()						)
					if pre_tok_state.get("""add_prefix_space"""							,     lowerCAmelCase__						) != add_prefix_space:
							__SCREAMING_SNAKE_CASE       :		List[str]           =		getattr(lowerCAmelCase__							,     pre_tok_state.pop("""type"""						)						)
							__SCREAMING_SNAKE_CASE       :		Any           =		add_prefix_space
							__SCREAMING_SNAKE_CASE       :		Dict           =		pre_tok_class(**lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		add_prefix_space

			def UpperCamelCase__					(							self     : Optional[Any]							,     *lowerCAmelCase__     : Union[str, Any]							,     **lowerCAmelCase__     : List[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		str           =		kwargs.get("""is_split_into_words"""							,     lowerCAmelCase__						)
					assert self.add_prefix_space or not is_split_into_words, (
					    F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
					    "to use it with pretokenized inputs."
					)

					return super()._batch_encode_plus(*lowerCAmelCase__							,     **lowerCAmelCase__						)

			def UpperCamelCase__					(							self     : Any							,     *lowerCAmelCase__     : str							,     **lowerCAmelCase__     : Tuple						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		int           =		kwargs.get("""is_split_into_words"""							,     lowerCAmelCase__						)

					assert self.add_prefix_space or not is_split_into_words, (
					    F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
					    "to use it with pretokenized inputs."
					)

					return super()._encode_plus(*lowerCAmelCase__							,     **lowerCAmelCase__						)

			def UpperCamelCase__					(							self     : Any							,     lowerCAmelCase__     : str							,     lowerCAmelCase__     : Optional[str] = None						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		List[Any]           =		self._tokenizer.model.save(lowerCAmelCase__							,     name=lowerCAmelCase__						)
					return tuple(lowerCAmelCase__						)



			def UpperCamelCase__					(							self     : Tuple							,     lowerCAmelCase__     : "Conversation"						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		[]
					for is_user, text in conversation.iter_texts():
							input_ids.extend(self.encode(lowerCAmelCase__							,     add_special_tokens=lowerCAmelCase__						) + [self.eos_token_id]						)

					if len(lowerCAmelCase__						) > self.model_max_length:
							__SCREAMING_SNAKE_CASE       :		Optional[int]           =		input_ids[-self.model_max_length :]
					return input_ids




'''simple docstring'''




import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
						from PIL import Image
else:






						class 						_UpperCamelCase  :


									'''simple docstring'''

									@staticmethod
									def UpperCamelCase__					(							*lowerCAmelCase__     : Any							,     **lowerCAmelCase__     : Any						):





											"""simple docstring"""






											pass






@is_pipeline_test
@require_vision
@require_torch
class 						_UpperCamelCase  (							unittest.TestCase    ):


			'''simple docstring'''

			_A      :		Optional[int]			    =		MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

			def UpperCamelCase__					(							self     : str							,     lowerCAmelCase__     : Dict							,     lowerCAmelCase__     : Tuple							,     lowerCAmelCase__     : Optional[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Any           =		pipeline(
					    """zero-shot-object-detection"""							,     model="""hf-internal-testing/tiny-random-owlvit-object-detection"""						)

					__SCREAMING_SNAKE_CASE       :		Any           =		[
					    {
					        """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
					        """candidate_labels""": ["""cat""", """remote""", """couch"""],
					    }
					]
					return object_detector, examples

			def UpperCamelCase__					(							self     : int							,     lowerCAmelCase__     : Optional[Any]							,     lowerCAmelCase__     : List[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		object_detector(examples[0]							,     threshold=0.0						)

					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		len(lowerCAmelCase__						)
					self.assertGreater(lowerCAmelCase__							,     0						)
					self.assertEqual(
					    lowerCAmelCase__							,     [
					        {
					            """score""": ANY(lowerCAmelCase__						),
					            """label""": ANY(lowerCAmelCase__						),
					            """box""": {"""xmin""": ANY(lowerCAmelCase__						), """ymin""": ANY(lowerCAmelCase__						), """xmax""": ANY(lowerCAmelCase__						), """ymax""": ANY(lowerCAmelCase__						)},
					        }
					        for i in range(lowerCAmelCase__						)
					    ]							,     )

			@require_tf
			@unittest.skip("""Zero Shot Object Detection not implemented in TF"""						)
			def UpperCamelCase__					(							self     : Optional[int]						):





					"""simple docstring"""






					pass

			@require_torch
			def UpperCamelCase__					(							self     : Optional[int]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		int           =		pipeline(
					    """zero-shot-object-detection"""							,     model="""hf-internal-testing/tiny-random-owlvit-object-detection"""						)

					__SCREAMING_SNAKE_CASE       :		int           =		object_detector(
					    """./tests/fixtures/tests_samples/COCO/000000039769.png"""							,     candidate_labels=["""cat""", """remote""", """couch"""]							,     threshold=0.64							,     )

					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        {"""score""": 0.72_35, """label""": """cat""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					        {"""score""": 0.72_18, """label""": """remote""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					        {"""score""": 0.71_84, """label""": """couch""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					        {"""score""": 0.67_48, """label""": """remote""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					        {"""score""": 0.66_56, """label""": """cat""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					        {"""score""": 0.66_14, """label""": """couch""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					        {"""score""": 0.64_56, """label""": """remote""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
					        {"""score""": 0.6_42, """label""": """remote""", """box""": {"""xmin""": 6_7, """ymin""": 2_7_4, """xmax""": 9_3, """ymax""": 2_9_7}},
					        {"""score""": 0.64_19, """label""": """cat""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
					    ]							,     )

					__SCREAMING_SNAKE_CASE       :		List[Any]           =		object_detector(
					    [
					        {
					            """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
					            """candidate_labels""": ["""cat""", """remote""", """couch"""],
					        }
					    ]							,     threshold=0.64							,     )

					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        [
					            {"""score""": 0.72_35, """label""": """cat""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					            {"""score""": 0.72_18, """label""": """remote""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					            {"""score""": 0.71_84, """label""": """couch""", """box""": {"""xmin""": 2_0_4, """ymin""": 1_6_7, """xmax""": 2_3_2, """ymax""": 1_9_0}},
					            {"""score""": 0.67_48, """label""": """remote""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					            {"""score""": 0.66_56, """label""": """cat""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					            {"""score""": 0.66_14, """label""": """couch""", """box""": {"""xmin""": 5_7_1, """ymin""": 8_3, """xmax""": 5_9_8, """ymax""": 1_0_3}},
					            {"""score""": 0.64_56, """label""": """remote""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
					            {"""score""": 0.6_42, """label""": """remote""", """box""": {"""xmin""": 6_7, """ymin""": 2_7_4, """xmax""": 9_3, """ymax""": 2_9_7}},
					            {"""score""": 0.64_19, """label""": """cat""", """box""": {"""xmin""": 4_9_4, """ymin""": 1_0_5, """xmax""": 5_2_1, """ymax""": 1_2_7}},
					        ]
					    ]							,     )

			@require_torch
			@slow
			def UpperCamelCase__					(							self     : str						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Any           =		pipeline("""zero-shot-object-detection"""						)

					__SCREAMING_SNAKE_CASE       :		List[str]           =		object_detector(
					    """http://images.cocodataset.org/val2017/000000039769.jpg"""							,     candidate_labels=["""cat""", """remote""", """couch"""]							,     )
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
					        {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
					        {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
					        {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
					        {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
					    ]							,     )

					__SCREAMING_SNAKE_CASE       :		Dict           =		object_detector(
					    [
					        {
					            """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""",
					            """candidate_labels""": ["""cat""", """remote""", """couch"""],
					        },
					        {
					            """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""",
					            """candidate_labels""": ["""cat""", """remote""", """couch"""],
					        },
					    ]							,     )
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        [
					            {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
					            {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
					            {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
					            {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
					            {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
					        ],
					        [
					            {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
					            {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
					            {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
					            {"""score""": 0.14_74, """label""": """remote""", """box""": {"""xmin""": 3_3_5, """ymin""": 7_4, """xmax""": 3_7_1, """ymax""": 1_8_7}},
					            {"""score""": 0.12_08, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 6_4_2, """ymax""": 4_7_6}},
					        ],
					    ]							,     )

			@require_tf
			@unittest.skip("""Zero Shot Object Detection not implemented in TF"""						)
			def UpperCamelCase__					(							self     : Any						):





					"""simple docstring"""






					pass

			@require_torch
			@slow
			def UpperCamelCase__					(							self     : List[str]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Dict           =		0.2
					__SCREAMING_SNAKE_CASE       :		Dict           =		pipeline("""zero-shot-object-detection"""						)

					__SCREAMING_SNAKE_CASE       :		Any           =		object_detector(
					    """http://images.cocodataset.org/val2017/000000039769.jpg"""							,     candidate_labels=["""cat""", """remote""", """couch"""]							,     threshold=lowerCAmelCase__							,     )
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
					        {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
					        {"""score""": 0.25_37, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 5_5, """xmax""": 3_1_5, """ymax""": 4_7_2}},
					    ]							,     )



			@require_torch
			@slow
			def UpperCamelCase__					(							self     : int						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		str           =		2
					__SCREAMING_SNAKE_CASE       :		int           =		pipeline("""zero-shot-object-detection"""						)

					__SCREAMING_SNAKE_CASE       :		int           =		object_detector(
					    """http://images.cocodataset.org/val2017/000000039769.jpg"""							,     candidate_labels=["""cat""", """remote""", """couch"""]							,     top_k=lowerCAmelCase__							,     )
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__							,     decimals=4						)							,     [
					        {"""score""": 0.28_68, """label""": """cat""", """box""": {"""xmin""": 3_2_4, """ymin""": 2_0, """xmax""": 6_4_0, """ymax""": 3_7_3}},
					        {"""score""": 0.2_77, """label""": """remote""", """box""": {"""xmin""": 4_0, """ymin""": 7_2, """xmax""": 1_7_7, """ymax""": 1_1_5}},
					    ]							,     )