

'''simple docstring'''



import os
import string
import sys


UpperCAmelCase :       Union[str, Any]			   =     1 << 8

UpperCAmelCase :       Any			   =     {
    'tab': ord('\t'),
    'newline': ord('\r'),
    'esc': 2_7,
    'up': 6_5 + ARROW_KEY_FLAG,
    'down': 6_6 + ARROW_KEY_FLAG,
    'right': 6_7 + ARROW_KEY_FLAG,
    'left': 6_8 + ARROW_KEY_FLAG,
    'mod_int': 9_1,
    'undefined': sys.maxsize,
    'interrupt': 3,
    'insert': 5_0,
    'delete': 5_1,
    'pg_up': 5_3,
    'pg_down': 5_4,
}

UpperCAmelCase :       Optional[Any]			   =     KEYMAP['up']
UpperCAmelCase :       Optional[int]			   =     KEYMAP['left']

if sys.platform == "win32":
  UpperCAmelCase :       Union[str, Any]			   =     []
  UpperCAmelCase :       Dict			   =     {
      b'\xe0H': KEYMAP['up'] - ARROW_KEY_FLAG,
      b'\x00H': KEYMAP['up'] - ARROW_KEY_FLAG,
      b'\xe0P': KEYMAP['down'] - ARROW_KEY_FLAG,
      b'\x00P': KEYMAP['down'] - ARROW_KEY_FLAG,
      b'\xe0M': KEYMAP['right'] - ARROW_KEY_FLAG,
      b'\x00M': KEYMAP['right'] - ARROW_KEY_FLAG,
      b'\xe0K': KEYMAP['left'] - ARROW_KEY_FLAG,
      b'\x00K': KEYMAP['left'] - ARROW_KEY_FLAG,
  }

for i in range(1_0):
  UpperCAmelCase :       Tuple			   =     ord(str(i))







def    a__       (  ):




     """simple docstring"""


     if os.name == "nt":
          import msvcrt

          __SCREAMING_SNAKE_CASE     =      """mbcs"""
          # Flush the keyboard buffer
          while msvcrt.kbhit():
               msvcrt.getch()
          if len(a__						) == 0:
               # Read the keystroke
               __SCREAMING_SNAKE_CASE     =      msvcrt.getch()

               # If it is a prefix char, get second part
               if ch in (b"\x00", b"\xe0"):
                    __SCREAMING_SNAKE_CASE     =      ch + msvcrt.getch()
                    # Translate actual Win chars to bullet char types
                    try:
                         __SCREAMING_SNAKE_CASE     =      chr(WIN_KEYMAP[cha]						)
                         WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""]						)						)
                         WIN_CH_BUFFER.append(a__						)
                         if ord(a__						) in (
                             KEYMAP["insert"] - 1 << 9,
                             KEYMAP["delete"] - 1 << 9,
                             KEYMAP["pg_up"] - 1 << 9,
                             KEYMAP["pg_down"] - 1 << 9,
                         ):
                              WIN_CH_BUFFER.append(chr(1_26						)						)
                         __SCREAMING_SNAKE_CASE     =      chr(KEYMAP["""esc"""]						)
                    except KeyError:
                         __SCREAMING_SNAKE_CASE     =      cha[1]
               else:
                    __SCREAMING_SNAKE_CASE     =      ch.decode(a__						)
          else:
               __SCREAMING_SNAKE_CASE     =      WIN_CH_BUFFER.pop(0						)
     elif os.name == "posix":
          import termios
          import tty

          __SCREAMING_SNAKE_CASE     =      sys.stdin.fileno()
          __SCREAMING_SNAKE_CASE     =      termios.tcgetattr(a__						)
          try:
               tty.setraw(a__						)
               __SCREAMING_SNAKE_CASE     =      sys.stdin.read(1						)
          finally:
               termios.tcsetattr(a__      ,    termios.TCSADRAIN      ,    a__						)
     return ch







