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


if is_vision_available():
			from PIL import Image
else:





			class      _lowerCAmelCase :




							@staticmethod
							def 					__a						( *_UpperCamelCase    ,    **_UpperCamelCase					)    ->				Optional[int]:
										pass





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




				def 					__a						( self    ,    _UpperCamelCase    ,    _UpperCamelCase    ,    _UpperCamelCase					)    ->				Optional[Any]:
							lowerCAmelCase_      =     pipeline("image-to-text"    ,    model=_UpperCamelCase    ,    tokenizer=_UpperCamelCase    ,    image_processor=_UpperCamelCase					)
							lowerCAmelCase_      =     [
							    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					),
							    "./tests/fixtures/tests_samples/COCO/000000039769.png",
							]
							return pipe, examples




				def 					__a						( self    ,    _UpperCamelCase    ,    _UpperCamelCase					)    ->				List[str]:
							lowerCAmelCase_      =     pipe(_UpperCamelCase					)
							self.assertEqual(
							    _UpperCamelCase    ,    [
							        [{"generated_text": ANY(_UpperCamelCase					)}],
							        [{"generated_text": ANY(_UpperCamelCase					)}],
							    ]    ,    )




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

							lowerCAmelCase_      =     pipe(_UpperCamelCase					)
							self.assertEqual(
							    _UpperCamelCase    ,    [
							        {
							            "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
							        },
							    ]    ,    )

							lowerCAmelCase_      =     pipe([image, image]					)
							self.assertEqual(
							    _UpperCamelCase    ,    [
							        [
							            {
							                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
							            }
							        ],
							        [
							            {
							                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
							            }
							        ],
							    ]    ,    )

							lowerCAmelCase_      =     pipe(_UpperCamelCase    ,    max_new_tokens=1					)
							self.assertEqual(
							    _UpperCamelCase    ,    [{"generated_text": "growth"}]    ,    )




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

							lowerCAmelCase_      =     pipe(_UpperCamelCase					)
							self.assertEqual(
							    _UpperCamelCase    ,    [
							        {
							            "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
							        },
							    ]    ,    )

							lowerCAmelCase_      =     pipe([image, image]					)
							self.assertEqual(
							    _UpperCamelCase    ,    [
							        [
							            {
							                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
							            }
							        ],
							        [
							            {
							                "generated_text": "growthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthgrowthGOGO"
							            }
							        ],
							    ]    ,    )




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

							lowerCAmelCase_      =     pipe(_UpperCamelCase    ,    prompt=_UpperCamelCase					)
							self.assertTrue(outputs[0]["generated_text"].startswith(_UpperCamelCase					)					)




				@slow
				@require_torch
				def 					__a						( self					)    ->				Tuple:
							lowerCAmelCase_      =     pipeline("image-to-text"    ,    model="ydshieh/vit-gpt2-coco-en"					)
							lowerCAmelCase_      =     "./tests/fixtures/tests_samples/COCO/000000039769.png"

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

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

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




				@slow
				@require_torch
				def 					__a						( self					)    ->				int:
							lowerCAmelCase_      =     pipeline("image-to-text"    ,    model="microsoft/git-base-coco"					)
							lowerCAmelCase_      =     "https://huggingface.co/datasets/sayakpaul/sample-datasets/resolve/main/pokemon.png"
							lowerCAmelCase_      =     Image.open(requests.get(_UpperCamelCase    ,    stream=_UpperCamelCase					).raw					)

							lowerCAmelCase_      =     pipe(_UpperCamelCase					)
							self.assertEqual(_UpperCamelCase    ,    [{"generated_text": "a cartoon of a purple character."}]					)




				@slow
				@require_torch
				def 					__a						( self					)    ->				str:
							lowerCAmelCase_      =     pipeline("image-to-text"    ,    model="Salesforce/blip-image-captioning-base"					)
							lowerCAmelCase_      =     "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/ai2d-demo.jpg"
							lowerCAmelCase_      =     Image.open(requests.get(_UpperCamelCase    ,    stream=_UpperCamelCase					).raw					)

							lowerCAmelCase_      =     "a photography of"

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

							with self.assertRaises(_UpperCamelCase					):
										lowerCAmelCase_      =     pipe([image, image]    ,    prompt=[prompt, prompt]					)




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

							lowerCAmelCase_      =     "a photo of a"

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

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

							lowerCAmelCase_      =     pipe(_UpperCamelCase    ,    prompt=_UpperCamelCase					)
							self.assertEqual(_UpperCamelCase    ,    [{"generated_text": "ash cloud"}]					)

							with self.assertRaises(_UpperCamelCase					):
										lowerCAmelCase_      =     pipe([image, image]    ,    prompt=[prompt, prompt]					)




				@slow
				@require_tf
				def 					__a						( self					)    ->				Dict:
							lowerCAmelCase_      =     pipeline("image-to-text"    ,    model="ydshieh/vit-gpt2-coco-en"    ,    framework="tf"					)
							lowerCAmelCase_      =     "./tests/fixtures/tests_samples/COCO/000000039769.png"

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

							lowerCAmelCase_      =     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 "}],
							    ]    ,    )








