



"""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[Any]     				=	False


if is_vision_available():
     from PIL import Image
else:


     class 	a__		:


        @staticmethod
        def 							lowercase					(				*lowerCAmelCase   :				Optional[int],  **lowerCAmelCase   :				Optional[Any]      )     ->							List[Any]:
              pass


@is_pipeline_test
@require_vision
class 	a__		(    unittest.TestCase  ):
   _lowerCamelCase       				=					MODEL_FOR_VISION_2_SEQ_MAPPING
   _lowerCamelCase       				=					TF_MODEL_FOR_VISION_2_SEQ_MAPPING


   def 							lowercase					(				self   :				Union[str, Any],  lowerCAmelCase   :				Optional[Any],  lowerCAmelCase   :				str,  lowerCAmelCase   :				str      )     ->							str:
         lowercase	:   Tuple											= pipeline('image-to-text',  model=lowerCAmelCase,  tokenizer=lowerCAmelCase,  image_processor=lowerCAmelCase      )
         lowercase	:   List[Any]											= [
             Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'      ),
             './tests/fixtures/tests_samples/COCO/000000039769.png',
         ]
         return pipe, examples


   def 							lowercase					(				self   :				List[Any],  lowerCAmelCase   :				List[Any],  lowerCAmelCase   :				List[Any]      )     ->							List[Any]:
         lowercase	:   str											= pipe(lowerCAmelCase      )
         self.assertEqual(
             lowerCAmelCase,  [
                 [{'generated_text': ANY(lowerCAmelCase      )}],
                 [{'generated_text': ANY(lowerCAmelCase      )}],
             ],  )


   @require_tf
   def 							lowercase					(				self   :				Optional[Any]      )     ->							Tuple:
         lowercase	:   Tuple											= pipeline('image-to-text',  model='hf-internal-testing/tiny-random-vit-gpt2',  framework='tf'      )
         lowercase	:   Optional[int]											= './tests/fixtures/tests_samples/COCO/000000039769.png'

         lowercase	:   str											= pipe(lowerCAmelCase      )
         self.assertEqual(
             lowerCAmelCase,  [
                 {
                     'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                 },
             ],  )

         lowercase	:   Optional[Any]											= pipe([image, image]      )
         self.assertEqual(
             lowerCAmelCase,  [
                 [
                     {
                         'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                     }
                 ],
                 [
                     {
                         'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                     }
                 ],
             ],  )

         lowercase	:   Union[str, Any]											= pipe(lowerCAmelCase,  max_new_tokens=1      )
         self.assertEqual(
             lowerCAmelCase,  [{'generated_text': 'growth'}],  )


   @require_torch
   def 							lowercase					(				self   :				List[Any]      )     ->							Tuple:
         lowercase	:   List[str]											= pipeline('image-to-text',  model='hf-internal-testing/tiny-random-vit-gpt2'      )
         lowercase	:   Any											= './tests/fixtures/tests_samples/COCO/000000039769.png'

         lowercase	:   str											= pipe(lowerCAmelCase      )
         self.assertEqual(
             lowerCAmelCase,  [
                 {
                     'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                 },
             ],  )

         lowercase	:   Any											= pipe([image, image]      )
         self.assertEqual(
             lowerCAmelCase,  [
                 [
                     {
                         'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                     }
                 ],
                 [
                     {
                         'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                     }
                 ],
             ],  )


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

         lowercase	:   List[Any]											= pipe(lowerCAmelCase,  prompt=lowerCAmelCase      )
         self.assertTrue(outputs[0]['generated_text'].startswith(lowerCAmelCase      )      )


   @slow
   @require_torch
   def 							lowercase					(				self   :				Optional[int]      )     ->							List[str]:
         lowercase	:   Dict											= pipeline('image-to-text',  model='ydshieh/vit-gpt2-coco-en'      )
         lowercase	:   Union[str, Any]											= './tests/fixtures/tests_samples/COCO/000000039769.png'

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

         lowercase	:   Dict											= 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 							lowercase					(				self   :				Optional[int]      )     ->							Dict:
         lowercase	:   List[str]											= pipeline('image-to-text',  model='Salesforce/blip-image-captioning-base'      )
         lowercase	:   List[str]											= 'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
         lowercase	:   Tuple											= Image.open(requests.get(lowerCAmelCase,  stream=lowerCAmelCase      ).raw      )

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


   @slow
   @require_torch
   def 							lowercase					(				self   :				Optional[Any]      )     ->							Any:
         lowercase	:   Union[str, Any]											= pipeline('image-to-text',  model='microsoft/git-base-coco'      )
         lowercase	:   List[Any]											= 'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
         lowercase	:   Tuple											= Image.open(requests.get(lowerCAmelCase,  stream=lowerCAmelCase      ).raw      )

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


   @slow
   @require_torch
   def 							lowercase					(				self   :				Any      )     ->							Optional[Any]:
         lowercase	:   List[Any]											= pipeline('image-to-text',  model='Salesforce/blip-image-captioning-base'      )
         lowercase	:   int											= 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
         lowercase	:   List[Any]											= Image.open(requests.get(lowerCAmelCase,  stream=lowerCAmelCase      ).raw      )

         lowercase	:   int											= 'a photography of'

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

         with self.assertRaises(lowerCAmelCase      ):
               lowercase	:   str											= pipe([image, image],  prompt=[prompt, prompt]      )


   @slow
   @require_torch
   def 							lowercase					(				self   :				List[Any]      )     ->							List[Any]:
         lowercase	:   Optional[Any]											= pipeline('image-to-text',  model='microsoft/git-base-coco'      )
         lowercase	:   int											= 'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
         lowercase	:   Tuple											= Image.open(requests.get(lowerCAmelCase,  stream=lowerCAmelCase      ).raw      )

         lowercase	:   List[Any]											= 'a photo of a'

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

         lowercase	:   Optional[int]											= 'What does the label 15 represent? (1) lava (2) core (3) tunnel (4) ash cloud'

         lowercase	:   Any											= pipe(lowerCAmelCase,  prompt=lowerCAmelCase      )
         self.assertEqual(lowerCAmelCase,  [{'generated_text': 'ash cloud'}]      )

         with self.assertRaises(lowerCAmelCase      ):
               lowercase	:   str											= pipe([image, image],  prompt=[prompt, prompt]      )






   @slow
   @require_tf
   def 							lowercase					(				self   :				Any      )     ->							List[Any]:
         lowercase	:   List[str]											= pipeline('image-to-text',  model='ydshieh/vit-gpt2-coco-en',  framework='tf'      )
         lowercase	:   Tuple											= './tests/fixtures/tests_samples/COCO/000000039769.png'

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

         lowercase	:   str											= 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 '}],
             ],  )





"""simple docstring"""




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


_UpperCamelCase:					Optional[Any]     				=	{
    'configuration_codegen': ['CODEGEN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CodeGenConfig', 'CodeGenOnnxConfig'],
    'tokenization_codegen': ['CodeGenTokenizer'],
}

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCamelCase:					Union[str, Any]     				=	['CodeGenTokenizerFast']

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _UpperCamelCase:					str     				=	[
         'CODEGEN_PRETRAINED_MODEL_ARCHIVE_LIST',
         'CodeGenForCausalLM',
         'CodeGenModel',
         'CodeGenPreTrainedModel',
     ]

if TYPE_CHECKING:
     from .configuration_codegen import CODEGEN_PRETRAINED_CONFIG_ARCHIVE_MAP, CodeGenConfig, CodeGenOnnxConfig
     from .tokenization_codegen import CodeGenTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_codegen_fast import CodeGenTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_codegen import (
              CODEGEN_PRETRAINED_MODEL_ARCHIVE_LIST,
              CodeGenForCausalLM,
              CodeGenModel,
              CodeGenPreTrainedModel,
          )

else:
     import sys

     _UpperCamelCase:					int     				=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
