




'''simple docstring'''




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:
							snake_case__          =						False


if is_vision_available():
							from PIL import Image
else:






							class     UpperCamelCase_     :







													"""simple docstring"""


													@staticmethod
													def       _a    (     *_lowerCamelCase      :       Optional[int]							, **_lowerCamelCase      :       List[Any]    ):





																		"""simple docstring"""







																		pass






@is_pipeline_test
@require_vision
class     UpperCamelCase_     (unittest.TestCase     ):







						"""simple docstring"""


						_lowerCAmelCase    						=					MODEL_FOR_VISION_2_SEQ_MAPPING
						_lowerCAmelCase    						=					TF_MODEL_FOR_VISION_2_SEQ_MAPPING







						def       _a    (     self      :       str							, _lowerCamelCase      :       Union[str, Any]							, _lowerCamelCase      :       Tuple							, _lowerCamelCase      :       List[str]    ):





											"""simple docstring"""







											A_ :     Tuple =							pipeline('''image-to-text'''							, model=_lowerCamelCase							, tokenizer=_lowerCamelCase							, image_processor=_lowerCamelCase    )
											A_ :     Union[str, Any] =							[
											    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    ),
											    '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
											]
											return pipe, examples







						def       _a    (     self      :       int							, _lowerCamelCase      :       Dict							, _lowerCamelCase      :       Any    ):





											"""simple docstring"""







											A_ :     Optional[int] =							pipe(_lowerCamelCase    )
											self.assertEqual(
											    _lowerCamelCase							, [
											        [{'''generated_text''': ANY(_lowerCamelCase    )}],
											        [{'''generated_text''': ANY(_lowerCamelCase    )}],
											    ]							, )







						@require_tf
						def       _a    (     self      :       Union[str, Any]    ):





											"""simple docstring"""







											A_ :     List[str] =							pipeline('''image-to-text'''							, model='''hf-internal-testing/tiny-random-vit-gpt2'''							, framework='''tf'''    )
											A_ :     int =							'''./tests/fixtures/tests_samples/COCO/000000039769.png'''

											A_ :     Optional[Any] =							pipe(_lowerCamelCase    )
											self.assertEqual(
											    _lowerCamelCase							, [
											        {
											            '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
											        },
											    ]							, )

											A_ :     Dict =							pipe([image, image]    )
											self.assertEqual(
											    _lowerCamelCase							, [
											        [
											            {
											                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
											            }
											        ],
											        [
											            {
											                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
											            }
											        ],
											    ]							, )

											A_ :     Optional[int] =							pipe(_lowerCamelCase							, max_new_tokens=1    )
											self.assertEqual(
											    _lowerCamelCase							, [{'''generated_text''': '''growth'''}]							, )







						@require_torch
						def       _a    (     self      :       Tuple    ):





											"""simple docstring"""







											A_ :     List[Any] =							pipeline('''image-to-text'''							, model='''hf-internal-testing/tiny-random-vit-gpt2'''    )
											A_ :     Dict =							'''./tests/fixtures/tests_samples/COCO/000000039769.png'''

											A_ :     int =							pipe(_lowerCamelCase    )
											self.assertEqual(
											    _lowerCamelCase							, [
											        {
											            '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
											        },
											    ]							, )

											A_ :     str =							pipe([image, image]    )
											self.assertEqual(
											    _lowerCamelCase							, [
											        [
											            {
											                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
											            }
											        ],
											        [
											            {
											                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
											            }
											        ],
											    ]							, )







						@require_torch
						def       _a    (     self      :       List[str]    ):





											"""simple docstring"""







											A_ :     List[Any] =							pipeline('''image-to-text'''							, model='''hf-internal-testing/tiny-random-BlipForConditionalGeneration'''    )
											A_ :     Optional[Any] =							'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
											A_ :     List[Any] =							'''a photo of'''

											A_ :     str =							pipe(_lowerCamelCase							, prompt=_lowerCamelCase    )
											self.assertTrue(outputs[0]['''generated_text'''].startswith(_lowerCamelCase    )    )







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





											"""simple docstring"""







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

											A_ :     List[str] =							pipe(_lowerCamelCase    )
											self.assertEqual(_lowerCamelCase							, [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}]    )

											A_ :     Any =							pipe([image, image]    )
											self.assertEqual(
											    _lowerCamelCase							, [
											        [{'''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       _a    (     self      :       Optional[Any]    ):





											"""simple docstring"""







											A_ :     int =							pipeline('''image-to-text'''							, model='''Salesforce/blip-image-captioning-base'''    )
											A_ :     Union[str, Any] =							'''https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'''
											A_ :     List[str] =							Image.open(requests.get(_lowerCamelCase							, stream=_lowerCamelCase    ).raw    )

											A_ :     Union[str, Any] =							pipe(_lowerCamelCase    )
											self.assertEqual(_lowerCamelCase							, [{'''generated_text''': '''a pink pokemon pokemon with a blue shirt and a blue shirt'''}]    )







						@slow
						@require_torch
						def       _a    (     self      :       Optional[Any]    ):





											"""simple docstring"""







											A_ :     Optional[Any] =							pipeline('''image-to-text'''							, model='''microsoft/git-base-coco'''    )
											A_ :     str =							'''https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'''
											A_ :     Tuple =							Image.open(requests.get(_lowerCamelCase							, stream=_lowerCamelCase    ).raw    )

											A_ :     List[Any] =							pipe(_lowerCamelCase    )
											self.assertEqual(_lowerCamelCase							, [{'''generated_text''': '''a cartoon of a purple character.'''}]    )







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





											"""simple docstring"""







											A_ :     Union[str, Any] =							pipeline('''image-to-text'''							, model='''Salesforce/blip-image-captioning-base'''    )
											A_ :     List[Any] =							'''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
											A_ :     List[str] =							Image.open(requests.get(_lowerCamelCase							, stream=_lowerCamelCase    ).raw    )

											A_ :     Any =							'''a photography of'''

											A_ :     Union[str, Any] =							pipe(_lowerCamelCase							, prompt=_lowerCamelCase    )
											self.assertEqual(_lowerCamelCase							, [{'''generated_text''': '''a photography of a volcano'''}]    )

											with self.assertRaises(_lowerCamelCase    ):
																A_ :     List[Any] =							pipe([image, image]							, prompt=[prompt, prompt]    )







						@slow
						@require_torch
						def       _a    (     self      :       Tuple    ):





											"""simple docstring"""







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

											A_ :     Any =							'''a photo of a'''

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

											with self.assertRaises(_lowerCamelCase    ):
																A_ :     Tuple =							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       _a    (     self      :       int    ):





											"""simple docstring"""







											A_ :     Dict =							pipeline('''image-to-text'''							, model='''google/pix2struct-ai2d-base'''    )
											A_ :     Any =							'''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
											A_ :     List[str] =							Image.open(requests.get(_lowerCamelCase							, stream=_lowerCamelCase    ).raw    )

											A_ :     Optional[Any] =							'''What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'''

											A_ :     Optional[Any] =							pipe(_lowerCamelCase							, prompt=_lowerCamelCase    )
											self.assertEqual(_lowerCamelCase							, [{'''generated_text''': '''ash cloud'''}]    )

											with self.assertRaises(_lowerCamelCase    ):
																A_ :     str =							pipe([image, image]							, prompt=[prompt, prompt]    )







						@slow
						@require_tf
						def       _a    (     self      :       List[str]    ):





											"""simple docstring"""







											A_ :     Optional[Any] =							pipeline('''image-to-text'''							, model='''ydshieh/vit-gpt2-coco-en'''							, framework='''tf'''    )
											A_ :     int =							'''./tests/fixtures/tests_samples/COCO/000000039769.png'''

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

											A_ :     Optional[int] =							pipe([image, image]    )
											self.assertEqual(
											    _lowerCamelCase							, [
											        [{'''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 '''}],
											    ]							, )









'''simple docstring'''




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


snake_case__          =						{"""configuration_nat""": ["""NAT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """NatConfig"""]}


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							snake_case__          =						[
							    """NAT_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """NatForImageClassification""",
							    """NatModel""",
							    """NatPreTrainedModel""",
							    """NatBackbone""",
							]

if TYPE_CHECKING:
							from .configuration_nat import NAT_PRETRAINED_CONFIG_ARCHIVE_MAP, NatConfig

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_nat import (
														    NAT_PRETRAINED_MODEL_ARCHIVE_LIST,
														    NatBackbone,
														    NatForImageClassification,
														    NatModel,
														    NatPreTrainedModel,
														)

else:
							import sys

							snake_case__          =						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)



