


'''simple docstring'''
import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
 from PIL import Image

 from transformers import OwlViTImageProcessor, OwlViTProcessor

@require_vision
class     _lowerCamelCase       (     unittest.TestCase					):




 '''simple docstring'''


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		List[Any]	  =      tempfile.mkdtemp()

  # fmt: off
  __A	:		Tuple	  =      ['', 'l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>']
  # fmt: on
  __A	:		List[Any]	  =      dict(zip(__lowercase   ,   range(len(__lowercase    )    )    )    )
  __A	:		Dict	  =      ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>', '']
  __A	:		List[str]	  =      {'unk_token': '<unk>'}

  __A	:		Dict	  =      os.path.join(self.tmpdirname   ,   VOCAB_FILES_NAMES['vocab_file']    )
  __A	:		Dict	  =      os.path.join(self.tmpdirname   ,   VOCAB_FILES_NAMES['merges_file']    )
  with open(self.vocab_file   ,   'w'   ,   encoding='utf-8'    ) as fp:
   fp.write(json.dumps(__lowercase    ) + '\n'    )
  with open(self.merges_file   ,   'w'   ,   encoding='utf-8'    ) as fp:
   fp.write('\n'.join(__lowercase    )    )

  __A	:		List[Any]	  =      {
      'do_resize': True,
      'size': 20,
      'do_center_crop': True,
      'crop_size': 18,
      'do_normalize': True,
      'image_mean': [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
      'image_std': [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
  }
  __A	:		int	  =      os.path.join(self.tmpdirname   ,   __lowercase    )
  with open(self.image_processor_file   ,   'w'   ,   encoding='utf-8'    ) as fp:
   json.dump(__lowercase   ,   __lowercase    )


 def 			snake_case__ (   self   ,   **__lowercase    ):

  """simple docstring"""







  return CLIPTokenizer.from_pretrained(self.tmpdirname   ,   pad_token='!'   ,   **__lowercase    )


 def 			snake_case__ (   self   ,   **__lowercase    ):

  """simple docstring"""







  return CLIPTokenizerFast.from_pretrained(self.tmpdirname   ,   pad_token='!'   ,   **__lowercase    )


 def 			snake_case__ (   self   ,   **__lowercase    ):

  """simple docstring"""







  return OwlViTImageProcessor.from_pretrained(self.tmpdirname   ,   **__lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  shutil.rmtree(self.tmpdirname    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Optional[int]	  =      [np.random.randint(255   ,   size=(3, 30, 400)   ,   dtype=np.uinta    )]

  __A	:		List[Any]	  =      [Image.fromarray(np.moveaxis(__lowercase   ,   0   ,   -1    )    ) for x in image_inputs]

  return image_inputs


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Dict	  =      self.get_tokenizer()
  __A	:		List[str]	  =      self.get_rust_tokenizer()
  __A	:		Any	  =      self.get_image_processor()

  __A	:		Union[str, Any]	  =      OwlViTProcessor(tokenizer=__lowercase   ,   image_processor=__lowercase    )
  processor_slow.save_pretrained(self.tmpdirname    )
  __A	:		List[str]	  =      OwlViTProcessor.from_pretrained(self.tmpdirname   ,   use_fast=__lowercase    )

  __A	:		Tuple	  =      OwlViTProcessor(tokenizer=__lowercase   ,   image_processor=__lowercase    )
  processor_fast.save_pretrained(self.tmpdirname    )
  __A	:		List[str]	  =      OwlViTProcessor.from_pretrained(self.tmpdirname    )

  self.assertEqual(processor_slow.tokenizer.get_vocab()   ,   tokenizer_slow.get_vocab()    )
  self.assertEqual(processor_fast.tokenizer.get_vocab()   ,   tokenizer_fast.get_vocab()    )
  self.assertEqual(tokenizer_slow.get_vocab()   ,   tokenizer_fast.get_vocab()    )
  self.assertIsInstance(processor_slow.tokenizer   ,   __lowercase    )
  self.assertIsInstance(processor_fast.tokenizer   ,   __lowercase    )

  self.assertEqual(processor_slow.image_processor.to_json_string()   ,   image_processor.to_json_string()    )
  self.assertEqual(processor_fast.image_processor.to_json_string()   ,   image_processor.to_json_string()    )
  self.assertIsInstance(processor_slow.image_processor   ,   __lowercase    )
  self.assertIsInstance(processor_fast.image_processor   ,   __lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		List[str]	  =      OwlViTProcessor(tokenizer=self.get_tokenizer()   ,   image_processor=self.get_image_processor()    )
  processor.save_pretrained(self.tmpdirname    )

  __A	:		List[str]	  =      self.get_tokenizer(bos_token='(BOS)'   ,   eos_token='(EOS)'    )
  __A	:		List[Any]	  =      self.get_image_processor(do_normalize=__lowercase    )

  __A	:		Optional[Any]	  =      OwlViTProcessor.from_pretrained(
      self.tmpdirname   ,   bos_token='(BOS)'   ,   eos_token='(EOS)'   ,   do_normalize=__lowercase    )

  self.assertEqual(processor.tokenizer.get_vocab()   ,   tokenizer_add_kwargs.get_vocab()    )
  self.assertIsInstance(processor.tokenizer   ,   __lowercase    )

  self.assertEqual(processor.image_processor.to_json_string()   ,   image_processor_add_kwargs.to_json_string()    )
  self.assertIsInstance(processor.image_processor   ,   __lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Dict	  =      self.get_image_processor()
  __A	:		Optional[Any]	  =      self.get_tokenizer()

  __A	:		str	  =      OwlViTProcessor(tokenizer=__lowercase   ,   image_processor=__lowercase    )

  __A	:		str	  =      self.prepare_image_inputs()

  __A	:		Any	  =      image_processor(__lowercase   ,   return_tensors='np'    )
  __A	:		List[str]	  =      processor(images=__lowercase   ,   return_tensors='np'    )

  for key in input_image_proc.keys():
   self.assertAlmostEqual(input_image_proc[key].sum()   ,   input_processor[key].sum()   ,   delta=1E-2    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		str	  =      self.get_image_processor()
  __A	:		Optional[int]	  =      self.get_tokenizer()

  __A	:		List[str]	  =      OwlViTProcessor(tokenizer=__lowercase   ,   image_processor=__lowercase    )

  __A	:		List[str]	  =      'lower newer'

  __A	:		Union[str, Any]	  =      processor(text=__lowercase   ,   return_tensors='np'    )

  __A	:		int	  =      tokenizer(__lowercase   ,   return_tensors='np'    )

  for key in encoded_tok.keys():
   self.assertListEqual(encoded_tok[key][0].tolist()   ,   encoded_processor[key][0].tolist()    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		str	  =      self.get_image_processor()
  __A	:		Tuple	  =      self.get_tokenizer()

  __A	:		str	  =      OwlViTProcessor(tokenizer=__lowercase   ,   image_processor=__lowercase    )

  __A	:		List[Any]	  =      'lower newer'
  __A	:		Union[str, Any]	  =      self.prepare_image_inputs()

  __A	:		List[str]	  =      processor(text=__lowercase   ,   images=__lowercase    )

  self.assertListEqual(list(inputs.keys()    )   ,   ['input_ids', 'attention_mask', 'pixel_values']    )

  # test if it raises when no input is passed
  with pytest.raises(__lowercase    ):
   processor()


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		List[Any]	  =      'google/owlvit-base-patch32'
  __A	:		str	  =      OwlViTProcessor.from_pretrained(__lowercase    )

  __A	:		Union[str, Any]	  =      ['cat', 'nasa badge']
  __A	:		Union[str, Any]	  =      processor(text=__lowercase    )

  __A	:		Tuple	  =      16
  self.assertListEqual(list(inputs.keys()    )   ,   ['input_ids', 'attention_mask']    )
  self.assertEqual(inputs['input_ids'].shape   ,   (2, seq_length)    )

  # test if it raises when no input is passed
  with pytest.raises(__lowercase    ):
   processor()


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		str	  =      'google/owlvit-base-patch32'
  __A	:		Optional[int]	  =      OwlViTProcessor.from_pretrained(__lowercase    )

  __A	:		Tuple	  =      [['cat', 'nasa badge'], ['person']]
  __A	:		int	  =      processor(text=__lowercase    )

  __A	:		Union[str, Any]	  =      16
  __A	:		Any	  =      len(__lowercase    )
  __A	:		Any	  =      max([len(__lowercase    ) for texts in input_texts]    )

  self.assertListEqual(list(inputs.keys()    )   ,   ['input_ids', 'attention_mask']    )
  self.assertEqual(inputs['input_ids'].shape   ,   (batch_size * num_max_text_queries, seq_length)    )

  # test if it raises when no input is passed
  with pytest.raises(__lowercase    ):
   processor()


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		int	  =      'google/owlvit-base-patch32'
  __A	:		Any	  =      OwlViTProcessor.from_pretrained(__lowercase    )

  __A	:		Optional[int]	  =      ['cat', 'nasa badge']
  __A	:		Optional[int]	  =      processor(text=__lowercase    )

  __A	:		List[str]	  =      16
  __A	:		Optional[int]	  =      inputs['input_ids']
  __A	:		Any	  =      [
      [49_406, 2_368, 49_407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      [49_406, 6_841, 11_301, 49_407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  ]

  self.assertListEqual(list(inputs.keys()    )   ,   ['input_ids', 'attention_mask']    )
  self.assertEqual(inputs['input_ids'].shape   ,   (2, seq_length)    )
  self.assertListEqual(list(input_ids[0]    )   ,   predicted_ids[0]    )
  self.assertListEqual(list(input_ids[1]    )   ,   predicted_ids[1]    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Optional[int]	  =      self.get_image_processor()
  __A	:		Optional[Any]	  =      self.get_tokenizer()

  __A	:		List[Any]	  =      OwlViTProcessor(tokenizer=__lowercase   ,   image_processor=__lowercase    )

  __A	:		int	  =      self.prepare_image_inputs()
  __A	:		Optional[int]	  =      self.prepare_image_inputs()

  __A	:		Dict	  =      processor(images=__lowercase   ,   query_images=__lowercase    )

  self.assertListEqual(list(inputs.keys()    )   ,   ['query_pixel_values', 'pixel_values']    )

  # test if it raises when no input is passed
  with pytest.raises(__lowercase    ):
   processor()







 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Union[str, Any]	  =      self.get_image_processor()
  __A	:		Optional[Any]	  =      self.get_tokenizer()

  __A	:		Dict	  =      OwlViTProcessor(tokenizer=__lowercase   ,   image_processor=__lowercase    )

  __A	:		int	  =      [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

  __A	:		Optional[int]	  =      processor.batch_decode(__lowercase    )
  __A	:		int	  =      tokenizer.batch_decode(__lowercase    )

  self.assertListEqual(__lowercase   ,   __lowercase    )










'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


UpperCAmelCase_			:  Optional[Any]										=   {
    'configuration_transfo_xl': ['TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TransfoXLConfig'],
    'tokenization_transfo_xl': ['TransfoXLCorpus', 'TransfoXLTokenizer'],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase_			:  Any										=   [
     'TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST',
     'AdaptiveEmbedding',
     'TransfoXLForSequenceClassification',
     'TransfoXLLMHeadModel',
     'TransfoXLModel',
     'TransfoXLPreTrainedModel',
     'load_tf_weights_in_transfo_xl',
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase_			:  Optional[Any]										=   [
     'TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST',
     'TFAdaptiveEmbedding',
     'TFTransfoXLForSequenceClassification',
     'TFTransfoXLLMHeadModel',
     'TFTransfoXLMainLayer',
     'TFTransfoXLModel',
     'TFTransfoXLPreTrainedModel',
 ]


if TYPE_CHECKING:
 from .configuration_transfo_xl import TRANSFO_XL_PRETRAINED_CONFIG_ARCHIVE_MAP, TransfoXLConfig
 from .tokenization_transfo_xl import TransfoXLCorpus, TransfoXLTokenizer

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_transfo_xl import (
      TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
      AdaptiveEmbedding,
      TransfoXLForSequenceClassification,
      TransfoXLLMHeadModel,
      TransfoXLModel,
      TransfoXLPreTrainedModel,
      load_tf_weights_in_transfo_xl,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_transfo_xl import (
      TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFAdaptiveEmbedding,
      TFTransfoXLForSequenceClassification,
      TFTransfoXLLMHeadModel,
      TFTransfoXLMainLayer,
      TFTransfoXLModel,
      TFTransfoXLPreTrainedModel,
  )

else:
 import sys

 UpperCAmelCase_			:  Optional[int]										=   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)






