



from typing import Callable, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__lowerCamelCase								=      logging.get_logger(__name__)

__lowerCamelCase								=      {
    '''microsoft/prophetnet-large-uncased''': (
        '''https://huggingface.co/microsoft/prophetnet-large-uncased/resolve/main/config.json'''
    ),
}


class 	snake_case_			(lowercase__	):




       """simple docstring"""






       _lowerCamelCase           =					"""prophetnet"""
       _lowerCamelCase           =					["""past_key_values"""]
       _lowerCamelCase           =					{
           """num_attention_heads""": """num_encoder_attention_heads""",
       }



       def __init__(  self  ,lowercase = 0.1  ,lowercase = "gelu"  ,lowercase = 30522  ,lowercase = 1024  ,lowercase = 4096  ,lowercase = 12  ,lowercase = 16  ,lowercase = 4096  ,lowercase = 12  ,lowercase = 16  ,lowercase = 0.1  ,lowercase = 0.1  ,lowercase = 512  ,lowercase = 0.02  ,lowercase = True  ,lowercase = True  ,lowercase = 0  ,lowercase = 2  ,lowercase = 32  ,lowercase = 128  ,lowercase = False  ,lowercase = 0.0  ,lowercase = True  ,lowercase = 0  ,lowercase = 1  ,lowercase = 2  ,**lowercase  ,):

             """simple docstring"""





             UpperCAmelCase_	:						Optional[Any]					   =		vocab_size
             UpperCAmelCase_	:						Dict					   =		hidden_size
             UpperCAmelCase_	:						str					   =		encoder_ffn_dim
             UpperCAmelCase_	:						Optional[int]					   =		num_encoder_layers
             UpperCAmelCase_	:						Any					   =		num_encoder_attention_heads
             UpperCAmelCase_	:						Any					   =		decoder_ffn_dim
             UpperCAmelCase_	:						str					   =		num_decoder_layers
             UpperCAmelCase_	:						List[str]					   =		num_decoder_attention_heads
             UpperCAmelCase_	:						Tuple					   =		max_position_embeddings
             UpperCAmelCase_	:						Optional[int]					   =		init_std  # Normal(0, this parameter)
             UpperCAmelCase_	:						Optional[int]					   =		activation_function

             # parameters for prophetnet
             UpperCAmelCase_	:						Union[str, Any]					   =		ngram
             UpperCAmelCase_	:						str					   =		num_buckets
             UpperCAmelCase_	:						List[str]					   =		relative_max_distance
             UpperCAmelCase_	:						Tuple					   =		disable_ngram_loss
             UpperCAmelCase_	:						Union[str, Any]					   =		eps

             # 3 Types of Dropout
             UpperCAmelCase_	:						Dict					   =		attention_dropout
             UpperCAmelCase_	:						List[Any]					   =		activation_dropout
             UpperCAmelCase_	:						List[str]					   =		dropout

             UpperCAmelCase_	:						int					   =		use_cache

             super().__init__(
                 pad_token_id=lowercase  ,bos_token_id=lowercase  ,eos_token_id=lowercase  ,is_encoder_decoder=lowercase  ,add_cross_attention=lowercase  ,decoder_start_token_id=lowercase  ,**lowercase  ,)



       @property
       def 			A_ (  self):

             """simple docstring"""





             return self.num_encoder_layers + self.num_decoder_layers



       @num_hidden_layers.setter
       def 			A_ (  self  ,lowercase):

             """simple docstring"""





             raise NotImplementedError(
                 "This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and"
                 " `num_decoder_layers`.")





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								=      False


if is_vision_available():
   from PIL import Image
