




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


					'''simple docstring'''


					@staticmethod
					def  _lowerCamelCase   (				*SCREAMING_SNAKE_CASE_				:       Any ,**SCREAMING_SNAKE_CASE_				:       Any ):
											pass

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


				'''simple docstring'''


				_lowerCAmelCase		     =MODEL_FOR_VISION_2_SEQ_MAPPING
				_lowerCAmelCase		     =TF_MODEL_FOR_VISION_2_SEQ_MAPPING

				def  _lowerCamelCase   (				self				:       Dict ,SCREAMING_SNAKE_CASE_				:       Optional[int] ,SCREAMING_SNAKE_CASE_				:       Optional[int] ,SCREAMING_SNAKE_CASE_				:       str ):
										snake_case__    :   Dict							     =   pipeline('''image-to-text''' ,model=SCREAMING_SNAKE_CASE_ ,tokenizer=SCREAMING_SNAKE_CASE_ ,image_processor=SCREAMING_SNAKE_CASE_ )
										snake_case__    :   int							     =   [
										    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' ),
										    '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
										]
										return pipe, examples

				def  _lowerCamelCase   (				self				:       Optional[int] ,SCREAMING_SNAKE_CASE_				:       Union[str, Any] ,SCREAMING_SNAKE_CASE_				:       Optional[Any] ):
										snake_case__    :   List[Any]							     =   pipe(SCREAMING_SNAKE_CASE_ )
										self.assertEqual(
										    SCREAMING_SNAKE_CASE_ ,[
										        [{'''generated_text''': ANY(SCREAMING_SNAKE_CASE_ )}],
										        [{'''generated_text''': ANY(SCREAMING_SNAKE_CASE_ )}],
										    ] ,)

				@require_tf
				def  _lowerCamelCase   (				self				:       Any ):
										snake_case__    :   List[str]							     =   pipeline('''image-to-text''' ,model='''hf-internal-testing/tiny-random-vit-gpt2''' ,framework='''tf''' )
										snake_case__    :   Any							     =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

										snake_case__    :   Dict							     =   pipe(SCREAMING_SNAKE_CASE_ )
										self.assertEqual(
										    SCREAMING_SNAKE_CASE_ ,[
										        {
										            '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										        },
										    ] ,)

										snake_case__    :   Any							     =   pipe([image, image] )
										self.assertEqual(
										    SCREAMING_SNAKE_CASE_ ,[
										        [
										            {
										                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										            }
										        ],
										        [
										            {
										                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										            }
										        ],
										    ] ,)

										snake_case__    :   int							     =   pipe(SCREAMING_SNAKE_CASE_ ,max_new_tokens=1 )
										self.assertEqual(
										    SCREAMING_SNAKE_CASE_ ,[{'''generated_text''': '''growth'''}] ,)

				@require_torch
				def  _lowerCamelCase   (				self				:       int ):
										snake_case__    :   Dict							     =   pipeline('''image-to-text''' ,model='''hf-internal-testing/tiny-random-vit-gpt2''' )
										snake_case__    :   List[Any]							     =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

										snake_case__    :   Dict							     =   pipe(SCREAMING_SNAKE_CASE_ )
										self.assertEqual(
										    SCREAMING_SNAKE_CASE_ ,[
										        {
										            '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										        },
										    ] ,)

										snake_case__    :   Optional[int]							     =   pipe([image, image] )
										self.assertEqual(
										    SCREAMING_SNAKE_CASE_ ,[
										        [
										            {
										                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										            }
										        ],
										        [
										            {
										                '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
										            }
										        ],
										    ] ,)

				@require_torch
				def  _lowerCamelCase   (				self				:       Optional[Any] ):
										snake_case__    :   List[Any]							     =   pipeline('''image-to-text''' ,model='''hf-internal-testing/tiny-random-BlipForConditionalGeneration''' )
										snake_case__    :   Union[str, Any]							     =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''
										snake_case__    :   Tuple							     =   '''a photo of'''

										snake_case__    :   Optional[int]							     =   pipe(SCREAMING_SNAKE_CASE_ ,prompt=SCREAMING_SNAKE_CASE_ )
										self.assertTrue(outputs[0]['''generated_text'''].startswith(SCREAMING_SNAKE_CASE_ ) )

				@slow
				@require_torch
				def  _lowerCamelCase   (				self				:       List[Any] ):
										snake_case__    :   Optional[int]							     =   pipeline('''image-to-text''' ,model='''ydshieh/vit-gpt2-coco-en''' )
										snake_case__    :   Union[str, Any]							     =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

										snake_case__    :   Union[str, Any]							     =   pipe(SCREAMING_SNAKE_CASE_ )
										self.assertEqual(SCREAMING_SNAKE_CASE_ ,[{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}] )

										snake_case__    :   Optional[int]							     =   pipe([image, image] )
										self.assertEqual(
										    SCREAMING_SNAKE_CASE_ ,[
										        [{'''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				:       List[Any] ):
										snake_case__    :   Dict							     =   pipeline('''image-to-text''' ,model='''Salesforce/blip-image-captioning-base''' )
										snake_case__    :   Any							     =   '''https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'''
										snake_case__    :   List[Any]							     =   Image.open(requests.get(SCREAMING_SNAKE_CASE_ ,stream=SCREAMING_SNAKE_CASE_ ).raw )

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

				@slow
				@require_torch
				def  _lowerCamelCase   (				self				:       Optional[Any] ):
										snake_case__    :   Dict							     =   pipeline('''image-to-text''' ,model='''microsoft/git-base-coco''' )
										snake_case__    :   List[str]							     =   '''https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'''
										snake_case__    :   int							     =   Image.open(requests.get(SCREAMING_SNAKE_CASE_ ,stream=SCREAMING_SNAKE_CASE_ ).raw )

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

				@slow
				@require_torch
				def  _lowerCamelCase   (				self				:       Optional[int] ):
										snake_case__    :   Optional[Any]							     =   pipeline('''image-to-text''' ,model='''Salesforce/blip-image-captioning-base''' )
										snake_case__    :   Any							     =   '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
										snake_case__    :   int							     =   Image.open(requests.get(SCREAMING_SNAKE_CASE_ ,stream=SCREAMING_SNAKE_CASE_ ).raw )

										snake_case__    :   Union[str, Any]							     =   '''a photography of'''

										snake_case__    :   Union[str, Any]							     =   pipe(SCREAMING_SNAKE_CASE_ ,prompt=SCREAMING_SNAKE_CASE_ )
										self.assertEqual(SCREAMING_SNAKE_CASE_ ,[{'''generated_text''': '''a photography of a volcano'''}] )

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

				@slow
				@require_torch
				def  _lowerCamelCase   (				self				:       str ):
										snake_case__    :   Union[str, Any]							     =   pipeline('''image-to-text''' ,model='''microsoft/git-base-coco''' )
										snake_case__    :   Tuple							     =   '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
										snake_case__    :   Optional[int]							     =   Image.open(requests.get(SCREAMING_SNAKE_CASE_ ,stream=SCREAMING_SNAKE_CASE_ ).raw )

										snake_case__    :   Dict							     =   '''a photo of a'''

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

										with self.assertRaises(SCREAMING_SNAKE_CASE_ ):
																snake_case__    :   Union[str, Any]							     =   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				:       List[Any] ):
										snake_case__    :   Optional[int]							     =   pipeline('''image-to-text''' ,model='''google/pix2struct-ai2d-base''' )
										snake_case__    :   Union[str, Any]							     =   '''https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'''
										snake_case__    :   Dict							     =   Image.open(requests.get(SCREAMING_SNAKE_CASE_ ,stream=SCREAMING_SNAKE_CASE_ ).raw )

										snake_case__    :   List[str]							     =   '''What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'''

										snake_case__    :   Dict							     =   pipe(SCREAMING_SNAKE_CASE_ ,prompt=SCREAMING_SNAKE_CASE_ )
										self.assertEqual(SCREAMING_SNAKE_CASE_ ,[{'''generated_text''': '''ash cloud'''}] )

										with self.assertRaises(SCREAMING_SNAKE_CASE_ ):
																snake_case__    :   List[str]							     =   pipe([image, image] ,prompt=[prompt, prompt] )





				@slow
				@require_tf
				def  _lowerCamelCase   (				self				:       List[str] ):
										snake_case__    :   Tuple							     =   pipeline('''image-to-text''' ,model='''ydshieh/vit-gpt2-coco-en''' ,framework='''tf''' )
										snake_case__    :   Optional[int]							     =   '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

										snake_case__    :   List[Any]							     =   pipe(SCREAMING_SNAKE_CASE_ )
										self.assertEqual(SCREAMING_SNAKE_CASE_ ,[{'''generated_text''': '''a cat laying on a blanket next to a cat laying on a bed '''}] )

										snake_case__    :   Union[str, Any]							     =   pipe([image, image] )
										self.assertEqual(
										    SCREAMING_SNAKE_CASE_ ,[
										        [{'''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"""





class  __lowercase    :


				'''simple docstring'''


				class  __lowercase    :


								'''simple docstring'''


								_lowerCAmelCase		     ="_prev", "_data", "_next"

								def __init__(				self				:       List[Any] ,SCREAMING_SNAKE_CASE_				:       int ,SCREAMING_SNAKE_CASE_				:       Union[str, Any] ,SCREAMING_SNAKE_CASE_				:       Any ):
														snake_case__    :   Any							     =   link_p
														snake_case__    :   List[str]							     =   element
														snake_case__    :   str							     =   link_n





								def  _lowerCamelCase   (				self				:       Optional[Any] ):
														return (
														    F''' Prev -> {self._prev is not None}, Next -> {self._next is not None}'''
														)







				def __init__(				self				:       Any ):
										snake_case__    :   int							     =   self._Node(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Optional[int]							     =   self._Node(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Union[str, Any]							     =   self._trailer
										snake_case__    :   Union[str, Any]							     =   self._header
										snake_case__    :   Any							     =   0

				def __len__(				self				:       str ):
										return self._size

				def  _lowerCamelCase   (				self				:       Union[str, Any] ):
										return self.__len__() == 0

				def  _lowerCamelCase   (				self				:       Any ,SCREAMING_SNAKE_CASE_				:       List[Any] ,SCREAMING_SNAKE_CASE_				:       List[str] ,SCREAMING_SNAKE_CASE_				:       Dict ):
										# Create new_node by setting it's prev.link -> header
										# setting it's next.link -> trailer
										snake_case__    :   Dict							     =   self._Node(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ )
										snake_case__    :   Any							     =   new_node
										snake_case__    :   str							     =   new_node
										self._size += 1
										return self





				def  _lowerCamelCase   (				self				:       Union[str, Any] ,SCREAMING_SNAKE_CASE_				:       Dict ):
										snake_case__    :   Union[str, Any]							     =   node._prev
										snake_case__    :   Union[str, Any]							     =   node._next

										snake_case__    :   List[Any]							     =   successor
										snake_case__    :   Dict							     =   predecessor
										self._size -= 1
										snake_case__    :   Dict							     =   node._data
										snake_case__    :   List[str]							     =   None
										del node
										return temp







class  __lowercase    (     _DoublyLinkedBase		):


				'''simple docstring'''


				def  _lowerCamelCase   (				self				:       Optional[int] ):
										if self.is_empty():
																raise Exception('''List is empty''' )
										return self._header._next._data

				def  _lowerCamelCase   (				self				:       Union[str, Any] ):
										if self.is_empty():
																raise Exception('''List is empty''' )
										return self._trailer._prev._data

				def  _lowerCamelCase   (				self				:       Any ,SCREAMING_SNAKE_CASE_				:       Dict ):
										return self._insert(self._header ,SCREAMING_SNAKE_CASE_ ,self._header._next )

				def  _lowerCamelCase   (				self				:       Dict ,SCREAMING_SNAKE_CASE_				:       Union[str, Any] ):
										return self._insert(self._trailer._prev ,SCREAMING_SNAKE_CASE_ ,self._trailer )

				def  _lowerCamelCase   (				self				:       Optional[int] ):
										if self.is_empty():
																raise IndexError('''remove_first from empty list''' )
										return self._delete(self._header._next )





				def  _lowerCamelCase   (				self				:       Optional[Any] ):
										if self.is_empty():
																raise IndexError('''remove_first from empty list''' )
										return self._delete(self._trailer._prev )


