




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


if is_vision_available():
		from PIL import Image
else:





		class __magic_name__:






								@staticmethod
								def       __lowerCAmelCase(   *__UpperCamelCase						:						List[str]					,   **__UpperCamelCase						:						Union[str, Any]       ):



											'''simple docstring'''






											pass





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






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



									'''simple docstring'''






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






						def       __lowerCAmelCase(   self						:						Optional[int]					,   __UpperCamelCase						:						Optional[Any]					,   __UpperCamelCase						:						Tuple       ):



									'''simple docstring'''






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






						@require_tf
						def       __lowerCAmelCase(   self						:						int       ):



									'''simple docstring'''






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

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

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

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






						@require_torch
						def       __lowerCAmelCase(   self						:						Any       ):



									'''simple docstring'''






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

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

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






						@require_torch
						def       __lowerCAmelCase(   self						:						Any       ):



									'''simple docstring'''






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

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






						@slow
						@require_torch
						def       __lowerCAmelCase(   self						:						Optional[Any]       ):



									'''simple docstring'''






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

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

									snake_case__          =						pipe([image, image]       )
									self.assertEqual(
									    a__					,   [
									        [{"""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       ):



									'''simple docstring'''






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

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






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



									'''simple docstring'''






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

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






						@slow
						@require_torch
						def       __lowerCAmelCase(   self						:						Optional[int]       ):



									'''simple docstring'''






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

									snake_case__          =						"""a photography of"""

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

									with self.assertRaises(a__       ):
												snake_case__          =						pipe([image, image]					,   prompt=[prompt, prompt]       )






						@slow
						@require_torch
						def       __lowerCAmelCase(   self						:						Union[str, Any]       ):



									'''simple docstring'''






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

									snake_case__          =						"""a photo of a"""

									snake_case__          =						pipe(a__					,   prompt=a__       )
									self.assertEqual(a__					,   [{"""generated_text""": """a photo of a tent with a tent and a tent in the background."""}]       )

									with self.assertRaises(a__       ):
												snake_case__          =						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						:						Dict       ):



									'''simple docstring'''






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

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

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

									with self.assertRaises(a__       ):
												snake_case__          =						pipe([image, image]					,   prompt=[prompt, prompt]       )







						@slow
						@require_tf
						def       __lowerCAmelCase(   self						:						List[Any]       ):



									'''simple docstring'''






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

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

									snake_case__          =						pipe([image, image]       )
									self.assertEqual(
									    a__					,   [
									        [{"""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"""


def   a__			(					__lowercase )						->						bool:
				# must be
				_A       	=      [list(__lowercase ) for row in matrix]
				if matrix and isinstance(__lowercase  ,      __lowercase ):
								if isinstance(matrix[0]  ,      __lowercase ):
												_A       	=      0
												for row in matrix:
																if prev_len == 0:
																				_A       	=      len(__lowercase )
																				_A       	=      True
																else:
																				_A       	=      prev_len == len(__lowercase )
								else:
												_A       	=      True
				else:
								_A       	=      False

				return result






def   a__			(					__lowercase )						->						None:
				if check_matrix(__lowercase ) and len(__lowercase ) > 0:
								_A       	=      [list(__lowercase ) for row in a]
								_A       	=      len(__lowercase )
								if isinstance(a[0]  ,      __lowercase ):
												_A       	=      len(a[0] )
								else:
												for dat in a:
																print(__lowercase )
												return

								# horizotal printing increasing
								for i in range(0  ,      __lowercase ):
												print(a[0][i] )
								# vertical printing down
								for i in range(1  ,      __lowercase ):
												print(a[i][mat_col - 1] )
								# horizotal printing decreasing
								if mat_row > 1:
												for i in range(mat_col - 2  ,      -1  ,      -1 ):
																print(a[mat_row - 1][i] )
        # vertical printing up
								for i in range(mat_row - 2  ,      0  ,      -1 ):
												print(a[i][0] )
								_A       	=      [row[1 : mat_col - 1] for row in a[1 : mat_row - 1]]
								if len(__lowercase ) > 0:
												spiral_print_clockwise(__lowercase )
								else:
												return
				else:
								print("Not a valid matrix" )
								return






def   a__			(					__lowercase )						->						list[int]:
				if matrix:
								return list(matrix.pop(0 ) ) + spiral_traversal(list(zip(*__lowercase ) )[::-1] )
				else:
								return []


# driver code
if __name__ == "__main__":
		import doctest

		doctest.testmod()

		a_											=     [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
		spiral_print_clockwise(a)