import json
import os
from dataclasses import dataclass

import numpy as np
import pyarrow as pa

import datasets
from utils import get_duration


_A       =					10_00_00_00_00_00
_A       =					1_00_00

_A,				_A       =					os.path.split(__file__)
_A       =					os.path.join(RESULTS_BASEPATH, "results", RESULTS_FILENAME.replace(".py", ".json"))






def        lowerCamelCase__    (    __lowerCAmelCase  :  int   ,							__lowerCAmelCase  :  int    ):




			"""simple docstring"""


			lowerCAmelCase_      =     pa.Table.from_pydict({"col": [0] * chunk_size}    )
			lowerCAmelCase_      =     pa.concat_tables([table] * (num_examples // chunk_size)    )
			return datasets.Dataset(__lowerCAmelCase   ,							fingerprint="table_100B"    )





@dataclass
class      _lowerCAmelCase :
				_lowercase      		=42
				_lowercase      		=42
				_lowercase      		=42
				_lowercase      		=42




				def 					__a						( self					)    ->				List[Any]:
							lowerCAmelCase_      =     np.random.default_rng(self.seed					)
							lowerCAmelCase_      =     rng.integers(low=self.low    ,    high=self.high    ,    size=self.size					).tolist()




				def __iter__( self					)    ->				List[str]:
							return iter(self._sampled_values					)




				def __len__( self					)    ->				Optional[int]:
							return self.size






@get_duration
def        lowerCamelCase__    (    __lowerCAmelCase  :  datasets.Dataset    ):




			"""simple docstring"""


			lowerCAmelCase_      =     dataset[0]






@get_duration
def        lowerCamelCase__    (    __lowerCAmelCase  :  datasets.Dataset    ):




			"""simple docstring"""


			lowerCAmelCase_      =     dataset[-1]






@get_duration
def        lowerCamelCase__    (    __lowerCAmelCase  :  datasets.Dataset    ):




			"""simple docstring"""


			lowerCAmelCase_      =     dataset[range(len(__lowerCAmelCase    ) // 2   ,							len(__lowerCAmelCase    ) // 2 + 1024    )]






@get_duration
def        lowerCamelCase__    (    __lowerCAmelCase  :  datasets.Dataset    ):




			"""simple docstring"""


			lowerCAmelCase_      =     dataset[RandIter(0   ,							len(__lowerCAmelCase    )   ,							1024   ,							seed=42    )]






def        lowerCamelCase__    (    ):




			"""simple docstring"""


			lowerCAmelCase_      =     {"num examples": SPEED_TEST_N_EXAMPLES}
			lowerCAmelCase_      =     (get_first_row, get_last_row, get_batch_of_aaaa_rows, get_batch_of_aaaa_random_rows)
			print("generating dataset"    )
			lowerCAmelCase_      =     generate_100B_dataset(num_examples=__lowerCAmelCase   ,							chunk_size=__lowerCAmelCase    )
			print("Functions"    )
			for func in functions:
						print(func.__name__    )
						lowerCAmelCase_      =     func(__lowerCAmelCase    )

			with open(__lowerCAmelCase   ,							"wb"    ) as f:
						f.write(json.dumps(__lowerCAmelCase    ).encode("utf-8"    )    )


if __name__ == "__main__":  # useful to run the profiler
			benchmark_table_100B()



