


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


if is_vision_available():
						from PIL import Image
else:






						class 						lowerCAmelCase__      :




									@staticmethod
									def 				lowerCAmelCase__     (		*lowerCamelCase__	:  Tuple						,	**lowerCamelCase__	:  Tuple			) ->Optional[int]:



											'''simple docstring'''







											pass






@is_pipeline_test
@require_vision
class 						lowerCAmelCase__      (      unittest.TestCase						):
			lowerCAmelCase			:						int							 =  MODEL_FOR_VISION_2_SEQ_MAPPING
			lowerCAmelCase			:						Tuple							 =  TF_MODEL_FOR_VISION_2_SEQ_MAPPING




			def 				lowerCAmelCase__     (		self	:  Optional[int]						,	lowerCamelCase__	:  Optional[int]						,	lowerCamelCase__	:  List[str]						,	lowerCamelCase__	:  Any			) ->Optional[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[int]             =   pipeline("image-to-text"						,	model=lowerCamelCase__						,	tokenizer=lowerCamelCase__						,	image_processor=lowerCamelCase__			)
					_UpperCAmelCase		:							Optional[int]             =   [
					    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			),
					    "./tests/fixtures/tests_samples/COCO/000000039769.png",
					]
					return pipe, examples




			def 				lowerCAmelCase__     (		self	:  Optional[int]						,	lowerCamelCase__	:  List[str]						,	lowerCamelCase__	:  str			) ->Tuple:



					'''simple docstring'''







					_UpperCAmelCase		:							Tuple             =   pipe(lowerCamelCase__			)
					self.assertEqual(
					    lowerCamelCase__						,	[
					        [{"generated_text": ANY(lowerCamelCase__			)}],
					        [{"generated_text": ANY(lowerCamelCase__			)}],
					    ]						,	)




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



					'''simple docstring'''







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

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

					_UpperCAmelCase		:							List[str]             =   pipe([image, image]			)
					self.assertEqual(
					    lowerCamelCase__						,	[
					        [
					            {
					                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
					            }
					        ],
					        [
					            {
					                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
					            }
					        ],
					    ]						,	)

					_UpperCAmelCase		:							int             =   pipe(lowerCamelCase__						,	max_new_tokens=1			)
					self.assertEqual(
					    lowerCamelCase__						,	[{"generated_text": "growth"}]						,	)




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



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[int]             =   pipeline("image-to-text"						,	model="hf-internal-testing/tiny-random-vit-gpt2"			)
					_UpperCAmelCase		:							Dict             =   "./tests/fixtures/tests_samples/COCO/000000039769.png"

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

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




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



					'''simple docstring'''







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

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




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



					'''simple docstring'''







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

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

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



					'''simple docstring'''







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

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




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



					'''simple docstring'''







					_UpperCAmelCase		:							Tuple             =   pipeline("image-to-text"						,	model="microsoft/git-base-coco"			)
					_UpperCAmelCase		:							Optional[Any]             =   "https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
					_UpperCAmelCase		:							Optional[Any]             =   Image.open(requests.get(lowerCamelCase__						,	stream=lowerCamelCase__			).raw			)

					_UpperCAmelCase		:							Union[str, Any]             =   pipe(lowerCamelCase__			)
					self.assertEqual(lowerCamelCase__						,	[{"generated_text": "a cartoon of a purple character."}]			)




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



					'''simple docstring'''







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

					_UpperCAmelCase		:							Tuple             =   "a photography of"

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

					with self.assertRaises(lowerCamelCase__			):
							_UpperCAmelCase		:							List[str]             =   pipe([image, image]						,	prompt=[prompt, prompt]			)




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



					'''simple docstring'''







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

					_UpperCAmelCase		:							Optional[int]             =   "a photo of a"

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

					with self.assertRaises(lowerCamelCase__			):
							_UpperCAmelCase		:							List[Any]             =   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	:  str			) ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:							List[str]             =   pipeline("image-to-text"						,	model="google/pix2struct-ai2d-base"			)
					_UpperCAmelCase		:							Any             =   "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
					_UpperCAmelCase		:							List[str]             =   Image.open(requests.get(lowerCamelCase__						,	stream=lowerCamelCase__			).raw			)

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

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

					with self.assertRaises(lowerCamelCase__			):
							_UpperCAmelCase		:							Union[str, Any]             =   pipe([image, image]						,	prompt=[prompt, prompt]			)






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



					'''simple docstring'''







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

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

					_UpperCAmelCase		:							List[str]             =   pipe([image, image]			)
					self.assertEqual(
					    lowerCamelCase__						,	[
					        [{"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 ..utils import deprecate
from .attention_processor import (  # noqa: F401
    Attention,
    AttentionProcessor,
    AttnAddedKVProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRALinearLayer,
    LoRAXFormersAttnProcessor,
    SlicedAttnAddedKVProcessor,
    SlicedAttnProcessor,
    XFormersAttnProcessor,
)
from .attention_processor import AttnProcessor as AttnProcessorRename  # noqa: F401


deprecate(
    'cross_attention',
    '0.20.0',
    'Importing from cross_attention is deprecated. Please import from diffusers.models.attention_processor instead.',
    standard_warn=False,
)


lowerCamelCase__     						=  AttentionProcessor






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  Union[str, Any]						,	*lowerCamelCase__	:  int						,	**lowerCamelCase__	:  List[str]			) ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:							Dict             =   F"""{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'			)			)} instead."""
					deprecate("cross_attention"						,	"0.20.0"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  Optional[Any]						,	*lowerCamelCase__	:  Any						,	**lowerCamelCase__	:  Tuple			) ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							int             =   F"""{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'			)			)} instead."""
					deprecate("cross_attention"						,	"0.20.0"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  List[Any]						,	*lowerCamelCase__	:  List[Any]						,	**lowerCamelCase__	:  List[str]			) ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							List[str]             =   F"""{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'			)			)} instead."""
					deprecate("cross_attention"						,	"0.20.0"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  int						,	*lowerCamelCase__	:  Tuple						,	**lowerCamelCase__	:  Union[str, Any]			) ->int:



					'''simple docstring'''







					_UpperCAmelCase		:							Dict             =   F"""{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'			)			)} instead."""
					deprecate("cross_attention"						,	"0.20.0"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  int						,	*lowerCamelCase__	:  List[str]						,	**lowerCamelCase__	:  Tuple			) ->Union[str, Any]:



					'''simple docstring'''







					_UpperCAmelCase		:							Optional[Any]             =   F"""{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'			)			)} instead."""
					deprecate("cross_attention"						,	"0.20.0"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  List[Any]						,	*lowerCamelCase__	:  int						,	**lowerCamelCase__	:  Optional[int]			) ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase		:							List[Any]             =   F"""{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'			)			)} instead."""
					deprecate("cross_attention"						,	"0.20.0"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  Any						,	*lowerCamelCase__	:  Optional[Any]						,	**lowerCamelCase__	:  Union[str, Any]			) ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase		:							int             =   F"""{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'			)			)} instead."""
					deprecate("cross_attention"						,	"0.20.0"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)






class 						lowerCAmelCase__      (      UpperCAmelCase__						):




			def __init__(		self	:  Optional[Any]						,	*lowerCamelCase__	:  List[Any]						,	**lowerCamelCase__	:  Dict			) ->Any:



					'''simple docstring'''







					_UpperCAmelCase		:							int             =   F"""{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'			)			)} instead."""
					deprecate("cross_attention"						,	"0.20.0"						,	lowerCamelCase__						,	standard_warn=lowerCamelCase__			)
					super().__init__(*lowerCamelCase__						,	**lowerCamelCase__			)



