
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_				:							int       =    False


if is_vision_available():
 from PIL import Image
else:







 class  _lowerCamelCase			:

      @staticmethod
      def 						__lowerCamelCase				(  *SCREAMING_SNAKE_CASE_							,       **SCREAMING_SNAKE_CASE_    ):
             pass







@is_pipeline_test
@require_vision
class  _lowerCamelCase			(unittest.TestCase					):
     lowercase__				  =			MODEL_FOR_VISION_2_SEQ_MAPPING
     lowercase__				  =			TF_MODEL_FOR_VISION_2_SEQ_MAPPING

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

     def 						__lowerCamelCase				(  self							,       SCREAMING_SNAKE_CASE_							,       SCREAMING_SNAKE_CASE_    ):
            __snake_case						       =   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    ):
            __snake_case						       =   pipeline('image-to-text'							,       model='hf-internal-testing/tiny-random-vit-gpt2'							,       framework='tf'    )
            __snake_case						       =   './tests/fixtures/tests_samples/COCO/000000039769.png'

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

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

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

     @require_torch
     def 						__lowerCamelCase				(  self    ):
            __snake_case						       =   pipeline('image-to-text'							,       model='hf-internal-testing/tiny-random-vit-gpt2'    )
            __snake_case						       =   './tests/fixtures/tests_samples/COCO/000000039769.png'

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

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

     @require_torch
     def 						__lowerCamelCase				(  self    ):
            __snake_case						       =   pipeline('image-to-text'							,       model='hf-internal-testing/tiny-random-BlipForConditionalGeneration'    )
            __snake_case						       =   './tests/fixtures/tests_samples/COCO/000000039769.png'
            __snake_case						       =   'a photo of'

            __snake_case						       =   pipe(SCREAMING_SNAKE_CASE_							,       prompt=SCREAMING_SNAKE_CASE_    )
            self.assertTrue(outputs[0]['generated_text'].startswith(SCREAMING_SNAKE_CASE_    )    )

     @slow
     @require_torch
     def 						__lowerCamelCase				(  self    ):
            __snake_case						       =   pipeline('image-to-text'							,       model='ydshieh/vit-gpt2-coco-en'    )
            __snake_case						       =   './tests/fixtures/tests_samples/COCO/000000039769.png'

            __snake_case						       =   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						       =   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    ):
            __snake_case						       =   pipeline('image-to-text'							,       model='Salesforce/blip-image-captioning-base'    )
            __snake_case						       =   'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
            __snake_case						       =   Image.open(requests.get(SCREAMING_SNAKE_CASE_							,       stream=SCREAMING_SNAKE_CASE_    ).raw    )

            __snake_case						       =   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    ):
            __snake_case						       =   pipeline('image-to-text'							,       model='microsoft/git-base-coco'    )
            __snake_case						       =   'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
            __snake_case						       =   Image.open(requests.get(SCREAMING_SNAKE_CASE_							,       stream=SCREAMING_SNAKE_CASE_    ).raw    )

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

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

            __snake_case						       =   'a photography of'

            __snake_case						       =   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						       =   pipe([image, image]							,       prompt=[prompt, prompt]    )

     @slow
     @require_torch
     def 						__lowerCamelCase				(  self    ):
            __snake_case						       =   pipeline('image-to-text'							,       model='microsoft/git-base-coco'    )
            __snake_case						       =   'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
            __snake_case						       =   Image.open(requests.get(SCREAMING_SNAKE_CASE_							,       stream=SCREAMING_SNAKE_CASE_    ).raw    )

            __snake_case						       =   'a photo of a'

            __snake_case						       =   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						       =   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						       =   pipeline('image-to-text'							,       model='google/pix2struct-ai2d-base'    )
            __snake_case						       =   'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
            __snake_case						       =   Image.open(requests.get(SCREAMING_SNAKE_CASE_							,       stream=SCREAMING_SNAKE_CASE_    ).raw    )

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

            __snake_case						       =   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						       =   pipe([image, image]							,       prompt=[prompt, prompt]    )

     @slow
     @require_tf
     def 						__lowerCamelCase				(  self    ):
            __snake_case						       =   pipeline('image-to-text'							,       model='ydshieh/vit-gpt2-coco-en'							,       framework='tf'    )
            __snake_case						       =   './tests/fixtures/tests_samples/COCO/000000039769.png'

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


from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


lowerCamelCase_				:							Union[str, Any]       =    logging.get_logger(__name__)

