






import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert 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 AlignProcessor, EfficientNetImageProcessor







@require_vision
class 	_UpperCAmelCase  (  unittest.TestCase		):




      """simple docstring"""



      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[Any]      							=				tempfile.mkdtemp()

          lowerCAmelCase__   :Dict      							=				[
              "[UNK]",
              "[CLS]",
              "[SEP]",
              "[PAD]",
              "[MASK]",
              "want",
              "##want",
              "##ed",
              "wa",
              "un",
              "runn",
              "##ing",
              ",",
              "low",
              "lowest",
          ]
          lowerCAmelCase__   :Tuple      							=				os.path.join(self.tmpdirname    ,	VOCAB_FILES_NAMES["vocab_file"]		)
          with open(self.vocab_file    ,	"w"    ,	encoding="utf-8"		) as vocab_writer:
              vocab_writer.write("".join([x + "\n" for x in vocab_tokens]		)		)

          lowerCAmelCase__   :Optional[int]      							=				{
              "do_resize": True,
              "size": 20,
              "do_center_crop": True,
              "crop_size": 18,
              "do_normalize": True,
              "image_mean": [0.4814_5466, 0.457_8275, 0.4082_1073],
              "image_std": [0.2686_2954, 0.2613_0258, 0.2757_7711],
          }
          lowerCAmelCase__   :int      							=				os.path.join(self.tmpdirname    ,	_lowerCAmelCase		)
          with open(self.image_processor_file    ,	"w"    ,	encoding="utf-8"		) as fp:
              json.dump(_lowerCAmelCase    ,	_lowerCAmelCase		)
      def 			snake_case_      (   self    ,	**_lowerCAmelCase		):



          '''simple docstring'''
          return BertTokenizer.from_pretrained(self.tmpdirname    ,	**_lowerCAmelCase		)
      def 			snake_case_      (   self    ,	**_lowerCAmelCase		):



          '''simple docstring'''
          return BertTokenizerFast.from_pretrained(self.tmpdirname    ,	**_lowerCAmelCase		)
      def 			snake_case_      (   self    ,	**_lowerCAmelCase		):



          '''simple docstring'''
          return EfficientNetImageProcessor.from_pretrained(self.tmpdirname    ,	**_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          shutil.rmtree(self.tmpdirname		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[int]      							=				[np.random.randint(255    ,	size=(3, 30, 400)    ,	dtype=np.uinta		)]
          lowerCAmelCase__   :Optional[int]      							=				[Image.fromarray(np.moveaxis(_lowerCAmelCase    ,	0    ,	-1		)		) for x in image_inputs]
          return image_inputs
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Tuple      							=				self.get_tokenizer()
          lowerCAmelCase__   :Optional[Any]      							=				self.get_rust_tokenizer()
          lowerCAmelCase__   :List[str]      							=				self.get_image_processor()

          lowerCAmelCase__   :int      							=				AlignProcessor(tokenizer=_lowerCAmelCase    ,	image_processor=_lowerCAmelCase		)
          processor_slow.save_pretrained(self.tmpdirname		)
          lowerCAmelCase__   :Optional[Any]      							=				AlignProcessor.from_pretrained(self.tmpdirname    ,	use_fast=_lowerCAmelCase		)

          lowerCAmelCase__   :Tuple      							=				AlignProcessor(tokenizer=_lowerCAmelCase    ,	image_processor=_lowerCAmelCase		)
          processor_fast.save_pretrained(self.tmpdirname		)
          lowerCAmelCase__   :str      							=				AlignProcessor.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    ,	_lowerCAmelCase		)
          self.assertIsInstance(processor_fast.tokenizer    ,	_lowerCAmelCase		)

          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    ,	_lowerCAmelCase		)
          self.assertIsInstance(processor_fast.image_processor    ,	_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[int]      							=				AlignProcessor(tokenizer=self.get_tokenizer()    ,	image_processor=self.get_image_processor()		)
          processor.save_pretrained(self.tmpdirname		)

          lowerCAmelCase__   :Tuple      							=				self.get_tokenizer(bos_token="(BOS)"    ,	eos_token="(EOS)"		)
          lowerCAmelCase__   :int      							=				self.get_image_processor(do_normalize=_lowerCAmelCase    ,	padding_value=1.0		)

          lowerCAmelCase__   :List[str]      							=				AlignProcessor.from_pretrained(
              self.tmpdirname    ,	bos_token="(BOS)"    ,	eos_token="(EOS)"    ,	do_normalize=_lowerCAmelCase    ,	padding_value=1.0		)

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

          self.assertEqual(processor.image_processor.to_json_string()    ,	image_processor_add_kwargs.to_json_string()		)
          self.assertIsInstance(processor.image_processor    ,	_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				self.get_image_processor()
          lowerCAmelCase__   :List[str]      							=				self.get_tokenizer()

          lowerCAmelCase__   :List[str]      							=				AlignProcessor(tokenizer=_lowerCAmelCase    ,	image_processor=_lowerCAmelCase		)

          lowerCAmelCase__   :Dict      							=				self.prepare_image_inputs()

          lowerCAmelCase__   :List[Any]      							=				image_processor(_lowerCAmelCase    ,	return_tensors="np"		)
          lowerCAmelCase__   :int      							=				processor(images=_lowerCAmelCase    ,	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'''
          lowerCAmelCase__   :Any      							=				self.get_image_processor()
          lowerCAmelCase__   :str      							=				self.get_tokenizer()

          lowerCAmelCase__   :Tuple      							=				AlignProcessor(tokenizer=_lowerCAmelCase    ,	image_processor=_lowerCAmelCase		)

          lowerCAmelCase__   :List[str]      							=				"lower newer"

          lowerCAmelCase__   :str      							=				processor(text=_lowerCAmelCase		)

          lowerCAmelCase__   :Optional[int]      							=				tokenizer(_lowerCAmelCase    ,	padding="max_length"    ,	max_length=64		)

          for key in encoded_tok.keys():
              self.assertListEqual(encoded_tok[key]    ,	encoded_processor[key]		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[Any]      							=				self.get_image_processor()
          lowerCAmelCase__   :str      							=				self.get_tokenizer()

          lowerCAmelCase__   :List[str]      							=				AlignProcessor(tokenizer=_lowerCAmelCase    ,	image_processor=_lowerCAmelCase		)

          lowerCAmelCase__   :Optional[Any]      							=				"lower newer"
          lowerCAmelCase__   :List[Any]      							=				self.prepare_image_inputs()

          lowerCAmelCase__   :Any      							=				processor(text=_lowerCAmelCase    ,	images=_lowerCAmelCase		)

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

          # test if it raises when no input is passed
          with pytest.raises(_lowerCAmelCase		):
              processor()
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Union[str, Any]      							=				self.get_image_processor()
          lowerCAmelCase__   :Union[str, Any]      							=				self.get_tokenizer()

          lowerCAmelCase__   :List[Any]      							=				AlignProcessor(tokenizer=_lowerCAmelCase    ,	image_processor=_lowerCAmelCase		)

          lowerCAmelCase__   :Tuple      							=				[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

          lowerCAmelCase__   :Optional[Any]      							=				processor.batch_decode(_lowerCAmelCase		)
          lowerCAmelCase__   :Optional[int]      							=				tokenizer.batch_decode(_lowerCAmelCase		)

          self.assertListEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)



      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				self.get_image_processor()
          lowerCAmelCase__   :List[str]      							=				self.get_tokenizer()

          lowerCAmelCase__   :Dict      							=				AlignProcessor(tokenizer=_lowerCAmelCase    ,	image_processor=_lowerCAmelCase		)

          lowerCAmelCase__   :List[Any]      							=				"lower newer"
          lowerCAmelCase__   :int      							=				self.prepare_image_inputs()

          lowerCAmelCase__   :str      							=				processor(text=_lowerCAmelCase    ,	images=_lowerCAmelCase		)

          self.assertListEqual(list(inputs.keys()		)    ,	processor.model_input_names		)











def 				snake_case__							(							UpperCAmelCase							:  float       ):

    if edge <= 0 or not isinstance(UpperCAmelCase    ,			UpperCAmelCase       ):
        raise ValueError("Length must be a positive."       )
    return 3 * ((2_5 + 1_0 * (5 ** (1 / 2))) ** (1 / 2)) * (edge**2)


def 				snake_case__							(							UpperCAmelCase							:  float       ):

    if edge <= 0 or not isinstance(UpperCAmelCase    ,			UpperCAmelCase       ):
        raise ValueError("Length must be a positive."       )
    return ((1_5 + (7 * (5 ** (1 / 2)))) / 4) * (edge**3)


if __name__ == "__main__":
 import doctest

 doctest.testmod()



