




import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
   import torch

if is_vision_available():
   from PIL import Image

   from transformers import DeiTImageProcessor

class   UpperCAmelCase    (      unittest.TestCase ):






   def __init__(						self :							List[str]    ,       UpperCamelCase_ :							List[Any]    ,       UpperCamelCase_ :							List[str]=7    ,       UpperCamelCase_ :							Tuple=3    ,       UpperCamelCase_ :							int=18    ,       UpperCamelCase_ :							Dict=30    ,       UpperCamelCase_ :							Optional[Any]=4_00    ,       UpperCamelCase_ :							int=True    ,       UpperCamelCase_ :							Any=None    ,       UpperCamelCase_ :							Any=True    ,       UpperCamelCase_ :							List[str]=None    ,       UpperCamelCase_ :							Optional[Any]=True    ,       UpperCamelCase_ :							Dict=[0.5, 0.5, 0.5]    ,       UpperCamelCase_ :							int=[0.5, 0.5, 0.5]    ,       )->      List[Any]:
     _lowercase:			List[str]      =     size if size is not None else {"""height""": 20, """width""": 20}
     _lowercase:			Tuple      =     crop_size if crop_size is not None else {"""height""": 18, """width""": 18}

     _lowercase:			Optional[Any]      =     parent
     _lowercase:			int      =     batch_size
     _lowercase:			Optional[Any]      =     num_channels
     _lowercase:			Union[str, Any]      =     image_size
     _lowercase:			Tuple      =     min_resolution
     _lowercase:			Optional[Any]      =     max_resolution
     _lowercase:			Optional[int]      =     do_resize
     _lowercase:			Tuple      =     size
     _lowercase:			Union[str, Any]      =     do_center_crop
     _lowercase:			Dict      =     crop_size
     _lowercase:			List[str]      =     do_normalize
     _lowercase:			Any      =     image_mean
     _lowercase:			Optional[Any]      =     image_std






   def 			_a    (						self :							Optional[int]  )->      Tuple:
     return {
         "do_resize": self.do_resize,
         "size": self.size,
         "do_center_crop": self.do_center_crop,
         "crop_size": self.crop_size,
         "do_normalize": self.do_normalize,
         "image_mean": self.image_mean,
         "image_std": self.image_std,
     }

