



'''simple docstring'''
from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    get_resize_output_image_size,
    normalize,
    pad,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging
from ...utils.import_utils import is_vision_available


_UpperCamelCase									=      logging.get_logger(__name__)


if is_vision_available():
					import PIL



class _A	(     __SCREAMING_SNAKE_CASE     ):
		_SCREAMING_SNAKE_CASE       :     Any       =      ["pixel_values"]





		def __init__(  self			,							__UpperCAmelCase = True			,							__UpperCAmelCase = None			,							__UpperCAmelCase = PILImageResampling.BILINEAR			,							__UpperCAmelCase = True			,							__UpperCAmelCase = False			,							__UpperCAmelCase = True			,							__UpperCAmelCase = True			,							__UpperCAmelCase = 1 / 255			,							__UpperCAmelCase = True			,							__UpperCAmelCase = None			,							__UpperCAmelCase = None			,							**__UpperCAmelCase			,							)							->  None:

					'''simple docstring'''
					super().__init__(**__UpperCAmelCase					)

					__UpperCAmelCase :   Any    					=  size if size is not None else {"""height""": 2_560, """width""": 1_920}
					if isinstance(__UpperCAmelCase			,							(tuple, list)					):
								# The previous feature extractor size parameter was in (width, height) format
								__UpperCAmelCase :   Optional[Any]    					=  size[::-1]
					__UpperCAmelCase :   Any    					=  get_size_dict(__UpperCAmelCase					)

					__UpperCAmelCase :   int    					=  do_resize
					__UpperCAmelCase :   Tuple    					=  size
					__UpperCAmelCase :   Optional[Any]    					=  resample
					__UpperCAmelCase :   List[Any]    					=  do_thumbnail
					__UpperCAmelCase :   Optional[int]    					=  do_align_long_axis
					__UpperCAmelCase :   Optional[Any]    					=  do_pad
					__UpperCAmelCase :   str    					=  do_rescale
					__UpperCAmelCase :   List[Any]    					=  rescale_factor
					__UpperCAmelCase :   Dict    					=  do_normalize
					__UpperCAmelCase :   List[Any]    					=  image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
					__UpperCAmelCase :   Union[str, Any]    					=  image_std if image_std is not None else IMAGENET_STANDARD_STD





		def 				__A							(  self			,							__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase = None					)							->  np.ndarray:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   Optional[Any]    					=  get_image_size(__UpperCAmelCase					)
					__UpperCAmelCase					,	__UpperCAmelCase :   List[str]    					=  size["""height"""], size["""width"""]

					if (output_width < output_height and input_width > input_height) or (
					    output_width > output_height and input_width < input_height
					):
								__UpperCAmelCase :   Union[str, Any]    					=  np.rotaa(__UpperCAmelCase			,							3					)

					if data_format is not None:
								__UpperCAmelCase :   Dict    					=  to_channel_dimension_format(__UpperCAmelCase			,							__UpperCAmelCase					)

					return image





		def 				__A							(  self			,							__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase = False			,							__UpperCAmelCase = None			,							)							->  np.ndarray:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   Tuple    					=  size["""height"""], size["""width"""]
					__UpperCAmelCase					,	__UpperCAmelCase :   Optional[Any]    					=  get_image_size(__UpperCAmelCase					)

					__UpperCAmelCase :   Optional[int]    					=  output_width - input_width
					__UpperCAmelCase :   int    					=  output_height - input_height

					if random_padding:
								__UpperCAmelCase :   Dict    					=  np.random.randint(low=0			,							high=delta_height + 1					)
								__UpperCAmelCase :   Optional[Any]    					=  np.random.randint(low=0			,							high=delta_width + 1					)
					else:
								__UpperCAmelCase :   Optional[Any]    					=  delta_height // 2
								__UpperCAmelCase :   Union[str, Any]    					=  delta_width // 2

					__UpperCAmelCase :   Optional[Any]    					=  delta_height - pad_top
					__UpperCAmelCase :   int    					=  delta_width - pad_left

					__UpperCAmelCase :   List[Any]    					=  ((pad_top, pad_bottom), (pad_left, pad_right))
					return pad(__UpperCAmelCase			,							__UpperCAmelCase			,							data_format=__UpperCAmelCase					)





		def 				__A							(  self			,							*__UpperCAmelCase			,							**__UpperCAmelCase					)							->  Optional[Any]:

					'''simple docstring'''
					logger.info("""pad is deprecated and will be removed in version 4.27. Please use pad_image instead."""					)
					return self.pad_image(*__UpperCAmelCase			,							**__UpperCAmelCase					)





		def 				__A							(  self			,							__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase = PILImageResampling.BICUBIC			,							__UpperCAmelCase = None			,							**__UpperCAmelCase			,							)							->  np.ndarray:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   Optional[int]    					=  get_image_size(__UpperCAmelCase					)
					__UpperCAmelCase					,	__UpperCAmelCase :   Dict    					=  size["""height"""], size["""width"""]

					# We always resize to the smallest of either the input or output size.
					__UpperCAmelCase :   int    					=  min(__UpperCAmelCase			,							__UpperCAmelCase					)
					__UpperCAmelCase :   Optional[Any]    					=  min(__UpperCAmelCase			,							__UpperCAmelCase					)

					if height == input_height and width == input_width:
								return image

					if input_height > input_width:
								__UpperCAmelCase :   List[Any]    					=  int(input_width * height / input_height					)
					elif input_width > input_height:
								__UpperCAmelCase :   List[Any]    					=  int(input_height * width / input_width					)

					return resize(
					    __UpperCAmelCase			,							size=(height, width)			,							resample=__UpperCAmelCase			,							reducing_gap=2.0			,							data_format=__UpperCAmelCase			,							**__UpperCAmelCase					)





		def 				__A							(  self			,							__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase = PILImageResampling.BICUBIC			,							__UpperCAmelCase = None			,							**__UpperCAmelCase			,							)							->  np.ndarray:

					'''simple docstring'''
					__UpperCAmelCase :   Tuple    					=  get_size_dict(__UpperCAmelCase					)
					__UpperCAmelCase :   Union[str, Any]    					=  min(size["""height"""]			,							size["""width"""]					)
					__UpperCAmelCase :   Optional[int]    					=  get_resize_output_image_size(__UpperCAmelCase			,							size=__UpperCAmelCase			,							default_to_square=__UpperCAmelCase					)
					__UpperCAmelCase :   Any    					=  resize(__UpperCAmelCase			,							size=__UpperCAmelCase			,							resample=__UpperCAmelCase			,							data_format=__UpperCAmelCase			,							**__UpperCAmelCase					)
					return resized_image





		def 				__A							(  self			,							__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase = None			,							**__UpperCAmelCase			,							)							->  Union[str, Any]:

					'''simple docstring'''
					return rescale(__UpperCAmelCase			,							scale=__UpperCAmelCase			,							data_format=__UpperCAmelCase			,							**__UpperCAmelCase					)





		def 				__A							(  self			,							__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase = None			,							**__UpperCAmelCase			,							)							->  np.ndarray:

					'''simple docstring'''
					return normalize(__UpperCAmelCase			,							mean=__UpperCAmelCase			,							std=__UpperCAmelCase			,							data_format=__UpperCAmelCase			,							**__UpperCAmelCase					)





		def 				__A							(  self			,							__UpperCAmelCase			,							__UpperCAmelCase = None			,							__UpperCAmelCase = None			,							__UpperCAmelCase = None			,							__UpperCAmelCase = None			,							__UpperCAmelCase = None			,							__UpperCAmelCase = None			,							__UpperCAmelCase = False			,							__UpperCAmelCase = None			,							__UpperCAmelCase = None			,							__UpperCAmelCase = None			,							__UpperCAmelCase = None			,							__UpperCAmelCase = None			,							__UpperCAmelCase = None			,							__UpperCAmelCase = ChannelDimension.FIRST			,							**__UpperCAmelCase			,							)							->  PIL.Image.Image:

					'''simple docstring'''
					__UpperCAmelCase :   Union[str, Any]    					=  do_resize if do_resize is not None else self.do_resize
					__UpperCAmelCase :   Union[str, Any]    					=  size if size is not None else self.size
					if isinstance(__UpperCAmelCase			,							(tuple, list)					):
								# Previous feature extractor had size in (width, height) format
								__UpperCAmelCase :   int    					=  size[::-1]
					__UpperCAmelCase :   Tuple    					=  get_size_dict(__UpperCAmelCase					)
					__UpperCAmelCase :   Optional[Any]    					=  resample if resample is not None else self.resample
					__UpperCAmelCase :   Any    					=  do_thumbnail if do_thumbnail is not None else self.do_thumbnail
					__UpperCAmelCase :   Optional[int]    					=  do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
					__UpperCAmelCase :   str    					=  do_pad if do_pad is not None else self.do_pad
					__UpperCAmelCase :   Optional[Any]    					=  do_rescale if do_rescale is not None else self.do_rescale
					__UpperCAmelCase :   int    					=  rescale_factor if rescale_factor is not None else self.rescale_factor
					__UpperCAmelCase :   int    					=  do_normalize if do_normalize is not None else self.do_normalize
					__UpperCAmelCase :   Any    					=  image_mean if image_mean is not None else self.image_mean
					__UpperCAmelCase :   Optional[Any]    					=  image_std if image_std is not None else self.image_std

					__UpperCAmelCase :   Any    					=  make_list_of_images(__UpperCAmelCase					)

					if not valid_images(__UpperCAmelCase					):
								raise ValueError(
								    """Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, """
								    """torch.Tensor, tf.Tensor or jax.ndarray."""					)

					if do_resize and size is None:
								raise ValueError("""Size must be specified if do_resize is True."""					)

					if do_rescale and rescale_factor is None:
								raise ValueError("""Rescale factor must be specified if do_rescale is True."""					)

					if do_pad and size is None:
								raise ValueError("""Size must be specified if do_pad is True."""					)

					if do_normalize and (image_mean is None or image_std is None):
								raise ValueError("""Image mean and std must be specified if do_normalize is True."""					)

					# All transformations expect numpy arrays.
					__UpperCAmelCase :   Dict    					=  [to_numpy_array(__UpperCAmelCase					) for image in images]

					if do_align_long_axis:
								__UpperCAmelCase :   Any    					=  [self.align_long_axis(__UpperCAmelCase			,							size=__UpperCAmelCase					) for image in images]

					if do_resize:
								__UpperCAmelCase :   Union[str, Any]    					=  [self.resize(image=__UpperCAmelCase			,							size=__UpperCAmelCase			,							resample=__UpperCAmelCase					) for image in images]

					if do_thumbnail:
								__UpperCAmelCase :   Dict    					=  [self.thumbnail(image=__UpperCAmelCase			,							size=__UpperCAmelCase					) for image in images]

					if do_pad:
								__UpperCAmelCase :   int    					=  [self.pad_image(image=__UpperCAmelCase			,							size=__UpperCAmelCase			,							random_padding=__UpperCAmelCase					) for image in images]

					if do_rescale:
								__UpperCAmelCase :   Optional[Any]    					=  [self.rescale(image=__UpperCAmelCase			,							scale=__UpperCAmelCase					) for image in images]

					if do_normalize:
								__UpperCAmelCase :   Optional[Any]    					=  [self.normalize(image=__UpperCAmelCase			,							mean=__UpperCAmelCase			,							std=__UpperCAmelCase					) for image in images]

					__UpperCAmelCase :   int    					=  [to_channel_dimension_format(__UpperCAmelCase			,							__UpperCAmelCase					) for image in images]

					__UpperCAmelCase :   List[Any]    					=  {"""pixel_values""": images}
					return BatchFeature(data=__UpperCAmelCase			,							tensor_type=__UpperCAmelCase					)









'''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					)