def    a__       (  ):




     """simple docstring"""


     __SCREAMING_SNAKE_CASE     =      get_raw_chars()
     if ord(a__						) in [KEYMAP["interrupt"], KEYMAP["newline"]]:
          return char

     elif ord(a__						) == KEYMAP["esc"]:
          __SCREAMING_SNAKE_CASE     =      get_raw_chars()
          if ord(a__						) == KEYMAP["mod_int"]:
               __SCREAMING_SNAKE_CASE     =      get_raw_chars()
               if ord(a__						) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(a__						) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG:
                    return chr(ord(a__						) + ARROW_KEY_FLAG						)
               else:
                    return KEYMAP["undefined"]
          else:
               return get_raw_chars()

     else:
          if char in string.printable:
               return char
          else:
               return KEYMAP["undefined"]









'''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 CLIPImageProcessor, CLIPProcessor







@require_vision
class 						lowerCAmelCase__   (      unittest.TestCase					):



 """simple docstring"""



 def       UpperCAmelCase__		(		self					:       Union[str, Any]			)  -> List[Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      tempfile.mkdtemp()

      # fmt: off
      __SCREAMING_SNAKE_CASE     =      ["""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
      __SCREAMING_SNAKE_CASE     =      dict(zip(__SCREAMING_SNAKE_CASE					,							range(len(__SCREAMING_SNAKE_CASE			)			)			)			)
      __SCREAMING_SNAKE_CASE     =      ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
      __SCREAMING_SNAKE_CASE     =      {"""unk_token""": """<unk>"""}

      __SCREAMING_SNAKE_CASE     =      os.path.join(self.tmpdirname					,							VOCAB_FILES_NAMES["""vocab_file"""]			)
      __SCREAMING_SNAKE_CASE     =      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(__SCREAMING_SNAKE_CASE			) + """\n"""			)
      with open(self.merges_file					,							"""w"""					,							encoding="""utf-8"""			) as fp:
           fp.write("""\n""".join(__SCREAMING_SNAKE_CASE			)			)

      __SCREAMING_SNAKE_CASE     =      {
          """do_resize""": True,
          """size""": 20,
          """do_center_crop""": True,
          """crop_size""": 18,
          """do_normalize""": True,
          """image_mean""": [0.48145466, 0.4578275, 0.40821073],
          """image_std""": [0.26862954, 0.26130258, 0.27577711],
      }
      __SCREAMING_SNAKE_CASE     =      os.path.join(self.tmpdirname					,							__SCREAMING_SNAKE_CASE			)
      with open(self.image_processor_file					,							"""w"""					,							encoding="""utf-8"""			) as fp:
           json.dump(__SCREAMING_SNAKE_CASE					,							__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Optional[int]					,							**__SCREAMING_SNAKE_CASE					:       Optional[int]			)  -> str:


      """simple docstring"""




      return CLIPTokenizer.from_pretrained(self.tmpdirname					,							**__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Tuple					,							**__SCREAMING_SNAKE_CASE					:       Any			)  -> int:


      """simple docstring"""




      return CLIPTokenizerFast.from_pretrained(self.tmpdirname					,							**__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Optional[Any]					,							**__SCREAMING_SNAKE_CASE					:       Optional[int]			)  -> List[str]:


      """simple docstring"""




      return CLIPImageProcessor.from_pretrained(self.tmpdirname					,							**__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Tuple			)  -> str:


      """simple docstring"""




      shutil.rmtree(self.tmpdirname			)
 def       UpperCAmelCase__		(		self					:       Optional[Any]			)  -> str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      [np.random.randint(255					,							size=(3, 30, 400)					,							dtype=np.uinta			)]

      __SCREAMING_SNAKE_CASE     =      [Image.fromarray(np.moveaxis(__SCREAMING_SNAKE_CASE					,							0					,							-1			)			) for x in image_inputs]

      return image_inputs
 def       UpperCAmelCase__		(		self					:       Optional[int]			)  -> Any:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()
      __SCREAMING_SNAKE_CASE     =      self.get_rust_tokenizer()
      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)
      processor_slow.save_pretrained(self.tmpdirname			)
      __SCREAMING_SNAKE_CASE     =      CLIPProcessor.from_pretrained(self.tmpdirname					,							use_fast=__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)
      processor_fast.save_pretrained(self.tmpdirname			)
      __SCREAMING_SNAKE_CASE     =      CLIPProcessor.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					,							__SCREAMING_SNAKE_CASE			)
      self.assertIsInstance(processor_fast.tokenizer					,							__SCREAMING_SNAKE_CASE			)

      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					,							__SCREAMING_SNAKE_CASE			)
      self.assertIsInstance(processor_fast.image_processor					,							__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       Optional[int]			)  -> Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=self.get_tokenizer()					,							image_processor=self.get_image_processor()			)
      processor.save_pretrained(self.tmpdirname			)

      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer(bos_token="""(BOS)"""					,							eos_token="""(EOS)"""			)
      __SCREAMING_SNAKE_CASE     =      self.get_image_processor(do_normalize=__SCREAMING_SNAKE_CASE					,							padding_value=1.0			)

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor.from_pretrained(
          self.tmpdirname					,							bos_token="""(BOS)"""					,							eos_token="""(EOS)"""					,							do_normalize=__SCREAMING_SNAKE_CASE					,							padding_value=1.0			)

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

      self.assertEqual(processor.image_processor.to_json_string()					,							image_processor_add_kwargs.to_json_string()			)
      self.assertIsInstance(processor.image_processor					,							__SCREAMING_SNAKE_CASE			)
 def       UpperCAmelCase__		(		self					:       List[str]			)  -> Dict:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()
      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE     =      image_processor(__SCREAMING_SNAKE_CASE					,							return_tensors="""np"""			)
      __SCREAMING_SNAKE_CASE     =      processor(images=__SCREAMING_SNAKE_CASE					,							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       UpperCAmelCase__		(		self					:       List[Any]			)  -> int:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()
      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      """lower newer"""

      __SCREAMING_SNAKE_CASE     =      processor(text=__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      tokenizer(__SCREAMING_SNAKE_CASE			)

      for key in encoded_tok.keys():
           self.assertListEqual(encoded_tok[key]					,							encoded_processor[key]			)
 def       UpperCAmelCase__		(		self					:       Dict			)  -> Union[str, Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()
      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      """lower newer"""
      __SCREAMING_SNAKE_CASE     =      self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE     =      processor(text=__SCREAMING_SNAKE_CASE					,							images=__SCREAMING_SNAKE_CASE			)

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

      # test if it raises when no input is passed
      with pytest.raises(__SCREAMING_SNAKE_CASE			):
           processor()
 def       UpperCAmelCase__		(		self					:       List[str]			)  -> Union[str, Any]:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()
      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

      __SCREAMING_SNAKE_CASE     =      processor.batch_decode(__SCREAMING_SNAKE_CASE			)
      __SCREAMING_SNAKE_CASE     =      tokenizer.batch_decode(__SCREAMING_SNAKE_CASE			)

      self.assertListEqual(__SCREAMING_SNAKE_CASE					,							__SCREAMING_SNAKE_CASE			)


 def       UpperCAmelCase__		(		self					:       int			)  -> str:


      """simple docstring"""




      __SCREAMING_SNAKE_CASE     =      self.get_image_processor()
      __SCREAMING_SNAKE_CASE     =      self.get_tokenizer()

      __SCREAMING_SNAKE_CASE     =      CLIPProcessor(tokenizer=__SCREAMING_SNAKE_CASE					,							image_processor=__SCREAMING_SNAKE_CASE			)

      __SCREAMING_SNAKE_CASE     =      """lower newer"""
      __SCREAMING_SNAKE_CASE     =      self.prepare_image_inputs()

      __SCREAMING_SNAKE_CASE     =      processor(text=__SCREAMING_SNAKE_CASE					,							images=__SCREAMING_SNAKE_CASE			)

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






