'''simple docstring'''




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__SCREAMING_SNAKE_CASE :  int  =						{
    """configuration_bart""": ["""BART_PRETRAINED_CONFIG_ARCHIVE_MAP""", """BartConfig""", """BartOnnxConfig"""],
    """tokenization_bart""": ["""BartTokenizer"""],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __SCREAMING_SNAKE_CASE :  Optional[Any]  =						["""BartTokenizerFast"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __SCREAMING_SNAKE_CASE :  Union[str, Any]  =						[
      """BART_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """BartForCausalLM""",
      """BartForConditionalGeneration""",
      """BartForQuestionAnswering""",
      """BartForSequenceClassification""",
      """BartModel""",
      """BartPretrainedModel""",
      """PretrainedBartModel""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __SCREAMING_SNAKE_CASE :  Optional[Any]  =						[
      """TFBartForConditionalGeneration""",
      """TFBartForSequenceClassification""",
      """TFBartModel""",
      """TFBartPretrainedModel""",
  ]

try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __SCREAMING_SNAKE_CASE :  Tuple  =						[
      """FlaxBartDecoderPreTrainedModel""",
      """FlaxBartForCausalLM""",
      """FlaxBartForConditionalGeneration""",
      """FlaxBartForQuestionAnswering""",
      """FlaxBartForSequenceClassification""",
      """FlaxBartModel""",
      """FlaxBartPreTrainedModel""",
  ]

if TYPE_CHECKING:
  from .configuration_bart import BART_PRETRAINED_CONFIG_ARCHIVE_MAP, BartConfig, BartOnnxConfig
  from .tokenization_bart import BartTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_bart_fast import BartTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_bart import (
        BART_PRETRAINED_MODEL_ARCHIVE_LIST,
        BartForCausalLM,
        BartForConditionalGeneration,
        BartForQuestionAnswering,
        BartForSequenceClassification,
        BartModel,
        BartPretrainedModel,
        PretrainedBartModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_bart import (
        TFBartForConditionalGeneration,
        TFBartForSequenceClassification,
        TFBartModel,
        TFBartPretrainedModel,
    )

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_bart import (
        FlaxBartDecoderPreTrainedModel,
        FlaxBartForCausalLM,
        FlaxBartForConditionalGeneration,
        FlaxBartForQuestionAnswering,
        FlaxBartForSequenceClassification,
        FlaxBartModel,
        FlaxBartPreTrainedModel,
    )

else:
  import sys

  __SCREAMING_SNAKE_CASE :  Union[str, Any]  =						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


'''simple docstring'''




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:
  __SCREAMING_SNAKE_CASE :  Any  =						False


if is_vision_available():
  from PIL import Image
else:

  class 			lowerCamelCase_			:
        '''simple docstring'''

        @staticmethod
        def   _A			( *A		:       str		,      **A		:       Optional[Any] ):
               pass

@is_pipeline_test
@require_vision
class 			lowerCamelCase_			(unittest.TestCase					):
      '''simple docstring'''

      __UpperCamelCase:						Union[str, Any]      			=		MODEL_FOR_VISION_2_SEQ_MAPPING
      __UpperCamelCase:						int      			=		TF_MODEL_FOR_VISION_2_SEQ_MAPPING




      def   _A			( self		:       Any		,      A		:       Any		,      A		:       str		,      A		:       Optional[int] ):
             _UpperCAmelCase  :						Dict							   =					pipeline("image-to-text"		,      model=A		,      tokenizer=A		,      image_processor=A )
             _UpperCAmelCase  :						Union[str, Any]							   =					[
                 Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" ),
                 "./tests/fixtures/tests_samples/COCO/000000039769.png",
             ]
             return pipe, examples




      def   _A			( self		:       Tuple		,      A		:       Union[str, Any]		,      A		:       Optional[Any] ):
             _UpperCAmelCase  :						List[str]							   =					pipe(A )
             self.assertEqual(
                 A		,      [
                     [{"generated_text": ANY(A )}],
                     [{"generated_text": ANY(A )}],
                 ]		,      )




      @require_tf
      def   _A			( self		:       Any ):
             _UpperCAmelCase  :						Any							   =					pipeline("image-to-text"		,      model="hf-internal-testing/tiny-random-vit-gpt2"		,      framework="tf" )
             _UpperCAmelCase  :						int							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

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

             _UpperCAmelCase  :						Tuple							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                 ]		,      )

             _UpperCAmelCase  :						Dict							   =					pipe(A		,      max_new_tokens=1 )
             self.assertEqual(
                 A		,      [{"generated_text": "growth"}]		,      )




      @require_torch
      def   _A			( self		:       Union[str, Any] ):
             _UpperCAmelCase  :						List[str]							   =					pipeline("image-to-text"		,      model="hf-internal-testing/tiny-random-vit-gpt2" )
             _UpperCAmelCase  :						Optional[int]							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

             _UpperCAmelCase  :						Optional[Any]							   =					pipe(A )
             self.assertEqual(
                 A		,      [
                     {
                         "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                     },
                 ]		,      )

             _UpperCAmelCase  :						str							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                     [
                         {
                             "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
                         }
                     ],
                 ]		,      )




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

             _UpperCAmelCase  :						str							   =					pipe(A		,      prompt=A )
             self.assertTrue(outputs[0]["generated_text"].startswith(A ) )




      @slow
      @require_torch
      def   _A			( self		:       List[Any] ):
             _UpperCAmelCase  :						Dict							   =					pipeline("image-to-text"		,      model="ydshieh/vit-gpt2-coco-en" )
             _UpperCAmelCase  :						int							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

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

             _UpperCAmelCase  :						int							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [{"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		:       Optional[Any] ):
             _UpperCAmelCase  :						Tuple							   =					pipeline("image-to-text"		,      model="Salesforce/blip-image-captioning-base" )
             _UpperCAmelCase  :						int							   =					"https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
             _UpperCAmelCase  :						Optional[int]							   =					Image.open(requests.get(A		,      stream=A ).raw )

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




      @slow
      @require_torch
      def   _A			( self		:       List[Any] ):
             _UpperCAmelCase  :						List[str]							   =					pipeline("image-to-text"		,      model="microsoft/git-base-coco" )
             _UpperCAmelCase  :						Tuple							   =					"https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
             _UpperCAmelCase  :						Dict							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						List[str]							   =					pipe(A )
             self.assertEqual(A		,      [{"generated_text": "a cartoon of a purple character."}] )




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

             _UpperCAmelCase  :						Dict							   =					"a photography of"

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

             with self.assertRaises(A ):
                    _UpperCAmelCase  :						List[Any]							   =					pipe([image, image]		,      prompt=[prompt, prompt] )




      @slow
      @require_torch
      def   _A			( self		:       Union[str, Any] ):
             _UpperCAmelCase  :						Optional[int]							   =					pipeline("image-to-text"		,      model="microsoft/git-base-coco" )
             _UpperCAmelCase  :						Optional[Any]							   =					"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             _UpperCAmelCase  :						List[str]							   =					Image.open(requests.get(A		,      stream=A ).raw )

             _UpperCAmelCase  :						List[Any]							   =					"a photo of a"

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

             with self.assertRaises(A ):
                    _UpperCAmelCase  :						List[str]							   =					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		:       Union[str, Any] ):
             _UpperCAmelCase  :						str							   =					pipeline("image-to-text"		,      model="google/pix2struct-ai2d-base" )
             _UpperCAmelCase  :						List[Any]							   =					"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
             _UpperCAmelCase  :						List[Any]							   =					Image.open(requests.get(A		,      stream=A ).raw )

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

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

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





      @slow
      @require_tf
      def   _A			( self		:       List[Any] ):
             _UpperCAmelCase  :						str							   =					pipeline("image-to-text"		,      model="ydshieh/vit-gpt2-coco-en"		,      framework="tf" )
             _UpperCAmelCase  :						str							   =					"./tests/fixtures/tests_samples/COCO/000000039769.png"

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

             _UpperCAmelCase  :						Optional[Any]							   =					pipe([image, image] )
             self.assertEqual(
                 A		,      [
                     [{"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 "}],
                 ]		,      )

