






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





'''simple docstring'''

from hashlib import mda, shaaaa

lowerCAmelCase_			:			Union[str, Any]    	=   (shaaaa, mda)





class 				lowercase    :


   def __init__( self					:						Optional[Any] ,		__lowerCAmelCase					:						int = 8)   ->       None:
         lowercase_     			=   0B0
         lowercase_     			=   size


   def __UpperCAmelCase							( self					:						Dict ,		__lowerCAmelCase					:						str)   ->       None:
         lowercase_     			=   self.hash_(__lowerCAmelCase)
         self.bitarray |= h


   def __UpperCAmelCase							( self					:						Union[str, Any] ,		__lowerCAmelCase					:						str)   ->       bool:
         lowercase_     			=   self.hash_(__lowerCAmelCase)
         return (h & self.bitarray) == h


   def __contains__( self					:						str ,		__lowerCAmelCase					:						str)   ->       bool:
         return self.exists(__lowerCAmelCase)


   def __UpperCAmelCase							( self					:						Union[str, Any] ,		__lowerCAmelCase					:						int)   ->       str:
         lowercase_     			=   bin(__lowerCAmelCase)[2:]
         return res.zfill(self.size)


   @property
   def __UpperCAmelCase							( self					:						Dict)   ->       str:
         return self.format_bin(self.bitarray)


   def __UpperCAmelCase							( self					:						Any ,		__lowerCAmelCase					:						str)   ->       int:
         lowercase_     			=   0B0
         for func in HASH_FUNCTIONS:
               lowercase_     			=   (
                   int.from_bytes(func(value.encode()).digest() ,		"little") % self.size
               )
               res |= 2**position
         return res


   def __UpperCAmelCase							( self					:						Union[str, Any] ,		__lowerCAmelCase					:						str)   ->       str:
         return self.format_bin(self.hash_(__lowerCAmelCase))


   @property
   def __UpperCAmelCase							( self					:						str)   ->       float:
         lowercase_     			=   bin(self.bitarray).count("1")
         return (n_ones / self.size) ** len(__lowerCAmelCase)






