




import importlib.metadata
import subprocess
import sys



def    UpperCAmelCase							(  _snake_case = False				):
							lowerCAmelCase =		False
							if "IPython" in sys.modules:
														lowerCAmelCase =		'''google.colab''' in str(sys.modules['''IPython'''].get_ipython()				)

							if in_colab:
														if upgrade:
																					lowerCAmelCase =		['''pip''', '''install''', '''-U''', '''torch''']
																					subprocess.run(_snake_case    ,			check=_snake_case				)
														# get the current version of torch
														lowerCAmelCase =		importlib.metadata.version('''torch'''				)
														lowerCAmelCase =		torch_version[: torch_version.rindex('''.'''				)]
														lowerCAmelCase =		F"""https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl"""
														lowerCAmelCase =		['''pip''', '''install''', xla_wheel]
														subprocess.run(_snake_case    ,			check=_snake_case				)
							else:
														raise RuntimeError('''`install_xla` utility works only on google colab.'''				)









import argparse

from transformers import CanineConfig, CanineModel, CanineTokenizer, load_tf_weights_in_canine
from transformers.utils import logging


logging.set_verbosity_info()



def    UpperCAmelCase							(  _snake_case    ,			_snake_case				):
							# Initialize PyTorch model
							lowerCAmelCase =		CanineConfig()
							lowerCAmelCase =		CanineModel(_snake_case				)
							model.eval()

							print(F"""Building PyTorch model from configuration: {config}"""				)

							# Load weights from tf checkpoint
							load_tf_weights_in_canine(_snake_case    ,			_snake_case    ,			_snake_case				)

							# Save pytorch-model (weights and configuration)
							print(F"""Save PyTorch model to {pytorch_dump_path}"""				)
							model.save_pretrained(_snake_case				)

							# Save tokenizer files
							lowerCAmelCase =		CanineTokenizer()
							print(F"""Save tokenizer files to {pytorch_dump_path}"""				)
							tokenizer.save_pretrained(_snake_case				)


if __name__ == "__main__":
		UpperCAmelCase_    =argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    """--tf_checkpoint_path""",
		    default=None,
		    type=str,
		    required=True,
		    help="""Path to the TensorFlow checkpoint. Should end with model.ckpt""",
		)
		parser.add_argument(
		    """--pytorch_dump_path""",
		    default=None,
		    type=str,
		    required=True,
		    help="""Path to a folder where the PyTorch model will be placed.""",
		)
		UpperCAmelCase_    =parser.parse_args()
		convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.pytorch_dump_path)



