'''simple docstring'''

import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding




class 		SCREAMING_SNAKE_CASE_					(_UpperCAmelCase      ):



			"""simple docstring"""






			__magic_name__ :				Tuple   							=       ["""image_processor""", """tokenizer"""]
			__magic_name__ :				List[str]   							=       """AutoImageProcessor"""
			__magic_name__ :				List[str]   							=       """AutoTokenizer"""



			def __init__(							self				,			snake_case_=None				,			snake_case_=None				,			**snake_case_					):
									snake_case :       Any				=    None
									if "feature_extractor" in kwargs:
															warnings.warn(
															    "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
															    " instead."				,			snake_case_				,			)
															snake_case :       str				=    kwargs.pop("feature_extractor"					)

									snake_case :       Optional[int]				=    image_processor if image_processor is not None else feature_extractor
									if image_processor is None:
															raise ValueError("You have to specify an image_processor."					)
									if tokenizer is None:
															raise ValueError("You have to specify a tokenizer."					)

									super().__init__(snake_case_				,			snake_case_					)
									snake_case :       Optional[int]				=    self.image_processor



			def __call__(							self				,			snake_case_=None				,			snake_case_=None				,			snake_case_=None				,			**snake_case_					):

									if text is None and images is None:
															raise ValueError("You have to specify either text or images. Both cannot be none."					)

									if text is not None:
															snake_case :       Optional[int]				=    self.tokenizer(snake_case_				,			return_tensors=snake_case_				,			**snake_case_					)

									if images is not None:
															snake_case :       List[Any]				=    self.image_processor(snake_case_				,			return_tensors=snake_case_				,			**snake_case_					)

									if text is not None and images is not None:
															snake_case :       str				=    image_features.pixel_values
															return encoding
									elif text is not None:
															return encoding
									else:
															return BatchEncoding(data=dict(**snake_case_					)				,			tensor_type=snake_case_					)



			def   lowerCamelCase (							self				,			*snake_case_				,			**snake_case_					):
									return self.tokenizer.batch_decode(*snake_case_				,			**snake_case_					)



			def   lowerCamelCase (							self				,			*snake_case_				,			**snake_case_					):
									return self.tokenizer.decode(*snake_case_				,			**snake_case_					)



			@property
			def   lowerCamelCase (							self					):
									snake_case :       List[str]				=    self.tokenizer.model_input_names
									snake_case :       Any				=    self.image_processor.model_input_names
									return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names					)					)



			@property
			def   lowerCamelCase (							self					):
									warnings.warn(
									    "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."				,			snake_case_				,			)
									return self.image_processor_class






			@property
			def   lowerCamelCase (							self					):
									warnings.warn(
									    "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."				,			snake_case_				,			)
									return self.image_processor






'''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:
						A_	   = False


if is_vision_available():
						from PIL import Image
else:




						class 		SCREAMING_SNAKE_CASE_					:



									"""simple docstring"""






									@staticmethod
									def   lowerCamelCase (							*snake_case_				,			**snake_case_					):
															pass




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



			"""simple docstring"""






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



			def   lowerCamelCase (							self				,			snake_case_				,			snake_case_				,			snake_case_					):
									snake_case :       str				=    pipeline("image-to-text"				,			model=snake_case_				,			tokenizer=snake_case_				,			image_processor=snake_case_					)
									snake_case :       List[Any]				=    [
									    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					),
									    "./tests/fixtures/tests_samples/COCO/000000039769.png",
									]
									return pipe, examples



			def   lowerCamelCase (							self				,			snake_case_				,			snake_case_					):
									snake_case :       Union[str, Any]				=    pipe(snake_case_					)
									self.assertEqual(
									    snake_case_				,			[
									        [{"generated_text": ANY(snake_case_					)}],
									        [{"generated_text": ANY(snake_case_					)}],
									    ]				,			)



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

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

									snake_case :       Tuple				=    pipe([image, image]					)
									self.assertEqual(
									    snake_case_				,			[
									        [
									            {
									                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
									            }
									        ],
									        [
									            {
									                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
									            }
									        ],
									    ]				,			)

									snake_case :       List[Any]				=    pipe(snake_case_				,			max_new_tokens=1					)
									self.assertEqual(
									    snake_case_				,			[{"generated_text": "growth"}]				,			)



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

									snake_case :       Any				=    pipe(snake_case_					)
									self.assertEqual(
									    snake_case_				,			[
									        {
									            "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
									        },
									    ]				,			)

									snake_case :       str				=    pipe([image, image]					)
									self.assertEqual(
									    snake_case_				,			[
									        [
									            {
									                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
									            }
									        ],
									        [
									            {
									                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
									            }
									        ],
									    ]				,			)



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

									snake_case :       List[str]				=    pipe(snake_case_				,			prompt=snake_case_					)
									self.assertTrue(outputs[0]["generated_text"].startswith(snake_case_					)					)



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

									snake_case :       Tuple				=    pipe(snake_case_					)
									self.assertEqual(snake_case_				,			[{"generated_text": "a cat laying on a blanket next to a cat laying on a bed "}]					)

									snake_case :       Tuple				=    pipe([image, image]					)
									self.assertEqual(
									    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					):
									snake_case :       Optional[Any]				=    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 :       Union[str, Any]				=    Image.open(requests.get(snake_case_				,			stream=snake_case_					).raw					)

									snake_case :       Optional[Any]				=    pipe(snake_case_					)
									self.assertEqual(snake_case_				,			[{"generated_text": "a pink pokemon pokemon with a blue shirt and a blue shirt"}]					)



			@slow
			@require_torch
			def   lowerCamelCase (							self					):
									snake_case :       Tuple				=    pipeline("image-to-text"				,			model="microsoft/git-base-coco"					)
									snake_case :       Tuple				=    "https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
									snake_case :       Tuple				=    Image.open(requests.get(snake_case_				,			stream=snake_case_					).raw					)

									snake_case :       Any				=    pipe(snake_case_					)
									self.assertEqual(snake_case_				,			[{"generated_text": "a cartoon of a purple character."}]					)



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

									snake_case :       str				=    "a photography of"

									snake_case :       int				=    pipe(snake_case_				,			prompt=snake_case_					)
									self.assertEqual(snake_case_				,			[{"generated_text": "a photography of a volcano"}]					)

									with self.assertRaises(snake_case_					):
															snake_case :       List[Any]				=    pipe([image, image]				,			prompt=[prompt, prompt]					)



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

									snake_case :       Any				=    "a photo of a"

									snake_case :       Dict				=    pipe(snake_case_				,			prompt=snake_case_					)
									self.assertEqual(snake_case_				,			[{"generated_text": "a photo of a tent with a tent and a tent in the background."}]					)

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

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

									snake_case :       str				=    pipe(snake_case_				,			prompt=snake_case_					)
									self.assertEqual(snake_case_				,			[{"generated_text": "ash cloud"}]					)

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






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

									snake_case :       Any				=    pipe(snake_case_					)
									self.assertEqual(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(
									    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 "}],
									    ]				,			)





