


import os
from pathlib import Path
from typing import Dict, List

import fire
import torch

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.utils.logging import get_logger


UpperCAmelCase__     			=							get_logger(__name__)
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case		)  ->    Any:







     """simple docstring"""


     if text.startswith(__snake_case		):
          return text[len(__snake_case		) :]
     return text  # or whatever
def        UpperCAmelCase_      (							__snake_case		)  ->    Dict:







     """simple docstring"""


     return {remove_prefix(__snake_case  ,		'''model.'''		): v for k, v in sd.items()}
def        UpperCAmelCase_      (							__snake_case		)  ->    Any:







     """simple docstring"""


     _lowercase							   ={}
     for k in state_dicts[0].keys():
          _lowercase							   =[sd[k] for sd in state_dicts]
          _lowercase							   =sum(__snake_case		) / len(__snake_case		)
          assert isinstance(__snake_case  ,		torch.Tensor		)
          _lowercase							   =new_t
     return new_sd
def        UpperCAmelCase_      (							__snake_case  ,		__snake_case  ,		__snake_case		)  ->    None:







     """simple docstring"""


     _lowercase							   =AutoModelForSeqaSeqLM.from_pretrained(__snake_case		)
     if os.path.isfile(__snake_case		):
          _lowercase							   =[pl_ckpt_path]
     else:
          assert os.path.isdir(__snake_case		)
          _lowercase							   =list(Path(__snake_case		).glob('''*.ckpt'''		)		)
          assert ckpt_files, F"could not find any ckpt files inside the {pl_ckpt_path} directory"

     if len(__snake_case		) > 1:
          logger.info(F"averaging the weights of {ckpt_files}"		)

     _lowercase							   =[sanitize(torch.load(__snake_case  ,		map_location='''cpu'''		)['''state_dict''']		) for x in ckpt_files]
     _lowercase							   =average_state_dicts(__snake_case		)

     _lowercase   ,					_lowercase							   =hf_model.load_state_dict(__snake_case  ,		strict=__snake_case		)
     assert not missing, F"missing keys: {missing}"
     hf_model.save_pretrained(__snake_case		)
     try:
          _lowercase							   =AutoTokenizer.from_pretrained(__snake_case		)
          tok.save_pretrained(__snake_case		)
     except Exception:
          pass
          # dont copy tokenizer if cant


if __name__ == "__main__":
  fire.Fire(convert_pl_to_hf)





import requests

try:
  from django.contrib.auth import authenticate, login
  from django.shortcuts import redirect, render
except ImportError:
  UpperCAmelCase__     			=							UpperCAmelCase__     			=							UpperCAmelCase__     			=							UpperCAmelCase__     			=							print
def        UpperCAmelCase_      (							__snake_case		)  ->    List[str]:







 """simple docstring"""


 _lowercase							   ='''secretKey'''
 _lowercase							   ='''https://www.google.com/recaptcha/api/siteverify'''

 # when method is not POST, direct user to login page
 if request.method != "POST":
  return render(__snake_case  ,		'''login.html'''		)

 # from the frontend, get username, password, and client_key
 _lowercase							   =request.POST.get('''username'''		)
 _lowercase							   =request.POST.get('''password'''		)
 _lowercase							   =request.POST.get('''g-recaptcha-response'''		)

 # post recaptcha response to Google's recaptcha api
 _lowercase							   =requests.post(__snake_case  ,		data={'''secret''': secret_key, '''response''': client_key}		)
 # if the recaptcha api verified our keys
 if response.json().get('''success'''  ,		__snake_case		):
  # authenticate the user
  _lowercase							   =authenticate(__snake_case  ,		username=__snake_case  ,		password=__snake_case		)
  if user_in_database:
   login(__snake_case  ,		__snake_case		)
   return redirect('''/your-webpage'''		)
 return render(__snake_case  ,		'''login.html'''		)

