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


if is_vision_available():
     from PIL import Image
else:







     class      SCREAMING_SNAKE_CASE__					:

            @staticmethod
            def 			snake_case__    (							*_lowerCAmelCase					:     int			,			**_lowerCAmelCase					:     List[Any]			):
                pass







@is_pipeline_test
@require_vision
class      SCREAMING_SNAKE_CASE__					(  unittest.TestCase    ):
       A							:	List[Any]          =	MODEL_FOR_VISION_2_SEQ_MAPPING
       A							:	Union[str, Any]          =	TF_MODEL_FOR_VISION_2_SEQ_MAPPING

       def 			snake_case__    (							self					:     Union[str, Any]			,			_lowerCAmelCase					:     int			,			_lowerCAmelCase					:     Optional[int]			,			_lowerCAmelCase					:     Optional[Any]			):
           __snake_case       :				Any 			=    pipeline("""image-to-text"""			,			model=_lowerCAmelCase			,			tokenizer=_lowerCAmelCase			,			image_processor=_lowerCAmelCase			)
           __snake_case       :				List[str] 			=    [
               Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			),
               """./tests/fixtures/tests_samples/COCO/000000039769.png""",
           ]
           return pipe, examples

       def 			snake_case__    (							self					:     int			,			_lowerCAmelCase					:     Optional[Any]			,			_lowerCAmelCase					:     List[str]			):
           __snake_case       :				List[str] 			=    pipe(_lowerCAmelCase			)
           self.assertEqual(
               _lowerCAmelCase			,			[
                   [{"""generated_text""": ANY(_lowerCAmelCase			)}],
                   [{"""generated_text""": ANY(_lowerCAmelCase			)}],
               ]			,			)

       @require_tf
       def 			snake_case__    (							self					:     List[str]			):
           __snake_case       :				Union[str, Any] 			=    pipeline("""image-to-text"""			,			model="""hf-internal-testing/tiny-random-vit-gpt2"""			,			framework="""tf"""			)
           __snake_case       :				str 			=    """./tests/fixtures/tests_samples/COCO/000000039769.png"""

           __snake_case       :				Any 			=    pipe(_lowerCAmelCase			)
           self.assertEqual(
               _lowerCAmelCase			,			[
                   {
                       """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                   },
               ]			,			)

           __snake_case       :				Any 			=    pipe([image, image]			)
           self.assertEqual(
               _lowerCAmelCase			,			[
                   [
                       {
                           """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                       }
                   ],
                   [
                       {
                           """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                       }
                   ],
               ]			,			)

           __snake_case       :				Optional[Any] 			=    pipe(_lowerCAmelCase			,			max_new_tokens=1			)
           self.assertEqual(
               _lowerCAmelCase			,			[{"""generated_text""": """growth"""}]			,			)

       @require_torch
       def 			snake_case__    (							self					:     Optional[Any]			):
           __snake_case       :				str 			=    pipeline("""image-to-text"""			,			model="""hf-internal-testing/tiny-random-vit-gpt2"""			)
           __snake_case       :				Optional[int] 			=    """./tests/fixtures/tests_samples/COCO/000000039769.png"""

           __snake_case       :				Tuple 			=    pipe(_lowerCAmelCase			)
           self.assertEqual(
               _lowerCAmelCase			,			[
                   {
                       """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                   },
               ]			,			)

           __snake_case       :				Union[str, Any] 			=    pipe([image, image]			)
           self.assertEqual(
               _lowerCAmelCase			,			[
                   [
                       {
                           """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                       }
                   ],
                   [
                       {
                           """generated_text""": """growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"""
                       }
                   ],
               ]			,			)

       @require_torch
       def 			snake_case__    (							self					:     int			):
           __snake_case       :				Union[str, Any] 			=    pipeline("""image-to-text"""			,			model="""hf-internal-testing/tiny-random-BlipForConditionalGeneration"""			)
           __snake_case       :				List[str] 			=    """./tests/fixtures/tests_samples/COCO/000000039769.png"""
           __snake_case       :				List[str] 			=    """a photo of"""

           __snake_case       :				Union[str, Any] 			=    pipe(_lowerCAmelCase			,			prompt=_lowerCAmelCase			)
           self.assertTrue(outputs[0]["""generated_text"""].startswith(_lowerCAmelCase			)			)

       @slow
       @require_torch
       def 			snake_case__    (							self					:     Optional[int]			):
           __snake_case       :				Any 			=    pipeline("""image-to-text"""			,			model="""ydshieh/vit-gpt2-coco-en"""			)
           __snake_case       :				List[Any] 			=    """./tests/fixtures/tests_samples/COCO/000000039769.png"""

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

           __snake_case       :				int 			=    pipe([image, image]			)
           self.assertEqual(
               _lowerCAmelCase			,			[
                   [{"""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					:     Optional[Any]			):
           __snake_case       :				int 			=    pipeline("""image-to-text"""			,			model="""Salesforce/blip-image-captioning-base"""			)
           __snake_case       :				Optional[int] 			=    """https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"""
           __snake_case       :				Optional[Any] 			=    Image.open(requests.get(_lowerCAmelCase			,			stream=_lowerCAmelCase			).raw			)

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

       @slow
       @require_torch
       def 			snake_case__    (							self					:     Union[str, Any]			):
           __snake_case       :				List[Any] 			=    pipeline("""image-to-text"""			,			model="""microsoft/git-base-coco"""			)
           __snake_case       :				Dict 			=    """https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"""
           __snake_case       :				int 			=    Image.open(requests.get(_lowerCAmelCase			,			stream=_lowerCAmelCase			).raw			)

           __snake_case       :				List[Any] 			=    pipe(_lowerCAmelCase			)
           self.assertEqual(_lowerCAmelCase			,			[{"""generated_text""": """a cartoon of a purple character."""}]			)

       @slow
       @require_torch
       def 			snake_case__    (							self					:     Tuple			):
           __snake_case       :				str 			=    pipeline("""image-to-text"""			,			model="""Salesforce/blip-image-captioning-base"""			)
           __snake_case       :				str 			=    """https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"""
           __snake_case       :				Optional[Any] 			=    Image.open(requests.get(_lowerCAmelCase			,			stream=_lowerCAmelCase			).raw			)

           __snake_case       :				Optional[int] 			=    """a photography of"""

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

           with self.assertRaises(_lowerCAmelCase			):
               __snake_case       :				Optional[int] 			=    pipe([image, image]			,			prompt=[prompt, prompt]			)

       @slow
       @require_torch
       def 			snake_case__    (							self					:     Union[str, Any]			):
           __snake_case       :				Dict 			=    pipeline("""image-to-text"""			,			model="""microsoft/git-base-coco"""			)
           __snake_case       :				Optional[Any] 			=    """https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"""
           __snake_case       :				Tuple 			=    Image.open(requests.get(_lowerCAmelCase			,			stream=_lowerCAmelCase			).raw			)

           __snake_case       :				int 			=    """a photo of a"""

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

           with self.assertRaises(_lowerCAmelCase			):
               __snake_case       :				Union[str, Any] 			=    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]			):
           __snake_case       :				List[Any] 			=    pipeline("""image-to-text"""			,			model="""google/pix2struct-ai2d-base"""			)
           __snake_case       :				Any 			=    """https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"""
           __snake_case       :				int 			=    Image.open(requests.get(_lowerCAmelCase			,			stream=_lowerCAmelCase			).raw			)

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

           __snake_case       :				Dict 			=    pipe(_lowerCAmelCase			,			prompt=_lowerCAmelCase			)
           self.assertEqual(_lowerCAmelCase			,			[{"""generated_text""": """ash cloud"""}]			)

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





       @slow
       @require_tf
       def 			snake_case__    (							self					:     str			):
           __snake_case       :				Optional[int] 			=    pipeline("""image-to-text"""			,			model="""ydshieh/vit-gpt2-coco-en"""			,			framework="""tf"""			)
           __snake_case       :				Tuple 			=    """./tests/fixtures/tests_samples/COCO/000000039769.png"""

           __snake_case       :				Union[str, Any] 			=    pipe(_lowerCAmelCase			)
           self.assertEqual(_lowerCAmelCase			,			[{"""generated_text""": """a cat laying on a blanket next to a cat laying on a bed """}]			)

           __snake_case       :				Tuple 			=    pipe([image, image]			)
           self.assertEqual(
               _lowerCAmelCase			,			[
                   [{"""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 torch
from torch import autograd







class      SCREAMING_SNAKE_CASE__					(  autograd.Function    ):

       @staticmethod
       def 			snake_case__    (							_lowerCAmelCase					:     Any			,			_lowerCAmelCase					:     torch.tensor			,			_lowerCAmelCase					:     float			,			_lowerCAmelCase					:     bool			):
           __snake_case       :				Optional[int] 			=    inputs.numel()
           __snake_case       :				str 			=    int(0.005 * nb_elems			) + 1
           if sigmoid:
               __snake_case       :				Union[str, Any] 			=    (torch.sigmoid(_lowerCAmelCase			) > threshold).type(inputs.type()			)
           else:
               __snake_case       :				Any 			=    (inputs > threshold).type(inputs.type()			)
           if mask.sum() < nb_min:
               # We limit the pruning so that at least 0.5% (half a percent) of the weights are remaining
               __snake_case       :				Dict 			=    inputs.flatten().kthvalue(max(nb_elems - nb_min			,			1			)			).values
               __snake_case       :				List[str] 			=    (inputs > k_threshold).type(inputs.type()			)
           return mask





       @staticmethod
       def 			snake_case__    (							_lowerCAmelCase					:     str			,			_lowerCAmelCase					:     Tuple			):
           return gradOutput, None, None







class      SCREAMING_SNAKE_CASE__					(  autograd.Function    ):

       @staticmethod
       def 			snake_case__    (							_lowerCAmelCase					:     Optional[int]			,			_lowerCAmelCase					:     torch.tensor			,			_lowerCAmelCase					:     float			):
           __snake_case       :				str 			=    inputs.clone()
           __snake_case			, __snake_case       :				Dict 			=    inputs.flatten().sort(descending=_lowerCAmelCase			)
           __snake_case       :				Optional[Any] 			=    int(threshold * inputs.numel()			)

           # flat_out and mask access the same memory.
           __snake_case       :				Optional[int] 			=    mask.flatten()
           __snake_case       :				Dict 			=    0
           __snake_case       :				Optional[Any] 			=    1
           return mask





       @staticmethod
       def 			snake_case__    (							_lowerCAmelCase					:     Any			,			_lowerCAmelCase					:     Optional[Any]			):
           return gradOutput, None







class      SCREAMING_SNAKE_CASE__					(  __UpperCamelCase    ):

       @staticmethod
       def 			snake_case__    (							_lowerCAmelCase					:     torch.tensor			,			_lowerCAmelCase					:     float			):
           __snake_case       :				List[str] 			=    inputs.clone()
           __snake_case			, __snake_case       :				str 			=    inputs.abs().flatten().sort(descending=_lowerCAmelCase			)
           __snake_case       :				List[Any] 			=    int(threshold * inputs.numel()			)

           # flat_out and mask access the same memory.
           __snake_case       :				List[Any] 			=    mask.flatten()
           __snake_case       :				int 			=    0
           __snake_case       :				Optional[int] 			=    1
           return mask



