



'''simple docstring'''






import argparse
import collections
from pathlib import Path

import torch
from torch.serialization import default_restore_location

from transformers import BertConfig, DPRConfig, DPRContextEncoder, DPRQuestionEncoder, DPRReader


A_				:					str							=      collections.namedtuple(
    """CheckpointState""", ["""model_dict""", """optimizer_dict""", """scheduler_dict""", """offset""", """epoch""", """encoder_params"""]
)




def        snake_case_   (	lowerCAmelCase_						)->						CheckpointState:





       '''simple docstring'''


       print(F'''Reading saved model from {model_file}'''						)
       _UpperCAmelCase    :		Tuple     	=					torch.load(lowerCAmelCase_			,  map_location=lambda lowerCAmelCase_			,  lowerCAmelCase_						: default_restore_location(lowerCAmelCase_			,  """cpu"""						)						)
       return CheckpointState(**lowerCAmelCase_						)



class 				lowercase			:


 """simple docstring"""


 def __init__(				self    ,a_				)						->  str:
        _UpperCAmelCase    :		Dict     	=					src_file







 def _snake_case					(				self				)						->  str:
        raise NotImplementedError







 @staticmethod
 def _snake_case					(				a_    ,*a_    ,**a_				)						->  "DPRState":
        if comp_type.startswith("""c"""				):
               return DPRContextEncoderState(*a_    ,**a_				)
        if comp_type.startswith("""q"""				):
               return DPRQuestionEncoderState(*a_    ,**a_				)
        if comp_type.startswith("""r"""				):
               return DPRReaderState(*a_    ,**a_				)
        else:
               raise ValueError("""Component type must be either 'ctx_encoder', 'question_encoder' or 'reader'."""				)







class 				lowercase			(    _lowerCamelCase							):


 """simple docstring"""


 def _snake_case					(				self				)						->  Optional[int]:
        _UpperCAmelCase    :		Optional[Any]     	=					DPRContextEncoder(DPRConfig(**BertConfig.get_config_dict("""bert-base-uncased"""				)[0]				)				)
        print(f'''Loading DPR biencoder from {self.src_file}'''				)
        _UpperCAmelCase    :		List[Any]     	=					load_states_from_checkpoint(self.src_file				)
        _UpperCAmelCase	,_UpperCAmelCase    :		List[Any]     	=					model.ctx_encoder, """ctx_model."""
        # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
        _UpperCAmelCase    :		List[Any]     	=					{"""bert_model.embeddings.position_ids""": model.ctx_encoder.bert_model.embeddings.position_ids}
        for key, value in saved_state.model_dict.items():
               if key.startswith(a_				):
                      _UpperCAmelCase    :		Tuple     	=					key[len(a_				) :]
                      if not key.startswith("""encode_proj."""				):
                             _UpperCAmelCase    :		Dict     	=					"""bert_model.""" + key
                      _UpperCAmelCase    :		List[str]     	=					value
        encoder.load_state_dict(a_				)
        return model







class 				lowercase			(    _lowerCamelCase							):


 """simple docstring"""


 def _snake_case					(				self				)						->  Tuple:
        _UpperCAmelCase    :		str     	=					DPRQuestionEncoder(DPRConfig(**BertConfig.get_config_dict("""bert-base-uncased"""				)[0]				)				)
        print(f'''Loading DPR biencoder from {self.src_file}'''				)
        _UpperCAmelCase    :		Any     	=					load_states_from_checkpoint(self.src_file				)
        _UpperCAmelCase	,_UpperCAmelCase    :		Any     	=					model.question_encoder, """question_model."""
        # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
        _UpperCAmelCase    :		List[str]     	=					{"""bert_model.embeddings.position_ids""": model.question_encoder.bert_model.embeddings.position_ids}
        for key, value in saved_state.model_dict.items():
               if key.startswith(a_				):
                      _UpperCAmelCase    :		Tuple     	=					key[len(a_				) :]
                      if not key.startswith("""encode_proj."""				):
                             _UpperCAmelCase    :		List[Any]     	=					"""bert_model.""" + key
                      _UpperCAmelCase    :		Optional[Any]     	=					value
        encoder.load_state_dict(a_				)
        return model







class 				lowercase			(    _lowerCamelCase							):


 """simple docstring"""


 def _snake_case					(				self				)						->  int:
        _UpperCAmelCase    :		List[str]     	=					DPRReader(DPRConfig(**BertConfig.get_config_dict("""bert-base-uncased"""				)[0]				)				)
        print(f'''Loading DPR reader from {self.src_file}'''				)
        _UpperCAmelCase    :		Optional[Any]     	=					load_states_from_checkpoint(self.src_file				)
        # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
        _UpperCAmelCase    :		Dict     	=					{
            """encoder.bert_model.embeddings.position_ids""": model.span_predictor.encoder.bert_model.embeddings.position_ids
        }
        for key, value in saved_state.model_dict.items():
               if key.startswith("""encoder."""				) and not key.startswith("""encoder.encode_proj"""				):
                      _UpperCAmelCase    :		str     	=					"""encoder.bert_model.""" + key[len("""encoder."""				) :]
               _UpperCAmelCase    :		Optional[int]     	=					value
        model.span_predictor.load_state_dict(a_				)
        return model







