




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	):

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  Optional[Any]		       =     tempfile.mkdtemp()

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

        lowerCAmelCase__				:  Any		       =     os.path.join(self.tmpdirname      ,				VOCAB_FILES_NAMES['''vocab_file''']		)
        lowerCAmelCase__				:  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		)		)

        lowerCAmelCase__				:  int		       =     {
            '''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],
        }
        lowerCAmelCase__				:  Dict		       =     os.path.join(self.tmpdirname      ,				__UpperCAmelCase		)
        with open(self.image_processor_file      ,				'''w'''      ,				encoding='''utf-8'''		) as fp:
          json.dump(__UpperCAmelCase      ,				__UpperCAmelCase		)

      def 	__magic_name__(   self      ,				**__UpperCAmelCase		):
        return CLIPTokenizer.from_pretrained(self.tmpdirname      ,				**__UpperCAmelCase		)

      def 	__magic_name__(   self      ,				**__UpperCAmelCase		):
        return CLIPTokenizerFast.from_pretrained(self.tmpdirname      ,				**__UpperCAmelCase		)

      def 	__magic_name__(   self      ,				**__UpperCAmelCase		):
        return CLIPImageProcessor.from_pretrained(self.tmpdirname      ,				**__UpperCAmelCase		)

      def 	__magic_name__(   self		):
        shutil.rmtree(self.tmpdirname		)

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  Tuple		       =     [np.random.randint(255      ,				size=(3, 30, 400)      ,				dtype=np.uinta		)]

        lowerCAmelCase__				:  int		       =     [Image.fromarray(np.moveaxis(__UpperCAmelCase      ,				0      ,				-1		)		) for x in image_inputs]

        return image_inputs

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  List[str]		       =     self.get_tokenizer()
        lowerCAmelCase__				:  Union[str, Any]		       =     self.get_rust_tokenizer()
        lowerCAmelCase__				:  Optional[Any]		       =     self.get_image_processor()

        lowerCAmelCase__				:  List[str]		       =     CLIPProcessor(tokenizer=__UpperCAmelCase      ,				image_processor=__UpperCAmelCase		)
        processor_slow.save_pretrained(self.tmpdirname		)
        lowerCAmelCase__				:  Union[str, Any]		       =     CLIPProcessor.from_pretrained(self.tmpdirname      ,				use_fast=__UpperCAmelCase		)

        lowerCAmelCase__				:  Dict		       =     CLIPProcessor(tokenizer=__UpperCAmelCase      ,				image_processor=__UpperCAmelCase		)
        processor_fast.save_pretrained(self.tmpdirname		)
        lowerCAmelCase__				:  List[str]		       =     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 	__magic_name__(   self		):
        lowerCAmelCase__				:  Union[str, Any]		       =     CLIPProcessor(tokenizer=self.get_tokenizer()      ,				image_processor=self.get_image_processor()		)
        processor.save_pretrained(self.tmpdirname		)

        lowerCAmelCase__				:  Optional[Any]		       =     self.get_tokenizer(bos_token='''(BOS)'''      ,				eos_token='''(EOS)'''		)
        lowerCAmelCase__				:  str		       =     self.get_image_processor(do_normalize=__UpperCAmelCase      ,				padding_value=1.0		)

        lowerCAmelCase__				:  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 	__magic_name__(   self		):
        lowerCAmelCase__				:  Union[str, Any]		       =     self.get_image_processor()
        lowerCAmelCase__				:  str		       =     self.get_tokenizer()

        lowerCAmelCase__				:  Optional[int]		       =     CLIPProcessor(tokenizer=__UpperCAmelCase      ,				image_processor=__UpperCAmelCase		)

        lowerCAmelCase__				:  Dict		       =     self.prepare_image_inputs()

        lowerCAmelCase__				:  List[str]		       =     image_processor(__UpperCAmelCase      ,				return_tensors='''np'''		)
        lowerCAmelCase__				:  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 	__magic_name__(   self		):
        lowerCAmelCase__				:  str		       =     self.get_image_processor()
        lowerCAmelCase__				:  str		       =     self.get_tokenizer()

        lowerCAmelCase__				:  Tuple		       =     CLIPProcessor(tokenizer=__UpperCAmelCase      ,				image_processor=__UpperCAmelCase		)

        lowerCAmelCase__				:  List[str]		       =     '''lower newer'''

        lowerCAmelCase__				:  Any		       =     processor(text=__UpperCAmelCase		)

        lowerCAmelCase__				:  Tuple		       =     tokenizer(__UpperCAmelCase		)

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

      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  int		       =     self.get_image_processor()
        lowerCAmelCase__				:  int		       =     self.get_tokenizer()

        lowerCAmelCase__				:  Any		       =     CLIPProcessor(tokenizer=__UpperCAmelCase      ,				image_processor=__UpperCAmelCase		)

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

        lowerCAmelCase__				:  Optional[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 	__magic_name__(   self		):
        lowerCAmelCase__				:  List[str]		       =     self.get_image_processor()
        lowerCAmelCase__				:  Any		       =     self.get_tokenizer()

        lowerCAmelCase__				:  Union[str, Any]		       =     CLIPProcessor(tokenizer=__UpperCAmelCase      ,				image_processor=__UpperCAmelCase		)

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

        lowerCAmelCase__				:  Dict		       =     processor.batch_decode(__UpperCAmelCase		)
        lowerCAmelCase__				:  str		       =     tokenizer.batch_decode(__UpperCAmelCase		)

        self.assertListEqual(__UpperCAmelCase      ,				__UpperCAmelCase		)







      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  str		       =     self.get_image_processor()
        lowerCAmelCase__				:  List[Any]		       =     self.get_tokenizer()

        lowerCAmelCase__				:  Optional[int]		       =     CLIPProcessor(tokenizer=__UpperCAmelCase      ,				image_processor=__UpperCAmelCase		)

        lowerCAmelCase__				:  Optional[Any]		       =     '''lower newer'''
        lowerCAmelCase__				:  str		       =     self.prepare_image_inputs()

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

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







def      __lowerCAmelCase			(					UpperCamelCase      )     ->	None:
  lowerCAmelCase__				:  Dict		       =     generate_pascal_triangle(UpperCamelCase      )
  for row_idx in range(UpperCamelCase      ):
    # Print left spaces
    for _ in range(num_rows - row_idx - 1      ):
      print(end=''' '''      )
    # Print row values
    for col_idx in range(row_idx + 1      ):
      if col_idx != row_idx:
        print(triangle[row_idx][col_idx]	,      end=''' '''      )
      else:
        print(triangle[row_idx][col_idx]	,      end=''''''      )
    print()



def      __lowerCAmelCase			(					UpperCamelCase      )     ->	list[list[int]]:

  if not isinstance(UpperCamelCase	,      UpperCamelCase      ):
    raise TypeError('''The input value of \'num_rows\' should be \'int\''''      )

  if num_rows == 0:
    return []
  elif num_rows < 0:
    raise ValueError(
        '''The input value of \'num_rows\' should be greater than or equal to 0'''      )

  lowerCAmelCase__				:  list[list[int]]		       =     []
  for current_row_idx in range(UpperCamelCase      ):
    lowerCAmelCase__				:  List[str]		       =     populate_current_row(UpperCamelCase	,      UpperCamelCase      )
    triangle.append(UpperCamelCase      )
  return triangle



