






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 			=							collections.namedtuple(
    'CheckpointState', ['model_dict', 'optimizer_dict', 'scheduler_dict', 'offset', 'epoch', 'encoder_params']
)
def    a__    (	A       ):
   print(F'''Reading saved model from {model_file}'''       )
   lowercase__    :						Dict       =			torch.load(A		,					map_location=lambda A		,					A       : default_restore_location(A		,					"""cpu"""       )       )
   return CheckpointState(**A       )
class 							__snake_case:



 def __init__(						self : Tuple			,		UpperCAmelCase__ : Path						):

    """simple docstring"""






    lowercase__    :						Union[str, Any]       =			src_file



 def     UpperCAmelCase     (						self : int						):

    """simple docstring"""






    raise NotImplementedError







 @staticmethod
 def     UpperCAmelCase     (						UpperCAmelCase__ : str			,		*UpperCAmelCase__ : Union[str, Any]			,		**UpperCAmelCase__ : str						):

    """simple docstring"""






    if comp_type.startswith("""c"""						):
       return DPRContextEncoderState(*UpperCAmelCase__			,		**UpperCAmelCase__						)
    if comp_type.startswith("""q"""						):
       return DPRQuestionEncoderState(*UpperCAmelCase__			,		**UpperCAmelCase__						)
    if comp_type.startswith("""r"""						):
       return DPRReaderState(*UpperCAmelCase__			,		**UpperCAmelCase__						)
    else:
       raise ValueError("""Component type must be either 'ctx_encoder', 'question_encoder' or 'reader'."""						)
class 							__snake_case(       __UpperCamelCase   ):



 def     UpperCAmelCase     (						self : List[str]						):

    """simple docstring"""






    lowercase__    :						Any       =			DPRContextEncoder(DPRConfig(**BertConfig.get_config_dict("""bert-base-uncased"""						)[0]						)						)
    print(f'''Loading DPR biencoder from {self.src_file}'''						)
    lowercase__    :						List[str]       =			load_states_from_checkpoint(self.src_file						)
    lowercase__  ,       lowercase__    :						int       =			model.ctx_encoder, """ctx_model."""
    # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
    lowercase__    :						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(UpperCAmelCase__						):
          lowercase__    :						Union[str, Any]       =			key[len(UpperCAmelCase__						) :]
          if not key.startswith("""encode_proj."""						):
             lowercase__    :						Optional[Any]       =			"""bert_model.""" + key
          lowercase__    :						Optional[int]       =			value
    encoder.load_state_dict(UpperCAmelCase__						)
    return model
class 							__snake_case(       __UpperCamelCase   ):



 def     UpperCAmelCase     (						self : List[str]						):

    """simple docstring"""






    lowercase__    :						Optional[int]       =			DPRQuestionEncoder(DPRConfig(**BertConfig.get_config_dict("""bert-base-uncased"""						)[0]						)						)
    print(f'''Loading DPR biencoder from {self.src_file}'''						)
    lowercase__    :						Tuple       =			load_states_from_checkpoint(self.src_file						)
    lowercase__  ,       lowercase__    :						List[str]       =			model.question_encoder, """question_model."""
    # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
    lowercase__    :						Optional[int]       =			{"""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(UpperCAmelCase__						):
          lowercase__    :						Optional[Any]       =			key[len(UpperCAmelCase__						) :]
          if not key.startswith("""encode_proj."""						):
             lowercase__    :						List[Any]       =			"""bert_model.""" + key
          lowercase__    :						str       =			value
    encoder.load_state_dict(UpperCAmelCase__						)
    return model
class 							__snake_case(       __UpperCamelCase   ):



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

    """simple docstring"""






    lowercase__    :						List[Any]       =			DPRReader(DPRConfig(**BertConfig.get_config_dict("""bert-base-uncased"""						)[0]						)						)
    print(f'''Loading DPR reader from {self.src_file}'''						)
    lowercase__    :						Any       =			load_states_from_checkpoint(self.src_file						)
    # Fix changes from https://github.com/huggingface/transformers/commit/614fef1691edb806de976756d4948ecbcd0c0ca3
    lowercase__    :						int       =			{
        """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"""						):
          lowercase__    :						Optional[int]       =			"""encoder.bert_model.""" + key[len("""encoder."""						) :]
       lowercase__    :						Optional[int]       =			value
    model.span_predictor.load_state_dict(UpperCAmelCase__						)
    return model
def    a__    (	A		,					A		,					A       ):
   lowercase__    :						Any       =			Path(A       )
   dest_dir.mkdir(exist_ok=A       )

   lowercase__    :						List[Any]       =			DPRState.from_type(A		,					src_file=A       )
   lowercase__    :						List[Any]       =			dpr_state.load_dpr_model()
   model.save_pretrained(A       )
   model.from_pretrained(A       )  # sanity check


if __name__ == "__main__":
 a 			=							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 			=							parser.parse_args()

 a 			=							Path(args.src)
 a 			=							F'converted-{src_file.name}' if args.dest is None else args.dest
 a 			=							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)










from datetime import date

import requests

a 			=							'https://api.carbonintensity.org.uk/intensity'
def    a__    (	):
   lowercase__    :						Union[str, Any]       =			requests.get(A       ).json()["""data"""][0]
   return last_half_hour["intensity"]["actual"]
def    a__    (	A		,					A       ):
   return requests.get(F'''{BASE_URL}/{start}/{end}'''       ).json()["data"]


if __name__ == "__main__":
 for entry in fetch_from_to(start=date(2020, 10, 1), end=date(2020, 10, 3)):
  print('from {from} to {to}: {intensity[actual]}'.format(**entry))
 print(F'{fetch_last_half_hour() = }')


