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

						def  A_				(			self				)					->      List[Any]:


								'''simple docstring'''

								_UpperCamelCase						    =	tempfile.mkdtemp()

								_UpperCamelCase						    =	[
								    """[UNK]""",
								    """[CLS]""",
								    """[SEP]""",
								    """[PAD]""",
								    """[MASK]""",
								    """的""",
								    """价""",
								    """格""",
								    """是""",
								    """15""",
								    """便""",
								    """alex""",
								    """##andra""",
								    """，""",
								    """。""",
								    """-""",
								    """t""",
								    """shirt""",
								]
								_UpperCamelCase						    =	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						    =	{
								    """do_resize""": True,
								    """size""": {"""height""": 2_24, """width""": 2_24},
								    """do_center_crop""": True,
								    """crop_size""": {"""height""": 18, """width""": 18},
								    """do_normalize""": True,
								    """image_mean""": [0.4814_5466, 0.457_8275, 0.4082_1073],
								    """image_std""": [0.2686_2954, 0.2613_0258, 0.2757_7711],
								    """do_convert_rgb""": True,
								}
								_UpperCamelCase						    =	os.path.join(self.tmpdirname       ,							a				)
								with open(self.image_processor_file       ,							"""w"""       ,							encoding="""utf-8"""				) as fp:
										json.dump(a       ,							a				)

						def  A_				(			self       ,							**a				)					->      Dict:


								'''simple docstring'''

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

						def  A_				(			self       ,							**a				)					->      Union[str, Any]:


								'''simple docstring'''

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

						def  A_				(			self       ,							**a				)					->      Any:


								'''simple docstring'''

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

						def  A_				(			self				)					->      List[str]:


								'''simple docstring'''

								shutil.rmtree(self.tmpdirname				)

						def  A_				(			self				)					->      Optional[Any]:


								'''simple docstring'''

								_UpperCamelCase						    =	[np.random.randint(2_55       ,							size=(3, 30, 4_00)       ,							dtype=np.uinta				)]

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

								return image_inputs

						def  A_				(			self				)					->      List[Any]:


								'''simple docstring'''

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

								_UpperCamelCase						    =	ChineseCLIPProcessor(tokenizer=a       ,							image_processor=a				)
								processor_slow.save_pretrained(self.tmpdirname				)
								_UpperCamelCase						    =	ChineseCLIPProcessor.from_pretrained(self.tmpdirname       ,							use_fast=a				)

								_UpperCamelCase						    =	ChineseCLIPProcessor(tokenizer=a       ,							image_processor=a				)
								processor_fast.save_pretrained(self.tmpdirname				)
								_UpperCamelCase						    =	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  A_				(			self				)					->      Tuple:


								'''simple docstring'''

								_UpperCamelCase						    =	ChineseCLIPProcessor(tokenizer=self.get_tokenizer()       ,							image_processor=self.get_image_processor()				)
								processor.save_pretrained(self.tmpdirname				)

								_UpperCamelCase						    =	self.get_tokenizer(cls_token="""(CLS)"""       ,							sep_token="""(SEP)"""				)
								_UpperCamelCase						    =	self.get_image_processor(do_normalize=a				)

								_UpperCamelCase						    =	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  A_				(			self				)					->      Union[str, Any]:


								'''simple docstring'''

								_UpperCamelCase						    =	self.get_image_processor()
								_UpperCamelCase						    =	self.get_tokenizer()

								_UpperCamelCase						    =	ChineseCLIPProcessor(tokenizer=a       ,							image_processor=a				)

								_UpperCamelCase						    =	self.prepare_image_inputs()

								_UpperCamelCase						    =	image_processor(a       ,							return_tensors="""np"""				)
								_UpperCamelCase						    =	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  A_				(			self				)					->      Any:


								'''simple docstring'''

								_UpperCamelCase						    =	self.get_image_processor()
								_UpperCamelCase						    =	self.get_tokenizer()

								_UpperCamelCase						    =	ChineseCLIPProcessor(tokenizer=a       ,							image_processor=a				)

								_UpperCamelCase						    =	"""Alexandra，T-shirt的价格是15便士。"""

								_UpperCamelCase						    =	processor(text=a				)

								_UpperCamelCase						    =	tokenizer(a				)

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

						def  A_				(			self				)					->      Any:


								'''simple docstring'''

								_UpperCamelCase						    =	self.get_image_processor()
								_UpperCamelCase						    =	self.get_tokenizer()

								_UpperCamelCase						    =	ChineseCLIPProcessor(tokenizer=a       ,							image_processor=a				)

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

								_UpperCamelCase						    =	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  A_				(			self				)					->      Optional[int]:


								'''simple docstring'''

								_UpperCamelCase						    =	self.get_image_processor()
								_UpperCamelCase						    =	self.get_tokenizer()

								_UpperCamelCase						    =	ChineseCLIPProcessor(tokenizer=a       ,							image_processor=a				)

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

								_UpperCamelCase						    =	processor.batch_decode(a				)
								_UpperCamelCase						    =	tokenizer.batch_decode(a				)

								self.assertListEqual(a       ,							a				)







						def  A_				(			self				)					->      List[Any]:


								'''simple docstring'''

								_UpperCamelCase						    =	self.get_image_processor()
								_UpperCamelCase						    =	self.get_tokenizer()

								_UpperCamelCase						    =	ChineseCLIPProcessor(tokenizer=a       ,							image_processor=a				)

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

								_UpperCamelCase						    =	processor(text=a       ,							images=a				)

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





