



"""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       lowercase__   :







         '''simple docstring'''


         @staticmethod
         def      lowercase__				(							*_UpperCAmelCase			:  Dict		,				**_UpperCAmelCase			:  Union[str, Any]    ) ->						Dict:


               '''simple docstring'''




               pass
@is_pipeline_test
@require_vision
class       lowercase__   (    unittest.TestCase      ):







       '''simple docstring'''


       UpperCamelCase						=     MODEL_FOR_VISION_2_SEQ_MAPPING
       UpperCamelCase						=     TF_MODEL_FOR_VISION_2_SEQ_MAPPING



       def      lowercase__				(							self			:  Optional[int]		,				_UpperCAmelCase			:  Optional[Any]		,				_UpperCAmelCase			:  List[Any]		,				_UpperCAmelCase			:  Union[str, Any]    ) ->						int:


             '''simple docstring'''




             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      lowercase__				(							self			:  Any		,				_UpperCAmelCase			:  Optional[int]		,				_UpperCAmelCase			:  Optional[Any]    ) ->						str:


             '''simple docstring'''




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



       @require_tf
       def      lowercase__				(							self			:  Dict    ) ->						List[str]:


             '''simple docstring'''




             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      lowercase__				(							self			:  str    ) ->						Optional[Any]:


             '''simple docstring'''




             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      lowercase__				(							self			:  List[str]    ) ->						Union[str, Any]:


             '''simple docstring'''




             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      lowercase__				(							self			:  Any    ) ->						List[Any]:


             '''simple docstring'''




             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      lowercase__				(							self			:  List[str]    ) ->						int:


             '''simple docstring'''




             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      lowercase__				(							self			:  int    ) ->						List[Any]:


             '''simple docstring'''




             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      lowercase__				(							self			:  List[Any]    ) ->						List[str]:


             '''simple docstring'''




             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      lowercase__				(							self			:  List[Any]    ) ->						int:


             '''simple docstring'''




             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      lowercase__				(							self			:  Tuple    ) ->						List[str]:


             '''simple docstring'''




             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      lowercase__				(							self			:  int    ) ->						Optional[int]:


             '''simple docstring'''




             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 "}],
                 ]		,				)





from __future__ import annotations

from PIL import Image

# Define the first generation of cells
# fmt: off
__SCREAMING_SNAKE_CASE						:    Optional[int]     = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
def 					snake_case						(__lowercase						)       ->       list[int]:

						'''simple docstring'''



						return [int(__lowercase						) for c in F"""{ruleset:08}"""[:8]]
def 					snake_case						(__lowercase					,			__lowercase					,			__lowercase						)       ->       list[int]:

						'''simple docstring'''



						_snake_case						:     Any				=    len(cells[0]						)  # 31
						_snake_case						:     str				=    []
						for i in range(__lowercase						):
												# Get the neighbors of each cell
												# Handle neighbours outside bounds by using 0 as their value
												_snake_case						:     Any				=    0 if i == 0 else cells[time][i - 1]
												_snake_case						:     List[str]				=    0 if i == population - 1 else cells[time][i + 1]
												# Define a new cell and add it to the new generation
												_snake_case						:     str				=    7 - int(F"""{left_neighbor}{cells[time][i]}{right_neighbor}"""					,			2						)
												next_generation.append(rule[situation]						)
						return next_generation
def 					snake_case						(__lowercase						)       ->       Image.Image:

						'''simple docstring'''



						_snake_case						:     str				=    Image.new("RGB"					,			(len(cells[0]						), len(__lowercase						))						)
						_snake_case						:     int				=    img.load()
						# Generates image
						for w in range(img.width						):
												for h in range(img.height						):
																		_snake_case						:     List[Any]				=    255 - int(255 * cells[h][w]						)
																		_snake_case						:     Union[str, Any]				=    (color, color, color)
						return img


if __name__ == "__main__":
				__SCREAMING_SNAKE_CASE						:    Any     = bin(int(input('Rule:\n').strip()))[2:]
				__SCREAMING_SNAKE_CASE						:    Tuple     = format_ruleset(int(rule_num))
				for time in range(1_6):
								CELLS.append(new_generation(CELLS, rule, time))
				__SCREAMING_SNAKE_CASE						:    List[Any]     = generate_image(CELLS)
				# Uncomment to save the image
				# img.save(f"rule_{rule_num}.png")
				img.show()