






"""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:
       lowercase_								=	False


if is_vision_available():
       from PIL import Image
else:


       class    snake_case						:


          '''simple docstring'''

          @staticmethod
          def 							_SCREAMING_SNAKE_CASE							(      *_lowerCamelCase					:	str,			**_lowerCamelCase					:	int    ):

            '''simple docstring'''
            pass


@is_pipeline_test
@require_vision
class    snake_case						(				unittest.TestCase		):


   '''simple docstring'''

   A_		:      Optional[int]							=					MODEL_FOR_VISION_2_SEQ_MAPPING
   A_		:      Optional[int]							=					TF_MODEL_FOR_VISION_2_SEQ_MAPPING


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[int],			_lowerCamelCase					:	int,			_lowerCamelCase					:	Optional[Any],			_lowerCamelCase					:	Union[str, Any]    ):

     '''simple docstring'''
     __A		     =					pipeline('''image-to-text''',			model=_lowerCamelCase,			tokenizer=_lowerCamelCase,			image_processor=_lowerCamelCase    )
     __A		     =					[
         Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''    ),
         '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
     ]
     return pipe, examples


   def 							_SCREAMING_SNAKE_CASE							(      self					:	List[str],			_lowerCamelCase					:	Optional[Any],			_lowerCamelCase					:	Optional[int]    ):

     '''simple docstring'''
     __A		     =					pipe(_lowerCamelCase    )
     self.assertEqual(
         _lowerCamelCase,			[
             [{'''generated_text''': ANY(_lowerCamelCase    )}],
             [{'''generated_text''': ANY(_lowerCamelCase    )}],
         ],			)


   @require_tf
   def 							_SCREAMING_SNAKE_CASE							(      self					:	str    ):

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

     __A		     =					pipe(_lowerCamelCase    )
     self.assertEqual(
         _lowerCamelCase,			[
             {
                 '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
             },
         ],			)

     __A		     =					pipe([image, image]    )
     self.assertEqual(
         _lowerCamelCase,			[
             [
                 {
                     '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                 }
             ],
             [
                 {
                     '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                 }
             ],
         ],			)

     __A		     =					pipe(_lowerCamelCase,			max_new_tokens=1    )
     self.assertEqual(
         _lowerCamelCase,			[{'''generated_text''': '''growth'''}],			)


   @require_torch
   def 							_SCREAMING_SNAKE_CASE							(      self					:	str    ):

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

     __A		     =					pipe(_lowerCamelCase    )
     self.assertEqual(
         _lowerCamelCase,			[
             {
                 '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
             },
         ],			)

     __A		     =					pipe([image, image]    )
     self.assertEqual(
         _lowerCamelCase,			[
             [
                 {
                     '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                 }
             ],
             [
                 {
                     '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                 }
             ],
         ],			)


   @require_torch
   def 							_SCREAMING_SNAKE_CASE							(      self					:	Dict    ):

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

     __A		     =					pipe(_lowerCamelCase,			prompt=_lowerCamelCase    )
     self.assertTrue(outputs[0]['''generated_text'''].startswith(_lowerCamelCase    )    )


   @slow
   @require_torch
   def 							_SCREAMING_SNAKE_CASE							(      self					:	Dict    ):

     '''simple docstring'''
     __A		     =					pipeline('''image-to-text''',			model='''ydshieh/vit-gpt2-coco-en'''    )
     __A		     =					'''./tests/fixtures/tests_samples/COCO/000000039769.png'''

     __A		     =					pipe(_lowerCamelCase    )
     self.assertEqual(_lowerCamelCase,			[{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}]    )

     __A		     =					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 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[Any]    ):

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

     __A		     =					pipe(_lowerCamelCase    )
     self.assertEqual(_lowerCamelCase,			[{'''generated_text''': '''a pink pokemon pokemon with a blue shirt and a blue shirt'''}]    )


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

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

     __A		     =					pipe(_lowerCamelCase    )
     self.assertEqual(_lowerCamelCase,			[{'''generated_text''': '''a cartoon of a purple character.'''}]    )


   @slow
   @require_torch
   def 							_SCREAMING_SNAKE_CASE							(      self					:	Any    ):

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

     __A		     =					'''a photography of'''

     __A		     =					pipe(_lowerCamelCase,			prompt=_lowerCamelCase    )
     self.assertEqual(_lowerCamelCase,			[{'''generated_text''': '''a photography of a volcano'''}]    )

     with self.assertRaises(_lowerCamelCase    ):
       __A		     =					pipe([image, image],			prompt=[prompt, prompt]    )


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

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

     __A		     =					'''a photo of a'''

     __A		     =					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    ):
       __A		     =					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 							_SCREAMING_SNAKE_CASE							(      self					:	Optional[Any]    ):

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

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

     __A		     =					pipe(_lowerCamelCase,			prompt=_lowerCamelCase    )
     self.assertEqual(_lowerCamelCase,			[{'''generated_text''': '''ash cloud'''}]    )

     with self.assertRaises(_lowerCamelCase    ):
       __A		     =					pipe([image, image],			prompt=[prompt, prompt]    )






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

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

     __A		     =					pipe(_lowerCamelCase    )
     self.assertEqual(_lowerCamelCase,			[{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}]    )

     __A		     =					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 '''}],
         ],			)











from itertools import compress, repeat
from math import ceil, sqrt




def 			lowerCAmelCase_			(				UpperCamelCase_	)  ->		list[int]:

       if num <= 2:
              return []
       if num == 3:
              return [2]

       # Odd sieve for numbers in range [3, num - 1]
       UpperCamelCase_ 							=	bytearray(b"\x01"	) * ((num >> 1) - 1)

       for i in range(3       ,		int(sqrt(UpperCamelCase_	)	) + 1       ,		2	):
              if sieve[(i >> 1) - 1]:
                     UpperCamelCase_ 							=	i**2
                     UpperCamelCase_ 							=	repeat(
                         0       ,		ceil((num - i_squared) / (i << 1)	)	)

       return [2] + list(compress(range(3       ,		UpperCamelCase_       ,		2	)       ,		UpperCamelCase_	)	)


if __name__ == "__main__":
     import doctest

     doctest.testmod()

