


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        lowerCAmelCase__	(  *_lowerCAmelCase							:							Dict     ,					**_lowerCAmelCase							:							Dict	) ->			int:




        """simple docstring"""
        pass


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






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING



  def        lowerCAmelCase__	(  self							:							str     ,					_lowerCAmelCase							:							Dict     ,					_lowerCAmelCase							:							Dict     ,					_lowerCAmelCase							:							Optional[Any]	) ->			List[Any]:




     """simple docstring"""
     snake_case_            =       pipeline("visual-question-answering"     ,					model="hf-internal-testing/tiny-vilt-random-vqa"	)
     snake_case_            =       [
         {
             "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        lowerCAmelCase__	(  self							:							List[Any]     ,					_lowerCAmelCase							:							int     ,					_lowerCAmelCase							:							Optional[Any]	) ->			List[str]:




     """simple docstring"""
     snake_case_            =       vqa_pipeline(_lowerCAmelCase     ,					top_k=1	)
     self.assertEqual(
         _lowerCAmelCase     ,					[
             [{"score": ANY(_lowerCAmelCase	), "answer": ANY(_lowerCAmelCase	)}],
             [{"score": ANY(_lowerCAmelCase	), "answer": ANY(_lowerCAmelCase	)}],
         ]     ,					)



  @require_torch
  def        lowerCAmelCase__	(  self							:							Optional[Any]	) ->			Optional[Any]:




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

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

     snake_case_            =       vqa_pipeline({"image": image, "question": question}     ,					top_k=2	)
     self.assertEqual(
         _lowerCAmelCase     ,					[{"score": ANY(_lowerCAmelCase	), "answer": ANY(_lowerCAmelCase	)}, {"score": ANY(_lowerCAmelCase	), "answer": ANY(_lowerCAmelCase	)}]	)



  @slow
  @require_torch
  def        lowerCAmelCase__	(  self							:							Dict	) ->			Dict:




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

     snake_case_            =       vqa_pipeline(image=_lowerCAmelCase     ,					question=_lowerCAmelCase     ,					top_k=2	)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase     ,					decimals=4	)     ,					[{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]	)

     snake_case_            =       vqa_pipeline({"image": image, "question": question}     ,					top_k=2	)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase     ,					decimals=4	)     ,					[{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]	)

     snake_case_            =       vqa_pipeline(
         [{"image": image, "question": question}, {"image": image, "question": question}]     ,					top_k=2	)
     self.assertEqual(
         nested_simplify(_lowerCAmelCase     ,					decimals=4	)     ,					[[{"score": 0.8_799, "answer": "2"}, {"score": 0.296, "answer": "1"}]] * 2     ,					)



  @require_tf
  @unittest.skip("Visual question answering not implemented in TF"	)
  def        lowerCAmelCase__	(  self							:							Any	) ->			Optional[Any]:




     """simple docstring"""
     pass








from __future__ import annotations



def       _lowerCAmelCase    (				lowerCAmelCase_				:int		,    lowerCAmelCase_				:int				)->list[str]:
   '''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!"				)
   snake_case_            =       number_of_bytes // partitions
   snake_case_            =       []
   for i in range(lowerCAmelCase_				):
      snake_case_            =       i * bytes_per_partition + 1
      snake_case_            =       (
          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()




