



import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
							from PIL import Image
else:


							class   __lowerCAmelCase  :
												"""simple docstring"""






												@staticmethod
												def      _a				(  *_snake_case       :							Any					,       **_snake_case       :							Optional[int]       ):






															"""simple docstring"""


															pass


@is_pipeline_test
@require_torch
@require_vision
class   __lowerCAmelCase  (		unittest.TestCase   ):
					"""simple docstring"""






					A__					:		Union[str, Any]      							=     MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING



					def      _a				(  self       :							List[Any]					,       _snake_case       :							Union[str, Any]					,       _snake_case       :							Tuple					,       _snake_case       :							Union[str, Any]       ):






								"""simple docstring"""


								A__	  =     pipeline('visual-question-answering'					,       model='hf-internal-testing/tiny-vilt-random-vqa'       )
								A__	  =     [
								    {
								        'image': Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       ),
								        'question': 'How many cats are there?',
								    },
								    {
								        'image': './tests/fixtures/tests_samples/COCO/000000039769.png',
								        'question': 'How many cats are there?',
								    },
								]
								return vqa_pipeline, examples



					def      _a				(  self       :							Optional[Any]					,       _snake_case       :							Union[str, Any]					,       _snake_case       :							List[str]       ):






								"""simple docstring"""


								A__	  =     vqa_pipeline(_snake_case					,       top_k=1       )
								self.assertEqual(
								    _snake_case					,       [
								        [{'score': ANY(_snake_case       ), 'answer': ANY(_snake_case       )}],
								        [{'score': ANY(_snake_case       ), 'answer': ANY(_snake_case       )}],
								    ]					,       )



					@require_torch
					def      _a				(  self       :							Any       ):






								"""simple docstring"""


								A__	  =     pipeline('visual-question-answering'					,       model='hf-internal-testing/tiny-vilt-random-vqa'       )
								A__	  =     './tests/fixtures/tests_samples/COCO/000000039769.png'
								A__	  =     'How many cats are there?'

								A__	  =     vqa_pipeline(image=_snake_case					,       question='How many cats are there?'					,       top_k=2       )
								self.assertEqual(
								    _snake_case					,       [{'score': ANY(_snake_case       ), 'answer': ANY(_snake_case       )}, {'score': ANY(_snake_case       ), 'answer': ANY(_snake_case       )}]       )

								A__	  =     vqa_pipeline({'image': image, 'question': question}					,       top_k=2       )
								self.assertEqual(
								    _snake_case					,       [{'score': ANY(_snake_case       ), 'answer': ANY(_snake_case       )}, {'score': ANY(_snake_case       ), 'answer': ANY(_snake_case       )}]       )



					@slow
					@require_torch
					def      _a				(  self       :							Any       ):






								"""simple docstring"""


								A__	  =     pipeline('visual-question-answering'					,       model='dandelin/vilt-b32-finetuned-vqa'       )
								A__	  =     './tests/fixtures/tests_samples/COCO/000000039769.png'
								A__	  =     'How many cats are there?'

								A__	  =     vqa_pipeline(image=_snake_case					,       question=_snake_case					,       top_k=2       )
								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}]       )

								A__	  =     vqa_pipeline({'image': image, 'question': question}					,       top_k=2       )
								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}]       )

								A__	  =     vqa_pipeline(
								    [{'image': image, 'question': question}, {'image': image, 'question': question}]					,       top_k=2       )
								self.assertEqual(
								    nested_simplify(_snake_case					,       decimals=4       )					,       [[{'score': 0.8799, 'answer': '2'}, {'score': 0.296, 'answer': '1'}]] * 2					,       )



					@require_tf
					@unittest.skip('Visual question answering not implemented in TF'       )
					def      _a				(  self       :							Dict       ):






								"""simple docstring"""


								pass











'''simple docstring'''




from __future__ import annotations



def 		__A		(  lowerCamelCase_			,			lowerCamelCase_	):


		"""simple docstring"""



		if partitions <= 0:
				raise ValueError("""partitions must be a positive number!"""	)
		if partitions > number_of_bytes:
				raise ValueError("""partitions can not > number_of_bytes!"""	)
		SCREAMING_SNAKE_CASE :				int		=			number_of_bytes // partitions
		SCREAMING_SNAKE_CASE :				int		=			[]
		for i in range(lowerCamelCase_	):
				SCREAMING_SNAKE_CASE :				Optional[Any]		=			i * bytes_per_partition + 1
				SCREAMING_SNAKE_CASE :				Optional[int]		=			(
				    number_of_bytes if i == partitions - 1 else (i + 1) * bytes_per_partition
				)
				allocation_list.append(f'''{start_bytes}-{end_bytes}'''	)
		return allocation_list


if __name__ == "__main__":
							import doctest

							doctest.testmod()



