




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


if is_vision_available():
    from PIL import Image
else:


    class        UpperCAmelCase__				:



         '''simple docstring'''
         @staticmethod
         def        lowerCAmelCase__  (				*_lowerCAmelCase ,    **_lowerCAmelCase	):
                pass


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



     '''simple docstring'''
     _SCREAMING_SNAKE_CASE      :					int     					=    MODEL_FOR_VISION_2_SEQ_MAPPING
     _SCREAMING_SNAKE_CASE      :					Tuple     					=    TF_MODEL_FOR_VISION_2_SEQ_MAPPING
     def        lowerCAmelCase__  (				self ,    _lowerCAmelCase ,    _lowerCAmelCase ,    _lowerCAmelCase	):
            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 ,    _lowerCAmelCase ,    _lowerCAmelCase	):
            a			   =pipe(__UpperCamelCase	)
            self.assertEqual(
                __UpperCamelCase ,    [
                    [{"""generated_text""": ANY(__UpperCamelCase	)}],
                    [{"""generated_text""": ANY(__UpperCamelCase	)}],
                ] ,    )
     @require_tf
     def        lowerCAmelCase__  (				self	):
            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	):
            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	):
            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	):
            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	):
            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	):
            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	):
            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	):
            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	):
            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	):
            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 """}],
                ] ,    )






'''simple docstring'''

import unittest
import warnings

from datasets.utils import experimental




@experimental
def    snake_case__     (  )			->       Union[str, Any]:





			'''simple docstring'''






			return "success"





class __magic_name__(						unittest.TestCase      ):






						def       __lowerCAmelCase(   self						:						List[Any]       ):



									'''simple docstring'''






									with warnings.catch_warnings(record=__UpperCamelCase       ) as w:
												warnings.simplefilter("""always"""       )
												self.assertEqual(dummy_function()					,   """success"""       )
									self.assertEqual(len(__UpperCamelCase       )					,   1       )