

import argparse
import collections

import jax
import jax.numpy as jnp
import torch
import torch.nn as nn
from clip.model import CLIP
from flax.training import checkpoints
from huggingface_hub import Repository

from transformers import (
    CLIPTokenizer,
    OwlViTConfig,
    OwlViTForObjectDetection,
    OwlViTImageProcessor,
    OwlViTModel,
    OwlViTProcessor,
)


__UpperCAmelCase     					=  {
    'vit_b32': {
        'embed_dim': 5_1_2,
        'image_resolution': 7_6_8,
        'context_length': 1_6,
        'vocab_size': 4_9_4_0_8,
        'vision_layers': 1_2,
        'vision_width': 7_6_8,
        'vision_patch_size': 3_2,
        'transformer_width': 5_1_2,
        'transformer_heads': 8,
        'transformer_layers': 1_2,
    },
    'vit_b16': {
        'embed_dim': 5_1_2,
        'image_resolution': 7_6_8,
        'context_length': 1_6,
        'vocab_size': 4_9_4_0_8,
        'vision_layers': 1_2,
        'vision_width': 7_6_8,
        'vision_patch_size': 1_6,
        'transformer_width': 5_1_2,
        'transformer_heads': 8,
        'transformer_layers': 1_2,
    },
    'vit_l14': {
        'embed_dim': 7_6_8,
        'image_resolution': 8_4_0,
        'context_length': 1_6,
        'vocab_size': 4_9_4_0_8,
        'vision_layers': 2_4,
        'vision_width': 1_0_2_4,
        'vision_patch_size': 1_4,
        'transformer_width': 7_6_8,
        'transformer_heads': 1_2,
        'transformer_layers': 1_2,
    },
}







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=""		,				SCREAMING_SNAKE_CASE="/"					)							->   Union[str, Any]:

     """simple docstring"""







     _lowerCAmelCase :       List[str]	   =     []

     for k, v in params.items():
          _lowerCAmelCase :       int	   =     parent_key + sep + k if parent_key else k

          if isinstance(SCREAMING_SNAKE_CASE		,				collections.MutableMapping					):
               items.extend(flatten_nested_dict(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				sep=SCREAMING_SNAKE_CASE					).items()					)
          else:
               items.append((new_key, v)					)
     return dict(SCREAMING_SNAKE_CASE					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE					)							->   Optional[int]:

     """simple docstring"""







     return jax.tree_util.tree_map(lambda SCREAMING_SNAKE_CASE					: x.astype(jnp.floataa					) if x.dtype == jnp.bfloataa else x		,				SCREAMING_SNAKE_CASE					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   Dict:

     """simple docstring"""







     _lowerCAmelCase :       int	   =     pt_attn_layer.in_proj_weight.chunk(3		,				dim=0					)
     _lowerCAmelCase :       Optional[int]	   =     pt_attn_layer.in_proj_bias.chunk(3		,				dim=0					)

     _lowerCAmelCase :       Optional[int]	   =     pt_attn_layer.out_proj.weight
     _lowerCAmelCase :       str	   =     pt_attn_layer.out_proj.bias

     _lowerCAmelCase :       Optional[Any]	   =     q_proj
     _lowerCAmelCase :       Any	   =     q_proj_bias

     _lowerCAmelCase :       Optional[int]	   =     k_proj
     _lowerCAmelCase :       Union[str, Any]	   =     k_proj_bias

     _lowerCAmelCase :       List[Any]	   =     v_proj
     _lowerCAmelCase :       List[str]	   =     v_proj_bias

     _lowerCAmelCase :       int	   =     out_proj_weights
     _lowerCAmelCase :       Any	   =     out_proj_bias







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   Dict:

     """simple docstring"""







     copy_linear(hf_mlp.fca		,				pt_mlp.c_fc					)
     copy_linear(hf_mlp.fca		,				pt_mlp.c_proj					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   int:

     """simple docstring"""







     _lowerCAmelCase :       List[str]	   =     pt_linear.weight
     _lowerCAmelCase :       Any	   =     pt_linear.bias







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   str:

     """simple docstring"""







     copy_linear(hf_layer.layer_norma		,				pt_layer.ln_a					)
     copy_linear(hf_layer.layer_norma		,				pt_layer.ln_a					)

     # copy MLP
     copy_mlp(hf_layer.mlp		,				pt_layer.mlp					)

     # copy attn
     copy_attn_layer(hf_layer.self_attn		,				pt_layer.attn					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   Tuple:

     """simple docstring"""







     for hf_layer, pt_layer in zip(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					):
          copy_layer(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   int:

     """simple docstring"""







     _lowerCAmelCase :       Dict	   =     pt_model.token_embedding.weight
     _lowerCAmelCase :       Any	   =     pt_model.positional_embedding

     # copy layer norm
     copy_linear(hf_encoder.final_layer_norm		,				pt_model.ln_final					)

     # copy hidden layers
     copy_layers(hf_encoder.encoder.layers		,				pt_model.transformer.resblocks					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   int:

     """simple docstring"""







     _lowerCAmelCase :       List[Any]	   =     pt_model.text_projection.data.T

     # copy text encoder
     copy_encoder(hf_model.text_model		,				SCREAMING_SNAKE_CASE					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   List[Any]:

     """simple docstring"""







     _lowerCAmelCase :       Dict	   =     pt_model.visual.proj.data.T

     # copy layer norms
     copy_linear(hf_model.vision_model.pre_layernorm		,				pt_model.visual.ln_pre					)
     copy_linear(hf_model.vision_model.post_layernorm		,				pt_model.visual.ln_post					)

     # copy embeds
     _lowerCAmelCase :       List[Any]	   =     pt_model.visual.conva.weight.data
     _lowerCAmelCase :       str	   =     pt_model.visual.class_embedding
     _lowerCAmelCase :       Optional[Any]	   =     pt_model.visual.positional_embedding.data

     # copy encoder
     copy_layers(hf_model.vision_model.encoder.layers		,				pt_model.visual.transformer.resblocks					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   Any:

     """simple docstring"""







     _lowerCAmelCase :       Optional[int]	   =     flatten_nested_dict(flax_params["backbone"]["merged_class_token"]					)

     _lowerCAmelCase :       str	   =     torch.from_numpy(flax_class_token_params["scale"]					)
     _lowerCAmelCase :       str	   =     torch.from_numpy(flax_class_token_params["bias"]					)
     _lowerCAmelCase :       str	   =     nn.Parameter(SCREAMING_SNAKE_CASE					)
     _lowerCAmelCase :       Any	   =     nn.Parameter(SCREAMING_SNAKE_CASE					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   int:

     """simple docstring"""







     _lowerCAmelCase :       Tuple	   =     hf_model.state_dict()
     _lowerCAmelCase :       Optional[Any]	   =     {}

     # Rename class prediction head flax params to pytorch HF
     _lowerCAmelCase :       Union[str, Any]	   =     flatten_nested_dict(flax_params["class_head"]					)

     for flax_key, v in flax_class_params.items():
          _lowerCAmelCase :       List[str]	   =     flax_key.replace("/"		,				"."					)
          _lowerCAmelCase :       List[str]	   =     torch_key.replace(".kernel"		,				".weight"					)
          _lowerCAmelCase :       int	   =     torch_key.replace("Dense_0"		,				"dense0"					)
          _lowerCAmelCase :       Optional[int]	   =     'class_head.' + torch_key

          if "weight" in torch_key and v.ndim == 2:
               _lowerCAmelCase :       List[Any]	   =     v.T

          _lowerCAmelCase :       Tuple	   =     nn.Parameter(torch.from_numpy(SCREAMING_SNAKE_CASE					)					)

     # Rename box prediction box flax params to pytorch HF
     _lowerCAmelCase :       Union[str, Any]	   =     flatten_nested_dict(flax_params["obj_box_head"]					)

     for flax_key, v in flax_box_params.items():
          _lowerCAmelCase :       str	   =     flax_key.replace("/"		,				"."					)
          _lowerCAmelCase :       str	   =     torch_key.replace(".kernel"		,				".weight"					)
          _lowerCAmelCase :       str	   =     torch_key.replace("_"		,				""					).lower()
          _lowerCAmelCase :       List[Any]	   =     'box_head.' + torch_key

          if "weight" in torch_key and v.ndim == 2:
               _lowerCAmelCase :       Optional[int]	   =     v.T

          _lowerCAmelCase :       str	   =     nn.Parameter(torch.from_numpy(SCREAMING_SNAKE_CASE					)					)

     # Copy flax params to PyTorch params
     for name, param in new_params.items():
          if name in pt_params.keys():
               pt_params[name].copy_(SCREAMING_SNAKE_CASE					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   Any:

     """simple docstring"""







     for k, v in flax_attn_params.items():
          if k.startswith("transformer"					):
               _lowerCAmelCase :       str	   =     k.replace("transformer.resblocks"		,				"text_model.encoder.layers"					)
          else:
               _lowerCAmelCase :       Union[str, Any]	   =     k.replace("visual.transformer.resblocks"		,				"vision_model.encoder.layers"					)

          _lowerCAmelCase :       int	   =     torch_key.replace("attn"		,				"self_attn"					)
          _lowerCAmelCase :       Optional[Any]	   =     torch_key.replace("key"		,				"k_proj"					)
          _lowerCAmelCase :       Dict	   =     torch_key.replace("value"		,				"v_proj"					)
          _lowerCAmelCase :       Union[str, Any]	   =     torch_key.replace("query"		,				"q_proj"					)
          _lowerCAmelCase :       int	   =     torch_key.replace("out"		,				"out_proj"					)

          if "bias" in torch_key and v.ndim == 2:
               _lowerCAmelCase :       List[str]	   =     v.shape[0] * v.shape[1]
               _lowerCAmelCase :       List[Any]	   =     v.reshape(SCREAMING_SNAKE_CASE					)

          if "weight" in torch_key and "out" in torch_key:
               _lowerCAmelCase :       Optional[int]	   =     (v.shape[0] * v.shape[1], v.shape[2])
               _lowerCAmelCase :       Tuple	   =     v.reshape(SCREAMING_SNAKE_CASE					).T

          if "weight" in torch_key and "out" not in torch_key:
               _lowerCAmelCase :       Any	   =     (v.shape[0], v.shape[1] * v.shape[2])
               _lowerCAmelCase :       Union[str, Any]	   =     v.reshape(SCREAMING_SNAKE_CASE					).T

          # Copy flax CLIP attn params to HF PyTorch params
          _lowerCAmelCase :       Any	   =     torch.from_numpy(SCREAMING_SNAKE_CASE					)
          hf_backbone.state_dict()[torch_key].copy_(SCREAMING_SNAKE_CASE					)







def 						_snake_case		(     SCREAMING_SNAKE_CASE					)							->   List[Any]:

     """simple docstring"""







     _lowerCAmelCase :       Optional[Any]	   =     {}
     _lowerCAmelCase :       List[Any]	   =     []

     for k, v in params.items():
          if "attn." in k:
               _lowerCAmelCase :       Optional[Any]	   =     k[: k.rindex("attn."					) + 5]
               if base in processed_attn_layers:
                    continue

               processed_attn_layers.append(SCREAMING_SNAKE_CASE					)
               _lowerCAmelCase :       List[str]	   =     params[base + 'out.weight'].shape[-1]
               _lowerCAmelCase :       str	   =     params[base + 'out.weight'].reshape(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					).T
               _lowerCAmelCase :       int	   =     params[base + 'out.bias']
          else:
               _lowerCAmelCase :       List[str]	   =     v
     return new_params







def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)							->   Any:

     """simple docstring"""







     _lowerCAmelCase :       str	   =     CLIP(**SCREAMING_SNAKE_CASE					)
     torch_model.eval()
     _lowerCAmelCase :       Any	   =     torch_model.state_dict()

     _lowerCAmelCase :       Dict	   =     flatten_nested_dict(flax_params["backbone"]["clip"]					)
     _lowerCAmelCase :       Tuple	   =     {}

     for flax_key, v in flax_clip_params.items():
          _lowerCAmelCase :       Optional[int]	   =     flax_key.replace("/"		,				"."					)
          _lowerCAmelCase :       Any	   =     torch_key.replace("text.token_embedding.embedding"		,				"token_embedding.kernel"					)

          if (
              torch_key.startswith("text.transformer"					)
              or torch_key.startswith("text.text_projection"					)
              or torch_key.startswith("text.ln_final"					)
              or torch_key.startswith("text.positional_embedding"					)
          ):
               _lowerCAmelCase :       Dict	   =     torch_key[5:]

          _lowerCAmelCase :       Any	   =     torch_key.replace("text_projection.kernel"		,				"text_projection"					)
          _lowerCAmelCase :       Tuple	   =     torch_key.replace("visual.proj.kernel"		,				"visual.proj"					)
          _lowerCAmelCase :       Optional[int]	   =     torch_key.replace(".scale"		,				".weight"					)
          _lowerCAmelCase :       List[str]	   =     torch_key.replace(".kernel"		,				".weight"					)

          if "conv" in torch_key or "downsample.0.weight" in torch_key:
               _lowerCAmelCase :       Dict	   =     v.transpose(3		,				2		,				0		,				1					)

          elif "weight" in torch_key and v.ndim == 2 and "embedding" not in torch_key:
               # Fully connected layers are transposed, embeddings are not
               _lowerCAmelCase :       List[Any]	   =     v.T

          _lowerCAmelCase :       str	   =     v

     _lowerCAmelCase :       str	   =     _convert_attn_layers(SCREAMING_SNAKE_CASE					)
     new_torch_params.update(SCREAMING_SNAKE_CASE					)
     _lowerCAmelCase :       str	   =     {}

     # Copy flax CLIP backbone params to PyTorch params
     for name, param in new_torch_params.items():
          if name in torch_clip_params.keys():
               _lowerCAmelCase :       Union[str, Any]	   =     torch.from_numpy(new_torch_params[name]					)
               torch_clip_params[name].copy_(SCREAMING_SNAKE_CASE					)
          else:
               _lowerCAmelCase :       Union[str, Any]	   =     param

     return torch_clip_params, torch_model, attn_params







@torch.no_grad()
def 						_snake_case		(     SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=None					)							->   int:

     """simple docstring"""







     _lowerCAmelCase :       Tuple	   =     Repository(SCREAMING_SNAKE_CASE		,				clone_from=f'''google/{pytorch_dump_folder_path}'''					)
     repo.git_pull()

     if config_path is not None:
          _lowerCAmelCase :       Tuple	   =     OwlViTConfig.from_pretrained(SCREAMING_SNAKE_CASE					)
     else:
          _lowerCAmelCase :       str	   =     OwlViTConfig()

     _lowerCAmelCase :       Union[str, Any]	   =     OwlViTModel(SCREAMING_SNAKE_CASE					).eval()
     _lowerCAmelCase :       Tuple	   =     OwlViTForObjectDetection(SCREAMING_SNAKE_CASE					).eval()

     copy_text_model_and_projection(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)
     copy_vision_model_and_projection(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)
     _lowerCAmelCase :       Optional[int]	   =     pt_backbone.logit_scale
     copy_flax_attn_params(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)

     _lowerCAmelCase :       List[str]	   =     hf_backbone
     copy_class_merge_token(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)
     copy_class_box_heads(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE					)

     # Save HF model
     hf_model.save_pretrained(repo.local_dir					)

     # Initialize image processor
     _lowerCAmelCase :       Tuple	   =     OwlViTImageProcessor(
         size=config.vision_config.image_size		,				crop_size=config.vision_config.image_size					)
     # Initialize tokenizer
     _lowerCAmelCase :       Optional[Any]	   =     CLIPTokenizer.from_pretrained("openai/clip-vit-base-patch32"		,				pad_token="!"		,				model_max_length=16					)

     # Initialize processor
     _lowerCAmelCase :       int	   =     OwlViTProcessor(image_processor=SCREAMING_SNAKE_CASE		,				tokenizer=SCREAMING_SNAKE_CASE					)
     image_processor.save_pretrained(repo.local_dir					)
     processor.save_pretrained(repo.local_dir					)

     repo.git_add()
     repo.git_commit("Upload model and processor"					)
     repo.git_push()


if __name__ == "__main__":
  __UpperCAmelCase     					=  argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      '--owlvit_version',
      default=None,
      type=str,
      required=True,
      help='OWL-ViT model name [clip_b16, clip_b32, clip_l14].',
  )
  parser.add_argument(
      '--owlvit_checkpoint', default=None, type=str, required=True, help='Path to flax model checkpoint.'
  )
  parser.add_argument('--hf_config', default=None, type=str, required=True, help='Path to HF model config.')
  parser.add_argument(
      '--pytorch_dump_folder_path', default='hf_model', type=str, help='Path to the output PyTorch model.'
  )
  __UpperCAmelCase     					=  parser.parse_args()

  # Initialize PyToch clip model
  __UpperCAmelCase     					=  args.owlvit_version
  if model_name == "clip_b16":
    __UpperCAmelCase     					=  CONFIGS['vit_b16']
  elif model_name == "clip_b32":
    __UpperCAmelCase     					=  CONFIGS['vit_b32']
  elif model_name == "clip_l14":
    __UpperCAmelCase     					=  CONFIGS['vit_l14']

  # Load from checkpoint and convert params to float-32
  __UpperCAmelCase     					=  checkpoints.restore_checkpoint(args.owlvit_checkpoint, target=None)['optimizer']['target']
  __UpperCAmelCase     					=  jax.tree_util.tree_map(lambda x: x.astype(jnp.floataa) if x.dtype == jnp.bfloataa else x, variables)
  del variables

  # Convert CLIP backbone
  __UpperCAmelCase					,__UpperCAmelCase					,__UpperCAmelCase     					=  convert_clip_backbone(flax_params, torch_config)

  convert_owlvit_checkpoint(clip_pt, flax_params, attn_params, args.pytorch_dump_folder_path, args.hf_config)










import datasets

from .evaluate import (
    apply_no_ans_threshold,
    find_all_best_thresh,
    get_raw_scores,
    make_eval_dict,
    make_qid_to_has_ans,
    merge_eval,
)


__lowerCAmelCase									=				'''\
@inproceedings{Rajpurkar2016SQuAD10,
  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},
  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},
  booktitle={EMNLP},
  year={2016}
}
'''

__lowerCAmelCase									=				'''
This metric wrap the official scoring script for version 2 of the Stanford Question
Answering Dataset (SQuAD).

Stanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by
crowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,
from the corresponding reading passage, or the question might be unanswerable.

SQuAD2.0 combines the 100,000 questions in SQuAD1.1 with over 50,000 unanswerable questions
written adversarially by crowdworkers to look similar to answerable ones.
To do well on SQuAD2.0, systems must not only answer questions when possible, but also
determine when no answer is supported by the paragraph and abstain from answering.
'''

__lowerCAmelCase									=				'''
Computes SQuAD v2 scores (F1 and EM).
Args:
    predictions: List of triple for question-answers to score with the following elements:
        - the question-answer \'id\' field as given in the references (see below)
        - the text of the answer
        - the probability that the question has no answer
    references: List of question-answers dictionaries with the following key-values:
            - \'id\': id of the question-answer pair (see above),
            - \'answers\': a list of Dict {\'text\': text of the answer as a string}
    no_answer_threshold: float
        Probability threshold to decide that a question has no answer.
Returns:
    \'exact\': Exact match (the normalized answer exactly match the gold answer)
    \'f1\': The F-score of predicted tokens versus the gold answer
    \'total\': Number of score considered
    \'HasAns_exact\': Exact match (the normalized answer exactly match the gold answer)
    \'HasAns_f1\': The F-score of predicted tokens versus the gold answer
    \'HasAns_total\': Number of score considered
    \'NoAns_exact\': Exact match (the normalized answer exactly match the gold answer)
    \'NoAns_f1\': The F-score of predicted tokens versus the gold answer
    \'NoAns_total\': Number of score considered
    \'best_exact\': Best exact match (with varying threshold)
    \'best_exact_thresh\': No-answer probability threshold associated to the best exact match
    \'best_f1\': Best F1 (with varying threshold)
    \'best_f1_thresh\': No-answer probability threshold associated to the best F1
Examples:

    >>> predictions = [{\'prediction_text\': \'1976\', \'id\': \'56e10a3be3433e1400422b22\', \'no_answer_probability\': 0.}]
    >>> references = [{\'answers\': {\'answer_start\': [97], \'text\': [\'1976\']}, \'id\': \'56e10a3be3433e1400422b22\'}]
    >>> squad_v2_metric = datasets.load_metric("squad_v2")
    >>> results = squad_v2_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'exact\': 100.0, \'f1\': 100.0, \'total\': 1, \'HasAns_exact\': 100.0, \'HasAns_f1\': 100.0, \'HasAns_total\': 1, \'best_exact\': 100.0, \'best_exact_thresh\': 0.0, \'best_f1\': 100.0, \'best_f1_thresh\': 0.0}
'''





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION			,			_KWARGS_DESCRIPTION )
class     __a     (       datasets.Metric ):
 def 						SCREAMING_SNAKE_CASE__  (  self					)   ->							Dict:




        '''simple docstring'''
        return datasets.MetricInfo(
            description=_DESCRIPTION							,					citation=_CITATION							,					inputs_description=_KWARGS_DESCRIPTION							,					features=datasets.Features(
                {
                    'predictions': {
                        'id': datasets.Value('string'					),
                        'prediction_text': datasets.Value('string'					),
                        'no_answer_probability': datasets.Value('float32'					),
                    },
                    'references': {
                        'id': datasets.Value('string'					),
                        'answers': datasets.features.Sequence(
                            {'text': datasets.Value('string'					), 'answer_start': datasets.Value('int32'					)}					),
                    },
                }					)							,					codebase_urls=['https://rajpurkar.github.io/SQuAD-explorer/']							,					reference_urls=['https://rajpurkar.github.io/SQuAD-explorer/']							,					)
 def 						SCREAMING_SNAKE_CASE__  (  self							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__=1.0					)   ->							str:




        '''simple docstring'''
        lowercase__:  int         =  {p['id']: p['no_answer_probability'] for p in predictions}
        lowercase__:  int         =  [{'paragraphs': [{'qas': references}]}]
        lowercase__:  Optional[int]         =  {p['id']: p['prediction_text'] for p in predictions}

        lowercase__:  Any         =  make_qid_to_has_ans(lowerCAmelCase__					)  # maps qid to True/False
        lowercase__:  Optional[Any]         =  [k for k, v in qid_to_has_ans.items() if v]
        lowercase__:  Dict         =  [k for k, v in qid_to_has_ans.items() if not v]

        lowercase__     ,		lowercase__:  Dict         =  get_raw_scores(lowerCAmelCase__							,					lowerCAmelCase__					)
        lowercase__:  Optional[int]         =  apply_no_ans_threshold(lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)
        lowercase__:  List[Any]         =  apply_no_ans_threshold(lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)
        lowercase__:  Dict         =  make_eval_dict(lowerCAmelCase__							,					lowerCAmelCase__					)

        if has_ans_qids:
               lowercase__:  List[Any]         =  make_eval_dict(lowerCAmelCase__							,					lowerCAmelCase__							,					qid_list=lowerCAmelCase__					)
               merge_eval(lowerCAmelCase__							,					lowerCAmelCase__							,					'HasAns'					)
        if no_ans_qids:
               lowercase__:  int         =  make_eval_dict(lowerCAmelCase__							,					lowerCAmelCase__							,					qid_list=lowerCAmelCase__					)
               merge_eval(lowerCAmelCase__							,					lowerCAmelCase__							,					'NoAns'					)
        find_all_best_thresh(lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__							,					lowerCAmelCase__					)
        return dict(lowerCAmelCase__					)


