


import unittest

import requests

from transformers import MODEL_FOR_VISION_2_SEQ_MAPPING, TF_MODEL_FOR_VISION_2_SEQ_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_torch_available():
		from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
		lowerCAmelCase__						:int         =							False


if is_vision_available():
		from PIL import Image
else:

		class 	__a   :
				@staticmethod
				def 			UpperCAmelCase__       (    *_SCREAMING_SNAKE_CASE    ,					**_SCREAMING_SNAKE_CASE		)				-> Optional[int]:




											"""simple docstring"""





											pass

@is_pipeline_test
@require_vision
class 	__a   (       unittest.TestCase    ):
		_a			:						Optional[Any]    			=			MODEL_FOR_VISION_2_SEQ_MAPPING
		_a			:						Tuple    			=			TF_MODEL_FOR_VISION_2_SEQ_MAPPING
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)				-> int:




									"""simple docstring"""





									_UpperCAmelCase					=    pipeline('image-to-text'    ,					model=_SCREAMING_SNAKE_CASE    ,					tokenizer=_SCREAMING_SNAKE_CASE    ,					image_processor=_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase					=    [
									    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'		),
									    './tests/fixtures/tests_samples/COCO/000000039769.png',
									]
									return pipe, examples
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE		)				-> Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE		)
									self.assertEqual(
									    _SCREAMING_SNAKE_CASE    ,					[
									        [{'generated_text': ANY(_SCREAMING_SNAKE_CASE		)}],
									        [{'generated_text': ANY(_SCREAMING_SNAKE_CASE		)}],
									    ]    ,					)
		@require_tf
		def 			UpperCAmelCase__       (    self		)				-> Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase					=    pipeline('image-to-text'    ,					model='hf-internal-testing/tiny-random-vit-gpt2'    ,					framework='tf'		)
									_UpperCAmelCase					=    './tests/fixtures/tests_samples/COCO/000000039769.png'

									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE		)
									self.assertEqual(
									    _SCREAMING_SNAKE_CASE    ,					[
									        {
									            'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
									        },
									    ]    ,					)

									_UpperCAmelCase					=    pipe([image, image]		)
									self.assertEqual(
									    _SCREAMING_SNAKE_CASE    ,					[
									        [
									            {
									                'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
									            }
									        ],
									        [
									            {
									                'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
									            }
									        ],
									    ]    ,					)

									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE    ,					max_new_tokens=1		)
									self.assertEqual(
									    _SCREAMING_SNAKE_CASE    ,					[{'generated_text': 'growth'}]    ,					)
		@require_torch
		def 			UpperCAmelCase__       (    self		)				-> Any:




									"""simple docstring"""





									_UpperCAmelCase					=    pipeline('image-to-text'    ,					model='hf-internal-testing/tiny-random-vit-gpt2'		)
									_UpperCAmelCase					=    './tests/fixtures/tests_samples/COCO/000000039769.png'

									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE		)
									self.assertEqual(
									    _SCREAMING_SNAKE_CASE    ,					[
									        {
									            'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
									        },
									    ]    ,					)

									_UpperCAmelCase					=    pipe([image, image]		)
									self.assertEqual(
									    _SCREAMING_SNAKE_CASE    ,					[
									        [
									            {
									                'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
									            }
									        ],
									        [
									            {
									                'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
									            }
									        ],
									    ]    ,					)
		@require_torch
		def 			UpperCAmelCase__       (    self		)				-> List[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    pipeline('image-to-text'    ,					model='hf-internal-testing/tiny-random-BlipForConditionalGeneration'		)
									_UpperCAmelCase					=    './tests/fixtures/tests_samples/COCO/000000039769.png'
									_UpperCAmelCase					=    'a photo of'

									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE    ,					prompt=_SCREAMING_SNAKE_CASE		)
									self.assertTrue(outputs[0]['generated_text'].startswith(_SCREAMING_SNAKE_CASE		)		)
		@slow
		@require_torch
		def 			UpperCAmelCase__       (    self		)				-> Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    pipeline('image-to-text'    ,					model='ydshieh/vit-gpt2-coco-en'		)
									_UpperCAmelCase					=    './tests/fixtures/tests_samples/COCO/000000039769.png'

									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE		)
									self.assertEqual(_SCREAMING_SNAKE_CASE    ,					[{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}]		)

									_UpperCAmelCase					=    pipe([image, image]		)
									self.assertEqual(
									    _SCREAMING_SNAKE_CASE    ,					[
									        [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
									        [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
									    ]    ,					)
		@slow
		@require_torch
		def 			UpperCAmelCase__       (    self		)				-> Dict:




									"""simple docstring"""





									_UpperCAmelCase					=    pipeline('image-to-text'    ,					model='Salesforce/blip-image-captioning-base'		)
									_UpperCAmelCase					=    'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
									_UpperCAmelCase					=    Image.open(requests.get(_SCREAMING_SNAKE_CASE    ,					stream=_SCREAMING_SNAKE_CASE		).raw		)

									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE		)
									self.assertEqual(_SCREAMING_SNAKE_CASE    ,					[{'generated_text': 'a pink pokemon pokemon with a blue shirt and a blue shirt'}]		)
		@slow
		@require_torch
		def 			UpperCAmelCase__       (    self		)				-> Any:




									"""simple docstring"""





									_UpperCAmelCase					=    pipeline('image-to-text'    ,					model='microsoft/git-base-coco'		)
									_UpperCAmelCase					=    'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
									_UpperCAmelCase					=    Image.open(requests.get(_SCREAMING_SNAKE_CASE    ,					stream=_SCREAMING_SNAKE_CASE		).raw		)

									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE		)
									self.assertEqual(_SCREAMING_SNAKE_CASE    ,					[{'generated_text': 'a cartoon of a purple character.'}]		)
		@slow
		@require_torch
		def 			UpperCAmelCase__       (    self		)				-> int:




									"""simple docstring"""





									_UpperCAmelCase					=    pipeline('image-to-text'    ,					model='Salesforce/blip-image-captioning-base'		)
									_UpperCAmelCase					=    'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
									_UpperCAmelCase					=    Image.open(requests.get(_SCREAMING_SNAKE_CASE    ,					stream=_SCREAMING_SNAKE_CASE		).raw		)

									_UpperCAmelCase					=    'a photography of'

									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE    ,					prompt=_SCREAMING_SNAKE_CASE		)
									self.assertEqual(_SCREAMING_SNAKE_CASE    ,					[{'generated_text': 'a photography of a volcano'}]		)

									with self.assertRaises(_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase					=    pipe([image, image]    ,					prompt=[prompt, prompt]		)
		@slow
		@require_torch
		def 			UpperCAmelCase__       (    self		)				-> Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase					=    pipeline('image-to-text'    ,					model='microsoft/git-base-coco'		)
									_UpperCAmelCase					=    'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
									_UpperCAmelCase					=    Image.open(requests.get(_SCREAMING_SNAKE_CASE    ,					stream=_SCREAMING_SNAKE_CASE		).raw		)

									_UpperCAmelCase					=    'a photo of a'

									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE    ,					prompt=_SCREAMING_SNAKE_CASE		)
									self.assertEqual(_SCREAMING_SNAKE_CASE    ,					[{'generated_text': 'a photo of a tent with a tent and a tent in the background.'}]		)

									with self.assertRaises(_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase					=    pipe([image, image]    ,					prompt=[prompt, prompt]		)
		@unittest.skipIf(
		    not is_torch_greater_or_equal_than_1_11    ,					reason='`Pix2StructImageProcessor` requires `torch>=1.11.0`.'		)
		@slow
		@require_torch
		def 			UpperCAmelCase__       (    self		)				-> str:




									"""simple docstring"""





									_UpperCAmelCase					=    pipeline('image-to-text'    ,					model='google/pix2struct-ai2d-base'		)
									_UpperCAmelCase					=    'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
									_UpperCAmelCase					=    Image.open(requests.get(_SCREAMING_SNAKE_CASE    ,					stream=_SCREAMING_SNAKE_CASE		).raw		)

									_UpperCAmelCase					=    'What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'

									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE    ,					prompt=_SCREAMING_SNAKE_CASE		)
									self.assertEqual(_SCREAMING_SNAKE_CASE    ,					[{'generated_text': 'ash cloud'}]		)

									with self.assertRaises(_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase					=    pipe([image, image]    ,					prompt=[prompt, prompt]		)






		@slow
		@require_tf
		def 			UpperCAmelCase__       (    self		)				-> Tuple:




									"""simple docstring"""





									_UpperCAmelCase					=    pipeline('image-to-text'    ,					model='ydshieh/vit-gpt2-coco-en'    ,					framework='tf'		)
									_UpperCAmelCase					=    './tests/fixtures/tests_samples/COCO/000000039769.png'

									_UpperCAmelCase					=    pipe(_SCREAMING_SNAKE_CASE		)
									self.assertEqual(_SCREAMING_SNAKE_CASE    ,					[{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}]		)

									_UpperCAmelCase					=    pipe([image, image]		)
									self.assertEqual(
									    _SCREAMING_SNAKE_CASE    ,					[
									        [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
									        [{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}],
									    ]    ,					)




import os
from shutil import copyfile
from typing import Optional, Tuple

from ...file_utils import is_sentencepiece_available
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging


if is_sentencepiece_available():
		from .tokenization_deberta_va import DebertaVaTokenizer
else:
		lowerCAmelCase__						:int         =							None

lowerCAmelCase__						:List[str]         =							logging.get_logger(__name__)

lowerCAmelCase__						:List[str]         =							{'''vocab_file''': '''spm.model''', '''tokenizer_file''': '''tokenizer.json'''}

lowerCAmelCase__						:List[str]         =							{
    '''vocab_file''': {
        '''microsoft/deberta-v2-xlarge''': '''https://huggingface.co/microsoft/deberta-v2-xlarge/resolve/main/spm.model''',
        '''microsoft/deberta-v2-xxlarge''': '''https://huggingface.co/microsoft/deberta-v2-xxlarge/resolve/main/spm.model''',
        '''microsoft/deberta-v2-xlarge-mnli''': (
            '''https://huggingface.co/microsoft/deberta-v2-xlarge-mnli/resolve/main/spm.model'''
        ),
        '''microsoft/deberta-v2-xxlarge-mnli''': (
            '''https://huggingface.co/microsoft/deberta-v2-xxlarge-mnli/resolve/main/spm.model'''
        ),
    }
}

lowerCAmelCase__						:List[str]         =							{
    '''microsoft/deberta-v2-xlarge''': 5_1_2,
    '''microsoft/deberta-v2-xxlarge''': 5_1_2,
    '''microsoft/deberta-v2-xlarge-mnli''': 5_1_2,
    '''microsoft/deberta-v2-xxlarge-mnli''': 5_1_2,
}

lowerCAmelCase__						:Any         =							{
    '''microsoft/deberta-v2-xlarge''': {'''do_lower_case''': False},
    '''microsoft/deberta-v2-xxlarge''': {'''do_lower_case''': False},
    '''microsoft/deberta-v2-xlarge-mnli''': {'''do_lower_case''': False},
    '''microsoft/deberta-v2-xxlarge-mnli''': {'''do_lower_case''': False},
}

class 	__a   (       UpperCAmelCase    ):
		_a			:						Tuple    			=			VOCAB_FILES_NAMES
		_a			:						int    			=			PRETRAINED_VOCAB_FILES_MAP
		_a			:						Optional[Any]    			=			PRETRAINED_INIT_CONFIGURATION
		_a			:						str    			=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		_a			:						Optional[int]    			=			DebertaVaTokenizer
		def __init__(    self    ,					_SCREAMING_SNAKE_CASE=None    ,					_SCREAMING_SNAKE_CASE=None    ,					_SCREAMING_SNAKE_CASE=False    ,					_SCREAMING_SNAKE_CASE=False    ,					_SCREAMING_SNAKE_CASE="[CLS]"    ,					_SCREAMING_SNAKE_CASE="[SEP]"    ,					_SCREAMING_SNAKE_CASE="[UNK]"    ,					_SCREAMING_SNAKE_CASE="[SEP]"    ,					_SCREAMING_SNAKE_CASE="[PAD]"    ,					_SCREAMING_SNAKE_CASE="[CLS]"    ,					_SCREAMING_SNAKE_CASE="[MASK]"    ,					**_SCREAMING_SNAKE_CASE    ,					)				-> None:




									"""simple docstring"""





									super().__init__(
									    _SCREAMING_SNAKE_CASE    ,					tokenizer_file=_SCREAMING_SNAKE_CASE    ,					do_lower_case=_SCREAMING_SNAKE_CASE    ,					bos_token=_SCREAMING_SNAKE_CASE    ,					eos_token=_SCREAMING_SNAKE_CASE    ,					unk_token=_SCREAMING_SNAKE_CASE    ,					sep_token=_SCREAMING_SNAKE_CASE    ,					pad_token=_SCREAMING_SNAKE_CASE    ,					cls_token=_SCREAMING_SNAKE_CASE    ,					mask_token=_SCREAMING_SNAKE_CASE    ,					split_by_punct=_SCREAMING_SNAKE_CASE    ,					**_SCREAMING_SNAKE_CASE    ,					)

									_UpperCAmelCase					=    do_lower_case
									_UpperCAmelCase					=    split_by_punct
									_UpperCAmelCase					=    vocab_file
									_UpperCAmelCase					=    False if not self.vocab_file else True
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE=None		)				-> str:




									"""simple docstring"""





									if token_ids_a is None:
																return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
									_UpperCAmelCase					=    [self.cls_token_id]
									_UpperCAmelCase					=    [self.sep_token_id]
									return cls + token_ids_a + sep + token_ids_a + sep
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE=None    ,					_SCREAMING_SNAKE_CASE=False		)				-> Any:




									"""simple docstring"""





									if already_has_special_tokens:
																return super().get_special_tokens_mask(
																    token_ids_a=_SCREAMING_SNAKE_CASE    ,					token_ids_a=_SCREAMING_SNAKE_CASE    ,					already_has_special_tokens=_SCREAMING_SNAKE_CASE		)

									if token_ids_a is not None:
																return [1] + ([0] * len(_SCREAMING_SNAKE_CASE		)) + [1] + ([0] * len(_SCREAMING_SNAKE_CASE		)) + [1]
									return [1] + ([0] * len(_SCREAMING_SNAKE_CASE		)) + [1]
		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE=None		)				-> Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase					=    [self.sep_token_id]
									_UpperCAmelCase					=    [self.cls_token_id]
									if token_ids_a is None:
																return len(cls + token_ids_a + sep		) * [0]
									return len(cls + token_ids_a + sep		) * [0] + len(token_ids_a + sep		) * [1]






		def 			UpperCAmelCase__       (    self    ,					_SCREAMING_SNAKE_CASE    ,					_SCREAMING_SNAKE_CASE = None		)				-> Tuple[str]:




									"""simple docstring"""





									if not self.can_save_slow_tokenizer:
																raise ValueError(
																    'Your fast tokenizer does not have the necessary information to save the vocabulary for a slow '
																    'tokenizer.'		)

									if not os.path.isdir(_SCREAMING_SNAKE_CASE		):
																logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''		)
																return
									_UpperCAmelCase					=    os.path.join(
									    _SCREAMING_SNAKE_CASE    ,					(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']		)

									if os.path.abspath(self.vocab_file		) != os.path.abspath(_SCREAMING_SNAKE_CASE		):
																copyfile(self.vocab_file    ,					_SCREAMING_SNAKE_CASE		)

									return (out_vocab_file,)