from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand


def 							__A(lowerCAmelCase )		->				List[str]:

		"""simple docstring"""






		return ConvertCommand(
		    args.model_type						,  args.tf_checkpoint						,  args.pytorch_dump_output						,  args.config						,  args.finetuning_task_name )


lowerCamelCase__	=							"\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n"







class        lowerCAmelCase__      (   __lowercase	):

						@staticmethod
						def  A_				(			a				)					->      Optional[int]:


								'''simple docstring'''

								_UpperCamelCase						    =	parser.add_parser(
								    """convert"""       ,							help="""CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints."""       ,							)
								train_parser.add_argument("""--model_type"""       ,							type=a       ,							required=a       ,							help="""Model's type."""				)
								train_parser.add_argument(
								    """--tf_checkpoint"""       ,							type=a       ,							required=a       ,							help="""TensorFlow checkpoint path or folder."""				)
								train_parser.add_argument(
								    """--pytorch_dump_output"""       ,							type=a       ,							required=a       ,							help="""Path to the PyTorch saved model output."""				)
								train_parser.add_argument("""--config"""       ,							type=a       ,							default=""""""       ,							help="""Configuration file path or folder."""				)
								train_parser.add_argument(
								    """--finetuning_task_name"""       ,							type=a       ,							default=a       ,							help="""Optional fine-tuning task name if the TF model was a finetuned model."""       ,							)
								train_parser.set_defaults(func=a				)

						def __init__(			self       ,							a       ,							a       ,							a       ,							a       ,							a       ,							*a       ,							)					->      Optional[Any]:


								'''simple docstring'''

								_UpperCamelCase						    =	logging.get_logger("""transformers-cli/converting"""				)

								self._logger.info(F'Loading model {model_type}'				)
								_UpperCamelCase						    =	model_type
								_UpperCamelCase						    =	tf_checkpoint
								_UpperCamelCase						    =	pytorch_dump_output
								_UpperCamelCase						    =	config
								_UpperCamelCase						    =	finetuning_task_name







						def  A_				(			self				)					->      Tuple:


								'''simple docstring'''

								if self._model_type == "albert":
										try:
												from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
												    convert_tf_checkpoint_to_pytorch,
												)
										except ImportError:
												raise ImportError(a				)

										convert_tf_checkpoint_to_pytorch(self._tf_checkpoint       ,							self._config       ,							self._pytorch_dump_output				)
								elif self._model_type == "bert":
										try:
												from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
												    convert_tf_checkpoint_to_pytorch,
												)
										except ImportError:
												raise ImportError(a				)

										convert_tf_checkpoint_to_pytorch(self._tf_checkpoint       ,							self._config       ,							self._pytorch_dump_output				)
								elif self._model_type == "funnel":
										try:
												from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
												    convert_tf_checkpoint_to_pytorch,
												)
										except ImportError:
												raise ImportError(a				)

										convert_tf_checkpoint_to_pytorch(self._tf_checkpoint       ,							self._config       ,							self._pytorch_dump_output				)
								elif self._model_type == "t5":
										try:
												from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
										except ImportError:
												raise ImportError(a				)

										convert_tf_checkpoint_to_pytorch(self._tf_checkpoint       ,							self._config       ,							self._pytorch_dump_output				)
								elif self._model_type == "gpt":
										from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
										    convert_openai_checkpoint_to_pytorch,
										)

										convert_openai_checkpoint_to_pytorch(self._tf_checkpoint       ,							self._config       ,							self._pytorch_dump_output				)
								elif self._model_type == "transfo_xl":
										try:
												from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
												    convert_transfo_xl_checkpoint_to_pytorch,
												)
										except ImportError:
												raise ImportError(a				)

										if "ckpt" in self._tf_checkpoint.lower():
												_UpperCamelCase						    =	self._tf_checkpoint
												_UpperCamelCase						    =	""""""
										else:
												_UpperCamelCase						    =	self._tf_checkpoint
												_UpperCamelCase						    =	""""""
										convert_transfo_xl_checkpoint_to_pytorch(
										    a       ,							self._config       ,							self._pytorch_dump_output       ,							a				)
								elif self._model_type == "gpt2":
										try:
												from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
												    convert_gpta_checkpoint_to_pytorch,
												)
										except ImportError:
												raise ImportError(a				)

										convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint       ,							self._config       ,							self._pytorch_dump_output				)
								elif self._model_type == "xlnet":
										try:
												from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
												    convert_xlnet_checkpoint_to_pytorch,
												)
										except ImportError:
												raise ImportError(a				)

										convert_xlnet_checkpoint_to_pytorch(
										    self._tf_checkpoint       ,							self._config       ,							self._pytorch_dump_output       ,							self._finetuning_task_name				)
								elif self._model_type == "xlm":
										from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
										    convert_xlm_checkpoint_to_pytorch,
										)

										convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint       ,							self._pytorch_dump_output				)
								elif self._model_type == "lxmert":
										from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
										    convert_lxmert_checkpoint_to_pytorch,
										)

										convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint       ,							self._pytorch_dump_output				)
								elif self._model_type == "rembert":
										from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
										    convert_rembert_tf_checkpoint_to_pytorch,
										)

										convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint       ,							self._config       ,							self._pytorch_dump_output				)
								else:
										raise ValueError(
										    """--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]"""				)



