




'''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 						snake_case_							():
    UpperCAmelCase			       =		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=_a    ,   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=_a    ,   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 						snake_case_							(_a	:							List[str]     ):
    UpperCAmelCase			       =		{}
    for article in dataset:
        for p in article["paragraphs"]:
            for qa in p["qas"]:
                UpperCAmelCase			       =		bool(qa['''answers''']['''text''']     )
    return qid_to_has_ans





def 						snake_case_							(_a	:							List[str]     ):

    def remove_articles(_a	:							Optional[Any]     ):
        return ARTICLES_REGEX.sub(''' '''    ,   _a     )

    def white_space_fix(_a	:							Any     ):
        return " ".join(text.split()     )

    def remove_punc(_a	:							Dict     ):
        UpperCAmelCase			       =		set(string.punctuation     )
        return "".join(ch for ch in text if ch not in exclude     )

    def lower(_a	:							Tuple     ):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(_a     )     )     )     )





def 						snake_case_							(_a	:							Union[str, Any]     ):
    if not s:
        return []
    return normalize_answer(_a     ).split()





def 						snake_case_							(_a	:							Dict    ,   _a	:							int     ):
    return int(normalize_answer(_a     ) == normalize_answer(_a     )     )





def 						snake_case_							(_a	:							Tuple    ,   _a	:							Optional[int]     ):
    UpperCAmelCase			       =		get_tokens(_a     )
    UpperCAmelCase			       =		get_tokens(_a     )
    UpperCAmelCase			       =		collections.Counter(_a     ) & collections.Counter(_a     )
    UpperCAmelCase			       =		sum(common.values()     )
    if len(_a     ) == 0 or len(_a     ) == 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
    UpperCAmelCase			       =		1.0 * num_same / len(_a     )
    UpperCAmelCase			       =		1.0 * num_same / len(_a     )
    UpperCAmelCase			       =		(2 * precision * recall) / (precision + recall)
    return fa





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





def 						snake_case_							(_a	:							List[str]    ,   _a	:							Dict    ,   _a	:							int    ,   _a	:							Optional[Any]     ):
    UpperCAmelCase			       =		{}
    for qid, s in scores.items():
        UpperCAmelCase			       =		na_probs[qid] > na_prob_thresh
        if pred_na:
            UpperCAmelCase			       =		float(not qid_to_has_ans[qid]     )
        else:
            UpperCAmelCase			       =		s
    return new_scores





def 						snake_case_							(_a	:							int    ,   _a	:							Dict    ,   _a	:							Union[str, Any]=None     ):
    if not qid_list:
        UpperCAmelCase			       =		len(_a     )
        return collections.OrderedDict(
            [
                ('''exact''', 100.0 * sum(exact_scores.values()     ) / total),
                ('''f1''', 100.0 * sum(fa_scores.values()     ) / total),
                ('''total''', total),
            ]     )
    else:
        UpperCAmelCase			       =		len(_a     )
        return collections.OrderedDict(
            [
                ('''exact''', 100.0 * sum(exact_scores[k] for k in qid_list     ) / total),
                ('''f1''', 100.0 * sum(fa_scores[k] for k in qid_list     ) / total),
                ('''total''', total),
            ]     )





def 						snake_case_							(_a	:							Dict    ,   _a	:							Optional[Any]    ,   _a	:							List[str]     ):
    for k in new_eval:
        UpperCAmelCase			       =		new_eval[k]





def 						snake_case_							(_a	:							Any    ,   _a	:							Union[str, Any]    ,   _a	:							int    ,   _a	:							List[Any]     ):
    plt.step(_a    ,   _a    ,   color='''b'''    ,   alpha=0.2    ,   where='''post'''     )
    plt.fill_between(_a    ,   _a    ,   step='''post'''    ,   alpha=0.2    ,   color='''b'''     )
    plt.xlabel('''Recall'''     )
    plt.ylabel('''Precision'''     )
    plt.xlim([0.0, 1.05]     )
    plt.ylim([0.0, 1.05]     )
    plt.title(_a     )
    plt.savefig(_a     )
    plt.clf()





def 						snake_case_							(_a	:							Union[str, Any]    ,   _a	:							Dict    ,   _a	:							Any    ,   _a	:							Optional[Any]    ,   _a	:							Optional[Any]=None    ,   _a	:							Tuple=None     ):
    UpperCAmelCase			       =		sorted(_a    ,   key=lambda _a     : na_probs[k]     )
    UpperCAmelCase			       =		0.0
    UpperCAmelCase			       =		1.0
    UpperCAmelCase			       =		0.0
    UpperCAmelCase			       =		[1.0]
    UpperCAmelCase			       =		[0.0]
    UpperCAmelCase			       =		0.0
    for i, qid in enumerate(_a     ):
        if qid_to_has_ans[qid]:
            true_pos += scores[qid]
        UpperCAmelCase			       =		true_pos / float(i + 1     )
        UpperCAmelCase			       =		true_pos / float(_a     )
        if i == len(_a     ) - 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(_a     )
            recalls.append(_a     )
    if out_image:
        plot_pr_curve(_a    ,   _a    ,   _a    ,   _a     )
    return {"ap": 100.0 * avg_prec}





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





def 						snake_case_							(_a	:							Any    ,   _a	:							str    ,   _a	:							str    ,   _a	:							List[Any]     ):
    if not qid_list:
        return
    UpperCAmelCase			       =		[na_probs[k] for k in qid_list]
    UpperCAmelCase			       =		np.ones_like(_a     ) / float(len(_a     )     )
    plt.hist(_a    ,   weights=_a    ,   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(_a    ,   F"na_prob_hist_{name}.png"     )     )
    plt.clf()





def 						snake_case_							(_a	:							Any    ,   _a	:							Union[str, Any]    ,   _a	:							List[Any]    ,   _a	:							str     ):
    UpperCAmelCase			       =		sum(1 for k in qid_to_has_ans if not qid_to_has_ans[k]     )
    UpperCAmelCase			       =		num_no_ans
    UpperCAmelCase			       =		cur_score
    UpperCAmelCase			       =		0.0
    UpperCAmelCase			       =		sorted(_a    ,   key=lambda _a     : na_probs[k]     )
    for i, qid in enumerate(_a     ):
        if qid not in scores:
            continue
        if qid_to_has_ans[qid]:
            UpperCAmelCase			       =		scores[qid]
        else:
            if preds[qid]:
                UpperCAmelCase			       =		-1
            else:
                UpperCAmelCase			       =		0
        cur_score += diff
        if cur_score > best_score:
            UpperCAmelCase			       =		cur_score
            UpperCAmelCase			       =		na_probs[qid]
    return 100.0 * best_score / len(_a     ), best_thresh





def 						snake_case_							(_a	:							str    ,   _a	:							Dict    ,   _a	:							List[Any]    ,   _a	:							str    ,   _a	:							int    ,   _a	:							str     ):
    UpperCAmelCase       ,     UpperCAmelCase			       =		find_best_thresh(_a    ,   _a    ,   _a    ,   _a     )
    UpperCAmelCase       ,     UpperCAmelCase			       =		find_best_thresh(_a    ,   _a    ,   _a    ,   _a     )
    UpperCAmelCase			       =		best_exact
    UpperCAmelCase			       =		exact_thresh
    UpperCAmelCase			       =		best_fa
    UpperCAmelCase			       =		fa_thresh





def 						snake_case_							():
    with open(OPTS.data_file     ) as f:
        UpperCAmelCase			       =		json.load(_a     )
        UpperCAmelCase			       =		dataset_json['''data''']
    with open(OPTS.pred_file     ) as f:
        UpperCAmelCase			       =		json.load(_a     )
    if OPTS.na_prob_file:
        with open(OPTS.na_prob_file     ) as f:
            UpperCAmelCase			       =		json.load(_a     )
    else:
        UpperCAmelCase			       =		{k: 0.0 for k in preds}
    UpperCAmelCase			       =		make_qid_to_has_ans(_a     )  # maps qid to True/False
    UpperCAmelCase			       =		[k for k, v in qid_to_has_ans.items() if v]
    UpperCAmelCase			       =		[k for k, v in qid_to_has_ans.items() if not v]
    UpperCAmelCase       ,     UpperCAmelCase			       =		get_raw_scores(_a    ,   _a     )
    UpperCAmelCase			       =		apply_no_ans_threshold(_a    ,   _a    ,   _a    ,   OPTS.na_prob_thresh     )
    UpperCAmelCase			       =		apply_no_ans_threshold(_a    ,   _a    ,   _a    ,   OPTS.na_prob_thresh     )
    UpperCAmelCase			       =		make_eval_dict(_a    ,   _a     )
    if has_ans_qids:
        UpperCAmelCase			       =		make_eval_dict(_a    ,   _a    ,   qid_list=_a     )
        merge_eval(_a    ,   _a    ,   '''HasAns'''     )
    if no_ans_qids:
        UpperCAmelCase			       =		make_eval_dict(_a    ,   _a    ,   qid_list=_a     )
        merge_eval(_a    ,   _a    ,   '''NoAns'''     )
    if OPTS.na_prob_file:
        find_all_best_thresh(_a    ,   _a    ,   _a    ,   _a    ,   _a    ,   _a     )
    if OPTS.na_prob_file and OPTS.out_image_dir:
        run_precision_recall_analysis(_a    ,   _a    ,   _a    ,   _a    ,   _a    ,   OPTS.out_image_dir     )
        histogram_na_prob(_a    ,   _a    ,   OPTS.out_image_dir    ,   '''hasAns'''     )
        histogram_na_prob(_a    ,   _a    ,   OPTS.out_image_dir    ,   '''noAns'''     )
    if OPTS.out_file:
        with open(OPTS.out_file    ,   '''w'''     ) as f:
            json.dump(_a    ,   _a     )
    else:
        print(json.dumps(_a    ,   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'''







from datetime import datetime

import requests
from bsa import BeautifulSoup

if __name__ == "__main__":
   A     =input('Enter image url: ').strip()
   print(f"""Downloading image from {url} ...""")
   A     =BeautifulSoup(requests.get(url).content, 'html.parser')
   # The image URL is in the content field of the first meta tag with property og:image
   A     =soup.find('meta', {'property': 'og:image'})['content']
   A     =requests.get(image_url).content
   A     =f"""{datetime.now():%Y-%m-%d_%H:%M:%S}.jpg"""
   with open(file_name, 'wb') as fp:
      fp.write(image_data)
   print(f"""Done. Image saved to disk as {file_name}.""")




