





import argparse
import os

import numpy as np
import tensorflow as tf
import torch

from transformers import BertModel



def 		A_				(     _lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     )						-> List[Any]:
 UpperCamelCase	:					Dict       	=   ("dense.weight", "attention.self.query", "attention.self.key", "attention.self.value")

 UpperCamelCase	:					Any       	=   (
     ("layer.", "layer_"),
     ("word_embeddings.weight", "word_embeddings"),
     ("position_embeddings.weight", "position_embeddings"),
     ("token_type_embeddings.weight", "token_type_embeddings"),
     (".", "/"),
     ("LayerNorm/weight", "LayerNorm/gamma"),
     ("LayerNorm/bias", "LayerNorm/beta"),
     ("weight", "kernel"),
 )

 if not os.path.isdir(_lowerCAmelCase     ):
  os.makedirs(_lowerCAmelCase     )

 UpperCamelCase	:					str       	=   model.state_dict()

 def to_tf_var_name(_lowerCAmelCase     ):
  for patt, repl in iter(_lowerCAmelCase     ):
   UpperCamelCase	:					str       	=   name.replace(_lowerCAmelCase							,   _lowerCAmelCase     )
  return F"""bert/{name}"""

 def create_tf_var(_lowerCAmelCase							,   _lowerCAmelCase							,   _lowerCAmelCase     ):
  UpperCamelCase	:					str       	=   tf.dtypes.as_dtype(tensor.dtype     )
  UpperCamelCase	:					str       	=   tf.get_variable(dtype=_lowerCAmelCase							,   shape=tensor.shape							,   name=_lowerCAmelCase							,   initializer=tf.zeros_initializer()     )
  session.run(tf.variables_initializer([tf_var]     )     )
  session.run(_lowerCAmelCase     )
  return tf_var

 tf.reset_default_graph()
 with tf.Session() as session:
  for var_name in state_dict:
   UpperCamelCase	:					str       	=   to_tf_var_name(_lowerCAmelCase     )
   UpperCamelCase	:					Union[str, Any]       	=   state_dict[var_name].numpy()
   if any(x in var_name for x in tensors_to_transpose     ):
    UpperCamelCase	:					Any       	=   torch_tensor.T
   UpperCamelCase	:					Any       	=   create_tf_var(tensor=_lowerCAmelCase							,   name=_lowerCAmelCase							,   session=_lowerCAmelCase     )
   tf.keras.backend.set_value(_lowerCAmelCase							,   _lowerCAmelCase     )
   UpperCamelCase	:					List[str]       	=   session.run(_lowerCAmelCase     )
   print(F"""Successfully created {tf_name}: {np.allclose(_lowerCAmelCase							,   _lowerCAmelCase     )}"""     )

  UpperCamelCase	:					Optional[Any]       	=   tf.train.Saver(tf.trainable_variables()     )
  saver.save(_lowerCAmelCase							,   os.path.join(_lowerCAmelCase							,   model_name.replace("-"							,   "_"     ) + ".ckpt"     )     )



def 		A_				(     _lowerCAmelCase=None     )						-> Dict:
 UpperCamelCase	:					str       	=   argparse.ArgumentParser()
 parser.add_argument("--model_name"							,   type=_lowerCAmelCase							,   required=_lowerCAmelCase							,   help="model name e.g. bert-base-uncased"     )
 parser.add_argument(
     "--cache_dir"							,   type=_lowerCAmelCase							,   default=_lowerCAmelCase							,   required=_lowerCAmelCase							,   help="Directory containing pytorch model"     )
 parser.add_argument("--pytorch_model_path"							,   type=_lowerCAmelCase							,   required=_lowerCAmelCase							,   help="/path/to/<pytorch-model-name>.bin"     )
 parser.add_argument("--tf_cache_dir"							,   type=_lowerCAmelCase							,   required=_lowerCAmelCase							,   help="Directory in which to save tensorflow model"     )
 UpperCamelCase	:					List[Any]       	=   parser.parse_args(_lowerCAmelCase     )

 UpperCamelCase	:					Dict       	=   BertModel.from_pretrained(
     pretrained_model_name_or_path=args.model_name							,   state_dict=torch.load(args.pytorch_model_path     )							,   cache_dir=args.cache_dir							,   )

 convert_pytorch_checkpoint_to_tf(model=_lowerCAmelCase							,   ckpt_dir=args.tf_cache_dir							,   model_name=args.model_name     )


if __name__ == "__main__":
    main()







import requests

snake_case__					:							int													=     'https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey='



def _a				(	lowerCamelCase:   str )	->						None:







      '''simple docstring'''






      __A          =				requests.get(_NEWS_API + bbc_news_api_key ).json()
      # each article in the list is a dict
      for i, article in enumerate(bbc_news_page['''articles''']					,				1 ):
            print(F"""{i}.) {article['title']}""" )


if __name__ == "__main__":
       fetch_bbc_news(bbc_news_api_key='<Your BBC News API key goes here>')





