






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 EfficientNetImageProcessor







class       UpperCAmelCase_						( unittest.TestCase			):



      """simple docstring"""
      def __init__(    self   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_=13   ,    SCREAMING_SNAKE_CASE_=3   ,    SCREAMING_SNAKE_CASE_=18   ,    SCREAMING_SNAKE_CASE_=30   ,    SCREAMING_SNAKE_CASE_=400   ,    SCREAMING_SNAKE_CASE_=True   ,    SCREAMING_SNAKE_CASE_=None   ,    SCREAMING_SNAKE_CASE_=True   ,    SCREAMING_SNAKE_CASE_=[0.5, 0.5, 0.5]   ,    SCREAMING_SNAKE_CASE_=[0.5, 0.5, 0.5]   ,    )    ->				Union[str, Any]:
          UpperCamelCase	:str     							=       size if size is not None else {'''height''': 18, '''width''': 18}
          UpperCamelCase	:Union[str, Any]     							=       parent
          UpperCamelCase	:str     							=       batch_size
          UpperCamelCase	:Union[str, Any]     							=       num_channels
          UpperCamelCase	:Tuple     							=       image_size
          UpperCamelCase	:Tuple     							=       min_resolution
          UpperCamelCase	:List[Any]     							=       max_resolution
          UpperCamelCase	:int     							=       do_resize
          UpperCamelCase	:int     							=       size
          UpperCamelCase	:Optional[int]     							=       do_normalize
          UpperCamelCase	:Tuple     							=       image_mean
          UpperCamelCase	:int     							=       image_std




      def    UpperCAmelCase						(    self			)    ->				str:
          return {
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_normalize": self.do_normalize,
              "do_resize": self.do_resize,
              "size": self.size,
          }







@require_torch
@require_vision
class       UpperCAmelCase_						( lowercase,     unittest.TestCase			):



      """simple docstring"""
      UpperCamelCase_    : Optional[Any]									=EfficientNetImageProcessor if is_vision_available() else None
      def    UpperCAmelCase						(    self			)    ->				Optional[int]:
          UpperCamelCase	:str     							=       EfficientNetImageProcessorTester(self			)
      @property
      def    UpperCAmelCase						(    self			)    ->				Any:
          return self.image_processor_tester.prepare_image_processor_dict()
      def    UpperCAmelCase						(    self			)    ->				Optional[int]:
          UpperCamelCase	:str     							=       self.image_processing_class(**self.image_processor_dict			)
          self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,    '''image_mean'''			)			)
          self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,    '''image_std'''			)			)
          self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,    '''do_normalize'''			)			)
          self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,    '''do_resize'''			)			)
          self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_   ,    '''size'''			)			)
      def    UpperCAmelCase						(    self			)    ->				Any:
          UpperCamelCase	:Optional[int]     							=       self.image_processing_class.from_dict(self.image_processor_dict			)
          self.assertEqual(image_processor.size   ,    {'''height''': 18, '''width''': 18}			)

          UpperCamelCase	:int     							=       self.image_processing_class.from_dict(self.image_processor_dict   ,    size=42			)
          self.assertEqual(image_processor.size   ,    {'''height''': 42, '''width''': 42}			)
      def    UpperCAmelCase						(    self			)    ->				Optional[int]:
          # Initialize image_processing
          UpperCamelCase	:Any     							=       self.image_processing_class(**self.image_processor_dict			)
          # create random PIL images
          UpperCamelCase	:Any     							=       prepare_image_inputs(self.image_processor_tester   ,    equal_resolution=SCREAMING_SNAKE_CASE_			)
          for image in image_inputs:
              self.assertIsInstance(SCREAMING_SNAKE_CASE_   ,    Image.Image			)

          # Test not batched input
          UpperCamelCase	: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.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )   ,    )

          # Test batched
          UpperCamelCase	:int     							=       image_processing(SCREAMING_SNAKE_CASE_   ,    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.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )   ,    )
      def    UpperCAmelCase						(    self			)    ->				List[Any]:
          # Initialize image_processing
          UpperCamelCase	:Union[str, Any]     							=       self.image_processing_class(**self.image_processor_dict			)
          # create random numpy tensors
          UpperCamelCase	:str     							=       prepare_image_inputs(self.image_processor_tester   ,    equal_resolution=SCREAMING_SNAKE_CASE_   ,    numpify=SCREAMING_SNAKE_CASE_			)
          for image in image_inputs:
              self.assertIsInstance(SCREAMING_SNAKE_CASE_   ,    np.ndarray			)

          # Test not batched input
          UpperCamelCase	:Optional[int]     							=       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.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )   ,    )

          # Test batched
          UpperCamelCase	:Optional[int]     							=       image_processing(SCREAMING_SNAKE_CASE_   ,    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.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )   ,    )




      def    UpperCAmelCase						(    self			)    ->				Tuple:
          # Initialize image_processing
          UpperCamelCase	:Union[str, Any]     							=       self.image_processing_class(**self.image_processor_dict			)
          # create random PyTorch tensors
          UpperCamelCase	:List[Any]     							=       prepare_image_inputs(self.image_processor_tester   ,    equal_resolution=SCREAMING_SNAKE_CASE_   ,    torchify=SCREAMING_SNAKE_CASE_			)
          for image in image_inputs:
              self.assertIsInstance(SCREAMING_SNAKE_CASE_   ,    torch.Tensor			)

          # Test not batched input
          UpperCamelCase	: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.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )   ,    )

          # Test batched
          UpperCamelCase	:Any     							=       image_processing(SCREAMING_SNAKE_CASE_   ,    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.size['''height'''],
                  self.image_processor_tester.size['''width'''],
              )   ,    )













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       UpperCAmelCase_						( lowercase			):



      """simple docstring"""
      UpperCamelCase_    : Optional[int]									=DPRContextEncoderTokenizer
      UpperCamelCase_    : Any									=DPRContextEncoderTokenizerFast
      UpperCamelCase_    : Optional[int]									=True







@require_tokenizers
class       UpperCAmelCase_						( lowercase			):



      """simple docstring"""
      UpperCamelCase_    : Tuple									=DPRQuestionEncoderTokenizer
      UpperCamelCase_    : Union[str, Any]									=DPRQuestionEncoderTokenizerFast
      UpperCamelCase_    : Any									=True







@require_tokenizers
class       UpperCAmelCase_						( lowercase			):



      """simple docstring"""
      UpperCamelCase_    : Tuple									=DPRReaderTokenizer
      UpperCamelCase_    : List[Any]									=DPRReaderTokenizerFast
      UpperCamelCase_    : Union[str, Any]									=True
      @slow
      def    UpperCAmelCase						(    self			)    ->				Dict:
          UpperCamelCase	:Optional[int]     							=       self.tokenizer_class.from_pretrained('''bert-base-uncased'''			)

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

          UpperCamelCase	:Dict     							=       [[0] * len(input_ids[0]			)]
          UpperCamelCase	:List[Any]     							=       [[0] * len(input_ids[0]			)]
          UpperCamelCase	:Tuple     							=       [0]
          UpperCamelCase	:Union[str, Any]     							=       DPRReaderOutput(SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_   ,    SCREAMING_SNAKE_CASE_			)

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




      @slow
      def    UpperCAmelCase						(    self			)    ->				Any:
          UpperCamelCase	:List[str]     							=       self.tokenizer_class.from_pretrained('''bert-base-uncased'''			)

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





