




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__              =						False


if is_vision_available():
							from PIL import Image
else:


							class 				__SCREAMING_SNAKE_CASE							:

												@staticmethod
												def 	lowercase_							(				*__lowercase ,       **__lowercase					):







													'''simple docstring'''




													pass


@is_pipeline_test
@require_vision
class 				__SCREAMING_SNAKE_CASE							(		unittest.TestCase  ):
					_lowerCamelCase		  = MODEL_FOR_VISION_2_SEQ_MAPPING
					_lowerCamelCase		  = TF_MODEL_FOR_VISION_2_SEQ_MAPPING

					def 	lowercase_							(				self ,       __lowercase ,       __lowercase ,       __lowercase					):







						'''simple docstring'''




						lowerCamelCase_							:		Dict							   =							pipeline('image-to-text' ,       model=__lowercase ,       tokenizer=__lowercase ,       image_processor=__lowercase					)
						lowerCamelCase_							:		str							   =							[
						    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					),
						    './tests/fixtures/tests_samples/COCO/000000039769.png',
						]
						return pipe, examples

					def 	lowercase_							(				self ,       __lowercase ,       __lowercase					):







						'''simple docstring'''




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

					@require_tf
					def 	lowercase_							(				self					):







						'''simple docstring'''




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

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

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

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

					@require_torch
					def 	lowercase_							(				self					):







						'''simple docstring'''




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

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

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

					@require_torch
					def 	lowercase_							(				self					):







						'''simple docstring'''




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

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

					@slow
					@require_torch
					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		str							   =							pipeline('image-to-text' ,       model='ydshieh/vit-gpt2-coco-en'					)
						lowerCamelCase_							:		str							   =							'./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_							:		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					):







						'''simple docstring'''




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

						lowerCamelCase_							:		Dict							   =							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					):







						'''simple docstring'''




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

						lowerCamelCase_							:		int							   =							pipe(__lowercase					)
						self.assertEqual(__lowercase ,       [{'generated_text': 'a cartoon of a purple character.'}]					)

					@slow
					@require_torch
					def 	lowercase_							(				self					):







						'''simple docstring'''




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

						lowerCamelCase_							:		Any							   =							'a photography of'

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

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

					@slow
					@require_torch
					def 	lowercase_							(				self					):







						'''simple docstring'''




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

						lowerCamelCase_							:		Dict							   =							'a photo of a'

						lowerCamelCase_							:		List[str]							   =							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_							:		Dict							   =							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					):







						'''simple docstring'''




						lowerCamelCase_							:		List[str]							   =							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_							:		Any							   =							Image.open(requests.get(__lowercase ,       stream=__lowercase					).raw					)

						lowerCamelCase_							:		Optional[int]							   =							'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_							:		List[Any]							   =							pipe([image, image] ,       prompt=[prompt, prompt]					)





					@slow
					@require_tf
					def 	lowercase_							(				self					):







						'''simple docstring'''




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

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

						lowerCamelCase_							:		List[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 '}],
						    ] ,       )






# as due to their complexity multi-gpu tests could impact other tests, and to aid debug we have those in a separate module.

import os
import sys

from transformers.testing_utils import TestCasePlus, execute_subprocess_async, get_gpu_count, require_torch_gpu, slow

from .utils import load_json


class 				__SCREAMING_SNAKE_CASE							(		lowerCamelCase  ):

					@classmethod
					def 	lowercase_							(				cls					):







						'''simple docstring'''




						return cls





					@slow
					@require_torch_gpu
					def 	lowercase_							(				self					):







						'''simple docstring'''




						lowerCamelCase_							:		Dict							   =							self.get_auto_remove_tmp_dir()
						lowerCamelCase_							:		Tuple							   =							f"\n            --model_name Helsinki-NLP/opus-mt-en-ro\n            --save_dir {output_dir}\n            --data_dir {self.test_file_dir_str}/test_data/wmt_en_ro\n            --num_beams 2\n            --task translation\n        ".split()

						# we want this test to run even if there is only one GPU, but if there are more we use them all
						lowerCamelCase_							:		List[Any]							   =							get_gpu_count()
						lowerCamelCase_							:		Union[str, Any]							   =							f"\n            -m torch.distributed.launch\n            --nproc_per_node={n_gpu}\n            {self.test_file_dir}/run_distributed_eval.py\n        ".split()
						lowerCamelCase_							:		int							   =							[sys.executable] + distributed_args + args
						execute_subprocess_async(__lowercase ,       env=self.get_env()					)

						lowerCamelCase_							:		Any							   =							os.path.join(__lowercase ,       'test_bleu.json'					)
						lowerCamelCase_							:		List[Any]							   =							load_json(__lowercase					)
						# print(metrics)
						self.assertGreaterEqual(metrics['bleu'] ,       25					)
