code: 
  
  
  import csv
  import dataclasses
  import json
  from dataclasses import dataclass
  from typing import List, Optional, Union
  
  from ...utils import is_tf_available, is_torch_available, logging
  
  
  __snake_case             =	logging.get_logger(__name__)
  
  
  
  @dataclass
  class   snake_case__ :
         _lowercase       							=    42
         _lowercase       							=    42
         _lowercase       							=    None
         _lowercase       							=    None
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Any				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              return json.dumps(dataclasses.asdict(self				),indent=2				) + "\n"
  
  
  
  @dataclass(frozen=_a	)
  class   snake_case__ :
         _lowercase       							=    42
         _lowercase       							=    None
         _lowercase       							=    None
         _lowercase       							=    None
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   int				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              return json.dumps(dataclasses.asdict(self				)				) + "\n"
  
  
  
  class   snake_case__ :
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Optional[int],A_:   str				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              raise NotImplementedError()
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[Any],A_:   Any				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              raise NotImplementedError()
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   str,A_:   Union[str, Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              raise NotImplementedError()
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Any,A_:   int				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              raise NotImplementedError()
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Optional[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              raise NotImplementedError()
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   List[Any],A_:   str				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              if len(self.get_labels()				) > 1:
                   __UpperCamelCase   = self.get_labels()[int(example.label				)]
              return example
  
  
  
  
  
  
  
         @classmethod
         def 		snake_case_  ( cls:   Optional[int],A_:   List[str],A_:   Union[str, Any]=None				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              with open(A_,'r',encoding='utf-8-sig'				) as f:
                   return list(csv.reader(A_,delimiter='\t',quotechar=A_				)				)
  
  
  
  class   snake_case__ (_a	):
  
  
  
  
  
  
  
         def __init__( self:   List[str],A_:   Optional[Any]=None,A_:   int=None,A_:   List[str]="classification",A_:   Tuple=False				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = [] if labels is None else labels
              __UpperCamelCase   = [] if examples is None else examples
              __UpperCamelCase   = mode
              __UpperCamelCase   = verbose
  
  
  
  
  
  
  
         def __len__( self:   Optional[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              return len(self.examples				)
  
  
  
  
  
  
  
         def __getitem__( self:   Optional[int],A_:   Optional[int]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              if isinstance(A_,A_				):
                   return SingleSentenceClassificationProcessor(labels=self.labels,examples=self.examples[idx]				)
              return self.examples[idx]
  
  
  
  
  
  
  
         @classmethod
         def 		snake_case_  ( cls:   Tuple,A_:   int,A_:   str="",A_:   List[Any]=0,A_:   Union[str, Any]=1,A_:   Optional[Any]=None,A_:   str=False,**A_:   Optional[Any]				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = cls(**A_				)
              processor.add_examples_from_csv(
                  A_,split_name=A_,column_label=A_,column_text=A_,column_id=A_,skip_first_row=A_,overwrite_labels=A_,overwrite_examples=A_,)
              return processor
  
  
  
  
  
  
  
         @classmethod
         def 		snake_case_  ( cls:   Optional[Any],A_:   Any,A_:   List[Any]=None,**A_:   Tuple				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = cls(**A_				)
              processor.add_examples(A_,labels=A_				)
              return processor
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Dict,A_:   Union[str, Any],A_:   List[str]="",A_:   str=0,A_:   int=1,A_:   Optional[Any]=None,A_:   Dict=False,A_:   str=False,A_:   str=False,):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              __UpperCamelCase   = self._read_tsv(A_				)
              if skip_first_row:
                   __UpperCamelCase   = lines[1:]
              __UpperCamelCase   = []
              __UpperCamelCase   = []
              __UpperCamelCase   = []
              for i, line in enumerate(A_				):
                   texts.append(line[column_text]				)
                   labels.append(line[column_label]				)
                   if column_id is not None:
                        ids.append(line[column_id]				)
                   else:
                        __UpperCamelCase   = F'''{split_name}-{i}''' if split_name else str(A_				)
                        ids.append(A_				)
  
              return self.add_examples(
                  A_,A_,A_,overwrite_labels=A_,overwrite_examples=A_				)
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Dict,A_:   Union[str, Any],A_:   Union[str, Any]=None,A_:   Tuple=None,A_:   int=False,A_:   List[str]=False				):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              if labels is not None and len(A_				) != len(A_				):
                   raise ValueError(
                       F'''Text and labels have mismatched lengths {len(A_				)} and {len(A_				)}'''				)
              if ids is not None and len(A_				) != len(A_				):
                   raise ValueError(F'''Text and ids have mismatched lengths {len(A_				)} and {len(A_				)}'''				)
              if ids is None:
                   __UpperCamelCase   = [None] * len(A_				)
              if labels is None:
                   __UpperCamelCase   = [None] * len(A_				)
              __UpperCamelCase   = []
              __UpperCamelCase   = set()
              for text_or_text_and_label, label, guid in zip(A_,A_,A_				):
                   if isinstance(A_,(tuple, list)				) and label is None:
                        __UpperCamelCase,						__UpperCamelCase   = text_or_text_and_label
                   else:
                        __UpperCamelCase   = text_or_text_and_label
                   added_labels.add(A_				)
                   examples.append(InputExample(guid=A_,text_a=A_,text_b=A_,label=A_				)				)
  
              # Update examples
              if overwrite_examples:
                   __UpperCamelCase   = examples
              else:
                   self.examples.extend(A_				)
  
              # Update labels
              if overwrite_labels:
                   __UpperCamelCase   = list(A_				)
              else:
                   __UpperCamelCase   = list(set(self.labels				).union(A_				)				)
  
              return self.examples
  
  
  
  
  
  
  
         def 		snake_case_  ( self:   Any,A_:   Tuple,A_:   Optional[int]=None,A_:   Union[str, Any]=False,A_:   List[str]=0,A_:   List[Any]=True,A_:   List[Any]=None,):
  
  
  
  
  
  
              '''simple docstring'''
  
  
  
  
              if max_length is None:
                   __UpperCamelCase   = tokenizer.max_len
  
              __UpperCamelCase   = {label: i for i, label in enumerate(self.labels				)}
  
              __UpperCamelCase   = []
              for ex_index, example in enumerate(self.examples				):
                   if ex_index % 1_0000 == 0:
                        logger.info(F'''Tokenizing example {ex_index}'''				)
  
                   __UpperCamelCase   = tokenizer.encode(
                       example.text_a,add_special_tokens=A_,max_length=min(A_,tokenizer.max_len				),)
                   all_input_ids.append(A_				)
  
              __UpperCamelCase   = max(len(A_				) for input_ids in all_input_ids				)
  
              __UpperCamelCase   = []
              for ex_index, (input_ids, example) in enumerate(zip(A_,self.examples				)				):
                   if ex_index % 1_0000 == 0:
                        logger.info(F'''Writing example {ex_index}/{len(self.examples				)}'''				)
                   # The mask has 1 for real tokens and 0 for padding tokens. Only real
                   # tokens are attended to.
                   __UpperCamelCase   = [1 if mask_padding_with_zero else 0] * len(A_				)
  
                   # Zero-pad up to the sequence length.
                   __UpperCamelCase   = batch_length - len(A_				)
                   if pad_on_left:
                        __UpperCamelCase   = ([pad_token] * padding_length) + input_ids
                        __UpperCamelCase   = ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
                   else:
                        __UpperCamelCase   = input_ids + ([pad_token] * padding_length)
                        __UpperCamelCase   = attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)
  
                   if len(A_				) != batch_length:
                        raise ValueError(F'''Error with input length {len(A_				)} vs {batch_length}'''				)
                   if len(A_				) != batch_length:
                        raise ValueError(F'''Error with input length {len(A_				)} vs {batch_length}'''				)
  
                   if self.mode == "classification":
                        __UpperCamelCase   = label_map[example.label]
                   elif self.mode == "regression":
                        __UpperCamelCase   = float(example.label				)
                   else:
                        raise ValueError(self.mode				)
  
                   if ex_index < 5 and self.verbose:
                        logger.info('*** Example ***'				)
                        logger.info(F'''guid: {example.guid}'''				)
                        logger.info(F'''input_ids: {' '.join([str(A_				) for x in input_ids]				)}'''				)
                        logger.info(F'''attention_mask: {' '.join([str(A_				) for x in attention_mask]				)}'''				)
                        logger.info(F'''label: {example.label} (id = {label})'''				)
  
                   features.append(InputFeatures(input_ids=A_,attention_mask=A_,label=A_				)				)
  
              if return_tensors is None:
                   return features
              elif return_tensors == "tf":
                   if not is_tf_available():
                        raise RuntimeError('return_tensors set to \'tf\' but TensorFlow 2.0 can\'t be imported'				)
                   import tensorflow as tf
  
                   def gen():
                        for ex in features:
                             yield ({"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label)
  
                   __UpperCamelCase   = tf.data.Dataset.from_generator(
                       A_,({'input_ids': tf.intaa, 'attention_mask': tf.intaa}, tf.intaa),({'input_ids': tf.TensorShape([None]				), 'attention_mask': tf.TensorShape([None]				)}, tf.TensorShape([]				)),)
                   return dataset
              elif return_tensors == "pt":
                   if not is_torch_available():
                        raise RuntimeError('return_tensors set to \'pt\' but PyTorch can\'t be imported'				)
                   import torch
                   from torch.utils.data import TensorDataset
  
                   __UpperCamelCase   = torch.tensor([f.input_ids for f in features],dtype=torch.long				)
                   __UpperCamelCase   = torch.tensor([f.attention_mask for f in features],dtype=torch.long				)
                   if self.mode == "classification":
                        __UpperCamelCase   = torch.tensor([f.label for f in features],dtype=torch.long				)
                   elif self.mode == "regression":
                        __UpperCamelCase   = torch.tensor([f.label for f in features],dtype=torch.float				)
  
                   __UpperCamelCase   = TensorDataset(A_,A_,A_				)
                   return dataset
              else:
                   raise ValueError('return_tensors should be one of \'tf\' or \'pt\''				)
  
  
  
style_context: 
  
  
  import jax
  import jax.numpy as jnp
  from bigbird_flax import FlaxBigBirdForNaturalQuestions
  from datasets import load_from_disk
  
  from transformers import BigBirdTokenizerFast
  
  
  __snake_case             =	{0: '''null''', 1: '''short''', 2: '''long''', 3: '''yes''', 4: '''no'''}
  __snake_case             =	set(''''''.join(['''‘''', '''’''', '''´''', '''`''', '''.''', ''',''', '''-''', '''"''']))
  
  
  
  
  
  
  def 		_A		(		_lowercase				,     _lowercase=0				,     _lowercase=None   )				->   Union[str, Any]:
  
  
       """simple docstring"""
  
  
       return [" ".join(x.split(' '   )[begin:end]   ) for x in answers if len(x.split(' '   )   ) > 1]
  
  
  
  
  
  
  def 		_A		(		_lowercase				,     _lowercase=False   )				->   Tuple:
  
  
       """simple docstring"""
  
  
       if make_sub_answers:
            # if answers are longer than one word, make sure a predictions is correct if it coresponds to the complete 1: or :-1 sub word
            # *e.g.* if the correct answer contains a prefix such as "the", or "a"
            __UpperCamelCase   = (
                given_answers + get_sub_answers(_lowercase				,     begin=1   ) + get_sub_answers(_lowercase				,     end=-1   )
            )
       __UpperCamelCase   = []
       for answer in given_answers:
            __UpperCamelCase   = answer.replace('_'				,     ' '   ).lower()
            __UpperCamelCase   = ''.join(c if c not in PUNCTUATION_SET_TO_EXCLUDE else ' ' for c in alias   )
            answers.append(' '.join(alias.split()   ).strip()   )
       return set(_lowercase   )
  
  
  
  
  
  
  def 		_A		(		_lowercase				,     _lowercase				,     _lowercase=1				,     _lowercase=1_00   )				->   Any:
  
  
       """simple docstring"""
  
  
       __UpperCamelCase,						__UpperCamelCase   = jax.lax.top_k(_lowercase				,     _lowercase   )
       __UpperCamelCase,						__UpperCamelCase   = jax.lax.top_k(_lowercase				,     _lowercase   )
  
       __UpperCamelCase   = best_end_idx[:, None] - best_start_idx[None, :]
       __UpperCamelCase   = jnp.logical_or(widths < 0				,     widths > max_size   )
       __UpperCamelCase   = (best_end_scores[:, None] + best_start_scores[None, :]) - (1e8 * mask)
       __UpperCamelCase   = jnp.argmax(_lowercase   ).item()
  
       return best_start_idx[best_score % top_k], best_end_idx[best_score // top_k]
  
  
  
  
  
  
  def 		_A		(		_lowercase   )				->   List[str]:
  
  
       """simple docstring"""
  
  
       __UpperCamelCase   = sample['question']['text']
       __UpperCamelCase   = sample['document']['tokens']['token']
       __UpperCamelCase   = sample['document']['tokens']['is_html']
       __UpperCamelCase   = sample['annotations']['long_answer']
       __UpperCamelCase   = sample['annotations']['short_answers']
  
       __UpperCamelCase   = ' '.join([context[i] for i in range(len(_lowercase   )   ) if not is_html[i]]   )
  
       # 0 - No ; 1 - Yes
       for answer in sample["annotations"]["yes_no_answer"]:
            if answer == 0 or answer == 1:
                 return {
                     "question": question,
                     "context": context_string,
                     "short": [],
                     "long": [],
                     "category": "no" if answer == 0 else "yes",
                 }
  
       __UpperCamelCase   = []
       for s in short_answers:
            short_targets.extend(s['text']   )
       __UpperCamelCase   = list(set(_lowercase   )   )
  
       __UpperCamelCase   = []
       for s in long_answers:
            if s["start_token"] == -1:
                 continue
            __UpperCamelCase   = context[s['start_token'] : s['end_token']]
            __UpperCamelCase   = is_html[s['start_token'] : s['end_token']]
            __UpperCamelCase   = ' '.join([answer[i] for i in range(len(_lowercase   )   ) if not html[i]]   )
            if new_answer not in long_targets:
                 long_targets.append(_lowercase   )
  
       __UpperCamelCase   = 'long_short' if len(short_targets + long_targets   ) > 0 else 'null'
  
       return {
           "question": question,
           "context": context_string,
           "short": short_targets,
           "long": long_targets,
           "category": category,
       }
  
  
  
  
  
  
  def 		_A		(		)				->   Optional[int]:
  
  
       """simple docstring"""
  
  
       __UpperCamelCase   = load_from_disk('natural-questions-validation'   )
       __UpperCamelCase   = dataset.map(_lowercase   ).remove_columns(['annotations', 'document', 'id']   )
       print(_lowercase   )
  
       __UpperCamelCase   = dataset.filter(lambda _lowercase   : (len(x['question']   ) + len(x['context']   )) < 4 * 40_96   )
       __UpperCamelCase   = short_validation_dataset.filter(lambda _lowercase   : x["category"] != "null"   )
       short_validation_dataset
  
       __UpperCamelCase   = 'vasudevgupta/flax-bigbird-natural-questions'
       __UpperCamelCase   = FlaxBigBirdForNaturalQuestions.from_pretrained(_lowercase   )
       __UpperCamelCase   = BigBirdTokenizerFast.from_pretrained(_lowercase   )
  
       @jax.jit
       def forward(*_lowercase				,     **_lowercase   ):
            __UpperCamelCase,						__UpperCamelCase,						__UpperCamelCase   = model(*_lowercase				,     **_lowercase   )
            return start_logits, end_logits, jnp.argmax(_lowercase				,     axis=-1   )
  
       def evaluate(_lowercase   ):
            # encode question and context so that they are separated by a tokenizer.sep_token and cut at max_length
            __UpperCamelCase   = tokenizer(
                example['question']				,     example['context']				,     return_tensors='np'				,     max_length=40_96				,     padding='max_length'				,     truncation=_lowercase				,     )
  
            __UpperCamelCase,						__UpperCamelCase,						__UpperCamelCase   = forward(**_lowercase   )
  
            __UpperCamelCase   = CATEGORY_MAPPING[category.item()]
  
            __UpperCamelCase   = example['long'] + example['short']
            if example["category"] in ["yes", "no", "null"]:
                 __UpperCamelCase   = [example['category']]
            __UpperCamelCase   = example['category'] != 'null'
            # Now target can be: "yes", "no", "null", "list of long & short answers"
  
            if predicted_category in ["yes", "no", "null"]:
                 __UpperCamelCase   = [predicted_category]
                 __UpperCamelCase   = example['output'] == example['targets']
                 __UpperCamelCase   = predicted_category != 'null'
                 return example
  
            __UpperCamelCase   = 38 if predicted_category == 'short' else 10_24
            __UpperCamelCase,						__UpperCamelCase   = get_best_valid_start_end_idx(
                start_scores[0]				,     end_scores[0]				,     top_k=8				,     max_size=_lowercase   )
  
            __UpperCamelCase   = inputs['input_ids'][0].tolist()
            __UpperCamelCase   = [tokenizer.decode(input_ids[start_score : end_score + 1]   )]
  
            __UpperCamelCase   = expand_to_aliases(example['targets']				,     make_sub_answers=_lowercase   )
            __UpperCamelCase   = expand_to_aliases(example['output']   )
  
            # some preprocessing to both prediction and answer
            __UpperCamelCase   = {''.join(a.split()   ) for a in answers}
            __UpperCamelCase   = {''.join(p.split()   ) for p in predictions}
            __UpperCamelCase   = {s for s in predictions if s not in ['``', '\'\'', '`', '\'']}
  
            # if there is a common element, it's a exact match
            __UpperCamelCase   = len(list(answers & predictions   )   ) > 0
            __UpperCamelCase   = predicted_category != 'null' and len(_lowercase   ) > 0
  
            return example
  
       __UpperCamelCase   = short_validation_dataset.map(_lowercase   )
  
       __UpperCamelCase   = len(_lowercase   )
       __UpperCamelCase   = len(short_validation_dataset.filter(lambda _lowercase   : x["match"] == 1   )   )
       print('EM score:'				,     (matched / total) * 1_00				,     '%'   )
  
  
  if __name__ == "__main__":
      main()
  
  
  