




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


if is_vision_available():
 from PIL import Image
else:

 class 			_lowerCamelCase    :

       """simple docstring"""

       @staticmethod
       def    snake_case						(  *snake_case       :			Tuple			,							**snake_case       :			Dict					):
           pass

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

      """simple docstring"""

      lowerCAmelCase__      :			List[Any]     =       MODEL_FOR_VISION_2_SEQ_MAPPING
      lowerCAmelCase__      :			Tuple     =       TF_MODEL_FOR_VISION_2_SEQ_MAPPING







      def    snake_case						(  self       :			Optional[int]			,							snake_case       :			Dict			,							snake_case       :			Any			,							snake_case       :			int					):
          __UpperCamelCase			=    pipeline('''image-to-text'''			,							model=__lowercase			,							tokenizer=__lowercase			,							image_processor=__lowercase					)
          __UpperCamelCase			=    [
              Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''					),
              '''./tests/fixtures/tests_samples/COCO/000000039769.png''',
          ]
          return pipe, examples







      def    snake_case						(  self       :			Union[str, Any]			,							snake_case       :			List[str]			,							snake_case       :			int					):
          __UpperCamelCase			=    pipe(__lowercase					)
          self.assertEqual(
              __lowercase			,							[
                  [{'''generated_text''': ANY(__lowercase					)}],
                  [{'''generated_text''': ANY(__lowercase					)}],
              ]			,							)







      @require_tf
      def    snake_case						(  self       :			Optional[Any]					):
          __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(__lowercase					)
          self.assertEqual(
              __lowercase			,							[
                  {
                      '''generated_text''': '''growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'''
                  },
              ]			,							)

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

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







      @require_torch
      def    snake_case						(  self       :			List[Any]					):
          __UpperCamelCase			=    pipeline('''image-to-text'''			,							model='''hf-internal-testing/tiny-random-vit-gpt2'''					)
          __UpperCamelCase			=    '''./tests/fixtures/tests_samples/COCO/000000039769.png'''

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

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







      @require_torch
      def    snake_case						(  self       :			List[Any]					):
          __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(__lowercase			,							prompt=__lowercase					)
          self.assertTrue(outputs[0]['''generated_text'''].startswith(__lowercase					)					)







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

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

          __UpperCamelCase			=    pipe([image, image]					)
          self.assertEqual(
              __lowercase			,							[
                  [{'''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    snake_case						(  self       :			Tuple					):
          __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(__lowercase			,							stream=__lowercase					).raw					)

          __UpperCamelCase			=    pipe(__lowercase					)
          self.assertEqual(__lowercase			,							[{'''generated_text''': '''a pink pokemon pokemon with a blue shirt and a blue shirt'''}]					)







      @slow
      @require_torch
      def    snake_case						(  self       :			List[Any]					):
          __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(__lowercase			,							stream=__lowercase					).raw					)

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







      @slow
      @require_torch
      def    snake_case						(  self       :			Union[str, Any]					):
          __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(__lowercase			,							stream=__lowercase					).raw					)

          __UpperCamelCase			=    '''a photography of'''

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

          with self.assertRaises(__lowercase					):
              __UpperCamelCase			=    pipe([image, image]			,							prompt=[prompt, prompt]					)







      @slow
      @require_torch
      def    snake_case						(  self       :			Optional[int]					):
          __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(__lowercase			,							stream=__lowercase					).raw					)

          __UpperCamelCase			=    '''a photo of a'''

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

          with self.assertRaises(__lowercase					):
              __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    snake_case						(  self       :			Tuple					):
          __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(__lowercase			,							stream=__lowercase					).raw					)

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

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

          with self.assertRaises(__lowercase					):
              __UpperCamelCase			=    pipe([image, image]			,							prompt=[prompt, prompt]					)







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

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

          __UpperCamelCase			=    pipe([image, image]					)
          self.assertEqual(
              __lowercase			,							[
                  [{'''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_tokenizers_available, is_torch_available


UpperCamelCase__ :			Any       	=			{
    """configuration_splinter""": ["""SPLINTER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SplinterConfig"""],
    """tokenization_splinter""": ["""SplinterTokenizer"""],
}

try:
				if not is_tokenizers_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase__ :			Any       	=			["""SplinterTokenizerFast"""]

try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase__ :			Union[str, Any]       	=			[
				    """SPLINTER_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """SplinterForQuestionAnswering""",
				    """SplinterForPreTraining""",
				    """SplinterLayer""",
				    """SplinterModel""",
				    """SplinterPreTrainedModel""",
				]


if TYPE_CHECKING:
				from .configuration_splinter import SPLINTER_PRETRAINED_CONFIG_ARCHIVE_MAP, SplinterConfig
				from .tokenization_splinter import SplinterTokenizer

				try:
								if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .tokenization_splinter_fast import SplinterTokenizerFast

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_splinter import (
								    SPLINTER_PRETRAINED_MODEL_ARCHIVE_LIST,
								    SplinterForPreTraining,
								    SplinterForQuestionAnswering,
								    SplinterLayer,
								    SplinterModel,
								    SplinterPreTrainedModel,
								)


else:
				import sys

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


