

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


if is_vision_available():
   from PIL import Image
else:



   class SCREAMING_SNAKE_CASE	:
     """simple docstring"""






     @staticmethod
     def        SCREAMING_SNAKE_CASE    (			*lowerCAmelCase :    Any       ,							**lowerCAmelCase :    List[str]    )							->	str:






        """simple docstring"""







        pass



@is_pipeline_test
@require_vision
class SCREAMING_SNAKE_CASE	(  unittest.TestCase ):
  """simple docstring"""






  lowerCamelCase    :			Any						      =MODEL_FOR_VISION_2_SEQ_MAPPING
  lowerCamelCase    :			List[Any]						      =TF_MODEL_FOR_VISION_2_SEQ_MAPPING







  def        SCREAMING_SNAKE_CASE    (			self :    Tuple       ,							lowerCAmelCase :    Dict       ,							lowerCAmelCase :    Optional[int]       ,							lowerCAmelCase :    Union[str, Any]    )							->	List[str]:






     """simple docstring"""







     __lowerCAmelCase  :			Dict					    =						pipeline("""image-to-text"""       ,							model=lowerCAmelCase       ,							tokenizer=lowerCAmelCase       ,							image_processor=lowerCAmelCase    )
     __lowerCAmelCase  :			List[Any]					    =						[
         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[Any]       ,							lowerCAmelCase :    str       ,							lowerCAmelCase :    Union[str, Any]    )							->	Optional[int]:






     """simple docstring"""







     __lowerCAmelCase  :			int					    =						pipe(lowerCAmelCase    )
     self.assertEqual(
         lowerCAmelCase       ,							[
             [{"""generated_text""": ANY(lowerCAmelCase    )}],
             [{"""generated_text""": ANY(lowerCAmelCase    )}],
         ]       ,							)







  @require_tf
  def        SCREAMING_SNAKE_CASE    (			self :    Optional[Any]    )							->	Union[str, Any]:






     """simple docstring"""







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

     __lowerCAmelCase  :			Union[str, Any]					    =						pipe(lowerCAmelCase    )
     self.assertEqual(
         lowerCAmelCase       ,							[
             {
                 """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
             },
         ]       ,							)

     __lowerCAmelCase  :			Optional[Any]					    =						pipe([image, image]    )
     self.assertEqual(
         lowerCAmelCase       ,							[
             [
                 {
                     """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                 }
             ],
             [
                 {
                     """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                 }
             ],
         ]       ,							)

     __lowerCAmelCase  :			int					    =						pipe(lowerCAmelCase       ,							max_new_tokens=1    )
     self.assertEqual(
         lowerCAmelCase       ,							[{"""generated_text""": """growth"""}]       ,							)







  @require_torch
  def        SCREAMING_SNAKE_CASE    (			self :    Dict    )							->	Optional[Any]:






     """simple docstring"""







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

     __lowerCAmelCase  :			Union[str, Any]					    =						pipe(lowerCAmelCase    )
     self.assertEqual(
         lowerCAmelCase       ,							[
             {
                 """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
             },
         ]       ,							)

     __lowerCAmelCase  :			Any					    =						pipe([image, image]    )
     self.assertEqual(
         lowerCAmelCase       ,							[
             [
                 {
                     """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                 }
             ],
             [
                 {
                     """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                 }
             ],
         ]       ,							)







  @require_torch
  def        SCREAMING_SNAKE_CASE    (			self :    int    )							->	List[Any]:






     """simple docstring"""







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

     __lowerCAmelCase  :			Optional[int]					    =						pipe(lowerCAmelCase       ,							prompt=lowerCAmelCase    )
     self.assertTrue(outputs[0]["""generated_text"""].startswith(lowerCAmelCase    )    )







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






     """simple docstring"""







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

     __lowerCAmelCase  :			Optional[int]					    =						pipe(lowerCAmelCase    )
     self.assertEqual(lowerCAmelCase       ,							[{"""generated_text""": """a cat laying on a blanket next to a cat laying on a bed """}]    )

     __lowerCAmelCase  :			Optional[Any]					    =						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]    )							->	Union[str, Any]:






     """simple docstring"""







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

     __lowerCAmelCase  :			str					    =						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 :    str    )							->	Dict:






     """simple docstring"""







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

     __lowerCAmelCase  :			Optional[Any]					    =						pipe(lowerCAmelCase    )
     self.assertEqual(lowerCAmelCase       ,							[{"""generated_text""": """a cartoon of a purple character."""}]    )







  @slow
  @require_torch
  def        SCREAMING_SNAKE_CASE    (			self :    Any    )							->	Dict:






     """simple docstring"""







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

     __lowerCAmelCase  :			Tuple					    =						"""a photography of"""

     __lowerCAmelCase  :			Optional[int]					    =						pipe(lowerCAmelCase       ,							prompt=lowerCAmelCase    )
     self.assertEqual(lowerCAmelCase       ,							[{"""generated_text""": """a photography of a volcano"""}]    )

     with self.assertRaises(lowerCAmelCase    ):
        __lowerCAmelCase  :			Optional[int]					    =						pipe([image, image]       ,							prompt=[prompt, prompt]    )







  @slow
  @require_torch
  def        SCREAMING_SNAKE_CASE    (			self :    str    )							->	str:






     """simple docstring"""







     __lowerCAmelCase  :			List[str]					    =						pipeline("""image-to-text"""       ,							model="""microsoft/git-base-coco"""    )
     __lowerCAmelCase  :			List[Any]					    =						"""https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"""
     __lowerCAmelCase  :			int					    =						Image.open(requests.get(lowerCAmelCase       ,							stream=lowerCAmelCase    ).raw    )

     __lowerCAmelCase  :			Union[str, Any]					    =						"""a photo of a"""

     __lowerCAmelCase  :			str					    =						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  :			Optional[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        SCREAMING_SNAKE_CASE    (			self :    Union[str, Any]    )							->	Optional[Any]:






     """simple docstring"""







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

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

     __lowerCAmelCase  :			Union[str, Any]					    =						pipe(lowerCAmelCase       ,							prompt=lowerCAmelCase    )
     self.assertEqual(lowerCAmelCase       ,							[{"""generated_text""": """ash cloud"""}]    )

     with self.assertRaises(lowerCAmelCase    ):
        __lowerCAmelCase  :			Optional[int]					    =						pipe([image, image]       ,							prompt=[prompt, prompt]    )







  @slow
  @require_tf
  def        SCREAMING_SNAKE_CASE    (			self :    Optional[int]    )							->	List[str]:






     """simple docstring"""







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

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

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





import importlib.metadata
import subprocess
import sys





def 	snake_case_      (__A     :						bool = False     )			->	str:
   __lowerCAmelCase  :			Any					    =						False
   if "IPython" in sys.modules:
      __lowerCAmelCase  :			Optional[int]					    =						"""google.colab""" in str(sys.modules["""IPython"""].get_ipython()     )

   if in_colab:
      if upgrade:
         __lowerCAmelCase  :			int					    =						["""pip""", """install""", """-U""", """torch"""]
         subprocess.run(__A					,   check=__A     )
      # get the current version of torch
      __lowerCAmelCase  :			List[Any]					    =						importlib.metadata.version("""torch"""     )
      __lowerCAmelCase  :			List[Any]					    =						torch_version[: torch_version.rindex("""."""     )]
      __lowerCAmelCase  :			int					    =						f'''https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl'''
      __lowerCAmelCase  :			List[Any]					    =						["""pip""", """install""", xla_wheel]
      subprocess.run(__A					,   check=__A     )
   else:
      raise RuntimeError("""`install_xla` utility works only on google colab."""     )


