

'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


lowerCamelCase				:   Dict      				=  {
    "configuration_donut_swin": ["DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP", "DonutSwinConfig"],
    "processing_donut": ["DonutProcessor"],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     lowerCamelCase				:   List[Any]      				=  [
         "DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST",
         "DonutSwinModel",
         "DonutSwinPreTrainedModel",
     ]

try:
     if not is_vision_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     lowerCamelCase				:   Tuple      				=  ["DonutFeatureExtractor"]
     lowerCamelCase				:   List[str]      				=  ["DonutImageProcessor"]


if TYPE_CHECKING:
     from .configuration_donut_swin import DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, DonutSwinConfig
     from .processing_donut import DonutProcessor

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_donut_swin import (
              DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
              DonutSwinModel,
              DonutSwinPreTrainedModel,
          )

     try:
          if not is_vision_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .feature_extraction_donut import DonutFeatureExtractor
          from .image_processing_donut import DonutImageProcessor

else:
     import sys

     lowerCamelCase				:   str      				=  _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:
     lowerCamelCase				:   Optional[Any]      				=  False


if is_vision_available():
     from PIL import Image
else:







     class 							A__					:





       @staticmethod
       def 		A						(	*_a   :			Any							,      **_a   :			str				)			->					Optional[int]:





            '''simple docstring'''






            pass







@is_pipeline_test
@require_vision
class 							A__					( unittest.TestCase						):
  A__					=       MODEL_FOR_VISION_2_SEQ_MAPPING
  A__					=       TF_MODEL_FOR_VISION_2_SEQ_MAPPING





  def 		A						(	self   :			List[str]							,      _a   :			List[Any]							,      _a   :			Any							,      _a   :			Dict				)			->					Any:





       '''simple docstring'''






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





  def 		A						(	self   :			str							,      _a   :			int							,      _a   :			Tuple				)			->					Optional[int]:





       '''simple docstring'''






       _SCREAMING_SNAKE_CASE												=pipe(_a				)
       self.assertEqual(
           _a							,      [
               [{'generated_text': ANY(_a				)}],
               [{'generated_text': ANY(_a				)}],
           ]							,      )





  @require_tf
  def 		A						(	self   :			Any				)			->					Optional[int]:





       '''simple docstring'''






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

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

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

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





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





       '''simple docstring'''






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

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

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





  @require_torch
  def 		A						(	self   :			Union[str, Any]				)			->					Tuple:





       '''simple docstring'''






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

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





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





       '''simple docstring'''






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

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

       _SCREAMING_SNAKE_CASE												=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   :			List[Any]				)			->					List[Any]:





       '''simple docstring'''






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

       _SCREAMING_SNAKE_CASE												=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   :			str				)			->					int:





       '''simple docstring'''






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

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





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





       '''simple docstring'''






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

       _SCREAMING_SNAKE_CASE												='a photography of'

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

       with self.assertRaises(_a				):
            _SCREAMING_SNAKE_CASE												=pipe([image, image]							,      prompt=[prompt, prompt]				)





  @slow
  @require_torch
  def 		A						(	self   :			str				)			->					Dict:





       '''simple docstring'''






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

       _SCREAMING_SNAKE_CASE												='a photo of a'

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





       '''simple docstring'''






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

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

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

       with self.assertRaises(_a				):
            _SCREAMING_SNAKE_CASE												=pipe([image, image]							,      prompt=[prompt, prompt]				)





  @slow
  @require_tf
  def 		A						(	self   :			List[str]				)			->					Tuple:





       '''simple docstring'''






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

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

       _SCREAMING_SNAKE_CASE												=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 '}],
           ]							,      )