else:


   class 	snake_case_			:




          """simple docstring"""






          @staticmethod
          def 			A_ (  *lowercase  ,**lowercase):

                """simple docstring"""





                pass


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




       """simple docstring"""






       _lowerCamelCase           =					MODEL_FOR_VISION_2_SEQ_MAPPING
       _lowerCamelCase           =					TF_MODEL_FOR_VISION_2_SEQ_MAPPING



       def 			A_ (  self  ,lowercase  ,lowercase  ,lowercase):

             """simple docstring"""





             UpperCAmelCase_	:						Optional[Any]					   =		pipeline("image-to-text"  ,model=lowercase  ,tokenizer=lowercase  ,image_processor=lowercase)
             UpperCAmelCase_	:						Any					   =		[
                 Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"),
                 "./tests/fixtures/tests_samples/COCO/000000039769.png",
             ]
             return pipe, examples



       def 			A_ (  self  ,lowercase  ,lowercase):

             """simple docstring"""





             UpperCAmelCase_	:						str					   =		pipe(lowercase)
             self.assertEqual(
                 lowercase  ,[
                     [{"generated_text": ANY(lowercase)}],
                     [{"generated_text": ANY(lowercase)}],
                 ]  ,)



       @require_tf
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						str					   =		pipeline("image-to-text"  ,model="hf-internal-testing/tiny-random-vit-gpt2"  ,framework="tf")
             UpperCAmelCase_	:						int					   =		"./tests/fixtures/tests_samples/COCO/000000039769.png"

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

             UpperCAmelCase_	:						Optional[int]					   =		pipe([image, image])
             self.assertEqual(
                 lowercase  ,[
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                 ]  ,)

             UpperCAmelCase_	:						Any					   =		pipe(lowercase  ,max_new_tokens=1)
             self.assertEqual(
                 lowercase  ,[{"generated_text": "growth"}]  ,)



       @require_torch
       def 			A_ (  self):

             """simple docstring"""





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

             UpperCAmelCase_	:						str					   =		pipe(lowercase)
             self.assertEqual(
                 lowercase  ,[
                     {
                         "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                     },
                 ]  ,)

             UpperCAmelCase_	:						Optional[int]					   =		pipe([image, image])
             self.assertEqual(
                 lowercase  ,[
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                 ]  ,)



       @require_torch
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						List[Any]					   =		pipeline("image-to-text"  ,model="hf-internal-testing/tiny-random-BlipForConditionalGeneration")
             UpperCAmelCase_	:						Union[str, Any]					   =		"./tests/fixtures/tests_samples/COCO/000000039769.png"
             UpperCAmelCase_	:						Optional[Any]					   =		"a photo of"

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



       @slow
       @require_torch
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						List[Any]					   =		pipeline("image-to-text"  ,model="ydshieh/vit-gpt2-coco-en")
             UpperCAmelCase_	:						List[str]					   =		"./tests/fixtures/tests_samples/COCO/000000039769.png"

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

             UpperCAmelCase_	:						Optional[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 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Any					   =		pipeline("image-to-text"  ,model="Salesforce/blip-image-captioning-base")
             UpperCAmelCase_	:						str					   =		"https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
             UpperCAmelCase_	:						Optional[Any]					   =		Image.open(requests.get(lowercase  ,stream=lowercase).raw)

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



       @slow
       @require_torch
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						str					   =		pipeline("image-to-text"  ,model="microsoft/git-base-coco")
             UpperCAmelCase_	:						Union[str, Any]					   =		"https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
             UpperCAmelCase_	:						int					   =		Image.open(requests.get(lowercase  ,stream=lowercase).raw)

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



       @slow
       @require_torch
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Any					   =		pipeline("image-to-text"  ,model="Salesforce/blip-image-captioning-base")
             UpperCAmelCase_	:						Union[str, Any]					   =		"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             UpperCAmelCase_	:						int					   =		Image.open(requests.get(lowercase  ,stream=lowercase).raw)

             UpperCAmelCase_	:						Any					   =		"a photography of"

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

             with self.assertRaises(lowercase):
                   UpperCAmelCase_	:						Tuple					   =		pipe([image, image]  ,prompt=[prompt, prompt])



       @slow
       @require_torch
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Optional[int]					   =		pipeline("image-to-text"  ,model="microsoft/git-base-coco")
             UpperCAmelCase_	:						List[str]					   =		"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_	:						Optional[Any]					   =		"a photo of a"

             UpperCAmelCase_	:						Tuple					   =		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_	:						Tuple					   =		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 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Optional[int]					   =		pipeline("image-to-text"  ,model="google/pix2struct-ai2d-base")
             UpperCAmelCase_	:						Any					   =		"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             UpperCAmelCase_	:						Union[str, Any]					   =		Image.open(requests.get(lowercase  ,stream=lowercase).raw)

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

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

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



       @slow
       @require_tf
       def 			A_ (  self):

             """simple docstring"""





             UpperCAmelCase_	:						Union[str, Any]					   =		pipeline("image-to-text"  ,model="ydshieh/vit-gpt2-coco-en"  ,framework="tf")
             UpperCAmelCase_	:						str					   =		"./tests/fixtures/tests_samples/COCO/000000039769.png"

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

             UpperCAmelCase_	:						int					   =		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 "}],
                 ]  ,)
