


"""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:
 SCREAMING_SNAKE_CASE__				:  List[str]						= False


if is_vision_available():
 from PIL import Image
else:


 class 	A_       :





   """simple docstring"""

   @staticmethod
   def 							lowercase_     (						*__UpperCAmelCase  ,					**__UpperCAmelCase						)				->					List[Any]:
      pass


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





  """simple docstring"""

  lowercase		:	Tuple   =						MODEL_FOR_VISION_2_SEQ_MAPPING
  lowercase		:	List[str]   =						TF_MODEL_FOR_VISION_2_SEQ_MAPPING
  def 							lowercase_     (						self  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase						)				->					List[Any]:
     a   :	Tuple						=    pipeline('image-to-text'  ,					model=__UpperCAmelCase  ,					tokenizer=__UpperCAmelCase  ,					image_processor=__UpperCAmelCase						)
     a   :	Optional[Any]						=    [
         Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						),
         './tests/fixtures/tests_samples/COCO/000000039769.png',
     ]
     return pipe, examples
  def 							lowercase_     (						self  ,					__UpperCAmelCase  ,					__UpperCAmelCase						)				->					List[str]:
     a   :	Dict						=    pipe(__UpperCAmelCase						)
     self.assertEqual(
         __UpperCAmelCase  ,					[
             [{'generated_text': ANY(__UpperCAmelCase						)}],
             [{'generated_text': ANY(__UpperCAmelCase						)}],
         ]  ,					)
  @require_tf
  def 							lowercase_     (						self						)				->					int:
     a   :	Union[str, Any]						=    pipeline('image-to-text'  ,					model='hf-internal-testing/tiny-random-vit-gpt2'  ,					framework='tf'						)
     a   :	List[str]						=    './tests/fixtures/tests_samples/COCO/000000039769.png'

     a   :	Dict						=    pipe(__UpperCAmelCase						)
     self.assertEqual(
         __UpperCAmelCase  ,					[
             {
                 'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
             },
         ]  ,					)

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

     a   :	List[Any]						=    pipe(__UpperCAmelCase  ,					max_new_tokens=1						)
     self.assertEqual(
         __UpperCAmelCase  ,					[{'generated_text': 'growth'}]  ,					)
  @require_torch
  def 							lowercase_     (						self						)				->					Any:
     a   :	List[str]						=    pipeline('image-to-text'  ,					model='hf-internal-testing/tiny-random-vit-gpt2'						)
     a   :	Dict						=    './tests/fixtures/tests_samples/COCO/000000039769.png'

     a   :	Optional[Any]						=    pipe(__UpperCAmelCase						)
     self.assertEqual(
         __UpperCAmelCase  ,					[
             {
                 'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
             },
         ]  ,					)

     a   :	Optional[int]						=    pipe([image, image]						)
     self.assertEqual(
         __UpperCAmelCase  ,					[
             [
                 {
                     'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                 }
             ],
             [
                 {
                     'generated_text': 'growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO'
                 }
             ],
         ]  ,					)
  @require_torch
  def 							lowercase_     (						self						)				->					List[Any]:
     a   :	str						=    pipeline('image-to-text'  ,					model='hf-internal-testing/tiny-random-BlipForConditionalGeneration'						)
     a   :	int						=    './tests/fixtures/tests_samples/COCO/000000039769.png'
     a   :	Dict						=    'a photo of'

     a   :	str						=    pipe(__UpperCAmelCase  ,					prompt=__UpperCAmelCase						)
     self.assertTrue(outputs[0]['generated_text'].startswith(__UpperCAmelCase						)						)
  @slow
  @require_torch
  def 							lowercase_     (						self						)				->					int:
     a   :	Optional[int]						=    pipeline('image-to-text'  ,					model='ydshieh/vit-gpt2-coco-en'						)
     a   :	Optional[int]						=    './tests/fixtures/tests_samples/COCO/000000039769.png'

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

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

     a   :	str						=    pipe(__UpperCAmelCase						)
     self.assertEqual(__UpperCAmelCase  ,					[{'generated_text': 'a pink pokemon pokemon with a blue shirt and a blue shirt'}]						)
  @slow
  @require_torch
  def 							lowercase_     (						self						)				->					List[str]:
     a   :	Any						=    pipeline('image-to-text'  ,					model='microsoft/git-base-coco'						)
     a   :	Tuple						=    'https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png'
     a   :	Any						=    Image.open(requests.get(__UpperCAmelCase  ,					stream=__UpperCAmelCase						).raw						)

     a   :	Any						=    pipe(__UpperCAmelCase						)
     self.assertEqual(__UpperCAmelCase  ,					[{'generated_text': 'a cartoon of a purple character.'}]						)
  @slow
  @require_torch
  def 							lowercase_     (						self						)				->					Optional[int]:
     a   :	Any						=    pipeline('image-to-text'  ,					model='Salesforce/blip-image-captioning-base'						)
     a   :	Dict						=    'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg'
     a   :	List[Any]						=    Image.open(requests.get(__UpperCAmelCase  ,					stream=__UpperCAmelCase						).raw						)

     a   :	Tuple						=    'a photography of'

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

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

     a   :	int						=    'a photo of a'

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

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

     a   :	Tuple						=    pipe(__UpperCAmelCase  ,					prompt=__UpperCAmelCase						)
     self.assertEqual(__UpperCAmelCase  ,					[{'generated_text': 'ash cloud'}]						)

     with self.assertRaises(__UpperCAmelCase						):
        a   :	Tuple						=    pipe([image, image]  ,					prompt=[prompt, prompt]						)






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

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

     a   :	int						=    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"""






from transformers.models.cpm.tokenization_cpm import CpmTokenizer
from transformers.testing_utils import custom_tokenizers

from ..xlnet.test_modeling_xlnet import XLNetModelTest


@custom_tokenizers
class 	A_       (     _UpperCAmelCase   ):





  """simple docstring"""

  def 							lowercase_     (						self  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase  ,					__UpperCAmelCase						)				->					int:
     return True






  def 							lowercase_     (						self						)				->					int:
     a   :	Optional[int]						=    CpmTokenizer.from_pretrained('TsinghuaAI/CPM-Generate'						)
     a   :	Any						=    'Hugging Face大法好，谁用谁知道。'
     a   :	Tuple						=    'Hugging Face大法好,谁用谁知道。<unk>'
     a   :	Optional[int]						=    '▁Hu gg ing ▁ ▂ ▁F ace ▁大法 ▁好 ▁ , ▁谁 ▁用 ▁谁 ▁知 道 ▁ 。'.split()

     a   :	Optional[int]						=    tokenizer.tokenize(__UpperCAmelCase						)
     self.assertListEqual(__UpperCAmelCase  ,					__UpperCAmelCase						)

     a   :	Optional[Any]						=    tokens + [tokenizer.unk_token]

     a   :	str						=    [1_37_89, 1_32_83, 14_21, 8, 10, 11_64, 1_36_08, 1_65_28, 63, 8, 9, 4_40, 1_08, 4_40, 1_21, 90, 8, 12, 0]
     self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCAmelCase						)  ,					__UpperCAmelCase						)

     a   :	List[Any]						=    tokenizer.decode(__UpperCAmelCase						)
     self.assertEqual(__UpperCAmelCase  ,					__UpperCAmelCase						)





