"""simple docstring"""






import argparse
import collections
import json
import os
import re
import string
import sys

import numpy as np


__A			   = re.compile(R"\b(a|an|the)\b", re.UNICODE)

__A			   = None



def SCREAMING_SNAKE_CASE__			(   )  ->  int:
   lowercase__:   Dict					=      argparse.ArgumentParser('''Official evaluation script for SQuAD version 2.0.''' )
   parser.add_argument('''data_file'''       ,    metavar='''data.json'''       ,    help='''Input data JSON file.''' )
   parser.add_argument('''pred_file'''       ,    metavar='''pred.json'''       ,    help='''Model predictions.''' )
   parser.add_argument(
       '''--out-file'''       ,    '''-o'''       ,    metavar='''eval.json'''       ,    help='''Write accuracy metrics to file (default is stdout).''' )
   parser.add_argument(
       '''--na-prob-file'''       ,    '''-n'''       ,    metavar='''na_prob.json'''       ,    help='''Model estimates of probability of no answer.''' )
   parser.add_argument(
       '''--na-prob-thresh'''       ,    '''-t'''       ,    type=__UpperCAmelCase       ,    default=1.0       ,    help='''Predict "" if no-answer probability exceeds this (default = 1.0).'''       ,    )
   parser.add_argument(
       '''--out-image-dir'''       ,    '''-p'''       ,    metavar='''out_images'''       ,    default=__UpperCAmelCase       ,    help='''Save precision-recall curves to directory.''' )
   parser.add_argument('''--verbose'''       ,    '''-v'''       ,    action='''store_true''' )
   if len(sys.argv ) == 1:
      parser.print_help()
      sys.exit(1 )
   return parser.parse_args()



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase )  ->  Dict:
   lowercase__:   int					=      {}
   for article in dataset:
      for p in article["paragraphs"]:
         for qa in p["qas"]:
            lowercase__:   Union[str, Any]					=      bool(qa['''answers''']['''text'''] )
   return qid_to_has_ans



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase )  ->  Dict:

   def remove_articles(__UpperCAmelCase ):
      return ARTICLES_REGEX.sub(''' '''       ,    __UpperCAmelCase )

   def white_space_fix(__UpperCAmelCase ):
      return " ".join(text.split() )

   def remove_punc(__UpperCAmelCase ):
      lowercase__:   int					=      set(string.punctuation )
      return "".join(ch for ch in text if ch not in exclude )

   def lower(__UpperCAmelCase ):
      return text.lower()

   return white_space_fix(remove_articles(remove_punc(lower(__UpperCAmelCase ) ) ) )



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase )  ->  Optional[int]:
   if not s:
      return []
   return normalize_answer(__UpperCAmelCase ).split()



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase )  ->  List[Any]:
   return int(normalize_answer(__UpperCAmelCase ) == normalize_answer(__UpperCAmelCase ) )



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase )  ->  Optional[Any]:
   lowercase__:   int					=      get_tokens(__UpperCAmelCase )
   lowercase__:   Any					=      get_tokens(__UpperCAmelCase )
   lowercase__:   Tuple					=      collections.Counter(__UpperCAmelCase ) & collections.Counter(__UpperCAmelCase )
   lowercase__:   Optional[int]					=      sum(common.values() )
   if len(__UpperCAmelCase ) == 0 or len(__UpperCAmelCase ) == 0:
      # If either is no-answer, then F1 is 1 if they agree, 0 otherwise
      return int(gold_toks == pred_toks )
   if num_same == 0:
      return 0
   lowercase__:   List[Any]					=      1.0 * num_same / len(__UpperCAmelCase )
   lowercase__:   Union[str, Any]					=      1.0 * num_same / len(__UpperCAmelCase )
   lowercase__:   List[str]					=      (2 * precision * recall) / (precision + recall)
   return fa



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase )  ->  Any:
   lowercase__:   str					=      {}
   lowercase__:   str					=      {}
   for article in dataset:
      for p in article["paragraphs"]:
         for qa in p["qas"]:
            lowercase__:   List[Any]					=      qa['''id''']
            lowercase__:   Union[str, Any]					=      [t for t in qa['''answers''']['''text'''] if normalize_answer(__UpperCAmelCase )]
            if not gold_answers:
               # For unanswerable questions, only correct answer is empty string
               lowercase__:   str					=      ['''''']
            if qid not in preds:
               print(F"""Missing prediction for {qid}""" )
               continue
            lowercase__:   int					=      preds[qid]
            # Take max over all gold answers
            lowercase__:   Union[str, Any]					=      max(compute_exact(__UpperCAmelCase       ,    __UpperCAmelCase ) for a in gold_answers )
            lowercase__:   int					=      max(compute_fa(__UpperCAmelCase       ,    __UpperCAmelCase ) for a in gold_answers )
   return exact_scores, fa_scores



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )  ->  Optional[int]:
   lowercase__:   List[Any]					=      {}
   for qid, s in scores.items():
      lowercase__:   int					=      na_probs[qid] > na_prob_thresh
      if pred_na:
         lowercase__:   Union[str, Any]					=      float(not qid_to_has_ans[qid] )
      else:
         lowercase__:   Optional[int]					=      s
   return new_scores



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase=None )  ->  List[str]:
   if not qid_list:
      lowercase__:   str					=      len(__UpperCAmelCase )
      return collections.OrderedDict(
          [
              ('''exact''', 1_0_0.0 * sum(exact_scores.values() ) / total),
              ('''f1''', 1_0_0.0 * sum(fa_scores.values() ) / total),
              ('''total''', total),
          ] )
   else:
      lowercase__:   Union[str, Any]					=      len(__UpperCAmelCase )
      return collections.OrderedDict(
          [
              ('''exact''', 1_0_0.0 * sum(exact_scores[k] for k in qid_list ) / total),
              ('''f1''', 1_0_0.0 * sum(fa_scores[k] for k in qid_list ) / total),
              ('''total''', total),
          ] )



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )  ->  Union[str, Any]:
   for k in new_eval:
      lowercase__:   Tuple					=      new_eval[k]



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )  ->  List[str]:
   plt.step(__UpperCAmelCase       ,    __UpperCAmelCase       ,    color='''b'''       ,    alpha=0.2       ,    where='''post''' )
   plt.fill_between(__UpperCAmelCase       ,    __UpperCAmelCase       ,    step='''post'''       ,    alpha=0.2       ,    color='''b''' )
   plt.xlabel('''Recall''' )
   plt.ylabel('''Precision''' )
   plt.xlim([0.0, 1.0_5] )
   plt.ylim([0.0, 1.0_5] )
   plt.title(__UpperCAmelCase )
   plt.savefig(__UpperCAmelCase )
   plt.clf()



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase=None       ,    __UpperCAmelCase=None )  ->  Union[str, Any]:
   lowercase__:   List[Any]					=      sorted(__UpperCAmelCase       ,    key=lambda __UpperCAmelCase : na_probs[k] )
   lowercase__:   List[Any]					=      0.0
   lowercase__:   Optional[Any]					=      1.0
   lowercase__:   Any					=      0.0
   lowercase__:   Any					=      [1.0]
   lowercase__:   Tuple					=      [0.0]
   lowercase__:   Dict					=      0.0
   for i, qid in enumerate(__UpperCAmelCase ):
      if qid_to_has_ans[qid]:
         true_pos += scores[qid]
      lowercase__:   Optional[Any]					=      true_pos / float(i + 1 )
      lowercase__:   List[Any]					=      true_pos / float(__UpperCAmelCase )
      if i == len(__UpperCAmelCase ) - 1 or na_probs[qid] != na_probs[qid_list[i + 1]]:
         # i.e., if we can put a threshold after this point
         avg_prec += cur_p * (cur_r - recalls[-1])
         precisions.append(__UpperCAmelCase )
         recalls.append(__UpperCAmelCase )
   if out_image:
      plot_pr_curve(__UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )
   return {"ap": 1_0_0.0 * avg_prec}



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )  ->  List[str]:
   if out_image_dir and not os.path.exists(__UpperCAmelCase ):
      os.makedirs(__UpperCAmelCase )
   lowercase__:   Union[str, Any]					=      sum(1 for v in qid_to_has_ans.values() if v )
   if num_true_pos == 0:
      return
   lowercase__:   List[str]					=      make_precision_recall_eval(
       __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    out_image=os.path.join(__UpperCAmelCase       ,    '''pr_exact.png''' )       ,    title='''Precision-Recall curve for Exact Match score'''       ,    )
   lowercase__:   Dict					=      make_precision_recall_eval(
       __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    out_image=os.path.join(__UpperCAmelCase       ,    '''pr_f1.png''' )       ,    title='''Precision-Recall curve for F1 score'''       ,    )
   lowercase__:   List[Any]					=      {k: float(__UpperCAmelCase ) for k, v in qid_to_has_ans.items()}
   lowercase__:   int					=      make_precision_recall_eval(
       __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    out_image=os.path.join(__UpperCAmelCase       ,    '''pr_oracle.png''' )       ,    title='''Oracle Precision-Recall curve (binary task of HasAns vs. NoAns)'''       ,    )
   merge_eval(__UpperCAmelCase       ,    __UpperCAmelCase       ,    '''pr_exact''' )
   merge_eval(__UpperCAmelCase       ,    __UpperCAmelCase       ,    '''pr_f1''' )
   merge_eval(__UpperCAmelCase       ,    __UpperCAmelCase       ,    '''pr_oracle''' )



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )  ->  List[str]:
   if not qid_list:
      return
   lowercase__:   Dict					=      [na_probs[k] for k in qid_list]
   lowercase__:   Union[str, Any]					=      np.ones_like(__UpperCAmelCase ) / float(len(__UpperCAmelCase ) )
   plt.hist(__UpperCAmelCase       ,    weights=__UpperCAmelCase       ,    bins=2_0       ,    range=(0.0, 1.0) )
   plt.xlabel('''Model probability of no-answer''' )
   plt.ylabel('''Proportion of dataset''' )
   plt.title(F"""Histogram of no-answer probability: {name}""" )
   plt.savefig(os.path.join(__UpperCAmelCase       ,    F"""na_prob_hist_{name}.png""" ) )
   plt.clf()



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )  ->  int:
   lowercase__:   Union[str, Any]					=      sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k] )
   lowercase__:   Optional[int]					=      num_no_ans
   lowercase__:   List[Any]					=      cur_score
   lowercase__:   Tuple					=      0.0
   lowercase__:   Optional[int]					=      sorted(__UpperCAmelCase       ,    key=lambda __UpperCAmelCase : na_probs[k] )
   for i, qid in enumerate(__UpperCAmelCase ):
      if qid not in scores:
         continue
      if qid_to_has_ans[qid]:
         lowercase__:   Tuple					=      scores[qid]
      else:
         if preds[qid]:
            lowercase__:   Optional[int]					=      -1
         else:
            lowercase__:   Optional[int]					=      0
      cur_score += diff
      if cur_score > best_score:
         lowercase__:   int					=      cur_score
         lowercase__:   Optional[Any]					=      na_probs[qid]
   return 1_0_0.0 * best_score / len(__UpperCAmelCase ), best_thresh



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )  ->  Dict:
   lowercase__, lowercase__:   str					=      find_best_thresh(__UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )
   lowercase__, lowercase__:   Optional[int]					=      find_best_thresh(__UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )
   lowercase__:   Optional[int]					=      best_exact
   lowercase__:   Tuple					=      exact_thresh
   lowercase__:   Optional[int]					=      best_fa
   lowercase__:   str					=      fa_thresh



