code: 
  
  
  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:
      __snake_case             =	False
  
  
  if is_vision_available():
      from PIL import Image
  else:
  
  
  
      class   __lowerCamelCase :
  
  
  
  
  
  
  
             @staticmethod
             def 		snake_case_  ( *A_:   List[str],**A_:   Any				):
  
  
  
  
  
  
                  '''simple docstring'''
  
  
  
  
                  pass
  
  
  
  @is_pipeline_test
  @require_vision
  class   __lowerCamelCase (unittest.TestCase	):
         _lowercase       							=    MODEL_FOR_VISION_2_SEQ_MAPPING
         _lowercase       							=    TF_MODEL_FOR_VISION_2_SEQ_MAPPING
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Dict,A_:   int,A_:   List[str],A_:   List[str]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model=A_,tokenizer=A_,image_processor=A_				)
              __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],A_:   Optional[Any],A_:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(
                  A_,[
                      [{'generated_text': ANY(A_				)}],
                      [{'generated_text': ANY(A_				)}],
                  ],)
  
  
  
  
  
  
  
         @require_tf
         def 		snake_case_  ( self:   str				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __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(A_				)
              self.assertEqual(
                  A_,[
                      {
                          'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                      },
                  ],)
  
              __UpperCamelCase   = pipe([image, image]				)
              self.assertEqual(
                  A_,[
                      [
                          {
                              'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                          }
                      ],
                      [
                          {
                              'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                          }
                      ],
                  ],)
  
              __UpperCamelCase   = pipe(A_,max_new_tokens=1				)
              self.assertEqual(
                  A_,[{'generated_text': 'growth'}],)
  
  
  
  
  
  
  
         @require_torch
         def 		snake_case_  ( self:   Any				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='hf-internal-testing/tiny-random-vit-gpt2'				)
              __UpperCamelCase   = './tests/fixtures/tests_samples/COCO/000000039769.png'
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(
                  A_,[
                      {
                          'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                      },
                  ],)
  
              __UpperCamelCase   = pipe([image, image]				)
              self.assertEqual(
                  A_,[
                      [
                          {
                              'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                          }
                      ],
                      [
                          {
                              'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                          }
                      ],
                  ],)
  
  
  
  
  
  
  
         @require_torch
         def 		snake_case_  ( self:   str				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __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(A_,prompt=A_				)
              self.assertTrue(outputs[0]['generated_text'].startswith(A_				)				)
  
  
  
  
  
  
  
         @slow
         @require_torch
         def 		snake_case_  ( self:   Dict				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='ydshieh/vit-gpt2-coco-en'				)
              __UpperCamelCase   = './tests/fixtures/tests_samples/COCO/000000039769.png'
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(A_,[{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}]				)
  
              __UpperCamelCase   = 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 		snake_case_  ( self:   Dict				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __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(A_,stream=A_				).raw				)
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(A_,[{'generated_text': 'a pink pokemon pokemon with a blue shirt and a blue shirt'}]				)
  
  
  
  
  
  
  
         @slow
         @require_torch
         def 		snake_case_  ( self:   Dict				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __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(A_,stream=A_				).raw				)
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(A_,[{'generated_text': 'a cartoon of a purple character.'}]				)
  
  
  
  
  
  
  
         @slow
         @require_torch
         def 		snake_case_  ( self:   Dict				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __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(A_,stream=A_				).raw				)
  
              __UpperCamelCase   = 'a photography of'
  
              __UpperCamelCase   = pipe(A_,prompt=A_				)
              self.assertEqual(A_,[{'generated_text': 'a photography of a volcano'}]				)
  
              with self.assertRaises(A_				):
                   __UpperCamelCase   = pipe([image, image],prompt=[prompt, prompt]				)
  
  
  
  
  
  
  
         @slow
         @require_torch
         def 		snake_case_  ( self:   List[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __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(A_,stream=A_				).raw				)
  
              __UpperCamelCase   = 'a photo of a'
  
              __UpperCamelCase   = 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   = 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:   List[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __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(A_,stream=A_				).raw				)
  
              __UpperCamelCase   = 'What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'
  
              __UpperCamelCase   = pipe(A_,prompt=A_				)
              self.assertEqual(A_,[{'generated_text': 'ash cloud'}]				)
  
              with self.assertRaises(A_				):
                   __UpperCamelCase   = pipe([image, image],prompt=[prompt, prompt]				)
  
  
  
  
  
  
  
         @slow
         @require_tf
         def 		snake_case_  ( self:   Any				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = pipeline('image-to-text',model='ydshieh/vit-gpt2-coco-en',framework='tf'				)
              __UpperCamelCase   = './tests/fixtures/tests_samples/COCO/000000039769.png'
  
              __UpperCamelCase   = pipe(A_				)
              self.assertEqual(A_,[{'generated_text': 'a cat laying on a blanket next to a cat laying on a bed '}]				)
  
              __UpperCamelCase   = 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 '}],
                  ],)
  
  
  
style_context: 
  
  
  import unittest
  
  import numpy as np
  
  from transformers.testing_utils import require_torch, require_vision
  from transformers.utils import is_torch_available, is_vision_available
  
  from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs
  
  
  if is_torch_available():
      import torch
  
  if is_vision_available():
      from PIL import Image
  
      from transformers import DeiTImageProcessor
  
  
  
  class   __lowerCamelCase (unittest.TestCase	):
  
  
  
  
  
  
  
         def __init__( self:   List[str],A_:   List[Any],A_:   List[Any]=7,A_:   str=3,A_:   Tuple=18,A_:   Union[str, Any]=30,A_:   Union[str, Any]=400,A_:   List[str]=True,A_:   List[Any]=None,A_:   Tuple=True,A_:   Union[str, Any]=None,A_:   int=True,A_:   Any=[0.5, 0.5, 0.5],A_:   Optional[Any]=[0.5, 0.5, 0.5],):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = size if size is not None else {'height': 20, 'width': 20}
              __UpperCamelCase   = crop_size if crop_size is not None else {'height': 18, 'width': 18}
  
              __UpperCamelCase   = parent
              __UpperCamelCase   = batch_size
              __UpperCamelCase   = num_channels
              __UpperCamelCase   = image_size
              __UpperCamelCase   = min_resolution
              __UpperCamelCase   = max_resolution
              __UpperCamelCase   = do_resize
              __UpperCamelCase   = size
              __UpperCamelCase   = do_center_crop
              __UpperCamelCase   = crop_size
              __UpperCamelCase   = do_normalize
              __UpperCamelCase   = image_mean
              __UpperCamelCase   = image_std
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[str]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              return {
                  "do_resize": self.do_resize,
                  "size": self.size,
                  "do_center_crop": self.do_center_crop,
                  "crop_size": self.crop_size,
                  "do_normalize": self.do_normalize,
                  "image_mean": self.image_mean,
                  "image_std": self.image_std,
              }
  
  
  
  @require_torch
  @require_vision
  class   __lowerCamelCase (_a							,			unittest.TestCase	):
         _lowercase       							=    DeiTImageProcessor if is_vision_available() else None
         _lowercase       							=    True
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = DeiTImageProcessingTester(self				)
  
  
  
  
  
  
  
         @property
         def 		snake_case_  ( self:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              return self.image_processor_tester.prepare_image_processor_dict()
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Union[str, Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.image_processing_class(**self.image_processor_dict				)
              self.assertTrue(hasattr(A_,'do_resize'				)				)
              self.assertTrue(hasattr(A_,'size'				)				)
              self.assertTrue(hasattr(A_,'do_center_crop'				)				)
              self.assertTrue(hasattr(A_,'center_crop'				)				)
              self.assertTrue(hasattr(A_,'do_normalize'				)				)
              self.assertTrue(hasattr(A_,'image_mean'				)				)
              self.assertTrue(hasattr(A_,'image_std'				)				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.image_processing_class.from_dict(self.image_processor_dict				)
              self.assertEqual(image_processor.size,{'height': 20, 'width': 20}				)
              self.assertEqual(image_processor.crop_size,{'height': 18, 'width': 18}				)
  
              __UpperCamelCase   = self.image_processing_class.from_dict(self.image_processor_dict,size=42,crop_size=84				)
              self.assertEqual(image_processor.size,{'height': 42, 'width': 42}				)
              self.assertEqual(image_processor.crop_size,{'height': 84, 'width': 84}				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Tuple				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              pass
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.image_processing_class(**self.image_processor_dict				)
              # create random PIL images
              __UpperCamelCase   = prepare_image_inputs(self.image_processor_tester,equal_resolution=A_				)
              for image in image_inputs:
                   self.assertIsInstance(A_,Image.Image				)
  
              # Test not batched input
              __UpperCamelCase   = image_processing(image_inputs[0],return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      1,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
              # Test batched
              __UpperCamelCase   = image_processing(A_,return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[str]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.image_processing_class(**self.image_processor_dict				)
              # create random numpy tensors
              __UpperCamelCase   = prepare_image_inputs(self.image_processor_tester,equal_resolution=A_,numpify=A_				)
              for image in image_inputs:
                   self.assertIsInstance(A_,np.ndarray				)
  
              # Test not batched input
              __UpperCamelCase   = image_processing(image_inputs[0],return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      1,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
              # Test batched
              __UpperCamelCase   = image_processing(A_,return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Any				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self.image_processing_class(**self.image_processor_dict				)
              # create random PyTorch tensors
              __UpperCamelCase   = prepare_image_inputs(self.image_processor_tester,equal_resolution=A_,torchify=A_				)
              for image in image_inputs:
                   self.assertIsInstance(A_,torch.Tensor				)
  
              # Test not batched input
              __UpperCamelCase   = image_processing(image_inputs[0],return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      1,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
              # Test batched
              __UpperCamelCase   = image_processing(A_,return_tensors='pt'				).pixel_values
              self.assertEqual(
                  encoded_images.shape,(
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size['height'],
                      self.image_processor_tester.crop_size['width'],
                  ),)
  
  
  