

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__			:  Tuple					     =     False


if is_vision_available():
  from PIL import Image
else:







  class 							a__							:







    """simple docstring"""




    @staticmethod
    def 			_lowercase   (      *UpperCAmelCase__     :			List[Any]					,					**UpperCAmelCase__     :			Optional[int]		)       ->Tuple:
          """simple docstring"""







          pass







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







  """simple docstring"""




  UpperCAmelCase__   :			Dict      							=MODEL_FOR_VISION_2_SEQ_MAPPING
  UpperCAmelCase__   :			int      							=TF_MODEL_FOR_VISION_2_SEQ_MAPPING


  def 			_lowercase   (      self     :			List[str]					,					UpperCAmelCase__     :			List[Any]					,					UpperCAmelCase__     :			Union[str, Any]					,					UpperCAmelCase__     :			str		)       ->List[str]:
        """simple docstring"""







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


  def 			_lowercase   (      self     :			int					,					UpperCAmelCase__     :			List[Any]					,					UpperCAmelCase__     :			Any		)       ->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE			:   Union[str, Any]	  = pipe(UpperCAmelCase__		)
        self.assertEqual(
            UpperCAmelCase__					,					[
                [{"""generated_text""": ANY(UpperCAmelCase__		)}],
                [{"""generated_text""": ANY(UpperCAmelCase__		)}],
            ]					,					)


  @require_tf
  def 			_lowercase   (      self     :			int		)       ->List[Any]:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE			:   List[str]	  = pipe(UpperCAmelCase__		)
        self.assertEqual(
            UpperCAmelCase__					,					[
                {
                    """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                },
            ]					,					)

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

        SCREAMING_SNAKE_CASE			:   List[str]	  = pipe(UpperCAmelCase__					,					max_new_tokens=1		)
        self.assertEqual(
            UpperCAmelCase__					,					[{"""generated_text""": """growth"""}]					,					)


  @require_torch
  def 			_lowercase   (      self     :			Tuple		)       ->Tuple:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE			:   Any	  = pipe(UpperCAmelCase__		)
        self.assertEqual(
            UpperCAmelCase__					,					[
                {
                    """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                },
            ]					,					)

        SCREAMING_SNAKE_CASE			:   List[Any]	  = pipe([image, image]		)
        self.assertEqual(
            UpperCAmelCase__					,					[
                [
                    {
                        """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                    }
                ],
                [
                    {
                        """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                    }
                ],
            ]					,					)


  @require_torch
  def 			_lowercase   (      self     :			List[Any]		)       ->List[str]:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE			:   List[Any]	  = pipe(UpperCAmelCase__					,					prompt=UpperCAmelCase__		)
        self.assertTrue(outputs[0]["""generated_text"""].startswith(UpperCAmelCase__		)		)


  @slow
  @require_torch
  def 			_lowercase   (      self     :			Tuple		)       ->List[Any]:
        """simple docstring"""







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

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

        SCREAMING_SNAKE_CASE			:   int	  = 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 			_lowercase   (      self     :			Optional[Any]		)       ->Any:
        """simple docstring"""







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

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


  @slow
  @require_torch
  def 			_lowercase   (      self     :			List[Any]		)       ->Dict:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE			:   Union[str, Any]	  = pipe(UpperCAmelCase__		)
        self.assertEqual(UpperCAmelCase__					,					[{"""generated_text""": """a cartoon of a purple character."""}]		)


  @slow
  @require_torch
  def 			_lowercase   (      self     :			List[str]		)       ->str:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE			:   Optional[Any]	  = """a photography of"""

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

        with self.assertRaises(UpperCAmelCase__		):
              SCREAMING_SNAKE_CASE			:   int	  = pipe([image, image]					,					prompt=[prompt, prompt]		)


  @slow
  @require_torch
  def 			_lowercase   (      self     :			Dict		)       ->int:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE			:   List[str]	  = """a photo of a"""

        SCREAMING_SNAKE_CASE			:   Dict	  = 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__		):
              SCREAMING_SNAKE_CASE			:   Union[str, 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 			_lowercase   (      self     :			Union[str, Any]		)       ->List[Any]:
        """simple docstring"""







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

        SCREAMING_SNAKE_CASE			:   Optional[int]	  = """What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud"""

        SCREAMING_SNAKE_CASE			:   str	  = pipe(UpperCAmelCase__					,					prompt=UpperCAmelCase__		)
        self.assertEqual(UpperCAmelCase__					,					[{"""generated_text""": """ash cloud"""}]		)

        with self.assertRaises(UpperCAmelCase__		):
              SCREAMING_SNAKE_CASE			:   Dict	  = pipe([image, image]					,					prompt=[prompt, prompt]		)




  @slow
  @require_tf
  def 			_lowercase   (      self     :			Optional[int]		)       ->Optional[int]:
        """simple docstring"""







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

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

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







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_torch_available,
)


UpperCAmelCase__			:  Tuple					     =     {
    """configuration_speech_to_text_2""": ["""SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP""", """Speech2Text2Config"""],
    """processing_speech_to_text_2""": ["""Speech2Text2Processor"""],
    """tokenization_speech_to_text_2""": ["""Speech2Text2Tokenizer"""],
}


try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  UpperCAmelCase__			:  Tuple					     =     [
      """SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """Speech2Text2ForCausalLM""",
      """Speech2Text2PreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_speech_to_text_a import SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextaConfig
  from .processing_speech_to_text_a import SpeechaTextaProcessor
  from .tokenization_speech_to_text_a import SpeechaTextaTokenizer

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_speech_to_text_a import (
        SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST,
        SpeechaTextaForCausalLM,
        SpeechaTextaPreTrainedModel,
    )

else:
  import sys

  UpperCAmelCase__			:  Optional[int]					     =     _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




