

'''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:
     lowerCamelCase				:   Optional[Any]      				=  False


if is_vision_available():
     from PIL import Image
else:







     class 							A__					:





       @staticmethod
       def 		A						(	*_a   :			Any							,      **_a   :			str				)			->					Optional[int]:





            '''simple docstring'''






            pass







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





  def 		A						(	self   :			List[str]							,      _a   :			List[Any]							,      _a   :			Any							,      _a   :			Dict				)			->					Any:





       '''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 		A						(	self   :			str							,      _a   :			int							,      _a   :			Tuple				)			->					Optional[int]:





       '''simple docstring'''






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





  @require_tf
  def 		A						(	self   :			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 		A						(	self   :			List[str]				)			->					Tuple:





       '''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 		A						(	self   :			Union[str, Any]				)			->					Tuple:





       '''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 		A						(	self   :			Any				)			->					List[str]:





       '''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 		A						(	self   :			List[Any]				)			->					List[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 		A						(	self   :			str				)			->					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 		A						(	self   :			Any				)			->					Dict:





       '''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 		A						(	self   :			str				)			->					Dict:





       '''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 		A						(	self   :			List[Any]				)			->					Union[str, 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 		A						(	self   :			List[str]				)			->					Tuple:





       '''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'''
from typing import List

import datasets
from datasets.tasks import ImageClassification

from ..folder_based_builder import folder_based_builder


lowerCamelCase				:   Dict      				=  datasets.utils.logging.get_logger(__name__)







class 							A__					( folder_based_builder.FolderBasedBuilderConfig						):
  A__					=       None
  A__					=       None







class 							A__					( folder_based_builder.FolderBasedBuilder						):
  A__					=       datasets.Image()
  A__					=       'image'
  A__					=       ImageFolderConfig
  A__					=       42  # definition at the bottom of the script
  A__					=       ImageClassification(image_column='image'  ,     label_column='label'						)

lowerCamelCase				:   Optional[int]      				=  [
    ".blp",
    ".bmp",
    ".dib",
    ".bufr",
    ".cur",
    ".pcx",
    ".dcx",
    ".dds",
    ".ps",
    ".eps",
    ".fit",
    ".fits",
    ".fli",
    ".flc",
    ".ftc",
    ".ftu",
    ".gbr",
    ".gif",
    ".grib",
    ".h5",
    ".hdf",
    ".png",
    ".apng",
    ".jp2",
    ".j2k",
    ".jpc",
    ".jpf",
    ".jpx",
    ".j2c",
    ".icns",
    ".ico",
    ".im",
    ".iim",
    ".tif",
    ".tiff",
    ".jfif",
    ".jpe",
    ".jpg",
    ".jpeg",
    ".mpg",
    ".mpeg",
    ".msp",
    ".pcd",
    ".pxr",
    ".pbm",
    ".pgm",
    ".ppm",
    ".pnm",
    ".psd",
    ".bw",
    ".rgb",
    ".rgba",
    ".sgi",
    ".ras",
    ".tga",
    ".icb",
    ".vda",
    ".vst",
    ".webp",
    ".wmf",
    ".emf",
    ".xbm",
    ".xpm",
]
lowerCamelCase				:   Any      				=  IMAGE_EXTENSIONS
