




'''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:
     UpperCAmelCase_				:     Optional[int]     			=						False


if is_vision_available():
     from PIL import Image
else:





     class 			lowerCAmelCase      :


            @staticmethod
            def 					lowerCAmelCase					(      *__SCREAMING_SNAKE_CASE			,     **__SCREAMING_SNAKE_CASE     )					->     Any:



                 '''simple docstring'''




                 pass





@is_pipeline_test
@require_vision
class 			lowerCAmelCase      (       unittest.TestCase):
       __lowercase     :					int       				=	MODEL_FOR_VISION_2_SEQ_MAPPING
       __lowercase     :					Union[str, Any]       				=	TF_MODEL_FOR_VISION_2_SEQ_MAPPING


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )					->     Tuple:



            '''simple docstring'''




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


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )					->     str:



            '''simple docstring'''




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


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



            '''simple docstring'''




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

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

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

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


       @require_torch
       def 					lowerCAmelCase					(      self     )					->     Any:



            '''simple docstring'''




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

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

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


       @require_torch
       def 					lowerCAmelCase					(      self     )					->     Optional[Any]:



            '''simple docstring'''




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

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


       @slow
       @require_torch
       def 					lowerCAmelCase					(      self     )					->     int:



            '''simple docstring'''




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

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

            __snake_case			    =     pipe([image, image]     )
            self.assertEqual(
                __SCREAMING_SNAKE_CASE			,     [
                    [{'''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     )					->     Dict:



            '''simple docstring'''




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

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


       @slow
       @require_torch
       def 					lowerCAmelCase					(      self     )					->     Any:



            '''simple docstring'''




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

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


       @slow
       @require_torch
       def 					lowerCAmelCase					(      self     )					->     Tuple:



            '''simple docstring'''




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

            __snake_case			    =     '''a photography of'''

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

            with self.assertRaises(__SCREAMING_SNAKE_CASE     ):
                 __snake_case			    =     pipe([image, image]			,     prompt=[prompt, prompt]     )


       @slow
       @require_torch
       def 					lowerCAmelCase					(      self     )					->     Tuple:



            '''simple docstring'''




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

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

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

            with self.assertRaises(__SCREAMING_SNAKE_CASE     ):
                 __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 					lowerCAmelCase					(      self     )					->     List[Any]:



            '''simple docstring'''




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

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

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

            with self.assertRaises(__SCREAMING_SNAKE_CASE     ):
                 __snake_case			    =     pipe([image, image]			,     prompt=[prompt, prompt]     )




       @slow
       @require_tf
       def 					lowerCAmelCase					(      self     )					->     Tuple:



            '''simple docstring'''




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

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

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












'''simple docstring'''



import warnings

from ...utils import logging
from .image_processing_detr import DetrImageProcessor


UpperCAmelCase_				:     Dict     			=						logging.get_logger(__name__)





class 			lowerCAmelCase      (       __lowerCAmelCase):


       def __init__(      self			,     *__SCREAMING_SNAKE_CASE			,     **__SCREAMING_SNAKE_CASE     )					->     None:



            '''simple docstring'''




            warnings.warn(
                '''The class DetrFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
                ''' Please use DetrImageProcessor instead.'''			,     __SCREAMING_SNAKE_CASE			,     )
            super().__init__(*__SCREAMING_SNAKE_CASE			,     **__SCREAMING_SNAKE_CASE     )






