

'''simple docstring'''

from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand





def    A_  ( snake_case ):
  return ConvertCommand(
      args.model_type      ,       args.tf_checkpoint      ,       args.pytorch_dump_output      ,       args.config      ,       args.finetuning_task_name )


A_       						=							"\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 							_snake_case (       _UpperCAmelCase						):
   @staticmethod
   def     __UpperCamelCase						( SCREAMING_SNAKE_CASE__			:      ArgumentParser							):
     SCREAMING_SNAKE_CASE:str						      =					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=lowerCamelCase_     ,required=lowerCamelCase_     ,help="Model\'s type."							)
     train_parser.add_argument(
         "--tf_checkpoint"     ,type=lowerCamelCase_     ,required=lowerCamelCase_     ,help="TensorFlow checkpoint path or folder."							)
     train_parser.add_argument(
         "--pytorch_dump_output"     ,type=lowerCamelCase_     ,required=lowerCamelCase_     ,help="Path to the PyTorch saved model output."							)
     train_parser.add_argument("--config"     ,type=lowerCamelCase_     ,default=""     ,help="Configuration file path or folder."							)
     train_parser.add_argument(
         "--finetuning_task_name"     ,type=lowerCamelCase_     ,default=lowerCamelCase_     ,help="Optional fine-tuning task name if the TF model was a finetuned model."     ,)
     train_parser.set_defaults(func=lowerCamelCase_							)
   def __init__( self			:      List[Any]     ,SCREAMING_SNAKE_CASE__			:      str     ,SCREAMING_SNAKE_CASE__			:      str     ,SCREAMING_SNAKE_CASE__			:      str     ,SCREAMING_SNAKE_CASE__			:      str     ,SCREAMING_SNAKE_CASE__			:      str     ,*SCREAMING_SNAKE_CASE__			:      Dict     ,):
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					logging.get_logger("transformers-cli/converting"							)

     self._logger.info(F'''Loading model {model_type}'''							)
     SCREAMING_SNAKE_CASE:Optional[Any]						      =					model_type
     SCREAMING_SNAKE_CASE:List[Any]						      =					tf_checkpoint
     SCREAMING_SNAKE_CASE:List[str]						      =					pytorch_dump_output
     SCREAMING_SNAKE_CASE:Optional[int]						      =					config
     SCREAMING_SNAKE_CASE:str						      =					finetuning_task_name







   def     __UpperCamelCase						( self			:      List[str]							):




     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(lowerCamelCase_							)

       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(lowerCamelCase_							)

       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(lowerCamelCase_							)

       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(lowerCamelCase_							)

       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(lowerCamelCase_							)

       if "ckpt" in self._tf_checkpoint.lower():
         SCREAMING_SNAKE_CASE:Dict						      =					self._tf_checkpoint
         SCREAMING_SNAKE_CASE:Tuple						      =					''''''
       else:
         SCREAMING_SNAKE_CASE:List[Any]						      =					self._tf_checkpoint
         SCREAMING_SNAKE_CASE:Optional[int]						      =					''''''
       convert_transfo_xl_checkpoint_to_pytorch(
           lowerCamelCase_     ,self._config     ,self._pytorch_dump_output     ,lowerCamelCase_							)
     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(lowerCamelCase_							)

       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(lowerCamelCase_							)

       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]"							)









'''simple docstring'''

import inspect
import re
from hashlib import shaaaa
from typing import Dict, List

from .arrow import arrow
from .audiofolder import audiofolder
from .csv import csv
from .imagefolder import imagefolder
from .json import json
from .pandas import pandas
from .parquet import parquet
from .sql import sql  # noqa F401
from .text import text





def    A_  ( snake_case ):
  SCREAMING_SNAKE_CASE:List[Any]						      =					[]
  for line in lines:
    SCREAMING_SNAKE_CASE:Any						      =					re.sub(r"#.*"      ,       ""      ,       snake_case )  # remove comments
    if line:
      filtered_lines.append(snake_case )
  SCREAMING_SNAKE_CASE:int						      =					"\n".join(snake_case )

  # Make a hash from all this code
  SCREAMING_SNAKE_CASE:str						      =					full_str.encode("utf-8" )
  return shaaaa(snake_case ).hexdigest()


# get importable module names and hash for caching
A_       						=							{
    "csv": (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())),
    "json": (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())),
    "pandas": (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())),
    "parquet": (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())),
    "arrow": (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())),
    "text": (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())),
    "imagefolder": (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())),
    "audiofolder": (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())),
}

# Used to infer the module to use based on the data files extensions
A_       						=							{
    ".csv": ("csv", {}),
    ".tsv": ("csv", {"sep": "\t"}),
    ".json": ("json", {}),
    ".jsonl": ("json", {}),
    ".parquet": ("parquet", {}),
    ".arrow": ("arrow", {}),
    ".txt": ("text", {}),
}
_EXTENSION_TO_MODULE.update({ext: ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ("imagefolder", {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext: ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ("audiofolder", {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
A_       						=							{"imagefolder", "audiofolder"}

# Used to filter data files based on extensions given a module name
A_       						=							{}
for _ext, (_module, _) in _EXTENSION_TO_MODULE.items():
    _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext)

_MODULE_TO_EXTENSIONS["imagefolder"].append(".zip")


_MODULE_TO_EXTENSIONS["audiofolder"].append(".zip")






