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


if is_vision_available():
      from PIL import Image
else:
      class 							lowercase_    :







          @staticmethod
          def   lowerCamelCase_    (	*UpperCamelCase__       ,     **UpperCamelCase__				)				->       int:





           """simple docstring"""

           pass
@is_pipeline_test
@require_vision
class 							lowercase_    (     unittest.TestCase						):
    a_             =   MODEL_FOR_VISION_2_SEQ_MAPPING
    a_             =   TF_MODEL_FOR_VISION_2_SEQ_MAPPING







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__       ,     UpperCamelCase__				)				->       Any:





     """simple docstring"""

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







    def   lowerCamelCase_    (	self       ,     UpperCamelCase__       ,     UpperCamelCase__				)				->       Tuple:





     """simple docstring"""

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







    @require_tf
    def   lowerCamelCase_    (	self				)				->       List[str]:





     """simple docstring"""

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

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

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

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







    @require_torch
    def   lowerCamelCase_    (	self				)				->       List[str]:





     """simple docstring"""

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

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

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







    @require_torch
    def   lowerCamelCase_    (	self				)				->       Dict:





     """simple docstring"""

     UpperCAmelCase_				       =					pipeline("image-to-text"       ,     model="hf-internal-testing/tiny-random-BlipForConditionalGeneration"				)
     UpperCAmelCase_				       =					"./tests/fixtures/tests_samples/COCO/000000039769.png"
     UpperCAmelCase_				       =					"a photo of"

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







    @slow
    @require_torch
    def   lowerCamelCase_    (	self				)				->       List[str]:





     """simple docstring"""

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

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

     UpperCAmelCase_				       =					pipe([image, image]				)
     self.assertEqual(
         UpperCamelCase__       ,     [
             [{"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				)				->       str:





     """simple docstring"""

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

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







    @slow
    @require_torch
    def   lowerCamelCase_    (	self				)				->       Optional[int]:





     """simple docstring"""

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

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







    @slow
    @require_torch
    def   lowerCamelCase_    (	self				)				->       Union[str, Any]:





     """simple docstring"""

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

     UpperCAmelCase_				       =					"a photography of"

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

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







    @slow
    @require_torch
    def   lowerCamelCase_    (	self				)				->       Any:





     """simple docstring"""

     UpperCAmelCase_				       =					pipeline("image-to-text"       ,     model="microsoft/git-base-coco"				)
     UpperCAmelCase_				       =					"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
     UpperCAmelCase_				       =					Image.open(requests.get(UpperCamelCase__       ,     stream=UpperCamelCase__				).raw				)

     UpperCAmelCase_				       =					"a photo of a"

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

     with self.assertRaises(UpperCamelCase__				):
      UpperCAmelCase_				       =					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				)				->       Optional[Any]:





     """simple docstring"""

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

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

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

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







    @slow
    @require_tf
    def   lowerCamelCase_    (	self				)				->       Union[str, Any]:





     """simple docstring"""

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

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

     UpperCAmelCase_				       =					pipe([image, image]				)
     self.assertEqual(
         UpperCamelCase__       ,     [
             [{"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_torch_available, is_vision_available


__snake_case       :   int  		=    {
    '''configuration_videomae''': ['''VIDEOMAE_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''VideoMAEConfig'''],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __snake_case       :   Optional[Any]  		=    [
          '''VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST''',
          '''VideoMAEForPreTraining''',
          '''VideoMAEModel''',
          '''VideoMAEPreTrainedModel''',
          '''VideoMAEForVideoClassification''',
      ]

try:
      if not is_vision_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      __snake_case       :   str  		=    ['''VideoMAEFeatureExtractor''']
      __snake_case       :   Any  		=    ['''VideoMAEImageProcessor''']

if TYPE_CHECKING:
      from .configuration_videomae import VIDEOMAE_PRETRAINED_CONFIG_ARCHIVE_MAP, VideoMAEConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_videomae import (
                VIDEOMAE_PRETRAINED_MODEL_ARCHIVE_LIST,
                VideoMAEForPreTraining,
                VideoMAEForVideoClassification,
                VideoMAEModel,
                VideoMAEPreTrainedModel,
            )

      try:
            if not is_vision_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .feature_extraction_videomae import VideoMAEFeatureExtractor
            from .image_processing_videomae import VideoMAEImageProcessor

else:
      import sys

      __snake_case       :   List[str]  		=    _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
