






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


if is_vision_available():
     from PIL import Image
else:






     class 	lowercase_      :






           '''simple docstring'''




           @staticmethod
           def    __lowerCAmelCase      (   *__UpperCAmelCase   :		Optional[int]				,      **__UpperCAmelCase   :		Union[str, Any]						)					->List[Any]:




            """simple docstring"""


            pass






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






      '''simple docstring'''




      __snake_case  						=						MODEL_FOR_VISION_2_SEQ_MAPPING
      __snake_case  						=						TF_MODEL_FOR_VISION_2_SEQ_MAPPING







      def    __lowerCAmelCase      (   self   :		Union[str, Any]				,      __UpperCAmelCase   :		str				,      __UpperCAmelCase   :		str				,      __UpperCAmelCase   :		Dict						)					->Dict:




       """simple docstring"""


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







      def    __lowerCAmelCase      (   self   :		List[str]				,      __UpperCAmelCase   :		Tuple				,      __UpperCAmelCase   :		str						)					->Dict:




       """simple docstring"""


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







      @require_tf
      def    __lowerCAmelCase      (   self   :		str						)					->Union[str, Any]:




       """simple docstring"""


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

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

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

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







      @require_torch
      def    __lowerCAmelCase      (   self   :		Tuple						)					->str:




       """simple docstring"""


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

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

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







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




       """simple docstring"""


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

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







      @slow
      @require_torch
      def    __lowerCAmelCase      (   self   :		Any						)					->str:




       """simple docstring"""


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

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

       a							=   pipe([image, image]						)
       self.assertEqual(
           __UpperCAmelCase				,      [
               [{'''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    __lowerCAmelCase      (   self   :		int						)					->int:




       """simple docstring"""


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

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







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




       """simple docstring"""


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

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







      @slow
      @require_torch
      def    __lowerCAmelCase      (   self   :		Union[str, Any]						)					->str:




       """simple docstring"""


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

       a							=   '''a photography of'''

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

       with self.assertRaises(__UpperCAmelCase						):
        a							=   pipe([image, image]				,      prompt=[prompt, prompt]						)







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




       """simple docstring"""


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

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

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

       with self.assertRaises(__UpperCAmelCase						):
        a							=   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    __lowerCAmelCase      (   self   :		List[Any]						)					->List[Any]:




       """simple docstring"""


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

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

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

       with self.assertRaises(__UpperCAmelCase						):
        a							=   pipe([image, image]				,      prompt=[prompt, prompt]						)







      @slow
      @require_tf
      def    __lowerCAmelCase      (   self   :		List[str]						)					->Dict:




       """simple docstring"""


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

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

       a							=   pipe([image, image]						)
       self.assertEqual(
           __UpperCAmelCase				,      [
               [{'''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 '''}],
           ]				,      )










import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class 	lowercase_      (			lowercase     ):






      '''simple docstring'''




      __snake_case  						=						['''image_processor''', '''tokenizer''']
      __snake_case  						=						'''AutoImageProcessor'''
      __snake_case  						=						'''AutoTokenizer'''







      def __init__(   self   :		Any				,      __UpperCAmelCase   :		Union[str, Any]=None				,      __UpperCAmelCase   :		int=None				,      **__UpperCAmelCase   :		Union[str, Any]						)					->Dict:




       """simple docstring"""


       a							=   None
       if "feature_extractor" in kwargs:
        warnings.warn(
            '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
            ''' instead.'''				,      __UpperCAmelCase				,      )
        a							=   kwargs.pop('''feature_extractor'''						)

       a							=   image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
        raise ValueError('''You have to specify an image_processor.'''						)
       if tokenizer is None:
        raise ValueError('''You have to specify a tokenizer.'''						)

       super().__init__(__UpperCAmelCase				,      __UpperCAmelCase						)
       a							=   self.image_processor







      def __call__(   self   :		List[Any]				,      __UpperCAmelCase   :		Optional[Any]=None				,      __UpperCAmelCase   :		Optional[int]=None				,      __UpperCAmelCase   :		str=None				,      **__UpperCAmelCase   :		List[Any]						)					->Dict:




       """simple docstring"""


       if text is None and images is None:
        raise ValueError('''You have to specify either text or images. Both cannot be none.'''						)

       if text is not None:
        a							=   self.tokenizer(__UpperCAmelCase				,      return_tensors=__UpperCAmelCase				,      **__UpperCAmelCase						)

       if images is not None:
        a							=   self.image_processor(__UpperCAmelCase				,      return_tensors=__UpperCAmelCase				,      **__UpperCAmelCase						)

       if text is not None and images is not None:
        a							=   image_features.pixel_values
        return encoding
       elif text is not None:
        return encoding
       else:
        return BatchEncoding(data=dict(**__UpperCAmelCase						)				,      tensor_type=__UpperCAmelCase						)







      def    __lowerCAmelCase      (   self   :		List[Any]				,      *__UpperCAmelCase   :		Optional[Any]				,      **__UpperCAmelCase   :		List[str]						)					->Optional[Any]:




       """simple docstring"""


       return self.tokenizer.batch_decode(*__UpperCAmelCase				,      **__UpperCAmelCase						)







      def    __lowerCAmelCase      (   self   :		str				,      *__UpperCAmelCase   :		List[Any]				,      **__UpperCAmelCase   :		Optional[Any]						)					->Union[str, Any]:




       """simple docstring"""


       return self.tokenizer.decode(*__UpperCAmelCase				,      **__UpperCAmelCase						)







      @property
      def    __lowerCAmelCase      (   self   :		str						)					->str:




       """simple docstring"""


       a							=   self.tokenizer.model_input_names
       a							=   self.image_processor.model_input_names
       return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names						)						)







      @property
      def    __lowerCAmelCase      (   self   :		str						)					->Optional[Any]:




       """simple docstring"""


       warnings.warn(
           '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''				,      __UpperCAmelCase				,      )
       return self.image_processor_class







      @property
      def    __lowerCAmelCase      (   self   :		int						)					->Optional[Any]:




       """simple docstring"""


       warnings.warn(
           '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''				,      __UpperCAmelCase				,      )
       return self.image_processor


