

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


if is_vision_available():
    from PIL import Image
else:

    class     __a   :

       """simple docstring"""

       @staticmethod
       def    _lowerCAmelCase			(							*lowercase_     :					Any						,  **lowercase_     :					List[Any]						):
            pass

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

   """simple docstring"""

   SCREAMING_SNAKE_CASE_						     =						MODEL_FOR_VISION_2_SEQ_MAPPING
   SCREAMING_SNAKE_CASE_						     =						TF_MODEL_FOR_VISION_2_SEQ_MAPPING






   def    _lowerCAmelCase			(							self     :					int						,  lowercase_     :					Union[str, Any]						,  lowercase_     :					Optional[int]						,  lowercase_     :					Union[str, Any]						):
        UpperCamelCase__		:    List[str]         =pipeline('''image-to-text'''						,  model=lowercase_						,  tokenizer=lowercase_						,  image_processor=lowercase_						)
        UpperCamelCase__		:    str         =[
            Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						),
            './tests/fixtures/tests_samples/COCO/000000039769.png',
        ]
        return pipe, examples






   def    _lowerCAmelCase			(							self     :					List[Any]						,  lowercase_     :					Any						,  lowercase_     :					List[str]						):
        UpperCamelCase__		:    str         =pipe(lowercase_						)
        self.assertEqual(
            lowercase_						,  [
                [{'''generated_text''': ANY(lowercase_						)}],
                [{'''generated_text''': ANY(lowercase_						)}],
            ]						,  )






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

        UpperCamelCase__		:    Optional[int]         =pipe(lowercase_						)
        self.assertEqual(
            lowercase_						,  [
                {
                    '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                },
            ]						,  )

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

        UpperCamelCase__		:    str         =pipe(lowercase_						,  max_new_tokens=1						)
        self.assertEqual(
            lowercase_						,  [{'''generated_text''': '''growth'''}]						,  )






   @require_torch
   def    _lowerCAmelCase			(							self     :					Tuple						):
        UpperCamelCase__		:    Union[str, Any]         =pipeline('''image-to-text'''						,  model='''hf-internal-testing/tiny-random-vit-gpt2'''						)
        UpperCamelCase__		:    Any         ='./tests/fixtures/tests_samples/COCO/000000039769.png'

        UpperCamelCase__		:    int         =pipe(lowercase_						)
        self.assertEqual(
            lowercase_						,  [
                {
                    '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                },
            ]						,  )

        UpperCamelCase__		:    Tuple         =pipe([image, image]						)
        self.assertEqual(
            lowercase_						,  [
                [
                    {
                        '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                    }
                ],
                [
                    {
                        '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                    }
                ],
            ]						,  )






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

        UpperCamelCase__		:    Dict         =pipe(lowercase_						,  prompt=lowercase_						)
        self.assertTrue(outputs[0]['''generated_text'''].startswith(lowercase_						)						)






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

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

        UpperCamelCase__		:    Union[str, 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 '''}],
            ]						,  )






   @slow
   @require_torch
   def    _lowerCAmelCase			(							self     :					List[Any]						):
        UpperCamelCase__		:    int         =pipeline('''image-to-text'''						,  model='''Salesforce/blip-image-captioning-base'''						)
        UpperCamelCase__		:    Tuple         ='https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
        UpperCamelCase__		:    List[Any]         =Image.open(requests.get(lowercase_						,  stream=lowercase_						).raw						)

        UpperCamelCase__		:    Optional[Any]         =pipe(lowercase_						)
        self.assertEqual(lowercase_						,  [{'''generated_text''': '''a pink pokemon pokemon with a blue shirt and a blue shirt'''}]						)






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

        UpperCamelCase__		:    Optional[int]         =pipe(lowercase_						)
        self.assertEqual(lowercase_						,  [{'''generated_text''': '''a cartoon of a purple character.'''}]						)






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

        UpperCamelCase__		:    int         ='a photography of'

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

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






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

        UpperCamelCase__		:    Optional[int]         ='a photo of a'

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

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

        UpperCamelCase__		:    Union[str, Any]         =pipe(lowercase_						,  prompt=lowercase_						)
        self.assertEqual(lowercase_						,  [{'''generated_text''': '''ash cloud'''}]						)

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






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

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

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





"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_SCREAMING_SNAKE_CASE       :  List[Any]        = {"""configuration_t5""": ["""T5_PRETRAINED_CONFIG_ARCHIVE_MAP""", """T5Config""", """T5OnnxConfig"""]}

try:
				if not is_sentencepiece_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_SCREAMING_SNAKE_CASE       :  Optional[Any]        = ["""T5Tokenizer"""]

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_SCREAMING_SNAKE_CASE       :  str        = ["""T5TokenizerFast"""]

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_SCREAMING_SNAKE_CASE       :  Dict        = [
				    """T5_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """T5EncoderModel""",
				    """T5ForConditionalGeneration""",
				    """T5Model""",
				    """T5PreTrainedModel""",
				    """load_tf_weights_in_t5""",
				    """T5ForQuestionAnswering""",
				]

try:
				if not is_tf_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_SCREAMING_SNAKE_CASE       :  Dict        = [
				    """TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """TFT5EncoderModel""",
				    """TFT5ForConditionalGeneration""",
				    """TFT5Model""",
				    """TFT5PreTrainedModel""",
				]

try:
				if not is_flax_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_SCREAMING_SNAKE_CASE       :  Any        = [
				    """FlaxT5EncoderModel""",
				    """FlaxT5ForConditionalGeneration""",
				    """FlaxT5Model""",
				    """FlaxT5PreTrainedModel""",
				]


if TYPE_CHECKING:
				from .configuration_ta import T5_PRETRAINED_CONFIG_ARCHIVE_MAP, TaConfig, TaOnnxConfig

				try:
								if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_ta import TaTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_ta_fast import TaTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_ta import (
								    T5_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TaEncoderModel,
								    TaForConditionalGeneration,
								    TaForQuestionAnswering,
								    TaModel,
								    TaPreTrainedModel,
								    load_tf_weights_in_ta,
								)

				try:
								if not is_tf_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_tf_ta import (
								    TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST,
								    TFTaEncoderModel,
								    TFTaForConditionalGeneration,
								    TFTaModel,
								    TFTaPreTrainedModel,
								)

				try:
								if not is_flax_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_flax_ta import (
								    FlaxTaEncoderModel,
								    FlaxTaForConditionalGeneration,
								    FlaxTaModel,
								    FlaxTaPreTrainedModel,
								)


else:
				import sys

				_SCREAMING_SNAKE_CASE       :  str        = _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


