

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






      @staticmethod
      def   UpperCAmelCase_							(							*_lowercase		,	**_lowercase  )->    int:
          pass






@is_pipeline_test
@require_vision
class 	__magic_name__				(       unittest.TestCase							):
     UpperCamelCase_  :int								=			MODEL_FOR_VISION_2_SEQ_MAPPING
     UpperCamelCase_  :int								=			TF_MODEL_FOR_VISION_2_SEQ_MAPPING






     def   UpperCAmelCase_							(							self		,	_lowercase		,	_lowercase		,	_lowercase  )->    Any:
         UpperCamelCase_      =	pipeline("image-to-text"		,	model=_lowercase		,	tokenizer=_lowercase		,	image_processor=_lowercase  )
         UpperCamelCase_      =	[
             Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  ),
             './tests/fixtures/tests_samples/COCO/000000039769.png',
         ]
         return pipe, examples






     def   UpperCAmelCase_							(							self		,	_lowercase		,	_lowercase  )->    Tuple:
         UpperCamelCase_      =	pipe(_lowercase  )
         self.assertEqual(
             _lowercase		,	[
                 [{"generated_text": ANY(_lowercase  )}],
                 [{"generated_text": ANY(_lowercase  )}],
             ]		,	)






     @require_tf
     def   UpperCAmelCase_							(							self  )->    List[Any]:
         UpperCamelCase_      =	pipeline("image-to-text"		,	model="hf-internal-testing/tiny-random-vit-gpt2"		,	framework="tf"  )
         UpperCamelCase_      =	'./tests/fixtures/tests_samples/COCO/000000039769.png'

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

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

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






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

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

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






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

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






     @slow
     @require_torch
     def   UpperCAmelCase_							(							self  )->    Union[str, Any]:
         UpperCamelCase_      =	pipeline("image-to-text"		,	model="ydshieh/vit-gpt2-coco-en"  )
         UpperCamelCase_      =	'./tests/fixtures/tests_samples/COCO/000000039769.png'

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

         UpperCamelCase_      =	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   UpperCAmelCase_							(							self  )->    List[Any]:
         UpperCamelCase_      =	pipeline("image-to-text"		,	model="Salesforce/blip-image-captioning-base"  )
         UpperCamelCase_      =	'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
         UpperCamelCase_      =	Image.open(requests.get(_lowercase		,	stream=_lowercase  ).raw  )

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






     @slow
     @require_torch
     def   UpperCAmelCase_							(							self  )->    Any:
         UpperCamelCase_      =	pipeline("image-to-text"		,	model="microsoft/git-base-coco"  )
         UpperCamelCase_      =	'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
         UpperCamelCase_      =	Image.open(requests.get(_lowercase		,	stream=_lowercase  ).raw  )

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






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

         UpperCamelCase_      =	'a photography of'

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

         with self.assertRaises(_lowercase  ):
             UpperCamelCase_      =	pipe([image, image]		,	prompt=[prompt, prompt]  )






     @slow
     @require_torch
     def   UpperCAmelCase_							(							self  )->    Dict:
         UpperCamelCase_      =	pipeline("image-to-text"		,	model="microsoft/git-base-coco"  )
         UpperCamelCase_      =	'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
         UpperCamelCase_      =	Image.open(requests.get(_lowercase		,	stream=_lowercase  ).raw  )

         UpperCamelCase_      =	'a photo of a'

         UpperCamelCase_      =	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  ):
             UpperCamelCase_      =	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  )->    int:
         UpperCamelCase_      =	pipeline("image-to-text"		,	model="google/pix2struct-ai2d-base"  )
         UpperCamelCase_      =	'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
         UpperCamelCase_      =	Image.open(requests.get(_lowercase		,	stream=_lowercase  ).raw  )

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

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

         with self.assertRaises(_lowercase  ):
             UpperCamelCase_      =	pipe([image, image]		,	prompt=[prompt, prompt]  )






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

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

         UpperCamelCase_      =	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 unittest

from transformers.image_processing_utils import get_size_dict






class 	__magic_name__				(       unittest.TestCase							):






     def   UpperCAmelCase_							(							self  )->    int:
         # Test a dict with the wrong keys raises an error
         UpperCamelCase_      =	{"wrong_key": 224}
         with self.assertRaises(_lowercase  ):
             get_size_dict(_lowercase  )

         UpperCamelCase_      =	{"height": 224}
         with self.assertRaises(_lowercase  ):
             get_size_dict(_lowercase  )

         UpperCamelCase_      =	{"width": 224, "shortest_edge": 224}
         with self.assertRaises(_lowercase  ):
             get_size_dict(_lowercase  )

         # Test a dict with the correct keys is returned as is
         UpperCamelCase_      =	{"height": 224, "width": 224}
         UpperCamelCase_      =	get_size_dict(_lowercase  )
         self.assertEqual(_lowercase		,	_lowercase  )

         UpperCamelCase_      =	{"shortest_edge": 224}
         UpperCamelCase_      =	get_size_dict(_lowercase  )
         self.assertEqual(_lowercase		,	{"shortest_edge": 224}  )

         UpperCamelCase_      =	{"longest_edge": 224, "shortest_edge": 224}
         UpperCamelCase_      =	get_size_dict(_lowercase  )
         self.assertEqual(_lowercase		,	{"longest_edge": 224, "shortest_edge": 224}  )

         # Test a single int value which  represents (size, size)
         UpperCamelCase_      =	get_size_dict(224  )
         self.assertEqual(_lowercase		,	{"height": 224, "width": 224}  )

         # Test a single int value which represents the shortest edge
         UpperCamelCase_      =	get_size_dict(224		,	default_to_square=_lowercase  )
         self.assertEqual(_lowercase		,	{"shortest_edge": 224}  )

         # Test a tuple of ints which represents (height, width)
         UpperCamelCase_      =	get_size_dict((150, 200)  )
         self.assertEqual(_lowercase		,	{"height": 150, "width": 200}  )

         # Test a tuple of ints which represents (width, height)
         UpperCamelCase_      =	get_size_dict((150, 200)		,	height_width_order=_lowercase  )
         self.assertEqual(_lowercase		,	{"height": 200, "width": 150}  )

         # Test an int representing the shortest edge and max_size which represents the longest edge
         UpperCamelCase_      =	get_size_dict(224		,	max_size=256		,	default_to_square=_lowercase  )
         self.assertEqual(_lowercase		,	{"shortest_edge": 224, "longest_edge": 256}  )

         # Test int with default_to_square=True and max_size fails
         with self.assertRaises(_lowercase  ):
             get_size_dict(224		,	max_size=256		,	default_to_square=_lowercase  )