def SCREAMING_SNAKE_CASE__			(   )  ->  Tuple:
   with open(OPTS.data_file ) as f:
      lowercase__:   Union[str, Any]					=      json.load(__UpperCAmelCase )
      lowercase__:   Tuple					=      dataset_json['''data''']
   with open(OPTS.pred_file ) as f:
      lowercase__:   List[str]					=      json.load(__UpperCAmelCase )
   if OPTS.na_prob_file:
      with open(OPTS.na_prob_file ) as f:
         lowercase__:   List[Any]					=      json.load(__UpperCAmelCase )
   else:
      lowercase__:   List[Any]					=      {k: 0.0 for k in preds}
   lowercase__:   Any					=      make_qid_to_has_ans(__UpperCAmelCase )  # maps qid to True/False
   lowercase__:   Dict					=      [k for k, v in qid_to_has_ans.items() if v]
   lowercase__:   Tuple					=      [k for k, v in qid_to_has_ans.items() if not v]
   lowercase__, lowercase__:   Union[str, Any]					=      get_raw_scores(__UpperCAmelCase       ,    __UpperCAmelCase )
   lowercase__:   Union[str, Any]					=      apply_no_ans_threshold(__UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    OPTS.na_prob_thresh )
   lowercase__:   List[str]					=      apply_no_ans_threshold(__UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    OPTS.na_prob_thresh )
   lowercase__:   Tuple					=      make_eval_dict(__UpperCAmelCase       ,    __UpperCAmelCase )
   if has_ans_qids:
      lowercase__:   Any					=      make_eval_dict(__UpperCAmelCase       ,    __UpperCAmelCase       ,    qid_list=__UpperCAmelCase )
      merge_eval(__UpperCAmelCase       ,    __UpperCAmelCase       ,    '''HasAns''' )
   if no_ans_qids:
      lowercase__:   Optional[Any]					=      make_eval_dict(__UpperCAmelCase       ,    __UpperCAmelCase       ,    qid_list=__UpperCAmelCase )
      merge_eval(__UpperCAmelCase       ,    __UpperCAmelCase       ,    '''NoAns''' )
   if OPTS.na_prob_file:
      find_all_best_thresh(__UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )
   if OPTS.na_prob_file and OPTS.out_image_dir:
      run_precision_recall_analysis(__UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    OPTS.out_image_dir )
      histogram_na_prob(__UpperCAmelCase       ,    __UpperCAmelCase       ,    OPTS.out_image_dir       ,    '''hasAns''' )
      histogram_na_prob(__UpperCAmelCase       ,    __UpperCAmelCase       ,    OPTS.out_image_dir       ,    '''noAns''' )
   if OPTS.out_file:
      with open(OPTS.out_file       ,    '''w''' ) as f:
         json.dump(__UpperCAmelCase       ,    __UpperCAmelCase )
   else:
      print(json.dumps(__UpperCAmelCase       ,    indent=2 ) )


