




"""simple docstring"""





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


			def       a_     (      self      :    List[str]):


						"""simple docstring"""




						__UpperCAmelCase	: int		=					tempfile.mkdtemp()

						__UpperCAmelCase	: int		=					[
						    "[UNK]",
						    "[CLS]",
						    "[SEP]",
						    "[PAD]",
						    "[MASK]",
						    "的",
						    "价",
						    "格",
						    "是",
						    "15",
						    "便",
						    "alex",
						    "##andra",
						    "，",
						    "。",
						    "-",
						    "t",
						    "shirt",
						]
						__UpperCAmelCase	: 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	: Any		=					{
						    "do_resize": True,
						    "size": {"height": 224, "width": 224},
						    "do_center_crop": True,
						    "crop_size": {"height": 18, "width": 18},
						    "do_normalize": True,
						    "image_mean": [0.48145466, 0.4578275, 0.40821073],
						    "image_std": [0.26862954, 0.26130258, 0.27577711],
						    "do_convert_rgb": True,
						}
						__UpperCAmelCase	: Any		=					os.path.join(self.tmpdirname      ,  UpperCamelCase_)
						with open(self.image_processor_file      ,  "w"      ,  encoding="utf-8") as fp:
									json.dump(UpperCamelCase_      ,  UpperCamelCase_)


			def       a_     (      self      :    int      ,  **UpperCamelCase_      :    Union[str, Any]):


						"""simple docstring"""




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


			def       a_     (      self      :    int      ,  **UpperCamelCase_      :    Union[str, Any]):


						"""simple docstring"""




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


			def       a_     (      self      :    str      ,  **UpperCamelCase_      :    Any):


						"""simple docstring"""




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


			def       a_     (      self      :    Any):


						"""simple docstring"""




						shutil.rmtree(self.tmpdirname)


			def       a_     (      self      :    List[str]):


						"""simple docstring"""




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

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

						return image_inputs


			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					self.get_tokenizer()
						__UpperCAmelCase	: Tuple		=					self.get_rust_tokenizer()
						__UpperCAmelCase	: int		=					self.get_image_processor()

						__UpperCAmelCase	: str		=					ChineseCLIPProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)
						processor_slow.save_pretrained(self.tmpdirname)
						__UpperCAmelCase	: Dict		=					ChineseCLIPProcessor.from_pretrained(self.tmpdirname      ,  use_fast=UpperCamelCase_)

						__UpperCAmelCase	: Tuple		=					ChineseCLIPProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)
						processor_fast.save_pretrained(self.tmpdirname)
						__UpperCAmelCase	: Optional[int]		=					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      ,  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       a_     (      self      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: str		=					ChineseCLIPProcessor(tokenizer=self.get_tokenizer()      ,  image_processor=self.get_image_processor())
						processor.save_pretrained(self.tmpdirname)

						__UpperCAmelCase	: str		=					self.get_tokenizer(cls_token="(CLS)"      ,  sep_token="(SEP)")
						__UpperCAmelCase	: Any		=					self.get_image_processor(do_normalize=UpperCamelCase_)

						__UpperCAmelCase	: Optional[Any]		=					ChineseCLIPProcessor.from_pretrained(
						    self.tmpdirname      ,  cls_token="(CLS)"      ,  sep_token="(SEP)"      ,  do_normalize=UpperCamelCase_)

						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       a_     (      self      :    List[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					self.get_image_processor()
						__UpperCAmelCase	: List[Any]		=					self.get_tokenizer()

						__UpperCAmelCase	: int		=					ChineseCLIPProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)

						__UpperCAmelCase	: Tuple		=					self.prepare_image_inputs()

						__UpperCAmelCase	: str		=					image_processor(UpperCamelCase_      ,  return_tensors="np")
						__UpperCAmelCase	: Any		=					processor(images=UpperCamelCase_      ,  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       a_     (      self      :    int):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					self.get_image_processor()
						__UpperCAmelCase	: Union[str, Any]		=					self.get_tokenizer()

						__UpperCAmelCase	: int		=					ChineseCLIPProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)

						__UpperCAmelCase	: List[Any]		=					"Alexandra，T-shirt的价格是15便士。"

						__UpperCAmelCase	: Any		=					processor(text=UpperCamelCase_)

						__UpperCAmelCase	: Union[str, Any]		=					tokenizer(UpperCamelCase_)

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


			def       a_     (      self      :    List[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					self.get_image_processor()
						__UpperCAmelCase	: Optional[Any]		=					self.get_tokenizer()

						__UpperCAmelCase	: int		=					ChineseCLIPProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)

						__UpperCAmelCase	: Tuple		=					"Alexandra，T-shirt的价格是15便士。"
						__UpperCAmelCase	: Any		=					self.prepare_image_inputs()

						__UpperCAmelCase	: Union[str, Any]		=					processor(text=UpperCamelCase_      ,  images=UpperCamelCase_)

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


			def       a_     (      self      :    List[Any]):


						"""simple docstring"""




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

						__UpperCAmelCase	: List[str]		=					ChineseCLIPProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)

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

						__UpperCAmelCase	: int		=					processor.batch_decode(UpperCamelCase_)
						__UpperCAmelCase	: List[Any]		=					tokenizer.batch_decode(UpperCamelCase_)

						self.assertListEqual(UpperCamelCase_      ,  UpperCamelCase_)


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


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					self.get_image_processor()
						__UpperCAmelCase	: Dict		=					self.get_tokenizer()

						__UpperCAmelCase	: Tuple		=					ChineseCLIPProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)

						__UpperCAmelCase	: List[str]		=					"Alexandra，T-shirt的价格是15便士。"
						__UpperCAmelCase	: str		=					self.prepare_image_inputs()

						__UpperCAmelCase	: List[str]		=					processor(text=UpperCamelCase_      ,  images=UpperCamelCase_)

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












"""simple docstring"""





from ...processing_utils import ProcessorMixin



class      a__				(	__magic_name__   ):
			lowercase_    =	["image_processor", "feature_extractor"]
			lowercase_    =	"TvltImageProcessor"
			lowercase_    =	"TvltFeatureExtractor"


			def __init__(      self      :    Tuple      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Dict):


						"""simple docstring"""




						super().__init__(image_processor=UpperCamelCase_      ,  feature_extractor=UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					image_processor
						__UpperCAmelCase	: Dict		=					feature_extractor


			def __call__(      self      :    Tuple      ,  UpperCamelCase_      :    str=None      ,  UpperCamelCase_      :    Optional[int]=None      ,  UpperCamelCase_      :    Optional[int]=None      ,  UpperCamelCase_      :    str=None      ,  UpperCamelCase_      :    List[str]=False      ,  UpperCamelCase_      :    Optional[int]=False      ,  *UpperCamelCase_      :    List[str]      ,  **UpperCamelCase_      :    str      ,  ):


						"""simple docstring"""




						if images is None and audio is None:
									raise ValueError("You need to specify either an `images` or `audio` input to process.")

						__UpperCAmelCase	: Optional[Any]		=					None
						if images is not None:
									__UpperCAmelCase	: int		=					self.image_processor(UpperCamelCase_      ,  mask_pixel=UpperCamelCase_      ,  *UpperCamelCase_      ,  **UpperCamelCase_)
						if images_mixed is not None:
									__UpperCAmelCase	: int		=					self.image_processor(UpperCamelCase_      ,  is_mixed=UpperCamelCase_      ,  *UpperCamelCase_      ,  **UpperCamelCase_)
						if audio is not None:
									__UpperCAmelCase	: List[Any]		=					self.feature_extractor(
									    UpperCamelCase_      ,  *UpperCamelCase_      ,  sampling_rate=UpperCamelCase_      ,  mask_audio=UpperCamelCase_      ,  **UpperCamelCase_)

						__UpperCAmelCase	: List[str]		=					{}
						if audio is not None:
									output_dict.update(UpperCamelCase_)
						if images is not None:
									output_dict.update(UpperCamelCase_)
						if images_mixed_dict is not None:
									output_dict.update(UpperCamelCase_)
						return output_dict


			@property
			def       a_     (      self      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					self.image_processor.model_input_names
						__UpperCAmelCase	: List[Any]		=					self.feature_extractor.model_input_names
						return list(dict.fromkeys(image_processor_input_names + feature_extractor_input_names))


