


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 '''}],
										    ] ,			)






def        lowerCAmelCase_       (							UpperCamelCase_    ,						UpperCamelCase_     )   ->						list:
       UpperCamelCase_      				=     sorted(UpperCamelCase_    ,						key=lambda UpperCamelCase_     : value[2]    ,						reverse=UpperCamelCase_     )

       # Create a list of size equal to the maximum deadline
       # and initialize it with -1
       UpperCamelCase_      				=     max(UpperCamelCase_    ,						key=lambda UpperCamelCase_     : value[1]     )[1]
       UpperCamelCase_      				=     [-1] * max_deadline

       # Finding the maximum profit and the count of jobs
       UpperCamelCase_      				=     0
       UpperCamelCase_      				=     0
       for job in jobs:
              # Find a free time slot for this job
              # (Note that we start from the last possible slot)
              for i in range(job[1] - 1    ,						-1    ,						-1     ):
                     if time_slots[i] == -1:
                            UpperCamelCase_      				=     job[0]
                            count += 1
                            max_profit += job[2]
                            break
       return [count, max_profit]


if __name__ == "__main__":
     import doctest

     doctest.testmod()