lowerCamelCase_				:							Optional[Any]       =    {
    "google/long-t5-local-base": "https://huggingface.co/google/long-t5-local-base/blob/main/config.json",
    "google/long-t5-local-large": "https://huggingface.co/google/long-t5-local-large/blob/main/config.json",
    "google/long-t5-tglobal-base": "https://huggingface.co/google/long-t5-tglobal-base/blob/main/config.json",
    "google/long-t5-tglobal-large": "https://huggingface.co/google/long-t5-tglobal-large/blob/main/config.json",
}







class  _lowerCamelCase			(lowerCamelCase					):
     lowercase__				  =			"""longt5"""
     lowercase__				  =			["""past_key_values"""]
     lowercase__				  =			{"""hidden_size""": """d_model""", """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers"""}

     def __init__(  self							,       SCREAMING_SNAKE_CASE_=32_128							,       SCREAMING_SNAKE_CASE_=512							,       SCREAMING_SNAKE_CASE_=64							,       SCREAMING_SNAKE_CASE_=2_048							,       SCREAMING_SNAKE_CASE_=6							,       SCREAMING_SNAKE_CASE_=None							,       SCREAMING_SNAKE_CASE_=8							,       SCREAMING_SNAKE_CASE_=127							,       SCREAMING_SNAKE_CASE_=16							,       SCREAMING_SNAKE_CASE_=32							,       SCREAMING_SNAKE_CASE_=128							,       SCREAMING_SNAKE_CASE_=0.1							,       SCREAMING_SNAKE_CASE_=1E-6							,       SCREAMING_SNAKE_CASE_=1.0							,       SCREAMING_SNAKE_CASE_="relu"							,       SCREAMING_SNAKE_CASE_=True							,       SCREAMING_SNAKE_CASE_="local"							,       SCREAMING_SNAKE_CASE_=True							,       SCREAMING_SNAKE_CASE_=0							,       SCREAMING_SNAKE_CASE_=1							,       **SCREAMING_SNAKE_CASE_							,       ):
            __snake_case						       =   vocab_size
            __snake_case						       =   d_model
            __snake_case						       =   d_kv
            __snake_case						       =   d_ff
            __snake_case						       =   num_layers
            # default = symmetry
            __snake_case						       =   num_decoder_layers if num_decoder_layers is not None else self.num_layers
            __snake_case						       =   num_heads
            __snake_case						       =   local_radius
            __snake_case						       =   global_block_size
            __snake_case						       =   relative_attention_num_buckets
            __snake_case						       =   relative_attention_max_distance
            __snake_case						       =   dropout_rate
            __snake_case						       =   layer_norm_epsilon
            __snake_case						       =   initializer_factor
            __snake_case						       =   feed_forward_proj
            __snake_case						       =   encoder_attention_type
            __snake_case						       =   use_cache

            __snake_case						       =   self.feed_forward_proj.split('-'    )
            __snake_case						       =   act_info[-1]
            __snake_case						       =   act_info[0] == 'gated'

            if len(SCREAMING_SNAKE_CASE_    ) > 1 and act_info[0] != "gated" or len(SCREAMING_SNAKE_CASE_    ) > 2:
                   raise ValueError(
                       f'''`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer.'''
                       'Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. '
                       '\'gated-gelu\' or \'relu\''    )

            # for backwards compatibility
            if feed_forward_proj == "gated-gelu":
                   __snake_case						       =   'gelu_new'

            super().__init__(
                pad_token_id=SCREAMING_SNAKE_CASE_							,       eos_token_id=SCREAMING_SNAKE_CASE_							,       is_encoder_decoder=SCREAMING_SNAKE_CASE_							,       **SCREAMING_SNAKE_CASE_							,       )







class  _lowerCamelCase			(lowerCamelCase					):

     @property
     def 						__lowerCamelCase				(  self    ):
            __snake_case						       =   {
                'input_ids': {0: 'batch', 1: 'encoder_sequence'},
                'attention_mask': {0: 'batch', 1: 'encoder_sequence'},
            }
            if self.use_past:
                   __snake_case						       =   'past_encoder_sequence + sequence'
                   __snake_case						       =   {0: 'batch'}
                   __snake_case						       =   {0: 'batch', 1: 'past_decoder_sequence + sequence'}
            else:
                   __snake_case						       =   {0: 'batch', 1: 'decoder_sequence'}
                   __snake_case						       =   {0: 'batch', 1: 'decoder_sequence'}

            if self.use_past:
                   self.fill_with_past_key_values_(SCREAMING_SNAKE_CASE_							,       direction='inputs'    )

            return common_inputs

     @property
     def 						__lowerCamelCase				(  self    ):
            return 13