if __name__ == "__main__":
       __A			   = parse_args()
       if OPTS.out_image_dir:
              import matplotlib

              matplotlib.use("Agg")
              import matplotlib.pyplot as plt
       main()

"""simple docstring"""






import argparse

import torch

from transformers import FunnelBaseModel, FunnelConfig, FunnelModel, load_tf_weights_in_funnel
from transformers.utils import logging


logging.set_verbosity_info()



def SCREAMING_SNAKE_CASE__			(   __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )  ->  List[Any]:
   # Initialise PyTorch model
   lowercase__:   Optional[Any]					=      FunnelConfig.from_json_file(__UpperCAmelCase )
   print(F"""Building PyTorch model from configuration: {config}""" )
   lowercase__:   List[Any]					=      FunnelBaseModel(__UpperCAmelCase ) if base_model else FunnelModel(__UpperCAmelCase )

   # Load weights from tf checkpoint
   load_tf_weights_in_funnel(__UpperCAmelCase       ,    __UpperCAmelCase       ,    __UpperCAmelCase )

   # Save pytorch-model
   print(F"""Save PyTorch model to {pytorch_dump_path}""" )
   torch.save(model.state_dict()       ,    __UpperCAmelCase )


if __name__ == "__main__":
       __A			   = argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
       )
       parser.add_argument(
           "--config_file",
           default=None,
           type=str,
           required=True,
           help="The config json file corresponding to the pre-trained model. \nThis specifies the model architecture.",
       )
       parser.add_argument(
           "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
       )
       parser.add_argument(
           "--base_model", action="store_true", help="Whether you want just the base model (no decoder) or not."
       )
       __A			   = parser.parse_args()
       convert_tf_checkpoint_to_pytorch(
           args.tf_checkpoint_path, args.config_file, args.pytorch_dump_path, args.base_model
       )