def        snake_case_   (	lowerCAmelCase_			,  lowerCAmelCase_			,  lowerCAmelCase_						)->						str:





       '''simple docstring'''


       _UpperCAmelCase    :		List[Any]     	=					Path(lowerCAmelCase_						)
       dest_dir.mkdir(exist_ok=lowerCAmelCase_						)

       _UpperCAmelCase    :		int     	=					DPRState.from_type(lowerCAmelCase_			,  src_file=lowerCAmelCase_						)
       _UpperCAmelCase    :		str     	=					dpr_state.load_dpr_model()
       model.save_pretrained(lowerCAmelCase_						)
       model.from_pretrained(lowerCAmelCase_						)  # sanity check


if __name__ == "__main__":
   A_				:					Tuple							=      argparse.ArgumentParser()
   # Required parameters
   parser.add_argument(
       """--type""", type=str, help="""Type of the component to convert: 'ctx_encoder', 'question_encoder' or 'reader'."""
   )
   parser.add_argument(
       """--src""",
       type=str,
       help=(
           """Path to the dpr checkpoint file. They can be downloaded from the official DPR repo"""
           """ https://github.com/facebookresearch/DPR. Note that in the official repo, both encoders are stored in the"""
           """ 'retriever' checkpoints."""
       ),
   )
   parser.add_argument("""--dest""", type=str, default=None, help="""Path to the output PyTorch model directory.""")
   A_				:					Union[str, Any]							=      parser.parse_args()

   A_				:					Any							=      Path(args.src)
   A_				:					Optional[int]							=      f"""converted-{src_file.name}""" if args.dest is None else args.dest
   A_				:					Dict							=      Path(dest_dir)
   assert src_file.exists()
   assert (
       args.type is not None
   ), "Please specify the component type of the DPR model to convert: 'ctx_encoder', 'question_encoder' or 'reader'."
   convert(args.type, src_file, dest_dir)







import subprocess
from argparse import ArgumentParser
from typing import List, Union

from huggingface_hub.hf_api import HfFolder, create_repo, whoami
from requests.exceptions import HTTPError

from . import BaseTransformersCLICommand




class  A_ (			a_  ):







 """simple docstring"""





 @staticmethod
 def 	_UpperCamelCase     (      _a       :			ArgumentParser					)     ->      str:
      UpperCAmelCase    :					str			=      parser.add_parser("login"							,     help="Log in using the same credentials as on huggingface.co"					)
      login_parser.set_defaults(func=lambda _a					: LoginCommand(_a					)					)
      UpperCAmelCase    :					str			=      parser.add_parser("whoami"							,     help="Find out which huggingface.co account you are logged in as."					)
      whoami_parser.set_defaults(func=lambda _a					: WhoamiCommand(_a					)					)
      UpperCAmelCase    :					List[Any]			=      parser.add_parser("logout"							,     help="Log out"					)
      logout_parser.set_defaults(func=lambda _a					: LogoutCommand(_a					)					)

      # new system: git-based repo system
      UpperCAmelCase    :					List[str]			=      parser.add_parser(
          "repo"							,     help="Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos."							,     )
      UpperCAmelCase    :					List[str]			=      repo_parser.add_subparsers(
          help="Deprecated: use `huggingface-cli` instead. huggingface.co repos related commands"					)
      UpperCAmelCase    :					Optional[Any]			=      repo_subparsers.add_parser(
          "create"							,     help="Deprecated: use `huggingface-cli` instead. Create a new repo on huggingface.co"					)
      repo_create_parser.add_argument(
          "name"							,     type=_a							,     help="Name for your model's repo. Will be namespaced under your username to build the model id."							,     )
      repo_create_parser.add_argument("--organization"							,     type=_a							,     help="Optional: organization namespace."					)
      repo_create_parser.add_argument("-y"							,     "--yes"							,     action="store_true"							,     help="Optional: answer Yes to the prompt"					)
      repo_create_parser.set_defaults(func=lambda _a					: RepoCreateCommand(_a					)					)




