


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:
							snake_case_  :	Tuple							  =							False


if is_vision_available():
							from PIL import Image
else:

							class   A__							:




													@staticmethod
													def 							__UpperCamelCase (				*_a    :					Tuple ,			**_a    :					int     )							->   int:


																	"""simple docstring"""





																	pass

@is_pipeline_test
@require_vision
class   A__							(						unittest.TestCase     ):
						UpperCAmelCase									=  MODEL_FOR_VISION_2_SEQ_MAPPING
						UpperCAmelCase									=  TF_MODEL_FOR_VISION_2_SEQ_MAPPING




						def 							__UpperCamelCase (				self    :					Tuple ,			_a    :					Optional[int] ,			_a    :					str ,			_a    :					int     )							->   Tuple:


										"""simple docstring"""





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




						def 							__UpperCamelCase (				self    :					int ,			_a    :					Any ,			_a    :					List[str]     )							->   str:


										"""simple docstring"""





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




						@require_tf
						def 							__UpperCamelCase (				self    :					List[Any]     )							->   Optional[int]:


										"""simple docstring"""





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

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

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

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




						@require_torch
						def 							__UpperCamelCase (				self    :					str     )							->   str:


										"""simple docstring"""





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

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

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




						@require_torch
						def 							__UpperCamelCase (				self    :					List[Any]     )							->   List[Any]:


										"""simple docstring"""





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

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




						@slow
						@require_torch
						def 							__UpperCamelCase (				self    :					str     )							->   Dict:


										"""simple docstring"""





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

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

										_SCREAMING_SNAKE_CASE	     =pipe([image, image]     )
										self.assertEqual(
										    _a ,			[
										        [{'''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 							__UpperCamelCase (				self    :					Dict     )							->   Any:


										"""simple docstring"""





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

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




						@slow
						@require_torch
						def 							__UpperCamelCase (				self    :					Tuple     )							->   Optional[int]:


										"""simple docstring"""





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

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




						@slow
						@require_torch
						def 							__UpperCamelCase (				self    :					Optional[Any]     )							->   Optional[int]:


										"""simple docstring"""





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

										_SCREAMING_SNAKE_CASE	     ='''a photography of'''

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

										with self.assertRaises(_a     ):
														_SCREAMING_SNAKE_CASE	     =pipe([image, image] ,			prompt=[prompt, prompt]     )




						@slow
						@require_torch
						def 							__UpperCamelCase (				self    :					int     )							->   str:


										"""simple docstring"""





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

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

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

										with self.assertRaises(_a     ):
														_SCREAMING_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 							__UpperCamelCase (				self    :					List[Any]     )							->   Optional[Any]:


										"""simple docstring"""





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

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

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

										with self.assertRaises(_a     ):
														_SCREAMING_SNAKE_CASE	     =pipe([image, image] ,			prompt=[prompt, prompt]     )




						@slow
						@require_tf
						def 							__UpperCamelCase (				self    :					List[Any]     )							->   List[Any]:


										"""simple docstring"""





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

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

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

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
   import torch

   from transformers import CamembertModel






@require_torch
@require_sentencepiece
@require_tokenizers
class 						snake_case  (  unittest.TestCase       ):





    @slow
    def 				lowerCamelCase__      (							self	:					str			):


      '''simple docstring'''




      a     :			int				=   CamembertModel.from_pretrained('camembert-base'			)
      model.to(A			)

      a     :			List[Any]				=   torch.tensor(
          [[5, 1_2_1, 1_1, 6_6_0, 1_6, 7_3_0, 2_5_5_4_3, 1_1_0, 8_3, 6]]				,		device=A				,		dtype=torch.long				,		)  # J'aime le camembert !
      with torch.no_grad():
        a     :			int				=   model(A			)['last_hidden_state']
      a     :			Union[str, Any]				=   torch.Size((1, 1_0, 7_6_8)			)
      self.assertEqual(output.shape				,		A			)
      # compare the actual values for a slice.
      a     :			List[str]				=   torch.tensor(
          [[[-0.02_54, 0.02_35, 0.10_27], [0.06_06, -0.18_11, -0.04_18], [-0.15_61, -0.11_27, 0.26_87]]]				,		device=A				,		dtype=torch.float				,		)
      # camembert = torch.hub.load('pytorch/fairseq', 'camembert.v0')
      # camembert.eval()
      # expected_slice = roberta.model.forward(input_ids)[0][:, :3, :3].detach()

      self.assertTrue(torch.allclose(output[:, :3, :3]				,		A				,		atol=1E-4			)			)




