'''simple docstring'''




import inspect
import unittest

from transformers import DPTConfig
from transformers.file_utils import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
  import torch
  from torch import nn

  from transformers import MODEL_MAPPING, DPTForDepthEstimation, DPTForSemanticSegmentation, DPTModel
  from transformers.models.dpt.modeling_dpt import DPT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import DPTImageProcessor

class 			lowerCamelCase_			:
      '''simple docstring'''

      def __init__( self		:       List[Any]		,      A		:       Optional[Any]		,      A		:       Optional[Any]=2		,      A		:       Union[str, Any]=32		,      A		:       Optional[int]=16		,      A		:       List[Any]=3		,      A		:       Optional[int]=True		,      A		:       Any=True		,      A		:       List[str]=32		,      A		:       Any=4		,      A		:       Tuple=[0, 1, 2, 3]		,      A		:       int=4		,      A		:       str=37		,      A		:       Any="gelu"		,      A		:       Any=0.1		,      A		:       Optional[Any]=0.1		,      A		:       Any=0.02		,      A		:       Tuple=3		,      A		:       List[str]=False		,      A		:       List[Any]=None		,      ):
             _UpperCAmelCase  :						Dict							   =					parent
             _UpperCAmelCase  :						Tuple							   =					batch_size
             _UpperCAmelCase  :						int							   =					image_size
             _UpperCAmelCase  :						List[str]							   =					patch_size
             _UpperCAmelCase  :						Optional[Any]							   =					num_channels
             _UpperCAmelCase  :						Optional[Any]							   =					is_training
             _UpperCAmelCase  :						Tuple							   =					use_labels
             _UpperCAmelCase  :						List[Any]							   =					hidden_size
             _UpperCAmelCase  :						str							   =					num_hidden_layers
             _UpperCAmelCase  :						List[str]							   =					backbone_out_indices
             _UpperCAmelCase  :						str							   =					num_attention_heads
             _UpperCAmelCase  :						Optional[Any]							   =					intermediate_size
             _UpperCAmelCase  :						List[Any]							   =					hidden_act
             _UpperCAmelCase  :						Optional[int]							   =					hidden_dropout_prob
             _UpperCAmelCase  :						str							   =					attention_probs_dropout_prob
             _UpperCAmelCase  :						Optional[int]							   =					initializer_range
             _UpperCAmelCase  :						Dict							   =					num_labels
             _UpperCAmelCase  :						int							   =					scope
             _UpperCAmelCase  :						Optional[int]							   =					is_hybrid
             # sequence length of DPT = num_patches + 1 (we add 1 for the [CLS] token)
             _UpperCAmelCase  :						str							   =					(image_size // patch_size) ** 2
             _UpperCAmelCase  :						List[Any]							   =					num_patches + 1




      def   _A			( self		:       str ):
             _UpperCAmelCase  :						List[Any]							   =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

             _UpperCAmelCase  :						Optional[int]							   =					None
             if self.use_labels:
                    _UpperCAmelCase  :						int							   =					ids_tensor([self.batch_size, self.image_size, self.image_size]		,      self.num_labels )

             _UpperCAmelCase  :						List[str]							   =					self.get_config()

             return config, pixel_values, labels




      def   _A			( self		:       Union[str, Any] ):
             return DPTConfig(
                 image_size=self.image_size		,      patch_size=self.patch_size		,      num_channels=self.num_channels		,      hidden_size=self.hidden_size		,      num_hidden_layers=self.num_hidden_layers		,      backbone_out_indices=self.backbone_out_indices		,      num_attention_heads=self.num_attention_heads		,      intermediate_size=self.intermediate_size		,      hidden_act=self.hidden_act		,      hidden_dropout_prob=self.hidden_dropout_prob		,      attention_probs_dropout_prob=self.attention_probs_dropout_prob		,      is_decoder=A		,      initializer_range=self.initializer_range		,      is_hybrid=self.is_hybrid		,      )




      def   _A			( self		:       Optional[Any]		,      A		:       List[str]		,      A		:       Union[str, Any]		,      A		:       Tuple ):
             _UpperCAmelCase  :						str							   =					DPTModel(config=A )
             model.to(A )
             model.eval()
             _UpperCAmelCase  :						int							   =					model(A )
             self.parent.assertEqual(result.last_hidden_state.shape		,      (self.batch_size, self.seq_length, self.hidden_size) )




      def   _A			( self		:       List[str]		,      A		:       Tuple		,      A		:       str		,      A		:       Optional[Any] ):
             _UpperCAmelCase  :						Union[str, Any]							   =					self.num_labels
             _UpperCAmelCase  :						int							   =					DPTForDepthEstimation(A )
             model.to(A )
             model.eval()
             _UpperCAmelCase  :						Optional[int]							   =					model(A )
             self.parent.assertEqual(result.predicted_depth.shape		,      (self.batch_size, self.image_size, self.image_size) )




      def   _A			( self		:       Tuple		,      A		:       Any		,      A		:       Any		,      A		:       Any ):
             _UpperCAmelCase  :						List[str]							   =					self.num_labels
             _UpperCAmelCase  :						List[Any]							   =					DPTForSemanticSegmentation(A )
             model.to(A )
             model.eval()
             _UpperCAmelCase  :						Optional[int]							   =					model(A		,      labels=A )
             self.parent.assertEqual(
                 result.logits.shape		,      (self.batch_size, self.num_labels, self.image_size, self.image_size) )





      def   _A			( self		:       Optional[int] ):
             _UpperCAmelCase  :						Dict							   =					self.prepare_config_and_inputs()
             _UpperCAmelCase   ,						_UpperCAmelCase   ,						_UpperCAmelCase  :						str							   =					config_and_inputs
             _UpperCAmelCase  :						List[Any]							   =					{"pixel_values": pixel_values}
             return config, inputs_dict

@require_torch
class 			lowerCamelCase_			(snake_case__							,				snake_case__							,				unittest.TestCase					):
      '''simple docstring'''

      __UpperCamelCase:						str      			=		(DPTModel, DPTForDepthEstimation, DPTForSemanticSegmentation) if is_torch_available() else ()
      __UpperCamelCase:						Any      			=		(
          {
              "depth-estimation": DPTForDepthEstimation,
              "feature-extraction": DPTModel,
              "image-segmentation": DPTForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      __UpperCamelCase:						Any      			=		False
      __UpperCamelCase:						Any      			=		False
      __UpperCamelCase:						Any      			=		False




      def   _A			( self		:       Any ):
             _UpperCAmelCase  :						Any							   =					DPTModelTester(self )
             _UpperCAmelCase  :						Any							   =					ConfigTester(self		,      config_class=A		,      has_text_modality=A		,      hidden_size=37 )




      def   _A			( self		:       Dict ):
             self.config_tester.run_common_tests()




      @unittest.skip(reason="DPT does not use inputs_embeds" )
      def   _A			( self		:       List[Any] ):
             pass




      def   _A			( self		:       List[str] ):
             _UpperCAmelCase   ,						_UpperCAmelCase  :						Union[str, Any]							   =					self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    _UpperCAmelCase  :						Optional[Any]							   =					model_class(A )
                    self.assertIsInstance(model.get_input_embeddings()		,      (nn.Module) )
                    _UpperCAmelCase  :						Optional[int]							   =					model.get_output_embeddings()
                    self.assertTrue(x is None or isinstance(A		,      nn.Linear ) )




      def   _A			( self		:       List[str] ):
             _UpperCAmelCase   ,						_UpperCAmelCase  :						Dict							   =					self.model_tester.prepare_config_and_inputs_for_common()

             for model_class in self.all_model_classes:
                    _UpperCAmelCase  :						List[Any]							   =					model_class(A )
                    _UpperCAmelCase  :						Dict							   =					inspect.signature(model.forward )
                    # signature.parameters is an OrderedDict => so arg_names order is deterministic
                    _UpperCAmelCase  :						List[str]							   =					[*signature.parameters.keys()]

                    _UpperCAmelCase  :						Optional[Any]							   =					["pixel_values"]
                    self.assertListEqual(arg_names[:1]		,      A )




      def   _A			( self		:       Dict ):
             _UpperCAmelCase  :						Tuple							   =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*A )




      def   _A			( self		:       Tuple ):
             _UpperCAmelCase  :						Tuple							   =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_depth_estimation(*A )




      def   _A			( self		:       List[str] ):
             _UpperCAmelCase  :						Tuple							   =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_semantic_segmentation(*A )




      def   _A			( self		:       List[str] ):
             for model_class in self.all_model_classes:
                    if model_class.__name__ == "DPTForDepthEstimation":
                           continue

                    _UpperCAmelCase   ,						_UpperCAmelCase  :						Optional[Any]							   =					self.model_tester.prepare_config_and_inputs_for_common()
                    _UpperCAmelCase  :						int							   =					True

                    if model_class in get_values(A ):
                           continue

                    _UpperCAmelCase  :						List[str]							   =					model_class(A )
                    model.to(A )
                    model.train()
                    _UpperCAmelCase  :						Tuple							   =					self._prepare_for_class(A		,      A		,      return_labels=A )
                    _UpperCAmelCase  :						Optional[int]							   =					model(**A ).loss
                    loss.backward()




      def   _A			( self		:       Union[str, Any] ):
             for model_class in self.all_model_classes:
                    if model_class.__name__ == "DPTForDepthEstimation":
                           continue

                    _UpperCAmelCase   ,						_UpperCAmelCase  :						Optional[int]							   =					self.model_tester.prepare_config_and_inputs_for_common()
                    _UpperCAmelCase  :						List[Any]							   =					False
                    _UpperCAmelCase  :						Dict							   =					True

                    if model_class in get_values(A ) or not model_class.supports_gradient_checkpointing:
                           continue
                    _UpperCAmelCase  :						Tuple							   =					model_class(A )
                    model.to(A )
                    model.gradient_checkpointing_enable()
                    model.train()
                    _UpperCAmelCase  :						Any							   =					self._prepare_for_class(A		,      A		,      return_labels=A )
                    _UpperCAmelCase  :						Tuple							   =					model(**A ).loss
                    loss.backward()




      def   _A			( self		:       int ):
             _UpperCAmelCase   ,						_UpperCAmelCase  :						Dict							   =					self.model_tester.prepare_config_and_inputs_for_common()

             _UpperCAmelCase  :						Optional[Any]							   =					_config_zero_init(A )
             for model_class in self.all_model_classes:
                    _UpperCAmelCase  :						Optional[Any]							   =					model_class(config=A )
                    # Skip the check for the backbone
                    _UpperCAmelCase  :						Dict							   =					[]
                    for name, module in model.named_modules():
                           if module.__class__.__name__ == "DPTViTHybridEmbeddings":
                                  _UpperCAmelCase  :						Any							   =					[F"""{name}.{key}""" for key in module.state_dict().keys()]
                                  break

                    for name, param in model.named_parameters():
                           if param.requires_grad:
                                  if name in backbone_params:
                                         continue
                                  self.assertIn(
                                      ((param.data.mean() * 1E9).round() / 1E9).item()		,      [0.0, 1.0]		,      msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""		,      )




      @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests." )
      def   _A			( self		:       List[Any] ):
             pass





      @slow
      def   _A			( self		:       Union[str, Any] ):
             for model_name in DPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                    _UpperCAmelCase  :						Dict							   =					DPTModel.from_pretrained(A )
                    self.assertIsNotNone(A )

def 			UpperCamelCase_							(      )					->					Tuple:


       """simple docstring"""







       _UpperCAmelCase  :						Union[str, Any]							   =					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
       return image


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

      def   _A			( self		:       Any ):
             _UpperCAmelCase  :						Any							   =					DPTImageProcessor.from_pretrained("Intel/dpt-large" )
             _UpperCAmelCase  :						Optional[int]							   =					DPTForDepthEstimation.from_pretrained("Intel/dpt-large" ).to(A )

             _UpperCAmelCase  :						List[str]							   =					prepare_img()
             _UpperCAmelCase  :						str							   =					image_processor(images=A		,      return_tensors="pt" ).to(A )

             # forward pass
             with torch.no_grad():
                    _UpperCAmelCase  :						List[str]							   =					model(**A )
                    _UpperCAmelCase  :						int							   =					outputs.predicted_depth

             # verify the predicted depth
             _UpperCAmelCase  :						Any							   =					torch.Size((1, 384, 384) )
             self.assertEqual(predicted_depth.shape		,      A )

             _UpperCAmelCase  :						Optional[Any]							   =					torch.tensor(
                 [[6.3_199, 6.3_629, 6.4_148], [6.3_850, 6.3_615, 6.4_166], [6.3_519, 6.3_176, 6.3_575]] ).to(A )

             self.assertTrue(torch.allclose(outputs.predicted_depth[0, :3, :3]		,      A		,      atol=1E-4 ) )




      def   _A			( self		:       Any ):
             _UpperCAmelCase  :						Dict							   =					DPTImageProcessor.from_pretrained("Intel/dpt-large-ade" )
             _UpperCAmelCase  :						Union[str, Any]							   =					DPTForSemanticSegmentation.from_pretrained("Intel/dpt-large-ade" ).to(A )

             _UpperCAmelCase  :						Optional[Any]							   =					prepare_img()
             _UpperCAmelCase  :						Optional[int]							   =					image_processor(images=A		,      return_tensors="pt" ).to(A )

             # forward pass
             with torch.no_grad():
                    _UpperCAmelCase  :						List[str]							   =					model(**A )

             # verify the logits
             _UpperCAmelCase  :						Union[str, Any]							   =					torch.Size((1, 150, 480, 480) )
             self.assertEqual(outputs.logits.shape		,      A )

             _UpperCAmelCase  :						Dict							   =					torch.tensor(
                 [[4.0_480, 4.2_420, 4.4_360], [4.3_124, 4.5_693, 4.8_261], [4.5_768, 4.8_965, 5.2_163]] ).to(A )

             self.assertTrue(torch.allclose(outputs.logits[0, 0, :3, :3]		,      A		,      atol=1E-4 ) )





      def   _A			( self		:       List[str] ):
             _UpperCAmelCase  :						Dict							   =					DPTImageProcessor.from_pretrained("Intel/dpt-large-ade" )
             _UpperCAmelCase  :						Tuple							   =					DPTForSemanticSegmentation.from_pretrained("Intel/dpt-large-ade" ).to(A )

             _UpperCAmelCase  :						int							   =					prepare_img()
             _UpperCAmelCase  :						List[str]							   =					image_processor(images=A		,      return_tensors="pt" ).to(A )

             # forward pass
             with torch.no_grad():
                    _UpperCAmelCase  :						str							   =					model(**A )

             _UpperCAmelCase  :						str							   =					outputs.logits.detach().cpu()

             _UpperCAmelCase  :						Tuple							   =					image_processor.post_process_semantic_segmentation(outputs=A		,      target_sizes=[(500, 300)] )
             _UpperCAmelCase  :						List[str]							   =					torch.Size((500, 300) )
             self.assertEqual(segmentation[0].shape		,      A )

             _UpperCAmelCase  :						Dict							   =					image_processor.post_process_semantic_segmentation(outputs=A )
             _UpperCAmelCase  :						List[Any]							   =					torch.Size((480, 480) )
             self.assertEqual(segmentation[0].shape		,      A )


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

