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


if is_vision_available():
       from PIL import Image
else:





       class 						__lowercase:


              """simple docstring"""







              @staticmethod
              def SCREAMING_SNAKE_CASE_				(     *_lowerCAmelCase					:			str  ,      **_lowerCAmelCase					:			List[str]       )					->   List[Any]:
                pass





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


       """simple docstring"""







       UpperCamelCase_				 =							MODEL_FOR_VISION_2_SEQ_MAPPING
       UpperCamelCase_				 =							TF_MODEL_FOR_VISION_2_SEQ_MAPPING



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



       def SCREAMING_SNAKE_CASE_				(     self					:			Optional[int]  ,      _lowerCAmelCase					:			Tuple  ,      _lowerCAmelCase					:			Union[str, Any]       )					->   Optional[int]:
         _lowerCAmelCase						=		pipe(lowerCAmelCase_       )
         self.assertEqual(
             lowerCAmelCase_  ,      [
                 [{'generated_text': ANY(lowerCAmelCase_       )}],
                 [{'generated_text': ANY(lowerCAmelCase_       )}],
             ]  ,      )



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

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

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

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



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

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

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



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

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



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

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

         _lowerCAmelCase						=		pipe([image, image]       )
         self.assertEqual(
             lowerCAmelCase_  ,      [
                 [{'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 SCREAMING_SNAKE_CASE_				(     self					:			List[Any]       )					->   List[str]:
         _lowerCAmelCase						=		pipeline('image-to-text'  ,      model='Salesforce/blip-image-captioning-base'       )
         _lowerCAmelCase						=		'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
         _lowerCAmelCase						=		Image.open(requests.get(lowerCAmelCase_  ,      stream=lowerCAmelCase_       ).raw       )

         _lowerCAmelCase						=		pipe(lowerCAmelCase_       )
         self.assertEqual(lowerCAmelCase_  ,      [{'generated_text': 'a pink pokemon pokemon with a blue shirt and a blue shirt'}]       )



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

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



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

         _lowerCAmelCase						=		'a photography of'

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

         with self.assertRaises(lowerCAmelCase_       ):
           _lowerCAmelCase						=		pipe([image, image]  ,      prompt=[prompt, prompt]       )



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

         _lowerCAmelCase						=		'a photo of a'

         _lowerCAmelCase						=		pipe(lowerCAmelCase_  ,      prompt=lowerCAmelCase_       )
         self.assertEqual(lowerCAmelCase_  ,      [{'generated_text': 'a photo of a tent with a tent and a tent in the background.'}]       )

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

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

         _lowerCAmelCase						=		pipe(lowerCAmelCase_  ,      prompt=lowerCAmelCase_       )
         self.assertEqual(lowerCAmelCase_  ,      [{'generated_text': 'ash cloud'}]       )

         with self.assertRaises(lowerCAmelCase_       ):
           _lowerCAmelCase						=		pipe([image, image]  ,      prompt=[prompt, prompt]       )






       @slow
       @require_tf
       def SCREAMING_SNAKE_CASE_				(     self					:			Optional[int]       )					->   Tuple:
         _lowerCAmelCase						=		pipeline('image-to-text'  ,      model='ydshieh/vit-gpt2-coco-en'  ,      framework='tf'       )
         _lowerCAmelCase						=		'./tests/fixtures/tests_samples/COCO/000000039769.png'

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

         _lowerCAmelCase						=		pipe([image, image]       )
         self.assertEqual(
             lowerCAmelCase_  ,      [
                 [{'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,
)


A							     = {
    "configuration_marian": ["MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP", "MarianConfig", "MarianOnnxConfig"],
}

try:
     if not is_sentencepiece_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     A							     = ["MarianTokenizer"]

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     A							     = [
         "MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST",
         "MarianForCausalLM",
         "MarianModel",
         "MarianMTModel",
         "MarianPreTrainedModel",
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     A							     = ["TFMarianModel", "TFMarianMTModel", "TFMarianPreTrainedModel"]

try:
     if not is_flax_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     A							     = ["FlaxMarianModel", "FlaxMarianMTModel", "FlaxMarianPreTrainedModel"]

if TYPE_CHECKING:
     from .configuration_marian import MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP, MarianConfig, MarianOnnxConfig

     try:
          if not is_sentencepiece_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_marian import MarianTokenizer

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_marian import (
              MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST,
              MarianForCausalLM,
              MarianModel,
              MarianMTModel,
              MarianPreTrainedModel,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_marian import TFMarianModel, TFMarianMTModel, TFMarianPreTrainedModel

     try:
          if not is_flax_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_flax_marian import FlaxMarianModel, FlaxMarianMTModel, FlaxMarianPreTrainedModel

else:
     import sys

     A							     = _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

