

'''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        a__  (							unittest.TestCase  ):


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Tuple      					=       tempfile.mkdtemp()

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

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

      _lowercase      :				Optional[int]      					=       {
          "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],
      }
      _lowercase      :				Optional[Any]      					=       os.path.join(self.tmpdirname      ,    _UpperCamelCase  )
      with open(self.image_processor_file      ,    "w"      ,    encoding="utf-8"  ) as fp:
         json.dump(_UpperCamelCase      ,    _UpperCamelCase  )


   def 						_lowerCamelCase       (			self      ,    **_UpperCamelCase  ):





      """simple docstring"""





      return CLIPTokenizer.from_pretrained(self.tmpdirname      ,    **_UpperCamelCase  )


   def 						_lowerCamelCase       (			self      ,    **_UpperCamelCase  ):





      """simple docstring"""





      return CLIPTokenizerFast.from_pretrained(self.tmpdirname      ,    **_UpperCamelCase  )


   def 						_lowerCamelCase       (			self      ,    **_UpperCamelCase  ):





      """simple docstring"""





      return CLIPImageProcessor.from_pretrained(self.tmpdirname      ,    **_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      shutil.rmtree(self.tmpdirname  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Union[str, Any]      					=       [np.random.randint(255      ,    size=(3, 30, 400)      ,    dtype=np.uinta  )]

      _lowercase      :				str      					=       [Image.fromarray(np.moveaxis(_UpperCamelCase      ,    0      ,    -1  )  ) for x in image_inputs]

      return image_inputs


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Tuple      					=       self.get_tokenizer()
      _lowercase      :				Tuple      					=       self.get_rust_tokenizer()
      _lowercase      :				str      					=       self.get_image_processor()

      _lowercase      :				Optional[Any]      					=       CLIPProcessor(tokenizer=_UpperCamelCase      ,    image_processor=_UpperCamelCase  )
      processor_slow.save_pretrained(self.tmpdirname  )
      _lowercase      :				Any      					=       CLIPProcessor.from_pretrained(self.tmpdirname      ,    use_fast=_UpperCamelCase  )

      _lowercase      :				Dict      					=       CLIPProcessor(tokenizer=_UpperCamelCase      ,    image_processor=_UpperCamelCase  )
      processor_fast.save_pretrained(self.tmpdirname  )
      _lowercase      :				Union[str, Any]      					=       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      ,    _UpperCamelCase  )
      self.assertIsInstance(processor_fast.tokenizer      ,    _UpperCamelCase  )

      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      ,    _UpperCamelCase  )
      self.assertIsInstance(processor_fast.image_processor      ,    _UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				int      					=       CLIPProcessor(tokenizer=self.get_tokenizer()      ,    image_processor=self.get_image_processor()  )
      processor.save_pretrained(self.tmpdirname  )

      _lowercase      :				Tuple      					=       self.get_tokenizer(bos_token="(BOS)"      ,    eos_token="(EOS)"  )
      _lowercase      :				List[Any]      					=       self.get_image_processor(do_normalize=_UpperCamelCase      ,    padding_value=1.0  )

      _lowercase      :				Dict      					=       CLIPProcessor.from_pretrained(
          self.tmpdirname      ,    bos_token="(BOS)"      ,    eos_token="(EOS)"      ,    do_normalize=_UpperCamelCase      ,    padding_value=1.0  )

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

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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Union[str, Any]      					=       self.get_image_processor()
      _lowercase      :				List[str]      					=       self.get_tokenizer()

      _lowercase      :				Tuple      					=       CLIPProcessor(tokenizer=_UpperCamelCase      ,    image_processor=_UpperCamelCase  )

      _lowercase      :				str      					=       self.prepare_image_inputs()

      _lowercase      :				int      					=       image_processor(_UpperCamelCase      ,    return_tensors="np"  )
      _lowercase      :				List[str]      					=       processor(images=_UpperCamelCase      ,    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 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Union[str, Any]      					=       self.get_image_processor()
      _lowercase      :				List[Any]      					=       self.get_tokenizer()

      _lowercase      :				Union[str, Any]      					=       CLIPProcessor(tokenizer=_UpperCamelCase      ,    image_processor=_UpperCamelCase  )

      _lowercase      :				Optional[Any]      					=       "lower newer"

      _lowercase      :				Optional[Any]      					=       processor(text=_UpperCamelCase  )

      _lowercase      :				str      					=       tokenizer(_UpperCamelCase  )

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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Dict      					=       self.get_image_processor()
      _lowercase      :				List[Any]      					=       self.get_tokenizer()

      _lowercase      :				Union[str, Any]      					=       CLIPProcessor(tokenizer=_UpperCamelCase      ,    image_processor=_UpperCamelCase  )

      _lowercase      :				Optional[Any]      					=       "lower newer"
      _lowercase      :				Dict      					=       self.prepare_image_inputs()

      _lowercase      :				Union[str, Any]      					=       processor(text=_UpperCamelCase      ,    images=_UpperCamelCase  )

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

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


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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

      _lowercase      :				Optional[int]      					=       CLIPProcessor(tokenizer=_UpperCamelCase      ,    image_processor=_UpperCamelCase  )

      _lowercase      :				Any      					=       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

      _lowercase      :				Tuple      					=       processor.batch_decode(_UpperCamelCase  )
      _lowercase      :				List[str]      					=       tokenizer.batch_decode(_UpperCamelCase  )

      self.assertListEqual(_UpperCamelCase      ,    _UpperCamelCase  )






   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





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

      _lowercase      :				Any      					=       CLIPProcessor(tokenizer=_UpperCamelCase      ,    image_processor=_UpperCamelCase  )

      _lowercase      :				Union[str, Any]      					=       "lower newer"
      _lowercase      :				Any      					=       self.prepare_image_inputs()

      _lowercase      :				List[str]      					=       processor(text=_UpperCamelCase      ,    images=_UpperCamelCase  )

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









'''simple docstring'''
import doctest
import logging
import os
import unittest
from pathlib import Path
from typing import List, Union

import transformers
from transformers.testing_utils import require_tf, require_torch, slow


_snake_case         =  logging.getLogger()


@unittest.skip('Temporarily disable the doc tests.'  )
@require_torch
@require_tf
@slow
class        a__  (							unittest.TestCase  ):


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase = None      ,    _UpperCamelCase = None      ,    _UpperCamelCase = None      ,    _UpperCamelCase = True      ,    ):





      """simple docstring"""





      _lowercase      :				str      					=       [file for file in os.listdir(_UpperCamelCase  ) if os.path.isfile(os.path.join(_UpperCamelCase      ,    _UpperCamelCase  )  )]

      if identifier is not None:
         _lowercase      :				str      					=       [file for file in files if identifier in file]

      if n_identifier is not None:
         if isinstance(_UpperCamelCase      ,    _UpperCamelCase  ):
            for n_ in n_identifier:
               _lowercase      :				Dict      					=       [file for file in files if n_ not in file]
         else:
            _lowercase      :				Optional[Any]      					=       [file for file in files if n_identifier not in file]

      _lowercase      :				Dict      					=       ignore_files or []
      ignore_files.append("__init__.py"  )
      _lowercase      :				List[str]      					=       [file for file in files if file not in ignore_files]

      for file in files:
         # Open all files
         print("Testing"      ,    _UpperCamelCase  )

         if only_modules:
            _lowercase      :				Optional[Any]      					=       file.split("."  )[0]
            try:
               _lowercase      :				Union[str, Any]      					=       getattr(_UpperCamelCase      ,    _UpperCamelCase  )
               _lowercase      :				Optional[int]      					=       doctest.DocTestSuite(_UpperCamelCase  )
               _lowercase      :				Tuple      					=       unittest.TextTestRunner().run(_UpperCamelCase  )
               self.assertIs(len(result.failures  )      ,    0  )
            except AttributeError:
               logger.info(f'''{module_identifier} is not a module.'''  )
         else:
            _lowercase      :				Optional[Any]      					=       doctest.testfile(str(".." / directory / file  )      ,    optionflags=doctest.ELLIPSIS  )
            self.assertIs(result.failed      ,    0  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Tuple      					=       Path("src/transformers"  )
      _lowercase      :				str      					=       "modeling"
      _lowercase      :				Tuple      					=       [
          "modeling_ctrl.py",
          "modeling_tf_ctrl.py",
      ]
      self.analyze_directory(_UpperCamelCase      ,    identifier=_UpperCamelCase      ,    ignore_files=_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Optional[int]      					=       Path("src/transformers"  )
      _lowercase      :				Any      					=       "tokenization"
      self.analyze_directory(_UpperCamelCase      ,    identifier=_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Tuple      					=       Path("src/transformers"  )
      _lowercase      :				Optional[Any]      					=       "configuration"
      self.analyze_directory(_UpperCamelCase      ,    identifier=_UpperCamelCase  )


   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Union[str, Any]      					=       Path("src/transformers"  )
      _lowercase      :				List[Any]      					=       ["configuration", "modeling", "tokenization"]
      self.analyze_directory(_UpperCamelCase      ,    n_identifier=_UpperCamelCase  )






   def 						_lowerCamelCase       (			self  ):





      """simple docstring"""





      _lowercase      :				Dict      					=       Path("docs/source"  )
      _lowercase      :				int      					=       ["favicon.ico"]
      self.analyze_directory(_UpperCamelCase      ,    ignore_files=_UpperCamelCase      ,    only_modules=_UpperCamelCase  )






