


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


if is_vision_available():
							from PIL import Image
else:





							class    lowerCAmelCase__				:






														"""simple docstring"""




														@staticmethod
														def 					_UpperCamelCase			(   *a_				,							**a_     ):
															pass





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






							"""simple docstring"""
							__UpperCAmelCase   :		Union[str, Any]  					=	MODEL_FOR_VISION_2_SEQ_MAPPING
							__UpperCAmelCase   :		List[str]  					=	TF_MODEL_FOR_VISION_2_SEQ_MAPPING




							def 					_UpperCamelCase			(   self				,							a_				,							a_				,							a_     ):
								lowerCamelCase_			:							Dict      =       pipeline("image-to-text"				,							model=_UpperCAmelCase				,							tokenizer=_UpperCAmelCase				,							image_processor=_UpperCAmelCase     )
								lowerCamelCase_			:							Tuple      =       [
								    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"     ),
								    "./tests/fixtures/tests_samples/COCO/000000039769.png",
								]
								return pipe, examples




							def 					_UpperCamelCase			(   self				,							a_				,							a_     ):
								lowerCamelCase_			:							Optional[Any]      =       pipe(_UpperCAmelCase     )
								self.assertEqual(
								    _UpperCAmelCase				,							[
								        [{"generated_text": ANY(_UpperCAmelCase     )}],
								        [{"generated_text": ANY(_UpperCAmelCase     )}],
								    ]				,							)




							@require_tf
							def 					_UpperCamelCase			(   self     ):
								lowerCamelCase_			:							Union[str, Any]      =       pipeline("image-to-text"				,							model="hf-internal-testing/tiny-random-vit-gpt2"				,							framework="tf"     )
								lowerCamelCase_			:							Dict      =       "./tests/fixtures/tests_samples/COCO/000000039769.png"

								lowerCamelCase_			:							Union[str, Any]      =       pipe(_UpperCAmelCase     )
								self.assertEqual(
								    _UpperCAmelCase				,							[
								        {
								            "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
								        },
								    ]				,							)

								lowerCamelCase_			:							Tuple      =       pipe([image, image]     )
								self.assertEqual(
								    _UpperCAmelCase				,							[
								        [
								            {
								                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
								            }
								        ],
								        [
								            {
								                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
								            }
								        ],
								    ]				,							)

								lowerCamelCase_			:							List[Any]      =       pipe(_UpperCAmelCase				,							max_new_tokens=1     )
								self.assertEqual(
								    _UpperCAmelCase				,							[{"generated_text": "growth"}]				,							)




							@require_torch
							def 					_UpperCamelCase			(   self     ):
								lowerCamelCase_			:							Optional[Any]      =       pipeline("image-to-text"				,							model="hf-internal-testing/tiny-random-vit-gpt2"     )
								lowerCamelCase_			:							List[str]      =       "./tests/fixtures/tests_samples/COCO/000000039769.png"

								lowerCamelCase_			:							Optional[int]      =       pipe(_UpperCAmelCase     )
								self.assertEqual(
								    _UpperCAmelCase				,							[
								        {
								            "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
								        },
								    ]				,							)

								lowerCamelCase_			:							Union[str, Any]      =       pipe([image, image]     )
								self.assertEqual(
								    _UpperCAmelCase				,							[
								        [
								            {
								                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
								            }
								        ],
								        [
								            {
								                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
								            }
								        ],
								    ]				,							)




							@require_torch
							def 					_UpperCamelCase			(   self     ):
								lowerCamelCase_			:							Any      =       pipeline("image-to-text"				,							model="hf-internal-testing/tiny-random-BlipForConditionalGeneration"     )
								lowerCamelCase_			:							str      =       "./tests/fixtures/tests_samples/COCO/000000039769.png"
								lowerCamelCase_			:							str      =       "a photo of"

								lowerCamelCase_			:							str      =       pipe(_UpperCAmelCase				,							prompt=_UpperCAmelCase     )
								self.assertTrue(outputs[0]["generated_text"].startswith(_UpperCAmelCase     )     )




							@slow
							@require_torch
							def 					_UpperCamelCase			(   self     ):
								lowerCamelCase_			:							Optional[int]      =       pipeline("image-to-text"				,							model="ydshieh/vit-gpt2-coco-en"     )
								lowerCamelCase_			:							str      =       "./tests/fixtures/tests_samples/COCO/000000039769.png"

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

								lowerCamelCase_			:							int      =       pipe([image, image]     )
								self.assertEqual(
								    _UpperCAmelCase				,							[
								        [{"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     ):
								lowerCamelCase_			:							Optional[Any]      =       pipeline("image-to-text"				,							model="Salesforce/blip-image-captioning-base"     )
								lowerCamelCase_			:							Dict      =       "https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
								lowerCamelCase_			:							Any      =       Image.open(requests.get(_UpperCAmelCase				,							stream=_UpperCAmelCase     ).raw     )

								lowerCamelCase_			:							Any      =       pipe(_UpperCAmelCase     )
								self.assertEqual(_UpperCAmelCase				,							[{"generated_text": "a pink pokemon pokemon with a blue shirt and a blue shirt"}]     )




							@slow
							@require_torch
							def 					_UpperCamelCase			(   self     ):
								lowerCamelCase_			:							Union[str, Any]      =       pipeline("image-to-text"				,							model="microsoft/git-base-coco"     )
								lowerCamelCase_			:							List[Any]      =       "https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
								lowerCamelCase_			:							Optional[Any]      =       Image.open(requests.get(_UpperCAmelCase				,							stream=_UpperCAmelCase     ).raw     )

								lowerCamelCase_			:							Dict      =       pipe(_UpperCAmelCase     )
								self.assertEqual(_UpperCAmelCase				,							[{"generated_text": "a cartoon of a purple character."}]     )




							@slow
							@require_torch
							def 					_UpperCamelCase			(   self     ):
								lowerCamelCase_			:							Any      =       pipeline("image-to-text"				,							model="Salesforce/blip-image-captioning-base"     )
								lowerCamelCase_			:							int      =       "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
								lowerCamelCase_			:							str      =       Image.open(requests.get(_UpperCAmelCase				,							stream=_UpperCAmelCase     ).raw     )

								lowerCamelCase_			:							List[Any]      =       "a photography of"

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

								with self.assertRaises(_UpperCAmelCase     ):
									lowerCamelCase_			:							Optional[int]      =       pipe([image, image]				,							prompt=[prompt, prompt]     )




							@slow
							@require_torch
							def 					_UpperCamelCase			(   self     ):
								lowerCamelCase_			:							Union[str, Any]      =       pipeline("image-to-text"				,							model="microsoft/git-base-coco"     )
								lowerCamelCase_			:							List[str]      =       "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
								lowerCamelCase_			:							Tuple      =       Image.open(requests.get(_UpperCAmelCase				,							stream=_UpperCAmelCase     ).raw     )

								lowerCamelCase_			:							Dict      =       "a photo of a"

								lowerCamelCase_			:							Union[str, Any]      =       pipe(_UpperCAmelCase				,							prompt=_UpperCAmelCase     )
								self.assertEqual(_UpperCAmelCase				,							[{"generated_text": "a photo of a tent with a tent and a tent in the background."}]     )

								with self.assertRaises(_UpperCAmelCase     ):
									lowerCamelCase_			:							int      =       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     ):
								lowerCamelCase_			:							int      =       pipeline("image-to-text"				,							model="google/pix2struct-ai2d-base"     )
								lowerCamelCase_			:							Optional[int]      =       "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
								lowerCamelCase_			:							Dict      =       Image.open(requests.get(_UpperCAmelCase				,							stream=_UpperCAmelCase     ).raw     )

								lowerCamelCase_			:							Union[str, Any]      =       "What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"

								lowerCamelCase_			:							Tuple      =       pipe(_UpperCAmelCase				,							prompt=_UpperCAmelCase     )
								self.assertEqual(_UpperCAmelCase				,							[{"generated_text": "ash cloud"}]     )

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




							@slow
							@require_tf
							def 					_UpperCamelCase			(   self     ):
								lowerCamelCase_			:							Optional[Any]      =       pipeline("image-to-text"				,							model="ydshieh/vit-gpt2-coco-en"				,							framework="tf"     )
								lowerCamelCase_			:							Optional[int]      =       "./tests/fixtures/tests_samples/COCO/000000039769.png"

								lowerCamelCase_			:							Union[str, Any]      =       pipe(_UpperCAmelCase     )
								self.assertEqual(_UpperCAmelCase				,							[{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}]     )

								lowerCamelCase_			:							Any      =       pipe([image, image]     )
								self.assertEqual(
								    _UpperCAmelCase				,							[
								        [{"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 "}],
								    ]				,							)





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__magic_name__       					=      {'''configuration_detr''': ['''DETR_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DetrConfig''', '''DetrOnnxConfig''']}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__magic_name__       					=      ['''DetrFeatureExtractor''']
							__magic_name__       					=      ['''DetrImageProcessor''']

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							__magic_name__       					=      [
							    '''DETR_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''DetrForObjectDetection''',
							    '''DetrForSegmentation''',
							    '''DetrModel''',
							    '''DetrPreTrainedModel''',
							]


if TYPE_CHECKING:
							from .configuration_detr import DETR_PRETRAINED_CONFIG_ARCHIVE_MAP, DetrConfig, DetrOnnxConfig

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_detr import DetrFeatureExtractor
														from .image_processing_detr import DetrImageProcessor

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_detr import (
														    DETR_PRETRAINED_MODEL_ARCHIVE_LIST,
														    DetrForObjectDetection,
														    DetrForSegmentation,
														    DetrModel,
														    DetrPreTrainedModel,
														)

else:
							import sys

							__magic_name__       					=      _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