@require_torch
@require_vision
class   UpperCAmelCase    (      __SCREAMING_SNAKE_CASE ,       unittest.TestCase ):
   __SCREAMING_SNAKE_CASE		:				str      					=       DeiTImageProcessor if is_vision_available() else None
   __SCREAMING_SNAKE_CASE		:				Tuple      					=       True






   def 			_a    (						self :							Any  )->      str:
     _lowercase:			Union[str, Any]      =     DeiTImageProcessingTester(self  )






   @property
   def 			_a    (						self :							Dict  )->      int:
     return self.image_processor_tester.prepare_image_processor_dict()






   def 			_a    (						self :							Optional[Any]  )->      str:
     _lowercase:			str      =     self.image_processing_class(**self.image_processor_dict  )
     self.assertTrue(hasattr(__UpperCAmelCase    ,       'do_resize'  )  )
     self.assertTrue(hasattr(__UpperCAmelCase    ,       'size'  )  )
     self.assertTrue(hasattr(__UpperCAmelCase    ,       'do_center_crop'  )  )
     self.assertTrue(hasattr(__UpperCAmelCase    ,       'center_crop'  )  )
     self.assertTrue(hasattr(__UpperCAmelCase    ,       'do_normalize'  )  )
     self.assertTrue(hasattr(__UpperCAmelCase    ,       'image_mean'  )  )
     self.assertTrue(hasattr(__UpperCAmelCase    ,       'image_std'  )  )






   def 			_a    (						self :							Any  )->      Any:
     _lowercase:			Any      =     self.image_processing_class.from_dict(self.image_processor_dict  )
     self.assertEqual(image_processor.size    ,       {'height': 20, 'width': 20}  )
     self.assertEqual(image_processor.crop_size    ,       {'height': 18, 'width': 18}  )

     _lowercase:			Any      =     self.image_processing_class.from_dict(self.image_processor_dict    ,       size=42    ,       crop_size=84  )
     self.assertEqual(image_processor.size    ,       {'height': 42, 'width': 42}  )
     self.assertEqual(image_processor.crop_size    ,       {'height': 84, 'width': 84}  )






   def 			_a    (						self :							Dict  )->      Tuple:
     pass






   def 			_a    (						self :							Tuple  )->      str:
     _lowercase:			List[Any]      =     self.image_processing_class(**self.image_processor_dict  )
     # create random PIL images
     _lowercase:			Union[str, Any]      =     prepare_image_inputs(self.image_processor_tester    ,       equal_resolution=__UpperCAmelCase  )
     for image in image_inputs:
       self.assertIsInstance(__UpperCAmelCase    ,       Image.Image  )

     # Test not batched input
     _lowercase:			Any      =     image_processing(image_inputs[0]    ,       return_tensors='pt'  ).pixel_values
     self.assertEqual(
         encoded_images.shape    ,       (
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )    ,       )

     # Test batched
     _lowercase:			Optional[Any]      =     image_processing(__UpperCAmelCase    ,       return_tensors='pt'  ).pixel_values
     self.assertEqual(
         encoded_images.shape    ,       (
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )    ,       )






   def 			_a    (						self :							List[Any]  )->      List[str]:
     _lowercase:			Any      =     self.image_processing_class(**self.image_processor_dict  )
     # create random numpy tensors
     _lowercase:			Union[str, Any]      =     prepare_image_inputs(self.image_processor_tester    ,       equal_resolution=__UpperCAmelCase    ,       numpify=__UpperCAmelCase  )
     for image in image_inputs:
       self.assertIsInstance(__UpperCAmelCase    ,       np.ndarray  )

     # Test not batched input
     _lowercase:			Dict      =     image_processing(image_inputs[0]    ,       return_tensors='pt'  ).pixel_values
     self.assertEqual(
         encoded_images.shape    ,       (
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )    ,       )

     # Test batched
     _lowercase:			Tuple      =     image_processing(__UpperCAmelCase    ,       return_tensors='pt'  ).pixel_values
     self.assertEqual(
         encoded_images.shape    ,       (
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )    ,       )






   def 			_a    (						self :							Any  )->      List[str]:
     _lowercase:			Union[str, Any]      =     self.image_processing_class(**self.image_processor_dict  )
     # create random PyTorch tensors
     _lowercase:			Any      =     prepare_image_inputs(self.image_processor_tester    ,       equal_resolution=__UpperCAmelCase    ,       torchify=__UpperCAmelCase  )
     for image in image_inputs:
       self.assertIsInstance(__UpperCAmelCase    ,       torch.Tensor  )

     # Test not batched input
     _lowercase:			Any      =     image_processing(image_inputs[0]    ,       return_tensors='pt'  ).pixel_values
     self.assertEqual(
         encoded_images.shape    ,       (
             1,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )    ,       )

     # Test batched
     _lowercase:			int      =     image_processing(__UpperCAmelCase    ,       return_tensors='pt'  ).pixel_values
     self.assertEqual(
         encoded_images.shape    ,       (
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )    ,       )





'''simple docstring'''
from transformers import (
    DPRContextEncoderTokenizer,
    DPRContextEncoderTokenizerFast,
    DPRQuestionEncoderTokenizer,
    DPRQuestionEncoderTokenizerFast,
    DPRReaderOutput,
    DPRReaderTokenizer,
    DPRReaderTokenizerFast,
)
from transformers.testing_utils import require_tokenizers, slow
from transformers.tokenization_utils_base import BatchEncoding

from ..bert.test_tokenization_bert import BertTokenizationTest



@require_tokenizers
class _A	(     __SCREAMING_SNAKE_CASE     ):
		_SCREAMING_SNAKE_CASE       :     List[str]       =      DPRContextEncoderTokenizer
		_SCREAMING_SNAKE_CASE       :     Optional[int]       =      DPRContextEncoderTokenizerFast
		_SCREAMING_SNAKE_CASE       :     Optional[Any]       =      True



