




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:
	__A						:		List[Any]          =						False


if is_vision_available():
	from PIL import Image
else:

	class 		lowercase_       :




						@staticmethod
						def     _lowercase						(   *_lowercase:		List[Any],     **_lowercase:		Optional[Any]):


										'''simple docstring'''


										pass

@is_pipeline_test
@require_vision
class 		lowercase_       (	unittest.TestCase				):
					__UpperCamelCase     	= MODEL_FOR_VISION_2_SEQ_MAPPING
					__UpperCamelCase     	= TF_MODEL_FOR_VISION_2_SEQ_MAPPING




					def     _lowercase						(   self:		Optional[Any],     _lowercase:		Union[str, Any],     _lowercase:		List[str],     _lowercase:		List[Any]):


									'''simple docstring'''


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




					def     _lowercase						(   self:		str,     _lowercase:		int,     _lowercase:		int):


									'''simple docstring'''


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




					@require_tf
					def     _lowercase						(   self:		Optional[Any]):


									'''simple docstring'''


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

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

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

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




					@require_torch
					def     _lowercase						(   self:		Any):


									'''simple docstring'''


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

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

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




					@require_torch
					def     _lowercase						(   self:		Dict):


									'''simple docstring'''


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

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




					@slow
					@require_torch
					def     _lowercase						(   self:		List[Any]):


									'''simple docstring'''


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

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

									__lowerCAmelCase					=  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:		Any):


									'''simple docstring'''


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

									__lowerCAmelCase					=  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:		Union[str, Any]):


									'''simple docstring'''


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

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




					@slow
					@require_torch
					def     _lowercase						(   self:		Tuple):


									'''simple docstring'''


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

									__lowerCAmelCase					=  """a photography of"""

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

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




					@slow
					@require_torch
					def     _lowercase						(   self:		List[str]):


									'''simple docstring'''


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

									__lowerCAmelCase					=  """a photo of a"""

									__lowerCAmelCase					=  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					=  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:		Union[str, Any]):


									'''simple docstring'''


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

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

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

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







					@slow
					@require_tf
					def     _lowercase						(   self:		Any):


									'''simple docstring'''


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

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

									__lowerCAmelCase					=  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 tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor





class 		__SCREAMING_SNAKE_CASE      (				unittest.TestCase      ):






				"""simple docstring"""

				def 		__lowerCamelCase(				self						,	SCREAMING_SNAKE_CASE__ = False      ):






					"""simple docstring"""

					_snake_case					:							Any					     =   10

					if only_cross_attention:
						_snake_case					:							Tuple					     =   12
					else:
						# when only cross attention is not set, the cross attention dim must be the same as the query dim
						_snake_case					:							Any					     =   query_dim

					return {
					    "query_dim": query_dim,
					    "cross_attention_dim": cross_attention_dim,
					    "heads": 2,
					    "dim_head": 4,
					    "added_kv_proj_dim": 6,
					    "norm_num_groups": 1,
					    "only_cross_attention": only_cross_attention,
					    "processor": AttnAddedKVProcessor(),
					}







				def 		__lowerCamelCase(				self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__      ):






					"""simple docstring"""

					_snake_case					:							Any					     =   2

					_snake_case					:							List[str]					     =   torch.rand(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	3						,	2      )
					_snake_case					:							List[str]					     =   torch.rand(SCREAMING_SNAKE_CASE__						,	4						,	SCREAMING_SNAKE_CASE__      )
					_snake_case					:							Optional[int]					     =   None

					return {
					    "hidden_states": hidden_states,
					    "encoder_hidden_states": encoder_hidden_states,
					    "attention_mask": attention_mask,
					}







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					torch.manual_seed(0      )

					_snake_case					:							Optional[int]					     =   self.get_constructor_arguments(only_cross_attention=SCREAMING_SNAKE_CASE__      )
					_snake_case					:							List[str]					     =   Attention(**SCREAMING_SNAKE_CASE__      )

					self.assertTrue(attn.to_k is not None      )
					self.assertTrue(attn.to_v is not None      )

					_snake_case					:							Dict					     =   self.get_forward_arguments(
					    query_dim=constructor_args["""query_dim"""]						,	added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )

					_snake_case					:							Any					     =   attn(**SCREAMING_SNAKE_CASE__      )

					# only self attention

					torch.manual_seed(0      )

					_snake_case					:							Any					     =   self.get_constructor_arguments(only_cross_attention=SCREAMING_SNAKE_CASE__      )
					_snake_case					:							List[str]					     =   Attention(**SCREAMING_SNAKE_CASE__      )

					self.assertTrue(attn.to_k is None      )
					self.assertTrue(attn.to_v is None      )

					_snake_case					:							str					     =   self.get_forward_arguments(
					    query_dim=constructor_args["""query_dim"""]						,	added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )

					_snake_case					:							Any					     =   attn(**SCREAMING_SNAKE_CASE__      )

					self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )





class 		__SCREAMING_SNAKE_CASE      (				unittest.TestCase      ):






				"""simple docstring"""

				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							str					     =   DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""						,	safety_checker=SCREAMING_SNAKE_CASE__      )

					_snake_case					:							Tuple					     =   pipe(
					    """foo"""						,	num_inference_steps=2						,	generator=torch.Generator("""cpu"""      ).manual_seed(0      )						,	output_type="""np"""						,	).images

					# the initial conversion succeeds
					_snake_case					:							List[str]					     =   DiffusionPipeline.from_pretrained(
					    """hf-internal-testing/tiny-stable-diffusion-pipe"""						,	device_map="""sequential"""						,	safety_checker=SCREAMING_SNAKE_CASE__      )

					_snake_case					:							Union[str, Any]					     =   pipe(
					    """foo"""						,	num_inference_steps=2						,	generator=torch.Generator("""cpu"""      ).manual_seed(0      )						,	output_type="""np"""						,	).images

					with tempfile.TemporaryDirectory() as tmpdir:
						# save the converted model
						pipe.save_pretrained(SCREAMING_SNAKE_CASE__      )

						# can also load the converted weights
						_snake_case					:							str					     =   DiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__						,	device_map="""sequential"""						,	safety_checker=SCREAMING_SNAKE_CASE__      )

					_snake_case					:							int					     =   pipe(
					    """foo"""						,	num_inference_steps=2						,	generator=torch.Generator("""cpu"""      ).manual_seed(0      )						,	output_type="""np"""						,	).images

					self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	atol=1e-5      )      )
					self.assertTrue(np.allclose(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	atol=1e-5      )      )





