
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 FEATURE_EXTRACTOR_NAME, is_vision_available


if is_vision_available():
   from PIL import Image

   from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor





@require_vision
class     __UpperCamelCase (							unittest.TestCase       ):






     """simple docstring"""







     def  UpperCAmelCase__							(				self							:							List[Any] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Any					=		tempfile.mkdtemp()

       __SCREAMING_SNAKE_CASE      :	List[Any]					=		[
           '''[UNK]''',
           '''[CLS]''',
           '''[SEP]''',
           '''[PAD]''',
           '''[MASK]''',
           '''的''',
           '''价''',
           '''格''',
           '''是''',
           '''15''',
           '''便''',
           '''alex''',
           '''##andra''',
           '''，''',
           '''。''',
           '''-''',
           '''t''',
           '''shirt''',
       ]
       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		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] ) )

       __SCREAMING_SNAKE_CASE      :	int					=		{
           '''do_resize''': True,
           '''size''': {'''height''': 224, '''width''': 224},
           '''do_center_crop''': True,
           '''crop_size''': {'''height''': 18, '''width''': 18},
           '''do_normalize''': True,
           '''image_mean''': [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
           '''image_std''': [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
           '''do_convert_rgb''': True,
       }
       __SCREAMING_SNAKE_CASE      :	Dict					=		os.path.join(self.tmpdirname     ,						_A )
       with open(self.image_processor_file     ,						'''w'''     ,						encoding='''utf-8''' ) as fp:
         json.dump(_A     ,						_A )



     def  UpperCAmelCase__							(				self							:							int     ,						**_A							:							Any ):







       """simple docstring"""




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



     def  UpperCAmelCase__							(				self							:							Optional[int]     ,						**_A							:							Optional[Any] ):







       """simple docstring"""




       return BertTokenizerFast.from_pretrained(self.tmpdirname     ,						**_A )



     def  UpperCAmelCase__							(				self							:							Union[str, Any]     ,						**_A							:							Optional[int] ):







       """simple docstring"""




       return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname     ,						**_A )



     def  UpperCAmelCase__							(				self							:							List[Any] ):







       """simple docstring"""




       shutil.rmtree(self.tmpdirname )



     def  UpperCAmelCase__							(				self							:							str ):







       """simple docstring"""




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

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

       return image_inputs



     def  UpperCAmelCase__							(				self							:							int ):







       """simple docstring"""




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

       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		ChineseCLIPProcessor(tokenizer=_A     ,						image_processor=_A )
       processor_slow.save_pretrained(self.tmpdirname )
       __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		ChineseCLIPProcessor.from_pretrained(self.tmpdirname     ,						use_fast=_A )

       __SCREAMING_SNAKE_CASE      :	List[str]					=		ChineseCLIPProcessor(tokenizer=_A     ,						image_processor=_A )
       processor_fast.save_pretrained(self.tmpdirname )
       __SCREAMING_SNAKE_CASE      :	List[str]					=		ChineseCLIPProcessor.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     ,						_A )
       self.assertIsInstance(processor_fast.tokenizer     ,						_A )

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



     def  UpperCAmelCase__							(				self							:							Tuple ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Dict					=		ChineseCLIPProcessor(tokenizer=self.get_tokenizer()     ,						image_processor=self.get_image_processor() )
       processor.save_pretrained(self.tmpdirname )

       __SCREAMING_SNAKE_CASE      :	str					=		self.get_tokenizer(cls_token='''(CLS)'''     ,						sep_token='''(SEP)''' )
       __SCREAMING_SNAKE_CASE      :	Tuple					=		self.get_image_processor(do_normalize=_A )

       __SCREAMING_SNAKE_CASE      :	str					=		ChineseCLIPProcessor.from_pretrained(
           self.tmpdirname     ,						cls_token='''(CLS)'''     ,						sep_token='''(SEP)'''     ,						do_normalize=_A )

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

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



     def  UpperCAmelCase__							(				self							:							str ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Tuple					=		self.get_image_processor()
       __SCREAMING_SNAKE_CASE      :	Any					=		self.get_tokenizer()

       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		ChineseCLIPProcessor(tokenizer=_A     ,						image_processor=_A )

       __SCREAMING_SNAKE_CASE      :	List[Any]					=		self.prepare_image_inputs()

       __SCREAMING_SNAKE_CASE      :	List[Any]					=		image_processor(_A     ,						return_tensors='''np''' )
       __SCREAMING_SNAKE_CASE      :	List[str]					=		processor(images=_A     ,						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							:							str ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Union[str, Any]					=		self.get_image_processor()
       __SCREAMING_SNAKE_CASE      :	Dict					=		self.get_tokenizer()

       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		ChineseCLIPProcessor(tokenizer=_A     ,						image_processor=_A )

       __SCREAMING_SNAKE_CASE      :	str					=		'''Alexandra，T-shirt的价格是15便士。'''

       __SCREAMING_SNAKE_CASE      :	List[Any]					=		processor(text=_A )

       __SCREAMING_SNAKE_CASE      :	str					=		tokenizer(_A )

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



     def  UpperCAmelCase__							(				self							:							Dict ):







       """simple docstring"""




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

       __SCREAMING_SNAKE_CASE      :	Tuple					=		ChineseCLIPProcessor(tokenizer=_A     ,						image_processor=_A )

       __SCREAMING_SNAKE_CASE      :	int					=		'''Alexandra，T-shirt的价格是15便士。'''
       __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		self.prepare_image_inputs()

       __SCREAMING_SNAKE_CASE      :	int					=		processor(text=_A     ,						images=_A )

       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(_A ):
         processor()



     def  UpperCAmelCase__							(				self							:							List[str] ):







       """simple docstring"""




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

       __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		ChineseCLIPProcessor(tokenizer=_A     ,						image_processor=_A )

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

       __SCREAMING_SNAKE_CASE      :	str					=		processor.batch_decode(_A )
       __SCREAMING_SNAKE_CASE      :	Optional[Any]					=		tokenizer.batch_decode(_A )

       self.assertListEqual(_A     ,						_A )




     def  UpperCAmelCase__							(				self							:							Any ):







       """simple docstring"""




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

       __SCREAMING_SNAKE_CASE      :	Tuple					=		ChineseCLIPProcessor(tokenizer=_A     ,						image_processor=_A )

       __SCREAMING_SNAKE_CASE      :	int					=		'''Alexandra，T-shirt的价格是15便士。'''
       __SCREAMING_SNAKE_CASE      :	Dict					=		self.prepare_image_inputs()

       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		processor(text=_A     ,						images=_A )

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




from __future__ import annotations

import matplotlib.pyplot as plt  # type: ignore
import numpy

# initial triangle of Koch snowflake
lowerCamelCase		   =			numpy.array([0, 0])
lowerCamelCase		   =			numpy.array([0.5, 0.866_0254])
lowerCamelCase		   =			numpy.array([1, 0])
lowerCamelCase		   =			[VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1]




def       a_ (       SCREAMING_SNAKE_CASE__   : list[numpy.ndarray]						,     SCREAMING_SNAKE_CASE__   : int	):
       '''simple docstring'''





       _lowerCamelCase			:						List[Any]            =initial_vectors
       for _ in range(SCREAMING_SNAKE_CASE__	):
              _lowerCamelCase			:						List[str]            =iteration_step(SCREAMING_SNAKE_CASE__	)
       return vectors




def       a_ (       SCREAMING_SNAKE_CASE__   : list[numpy.ndarray]	):
       '''simple docstring'''





       _lowerCamelCase			:						Dict            =[]
       for i, start_vector in enumerate(vectors[:-1]	):
              _lowerCamelCase			:						Tuple            =vectors[i + 1]
              new_vectors.append(SCREAMING_SNAKE_CASE__	)
              _lowerCamelCase			:						Tuple            =end_vector - start_vector
              new_vectors.append(start_vector + difference_vector / 3	)
              new_vectors.append(
                  start_vector + difference_vector / 3 + rotate(difference_vector / 3						,     60	)	)
              new_vectors.append(start_vector + difference_vector * 2 / 3	)
       new_vectors.append(vectors[-1]	)
       return new_vectors




def       a_ (       SCREAMING_SNAKE_CASE__   : numpy.ndarray						,     SCREAMING_SNAKE_CASE__   : float	):
       '''simple docstring'''





       _lowerCamelCase			:						Union[str, Any]            =numpy.radians(SCREAMING_SNAKE_CASE__	)
       _lowerCamelCase      ,	_lowerCamelCase			:						List[Any]            =numpy.cos(SCREAMING_SNAKE_CASE__	), numpy.sin(SCREAMING_SNAKE_CASE__	)
       _lowerCamelCase			:						str            =numpy.array(((c, -s), (s, c))	)
       return numpy.dot(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)




def       a_ (       SCREAMING_SNAKE_CASE__   : list[numpy.ndarray]	):
       '''simple docstring'''





       _lowerCamelCase			:						Optional[Any]            =plt.gca()
       axes.set_aspect('equal'	)

       # matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all
       # y-coordinates as inputs, which are constructed from the vector-list using
       # zip()
       _lowerCamelCase      ,	_lowerCamelCase			:						Tuple            =zip(*SCREAMING_SNAKE_CASE__	)
       plt.plot(SCREAMING_SNAKE_CASE__						,     SCREAMING_SNAKE_CASE__	)
       plt.show()


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 lowerCamelCase		   =			iterate(INITIAL_VECTORS, 5)
 plot(processed_vectors)