@require_tokenizers
class _A	(     __SCREAMING_SNAKE_CASE     ):
		_SCREAMING_SNAKE_CASE       :     str       =      DPRQuestionEncoderTokenizer
		_SCREAMING_SNAKE_CASE       :     List[str]       =      DPRQuestionEncoderTokenizerFast
		_SCREAMING_SNAKE_CASE       :     Union[str, Any]       =      True



@require_tokenizers
class _A	(     __SCREAMING_SNAKE_CASE     ):
		_SCREAMING_SNAKE_CASE       :     List[str]       =      DPRReaderTokenizer
		_SCREAMING_SNAKE_CASE       :     Optional[Any]       =      DPRReaderTokenizerFast
		_SCREAMING_SNAKE_CASE       :     Dict       =      True





		@slow
		def 				__A							(  self					)							->  List[Any]:

					'''simple docstring'''
					__UpperCAmelCase :   Dict    					=  self.tokenizer_class.from_pretrained("""bert-base-uncased"""					)

					__UpperCAmelCase :   int    					=  tokenizer.encode("""question sequence"""			,							add_special_tokens=__UpperCAmelCase					)
					__UpperCAmelCase :   Optional[Any]    					=  tokenizer.encode("""title sequence"""			,							add_special_tokens=__UpperCAmelCase					)
					__UpperCAmelCase :   Any    					=  tokenizer.encode("""text sequence """ * 4			,							add_special_tokens=__UpperCAmelCase					)
					__UpperCAmelCase :   Tuple    					=  [[101] + text_a + [102] + text_a + [102] + text_a]
					__UpperCAmelCase :   Optional[int]    					=  BatchEncoding({"""input_ids""": input_ids}					)

					__UpperCAmelCase :   str    					=  [[0] * len(input_ids[0]					)]
					__UpperCAmelCase :   str    					=  [[0] * len(input_ids[0]					)]
					__UpperCAmelCase :   Optional[int]    					=  [0]
					__UpperCAmelCase :   List[str]    					=  DPRReaderOutput(__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase					)

					__UpperCAmelCase					,	__UpperCAmelCase :   Any    					=  8, 9
					__UpperCAmelCase :   Optional[int]    					=  10
					__UpperCAmelCase :   int    					=  10
					__UpperCAmelCase :   str    					=  tokenizer.decode_best_spans(__UpperCAmelCase			,							__UpperCAmelCase					)
					self.assertEqual(predicted_spans[0].start_index			,							__UpperCAmelCase					)
					self.assertEqual(predicted_spans[0].end_index			,							__UpperCAmelCase					)
					self.assertEqual(predicted_spans[0].doc_id			,							0					)





		@slow
		def 				__A							(  self					)							->  int:

					'''simple docstring'''
					__UpperCAmelCase :   Any    					=  self.tokenizer_class.from_pretrained("""bert-base-uncased"""					)

					__UpperCAmelCase :   List[Any]    					=  tokenizer.encode("""question sequence"""			,							add_special_tokens=__UpperCAmelCase					)
					__UpperCAmelCase :   List[str]    					=  tokenizer.encode("""title sequence"""			,							add_special_tokens=__UpperCAmelCase					)
					__UpperCAmelCase :   Union[str, Any]    					=  tokenizer.encode("""text sequence"""			,							add_special_tokens=__UpperCAmelCase					)
					__UpperCAmelCase :   Optional[int]    					=  [101] + text_a + [102] + text_a + [102] + text_a
					__UpperCAmelCase :   int    					=  tokenizer(questions=["""question sequence"""]			,							titles=["""title sequence"""]			,							texts=["""text sequence"""]					)
					self.assertIn("""input_ids"""			,							__UpperCAmelCase					)
					self.assertIn("""attention_mask"""			,							__UpperCAmelCase					)
					self.assertListEqual(encoded_input["""input_ids"""][0]			,							__UpperCAmelCase					)




