


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:
						_UpperCAmelCase      :	Tuple  =False


if is_vision_available():
						from PIL import Image
else:







						class 					snake_case__:







												'''simple docstring'''




												@staticmethod
												def     lowercase_			(					*__lowercase ,    **__lowercase				)       ->     Optional[int]:
																pass







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







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__   :	Optional[Any]       =				MODEL_FOR_VISION_2_SEQ_MAPPING
						SCREAMING_SNAKE_CASE__   :	Union[str, Any]       =				TF_MODEL_FOR_VISION_2_SEQ_MAPPING




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase				)       ->     List[str]:
										lowerCAmelCase_			:   str     					=					pipeline('''image-to-text''' ,    model=__lowercase ,    tokenizer=__lowercase ,    image_processor=__lowercase				)
										lowerCAmelCase_			:   Union[str, Any]     					=					[
										    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				),
										    '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
										]
										return pipe, examples




						def     lowercase_			(					self ,    __lowercase ,    __lowercase				)       ->     Union[str, Any]:
										lowerCAmelCase_			:   List[str]     					=					pipe(__lowercase				)
										self.assertEqual(
										    __lowercase ,    [
										        [{'''generated_text''': ANY(__lowercase				)}],
										        [{'''generated_text''': ANY(__lowercase				)}],
										    ] ,    )




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

										lowerCAmelCase_			:   str     					=					pipe(__lowercase				)
										self.assertEqual(
										    __lowercase ,    [
										        {
										            '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										        },
										    ] ,    )

										lowerCAmelCase_			:   List[Any]     					=					pipe([image, image]				)
										self.assertEqual(
										    __lowercase ,    [
										        [
										            {
										                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										            }
										        ],
										        [
										            {
										                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										            }
										        ],
										    ] ,    )

										lowerCAmelCase_			:   str     					=					pipe(__lowercase ,    max_new_tokens=1				)
										self.assertEqual(
										    __lowercase ,    [{'''generated_text''': '''growth'''}] ,    )




						@require_torch
						def     lowercase_			(					self				)       ->     Dict:
										lowerCAmelCase_			:   Any     					=					pipeline('''image-to-text''' ,    model='''hf-internal-testing/tiny-random-vit-gpt2'''				)
										lowerCAmelCase_			:   str     					=					'''./tests/fixtures/tests_samples/COCO/000000039769.png'''

										lowerCAmelCase_			:   List[Any]     					=					pipe(__lowercase				)
										self.assertEqual(
										    __lowercase ,    [
										        {
										            '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										        },
										    ] ,    )

										lowerCAmelCase_			:   List[str]     					=					pipe([image, image]				)
										self.assertEqual(
										    __lowercase ,    [
										        [
										            {
										                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										            }
										        ],
										        [
										            {
										                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										            }
										        ],
										    ] ,    )




						@require_torch
						def     lowercase_			(					self				)       ->     List[Any]:
										lowerCAmelCase_			:   str     					=					pipeline('''image-to-text''' ,    model='''hf-internal-testing/tiny-random-BlipForConditionalGeneration'''				)
										lowerCAmelCase_			:   Dict     					=					'''./tests/fixtures/tests_samples/COCO/000000039769.png'''
										lowerCAmelCase_			:   Optional[Any]     					=					'''a photo of'''

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




						@slow
						@require_torch
						def     lowercase_			(					self				)       ->     Dict:
										lowerCAmelCase_			:   int     					=					pipeline('''image-to-text''' ,    model='''ydshieh/vit-gpt2-coco-en'''				)
										lowerCAmelCase_			:   List[str]     					=					'''./tests/fixtures/tests_samples/COCO/000000039769.png'''

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

										lowerCAmelCase_			:   Tuple     					=					pipe([image, image]				)
										self.assertEqual(
										    __lowercase ,    [
										        [{'''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     lowercase_			(					self				)       ->     Dict:
										lowerCAmelCase_			:   List[str]     					=					pipeline('''image-to-text''' ,    model='''Salesforce/blip-image-captioning-base'''				)
										lowerCAmelCase_			:   Tuple     					=					'''https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'''
										lowerCAmelCase_			:   Union[str, Any]     					=					Image.open(requests.get(__lowercase ,    stream=__lowercase				).raw				)

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




						@slow
						@require_torch
						def     lowercase_			(					self				)       ->     int:
										lowerCAmelCase_			:   int     					=					pipeline('''image-to-text''' ,    model='''microsoft/git-base-coco'''				)
										lowerCAmelCase_			:   Union[str, Any]     					=					'''https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'''
										lowerCAmelCase_			:   Any     					=					Image.open(requests.get(__lowercase ,    stream=__lowercase				).raw				)

										lowerCAmelCase_			:   Union[str, Any]     					=					pipe(__lowercase				)
										self.assertEqual(__lowercase ,    [{'''generated_text''': '''a cartoon of a purple character.'''}]				)




						@slow
						@require_torch
						def     lowercase_			(					self				)       ->     Any:
										lowerCAmelCase_			:   int     					=					pipeline('''image-to-text''' ,    model='''Salesforce/blip-image-captioning-base'''				)
										lowerCAmelCase_			:   Tuple     					=					'''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
										lowerCAmelCase_			:   List[str]     					=					Image.open(requests.get(__lowercase ,    stream=__lowercase				).raw				)

										lowerCAmelCase_			:   Tuple     					=					'''a photography of'''

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

										with self.assertRaises(__lowercase				):
														lowerCAmelCase_			:   int     					=					pipe([image, image] ,    prompt=[prompt, prompt]				)




						@slow
						@require_torch
						def     lowercase_			(					self				)       ->     Optional[int]:
										lowerCAmelCase_			:   List[str]     					=					pipeline('''image-to-text''' ,    model='''microsoft/git-base-coco'''				)
										lowerCAmelCase_			:   Any     					=					'''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
										lowerCAmelCase_			:   List[Any]     					=					Image.open(requests.get(__lowercase ,    stream=__lowercase				).raw				)

										lowerCAmelCase_			:   int     					=					'''a photo of a'''

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

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

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

										lowerCAmelCase_			:   Union[str, Any]     					=					pipe(__lowercase ,    prompt=__lowercase				)
										self.assertEqual(__lowercase ,    [{'''generated_text''': '''ash cloud'''}]				)

										with self.assertRaises(__lowercase				):
														lowerCAmelCase_			:   Optional[Any]     					=					pipe([image, image] ,    prompt=[prompt, prompt]				)




						@slow
						@require_tf
						def     lowercase_			(					self				)       ->     int:
										lowerCAmelCase_			:   int     					=					pipeline('''image-to-text''' ,    model='''ydshieh/vit-gpt2-coco-en''' ,    framework='''tf'''				)
										lowerCAmelCase_			:   Dict     					=					'''./tests/fixtures/tests_samples/COCO/000000039769.png'''

										lowerCAmelCase_			:   Optional[int]     					=					pipe(__lowercase				)
										self.assertEqual(__lowercase ,    [{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}]				)

										lowerCAmelCase_			:   Union[str, Any]     					=					pipe([image, image]				)
										self.assertEqual(
										    __lowercase ,    [
										        [{'''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 torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
						import transformer_engine.pytorch as te



def       lowerCAmelCase			(		lowerCAmelCase_			, lowerCAmelCase_=True			, lowerCAmelCase_=True			, lowerCAmelCase_=True			)->			Union[str, Any]:
				if not is_fpa_available():
								raise ImportError('''Using `convert_model` requires transformer_engine to be installed.'''			)
				for name, module in model.named_children():
								if isinstance(lowerCAmelCase_			, nn.Linear			) and to_transformer_engine and _convert_linear:
												# Return early if the linear layer weights are not multiples of 16
												if any(p % 16 != 0 for p in module.weight.shape			):
																return
												lowerCAmelCase_			:   Optional[int]     					=					module.bias is not None
												lowerCAmelCase_			:   Any     					=					te.Linear(
												    module.in_features			, module.out_features			, bias=lowerCAmelCase_			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Dict     					=					module.weight.data.clone()
												if has_bias:
																lowerCAmelCase_			:   Any     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, nn.LayerNorm			) and to_transformer_engine and _convert_ln:
												lowerCAmelCase_			:   List[str]     					=					te.LayerNorm(module.normalized_shape[0]			, eps=module.eps			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Union[str, Any]     					=					module.weight.data.clone()
												lowerCAmelCase_			:   List[Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, te.Linear			) and not to_transformer_engine and _convert_linear:
												lowerCAmelCase_			:   Dict     					=					module.bias is not None
												lowerCAmelCase_			:   Union[str, Any]     					=					nn.Linear(
												    module.in_features			, module.out_features			, bias=lowerCAmelCase_			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Optional[Any]     					=					module.weight.data.clone()
												if has_bias:
																lowerCAmelCase_			:   Union[str, Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, te.LayerNorm			) and not to_transformer_engine and _convert_ln:
												lowerCAmelCase_			:   Union[str, Any]     					=					nn.LayerNorm(module.normalized_shape[0]			, eps=module.eps			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   List[str]     					=					module.weight.data.clone()
												lowerCAmelCase_			:   List[Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								else:
												convert_model(
												    lowerCAmelCase_			, to_transformer_engine=lowerCAmelCase_			, _convert_linear=_convert_linear			, _convert_ln=_convert_ln			, )




def       lowerCAmelCase			(		lowerCAmelCase_			)->			str:
				if not is_fpa_available():
								raise ImportError('''Using `has_transformer_engine_layers` requires transformer_engine to be installed.'''			)
				for m in model.modules():
								if isinstance(lowerCAmelCase_			, (te.LayerNorm, te.Linear)			):
												return True
				return False