



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					:				Optional[int]   	=False


if is_vision_available():
		from PIL import Image
else:






		class 	_a			:






					@staticmethod
					def 							_snake_case   (    *lowercase_    ,  **lowercase_ )	->					Union[str, Any]:
											pass






@is_pipeline_test
@require_vision
class 	_a			(      unittest.TestCase  ):
			_UpperCamelCase:   List[str]	     =		MODEL_FOR_VISION_2_SEQ_MAPPING
			_UpperCamelCase:   Dict	     =		TF_MODEL_FOR_VISION_2_SEQ_MAPPING






			def 							_snake_case   (    self    ,  lowercase_    ,  lowercase_    ,  lowercase_ )	->					int:
									lowerCAmelCase	:   List[Any]					     =					pipeline("""image-to-text"""    ,  model=lowercase_    ,  tokenizer=lowercase_    ,  image_processor=lowercase_ )
									lowerCAmelCase	:   Optional[Any]					     =					[
									    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" ),
									    """./tests/fixtures/tests_samples/COCO/000000039769.png""",
									]
									return pipe, examples






			def 							_snake_case   (    self    ,  lowercase_    ,  lowercase_ )	->					List[Any]:
									lowerCAmelCase	:   Optional[Any]					     =					pipe(lowercase_ )
									self.assertEqual(
									    lowercase_    ,  [
									        [{"""generated_text""": ANY(lowercase_ )}],
									        [{"""generated_text""": ANY(lowercase_ )}],
									    ]    ,  )






			@require_tf
			def 							_snake_case   (    self )	->					int:
									lowerCAmelCase	:   Union[str, Any]					     =					pipeline("""image-to-text"""    ,  model="""hf-internal-testing/tiny-random-vit-gpt2"""    ,  framework="""tf""" )
									lowerCAmelCase	:   Union[str, Any]					     =					"""./tests/fixtures/tests_samples/COCO/000000039769.png"""

									lowerCAmelCase	:   Dict					     =					pipe(lowercase_ )
									self.assertEqual(
									    lowercase_    ,  [
									        {
									            """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									        },
									    ]    ,  )

									lowerCAmelCase	:   Any					     =					pipe([image, image] )
									self.assertEqual(
									    lowercase_    ,  [
									        [
									            {
									                """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									            }
									        ],
									        [
									            {
									                """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									            }
									        ],
									    ]    ,  )

									lowerCAmelCase	:   Optional[int]					     =					pipe(lowercase_    ,  max_new_tokens=1 )
									self.assertEqual(
									    lowercase_    ,  [{"""generated_text""": """growth"""}]    ,  )






			@require_torch
			def 							_snake_case   (    self )	->					Any:
									lowerCAmelCase	:   List[str]					     =					pipeline("""image-to-text"""    ,  model="""hf-internal-testing/tiny-random-vit-gpt2""" )
									lowerCAmelCase	:   Any					     =					"""./tests/fixtures/tests_samples/COCO/000000039769.png"""

									lowerCAmelCase	:   Tuple					     =					pipe(lowercase_ )
									self.assertEqual(
									    lowercase_    ,  [
									        {
									            """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									        },
									    ]    ,  )

									lowerCAmelCase	:   List[Any]					     =					pipe([image, image] )
									self.assertEqual(
									    lowercase_    ,  [
									        [
									            {
									                """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									            }
									        ],
									        [
									            {
									                """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
									            }
									        ],
									    ]    ,  )






			@require_torch
			def 							_snake_case   (    self )	->					List[str]:
									lowerCAmelCase	:   str					     =					pipeline("""image-to-text"""    ,  model="""hf-internal-testing/tiny-random-BlipForConditionalGeneration""" )
									lowerCAmelCase	:   Any					     =					"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
									lowerCAmelCase	:   List[Any]					     =					"""a photo of"""

									lowerCAmelCase	:   Any					     =					pipe(lowercase_    ,  prompt=lowercase_ )
									self.assertTrue(outputs[0]["""generated_text"""].startswith(lowercase_ ) )






			@slow
			@require_torch
			def 							_snake_case   (    self )	->					Union[str, Any]:
									lowerCAmelCase	:   Optional[Any]					     =					pipeline("""image-to-text"""    ,  model="""ydshieh/vit-gpt2-coco-en""" )
									lowerCAmelCase	:   Dict					     =					"""./tests/fixtures/tests_samples/COCO/000000039769.png"""

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

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

									lowerCAmelCase	:   List[Any]					     =					pipe(lowercase_ )
									self.assertEqual(lowercase_    ,  [{"""generated_text""": """a pink pokemon pokemon with a blue shirt and a blue shirt"""}] )






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

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






			@slow
			@require_torch
			def 							_snake_case   (    self )	->					List[str]:
									lowerCAmelCase	:   List[str]					     =					pipeline("""image-to-text"""    ,  model="""Salesforce/blip-image-captioning-base""" )
									lowerCAmelCase	:   Tuple					     =					"""https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"""
									lowerCAmelCase	:   List[str]					     =					Image.open(requests.get(lowercase_    ,  stream=lowercase_ ).raw )

									lowerCAmelCase	:   Dict					     =					"""a photography of"""

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

									with self.assertRaises(lowercase_ ):
															lowerCAmelCase	:   Any					     =					pipe([image, image]    ,  prompt=[prompt, prompt] )






			@slow
			@require_torch
			def 							_snake_case   (    self )	->					Optional[Any]:
									lowerCAmelCase	:   List[str]					     =					pipeline("""image-to-text"""    ,  model="""microsoft/git-base-coco""" )
									lowerCAmelCase	:   Union[str, Any]					     =					"""https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"""
									lowerCAmelCase	:   Dict					     =					Image.open(requests.get(lowercase_    ,  stream=lowercase_ ).raw )

									lowerCAmelCase	:   Tuple					     =					"""a photo of a"""

									lowerCAmelCase	:   Union[str, Any]					     =					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_ ):
															lowerCAmelCase	:   str					     =					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 							_snake_case   (    self )	->					str:
									lowerCAmelCase	:   str					     =					pipeline("""image-to-text"""    ,  model="""google/pix2struct-ai2d-base""" )
									lowerCAmelCase	:   Dict					     =					"""https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"""
									lowerCAmelCase	:   Tuple					     =					Image.open(requests.get(lowercase_    ,  stream=lowercase_ ).raw )

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

									lowerCAmelCase	:   Dict					     =					pipe(lowercase_    ,  prompt=lowercase_ )
									self.assertEqual(lowercase_    ,  [{"""generated_text""": """ash cloud"""}] )

									with self.assertRaises(lowercase_ ):
															lowerCAmelCase	:   Optional[Any]					     =					pipe([image, image]    ,  prompt=[prompt, prompt] )






			@slow
			@require_tf
			def 							_snake_case   (    self )	->					Tuple:
									lowerCAmelCase	:   Union[str, Any]					     =					pipeline("""image-to-text"""    ,  model="""ydshieh/vit-gpt2-coco-en"""    ,  framework="""tf""" )
									lowerCAmelCase	:   Any					     =					"""./tests/fixtures/tests_samples/COCO/000000039769.png"""

									lowerCAmelCase	:   Optional[int]					     =					pipe(lowercase_ )
									self.assertEqual(lowercase_    ,  [{"""generated_text""": """a cat laying on a blanket next to a cat laying on a bed """}] )

									lowerCAmelCase	:   List[Any]					     =					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 """}],
									    ]    ,  )






def  _UpperCAmelCase  (				SCREAMING_SNAKE_CASE__  ):


						'''simple docstring'''




						lowerCAmelCase	:   List[str]					     =					str(SCREAMING_SNAKE_CASE__  ).strip()
						if not bin_string:
												raise ValueError("""Empty string was passed to the function"""  )
						lowerCAmelCase	:   Optional[int]					     =					bin_string[0] == """-"""
						if is_negative:
												lowerCAmelCase	:   Tuple					     =					bin_string[1:]
						if not all(char in """01""" for char in bin_string  ):
												raise ValueError("""Non-binary value was passed to the function"""  )
						lowerCAmelCase	:   Union[str, Any]					     =					0
						for char in bin_string:
												lowerCAmelCase	:   int					     =					2 * decimal_number + int(SCREAMING_SNAKE_CASE__  )
						return -decimal_number if is_negative else decimal_number


if __name__ == "__main__":
		from doctest import testmod

		testmod()