def      __lowerCAmelCase			(					UpperCamelCase	,      UpperCamelCase      )     ->	list[int]:
  lowerCAmelCase__				:  Optional[Any]		       =     [-1] * (current_row_idx + 1)
  # first and last elements of current row are equal to 1
  lowerCAmelCase__       ,			lowerCAmelCase__				:  int		       =     1, 1
  for current_col_idx in range(1	,      UpperCamelCase      ):
    calculate_current_element(
        UpperCamelCase	,      UpperCamelCase	,      UpperCamelCase	,      UpperCamelCase      )
  return current_row



def      __lowerCAmelCase			(					UpperCamelCase	,      UpperCamelCase	,      UpperCamelCase	,      UpperCamelCase	,      )     ->	None:
  lowerCAmelCase__				:  Optional[int]		       =     triangle[current_row_idx - 1][current_col_idx - 1]
  lowerCAmelCase__				:  List[str]		       =     triangle[current_row_idx - 1][current_col_idx]
  lowerCAmelCase__				:  List[Any]		       =     above_to_left_elt + above_to_right_elt



def      __lowerCAmelCase			(					UpperCamelCase      )     ->	list[list[int]]:

  if not isinstance(UpperCamelCase	,      UpperCamelCase      ):
    raise TypeError('''The input value of \'num_rows\' should be \'int\''''      )

  if num_rows == 0:
    return []
  elif num_rows < 0:
    raise ValueError(
        '''The input value of \'num_rows\' should be greater than or equal to 0'''      )

  lowerCAmelCase__				:  list[list[int]]		       =     [[1]]

  for row_index in range(1	,      UpperCamelCase      ):
    lowerCAmelCase__				:  Optional[int]		       =     [0] + result[-1] + [0]
    lowerCAmelCase__				:  List[str]		       =     row_index + 1
    # Calculate the number of distinct elements in a row
    lowerCAmelCase__				:  Tuple		       =     sum(divmod(UpperCamelCase	,      2      )      )
    lowerCAmelCase__				:  Tuple		       =     [
        temp_row[i - 1] + temp_row[i] for i in range(1	,      distinct_elements + 1      )
    ]
    lowerCAmelCase__				:  int		       =     row_first_half[: (row_index + 1) // 2]
    row_second_half.reverse()
    lowerCAmelCase__				:  Dict		       =     row_first_half + row_second_half
    result.append(UpperCamelCase      )

  return result



def      __lowerCAmelCase			(					)     ->	None:
  from collections.abc import Callable
  from timeit import timeit

  def benchmark_a_function(UpperCamelCase	,      UpperCamelCase      ) -> None:
    lowerCAmelCase__				:  Tuple		       =     F"""{func.__name__}({value})"""
    lowerCAmelCase__				:  Dict		       =     timeit(F"""__main__.{call}"""	,      setup='''import __main__'''      )
    # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
    print(F"""{call:38} -- {timing:.4f} seconds"""      )

  for value in range(15      ):  # (1, 7, 14):
    for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
      benchmark_a_function(UpperCamelCase	,      UpperCamelCase      )
    print()


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     benchmark()