class  A_ :







 """simple docstring"""





 _UpperCamelCase					:			str     	=       '''\u001b[1m'''
 _UpperCamelCase					:			Optional[Any]     	=       '''\u001b[31m'''
 _UpperCamelCase					:			List[Any]     	=       '''\u001b[90m'''
 _UpperCamelCase					:			Tuple     	=       '''\u001b[0m'''
 @classmethod
 def 	_UpperCamelCase     (      cls       :			Any							,     _a       :			Tuple					)     ->      Any:
      return f'{cls._bold}{s}{cls._reset}'
 @classmethod
 def 	_UpperCamelCase     (      cls       :			Optional[Any]							,     _a       :			List[Any]					)     ->      str:
      return f'{cls._bold}{cls._red}{s}{cls._reset}'

 @classmethod
 def 	_UpperCamelCase     (      cls       :			str							,     _a       :			Optional[Any]					)     ->      Tuple:
      return f'{cls._gray}{s}{cls._reset}'







def 	A (		A__		,		A__			):
     UpperCAmelCase    :					str			=      [max(len(str(A__			)			) for x in col			) for col in zip(*A__		,		A__			)]
     UpperCAmelCase    :					str			=      ("{{:{}}} " * len(A__			)).format(*A__			)
     UpperCAmelCase    :					Dict			=      []
     lines.append(row_format.format(*A__			)			)
     lines.append(row_format.format(*["-" * w for w in col_widths]			)			)
     for row in rows:
          lines.append(row_format.format(*A__			)			)
     return "\n".join(A__			)




class  A_ :







 """simple docstring"""





 def __init__(      self       :			List[str]							,     _a       :			str					)     ->      str:
      UpperCAmelCase    :					str			=      args




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			Optional[int]					)     ->      Any:
      print(
          ANSI.red(
              "ERROR! `huggingface-cli login` uses an outdated login mechanism "
              "that is not compatible with the Hugging Face Hub backend anymore. "
              "Please use `huggingface-cli login instead."					)					)




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			Tuple					)     ->      Tuple:
      print(
          ANSI.red(
              "WARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use "
              "`huggingface-cli whoami` instead."					)					)
      UpperCAmelCase    :					Dict			=      HfFolder.get_token()
      if token is None:
           print("Not logged in"					)
           exit()
      try:
           UpperCAmelCase	,			UpperCAmelCase    :					Optional[int]			=      whoami(_a					)
           print(_a					)
           if orgs:
                print(ANSI.bold("orgs: "					)							,     ",".join(_a					)					)
      except HTTPError as e:
           print(_a					)
           print(ANSI.red(e.response.text					)					)
           exit(1					)




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			List[Any]					)     ->      int:
      print(
          ANSI.red(
              "ERROR! `transformers-cli logout` uses an outdated logout mechanism "
              "that is not compatible with the Hugging Face Hub backend anymore. "
              "Please use `huggingface-cli logout instead."					)					)




class  A_ (			a_  ):







 """simple docstring"""





 def 	_UpperCamelCase     (      self       :			List[str]					)     ->      Union[str, Any]:
      print(
          ANSI.red(
              "WARNING! Managing repositories through transformers-cli is deprecated. "
              "Please use `huggingface-cli` instead."					)					)
      UpperCAmelCase    :					List[Any]			=      HfFolder.get_token()
      if token is None:
           print("Not logged in"					)
           exit(1					)
      try:
           UpperCAmelCase    :					List[str]			=      subprocess.check_output(["git", "--version"]					).decode("utf-8"					)
           print(ANSI.gray(stdout.strip()					)					)
      except FileNotFoundError:
           print("Looks like you do not have git installed, please install."					)

      try:
           UpperCAmelCase    :					Tuple			=      subprocess.check_output(["git-lfs", "--version"]					).decode("utf-8"					)
           print(ANSI.gray(stdout.strip()					)					)
      except FileNotFoundError:
           print(
               ANSI.red(
                   "Looks like you do not have git-lfs installed, please install."
                   " You can install from https://git-lfs.github.com/."
                   " Then run `git lfs install` (you only have to do this once)."					)					)
      print(""					)

      UpperCAmelCase	,			UpperCAmelCase    :					Tuple			=      whoami(_a					)
      UpperCAmelCase    :					Union[str, Any]			=      self.args.organization if self.args.organization is not None else user
      UpperCAmelCase    :					Dict			=      f'{namespace}/{self.args.name}'
      print(f'You are about to create {ANSI.bold(_a					)}'					)

      if not self.args.yes:
           UpperCAmelCase    :					Any			=      input("Proceed? [Y/n] "					).lower()
           if not (choice == "" or choice == "y" or choice == "yes"):
                print("Abort"					)
                exit()
      try:
           UpperCAmelCase    :					List[Any]			=      create_repo(_a							,     name=self.args.name							,     organization=self.args.organization					)
      except HTTPError as e:
           print(_a					)
           print(ANSI.red(e.response.text					)					)
           exit(1					)
      print("\nYour repo now lives at:"					)
      print(f'  {ANSI.bold(_a					)}'					)
      print("\nYou can clone it locally with the command below, and commit/push as usual."					)
      print(f'\n  git clone {url}'					)
      print(""					)



