




'''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:
		_SCREAMING_SNAKE_CASE							:     Dict   	=				False


if is_vision_available():
		from PIL import Image
else:




		class 				_snake_case	:




					@staticmethod
					def      lowerCAmelCase__       (      *a__       ,							**a__							)       ->       Union[str, Any]:

												'''simple docstring'''




												pass




@is_pipeline_test
@require_vision
class 				_snake_case	(			unittest.TestCase							):
			lowerCAmelCase_  :   int		 =					MODEL_FOR_VISION_2_SEQ_MAPPING
			lowerCAmelCase_  :   int		 =					TF_MODEL_FOR_VISION_2_SEQ_MAPPING




			def      lowerCAmelCase__       (      self       ,							a__       ,							a__       ,							a__							)       ->       Union[str, Any]:

										'''simple docstring'''




										snake_case_			      =   pipeline("image-to-text"       ,							model=a__       ,							tokenizer=a__       ,							image_processor=a__							)
										snake_case_			      =   [
										    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							),
										    './tests/fixtures/tests_samples/COCO/000000039769.png',
										]
										return pipe, examples




			def      lowerCAmelCase__       (      self       ,							a__       ,							a__							)       ->       Tuple:

										'''simple docstring'''




										snake_case_			      =   pipe(a__							)
										self.assertEqual(
										    a__       ,							[
										        [{"generated_text": ANY(a__							)}],
										        [{"generated_text": ANY(a__							)}],
										    ]       ,							)




			@require_tf
			def      lowerCAmelCase__       (      self							)       ->       Any:

										'''simple docstring'''




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

										snake_case_			      =   pipe(a__							)
										self.assertEqual(
										    a__       ,							[
										        {
										            "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
										        },
										    ]       ,							)

										snake_case_			      =   pipe([image, image]							)
										self.assertEqual(
										    a__       ,							[
										        [
										            {
										                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
										            }
										        ],
										        [
										            {
										                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
										            }
										        ],
										    ]       ,							)

										snake_case_			      =   pipe(a__       ,							max_new_tokens=1							)
										self.assertEqual(
										    a__       ,							[{"generated_text": "growth"}]       ,							)




			@require_torch
			def      lowerCAmelCase__       (      self							)       ->       int:

										'''simple docstring'''




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

										snake_case_			      =   pipe(a__							)
										self.assertEqual(
										    a__       ,							[
										        {
										            "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
										        },
										    ]       ,							)

										snake_case_			      =   pipe([image, image]							)
										self.assertEqual(
										    a__       ,							[
										        [
										            {
										                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
										            }
										        ],
										        [
										            {
										                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
										            }
										        ],
										    ]       ,							)




			@require_torch
			def      lowerCAmelCase__       (      self							)       ->       Optional[int]:

										'''simple docstring'''




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

										snake_case_			      =   pipe(a__       ,							prompt=a__							)
										self.assertTrue(outputs[0]["generated_text"].startswith(a__							)							)




			@slow
			@require_torch
			def      lowerCAmelCase__       (      self							)       ->       Optional[int]:

										'''simple docstring'''




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

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

										snake_case_			      =   pipe([image, image]							)
										self.assertEqual(
										    a__       ,							[
										        [{"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      lowerCAmelCase__       (      self							)       ->       int:

										'''simple docstring'''




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

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




			@slow
			@require_torch
			def      lowerCAmelCase__       (      self							)       ->       int:

										'''simple docstring'''




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

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




			@slow
			@require_torch
			def      lowerCAmelCase__       (      self							)       ->       Union[str, Any]:

										'''simple docstring'''




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

										snake_case_			      =   'a photography of'

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

										with self.assertRaises(a__							):
																	snake_case_			      =   pipe([image, image]       ,							prompt=[prompt, prompt]							)




			@slow
			@require_torch
			def      lowerCAmelCase__       (      self							)       ->       int:

										'''simple docstring'''




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

										snake_case_			      =   'a photo of a'

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

										with self.assertRaises(a__							):
																	snake_case_			      =   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      lowerCAmelCase__       (      self							)       ->       Optional[int]:

										'''simple docstring'''




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

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

										snake_case_			      =   pipe(a__       ,							prompt=a__							)
										self.assertEqual(a__       ,							[{"generated_text": "ash cloud"}]							)

										with self.assertRaises(a__							):
																	snake_case_			      =   pipe([image, image]       ,							prompt=[prompt, prompt]							)




			@slow
			@require_tf
			def      lowerCAmelCase__       (      self							)       ->       List[str]:

										'''simple docstring'''




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

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

										snake_case_			      =   pipe([image, image]							)
										self.assertEqual(
										    a__       ,							[
										        [{"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,
    is_vision_available,
)


_SCREAMING_SNAKE_CASE							:     Dict   	=				{
    "configuration_mobilevitv2": [
        "MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "MobileViTV2Config",
        "MobileViTV2OnnxConfig",
    ],
}


try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		_SCREAMING_SNAKE_CASE							:     Optional[Any]   	=				[
		    "MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST",
		    "MobileViTV2ForImageClassification",
		    "MobileViTV2ForSemanticSegmentation",
		    "MobileViTV2Model",
		    "MobileViTV2PreTrainedModel",
		]

if TYPE_CHECKING:
		from .configuration_mobilevitva import (
		    MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    MobileViTVaConfig,
		    MobileViTVaOnnxConfig,
		)

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_mobilevitva import (
				    MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
				    MobileViTVaForImageClassification,
				    MobileViTVaForSemanticSegmentation,
				    MobileViTVaModel,
				    MobileViTVaPreTrainedModel,
				)

else:
		import sys

		_SCREAMING_SNAKE_CASE							:     Any   	=				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




