
'''simple docstring'''





import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
      from PIL import Image

      from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor






@require_tokenizers
@require_vision
class 							UpperCAmelCase_							(    unittest.TestCase				):

 '''simple docstring'''







 def 							UpperCamelCase  (					self		:  Optional[int] ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							tempfile.mkdtemp()

   # fmt: off
   UpperCAmelCase__      :					str				       =							["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest"]
   # fmt: on
   UpperCAmelCase__      :					List[str]				       =							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] ) )

   UpperCAmelCase__      :					List[str]				       =							{
       "do_resize": True,
       "size": {"height": 18, "width": 18},
       "do_normalize": True,
       "image_mean": [0.5, 0.5, 0.5],
       "image_std": [0.5, 0.5, 0.5],
   }
   UpperCAmelCase__      :					Optional[int]				       =							os.path.join(self.tmpdirname			,				snake_case__ )
   with open(self.image_processor_file			,				"w"			,				encoding="utf-8" ) as fp:
     json.dump(snake_case__			,				snake_case__ )







 def 							UpperCamelCase  (					self		:  Tuple			,				**snake_case__		:  Tuple ):



   '''simple docstring'''




   return BertTokenizer.from_pretrained(self.tmpdirname			,				**snake_case__ )







 def 							UpperCamelCase  (					self		:  List[str]			,				**snake_case__		:  List[Any] ):



   '''simple docstring'''




   return ViTImageProcessor.from_pretrained(self.tmpdirname			,				**snake_case__ )







 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   shutil.rmtree(self.tmpdirname )







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							[np.random.randint(2_55			,				size=(3, 30, 4_00)			,				dtype=np.uinta )]

   UpperCAmelCase__      :					Dict				       =							[Image.fromarray(np.moveaxis(snake_case__			,				0			,				-1 ) ) for x in image_inputs]

   return image_inputs







 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   UpperCAmelCase__      :					Dict				       =							self.get_tokenizer()
   UpperCAmelCase__      :					Dict				       =							self.get_image_processor()

   UpperCAmelCase__      :					int				       =							VisionTextDualEncoderProcessor(tokenizer=snake_case__			,				image_processor=snake_case__ )

   processor.save_pretrained(self.tmpdirname )
   UpperCAmelCase__      :					List[str]				       =							VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname )

   self.assertEqual(processor.tokenizer.get_vocab()			,				tokenizer.get_vocab() )
   self.assertIsInstance(processor.tokenizer			,				(BertTokenizer, BertTokenizerFast) )

   self.assertEqual(processor.image_processor.to_json_string()			,				image_processor.to_json_string() )
   self.assertIsInstance(processor.image_processor			,				snake_case__ )







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[Any]				       =							VisionTextDualEncoderProcessor(
       tokenizer=self.get_tokenizer()			,				image_processor=self.get_image_processor() )
   processor.save_pretrained(self.tmpdirname )

   UpperCAmelCase__      :					Any				       =							self.get_tokenizer(bos_token="(BOS)"			,				eos_token="(EOS)" )
   UpperCAmelCase__      :					int				       =							self.get_image_processor(do_normalize=snake_case__			,				padding_value=1.0 )

   UpperCAmelCase__      :					List[Any]				       =							VisionTextDualEncoderProcessor.from_pretrained(
       self.tmpdirname			,				bos_token="(BOS)"			,				eos_token="(EOS)"			,				do_normalize=snake_case__			,				padding_value=1.0 )

   self.assertEqual(processor.tokenizer.get_vocab()			,				tokenizer_add_kwargs.get_vocab() )
   self.assertIsInstance(processor.tokenizer			,				(BertTokenizer, BertTokenizerFast) )

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







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Dict				       =							self.get_image_processor()
   UpperCAmelCase__      :					int				       =							self.get_tokenizer()

   UpperCAmelCase__      :					Tuple				       =							VisionTextDualEncoderProcessor(tokenizer=snake_case__			,				image_processor=snake_case__ )

   UpperCAmelCase__      :					Dict				       =							self.prepare_image_inputs()

   UpperCAmelCase__      :					int				       =							image_processor(snake_case__			,				return_tensors="np" )
   UpperCAmelCase__      :					Optional[int]				       =							processor(images=snake_case__			,				return_tensors="np" )

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







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							self.get_image_processor()
   UpperCAmelCase__      :					List[Any]				       =							self.get_tokenizer()

   UpperCAmelCase__      :					Optional[Any]				       =							VisionTextDualEncoderProcessor(tokenizer=snake_case__			,				image_processor=snake_case__ )

   UpperCAmelCase__      :					List[str]				       =							"lower newer"

   UpperCAmelCase__      :					Optional[int]				       =							processor(text=snake_case__ )

   UpperCAmelCase__      :					int				       =							tokenizer(snake_case__ )

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







 def 							UpperCamelCase  (					self		:  List[Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[Any]				       =							self.get_image_processor()
   UpperCAmelCase__      :					List[str]				       =							self.get_tokenizer()

   UpperCAmelCase__      :					Optional[Any]				       =							VisionTextDualEncoderProcessor(tokenizer=snake_case__			,				image_processor=snake_case__ )

   UpperCAmelCase__      :					Tuple				       =							"lower newer"
   UpperCAmelCase__      :					List[str]				       =							self.prepare_image_inputs()

   UpperCAmelCase__      :					List[str]				       =							processor(text=snake_case__			,				images=snake_case__ )

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

   # test if it raises when no input is passed
   with self.assertRaises(snake_case__ ):
     processor()







 def 							UpperCamelCase  (					self		:  Dict ):



   '''simple docstring'''




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

   UpperCAmelCase__      :					Optional[Any]				       =							VisionTextDualEncoderProcessor(tokenizer=snake_case__			,				image_processor=snake_case__ )

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

   UpperCAmelCase__      :					Union[str, Any]				       =							processor.batch_decode(snake_case__ )
   UpperCAmelCase__      :					Any				       =							tokenizer.batch_decode(snake_case__ )

   self.assertListEqual(snake_case__			,				snake_case__ )







 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   UpperCAmelCase__      :					str				       =							self.get_image_processor()
   UpperCAmelCase__      :					Dict				       =							self.get_tokenizer()

   UpperCAmelCase__      :					Union[str, Any]				       =							VisionTextDualEncoderProcessor(tokenizer=snake_case__			,				image_processor=snake_case__ )

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

   UpperCAmelCase__      :					Dict				       =							processor(text=snake_case__			,				images=snake_case__ )

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





'''simple docstring'''





from math import factorial

SCREAMING_SNAKE_CASE     =       {str(digit): factorial(digit) for digit in range(1_0)}







def snake_case_     (					lowercase__		):
  if not isinstance(lowercase__   ,       lowercase__		):
    raise TypeError("Parameter number must be int"		)

  if number < 0:
    raise ValueError("Parameter number must be greater than or equal to 0"		)

  # Converts number in string to iterate on its digits and adds its factorial.
  return sum(DIGIT_FACTORIAL[digit] for digit in str(lowercase__		)		)







def snake_case_     (					lowercase__ = 6_0   ,       lowercase__ = 1_0_0_0_0_0_0		):

  if not isinstance(lowercase__   ,       lowercase__		) or not isinstance(lowercase__   ,       lowercase__		):
    raise TypeError("Parameters chain_length and number_limit must be int"		)

  if chain_length <= 0 or number_limit <= 0:
    raise ValueError(
        "Parameters chain_length and number_limit must be greater than 0"		)

  # the counter for the chains with the exact desired length
  UpperCAmelCase__      :					Union[str, Any]				       =							0
  # the cached sizes of the previous chains
  UpperCAmelCase__      :					dict[int, int]				       =							{}

  for start_chain_element in range(1   ,       lowercase__		):
    # The temporary set will contain the elements of the chain
    UpperCAmelCase__      :					Any				       =							set()
    UpperCAmelCase__      :					int				       =							0

    # Stop computing the chain when you find a cached size, a repeating item or the
    # length is greater then the desired one.
    UpperCAmelCase__      :					List[str]				       =							start_chain_element
    while (
        chain_element not in chain_sets_lengths
        and chain_element not in chain_set
        and chain_set_length <= chain_length
    ):
      chain_set.add(lowercase__		)
      chain_set_length += 1
      UpperCAmelCase__      :					List[str]				       =							digit_factorial_sum(lowercase__		)

    if chain_element in chain_sets_lengths:
      chain_set_length += chain_sets_lengths[chain_element]

    UpperCAmelCase__      :					List[str]				       =							chain_set_length

    # If chain contains the exact amount of elements increase the counter
    if chain_set_length == chain_length:
      chains_counter += 1

  return chains_counter


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      print(F'{solution()}')



