


import jax
import jax.numpy as jnp
from bigbird_flax import FlaxBigBirdForNaturalQuestions
from datasets import load_from_disk

from transformers import BigBirdTokenizerFast


lowercase_           =	{0: 'null', 1: 'short', 2: 'long', 3: 'yes', 4: 'no'}
lowercase_           =	set(''.join(['‘', '’', '´', '`', '.', ',', '-', '"']))
def       UpperCamelCase__     (  SCREAMING_SNAKE_CASE__			,       SCREAMING_SNAKE_CASE__=0			,       SCREAMING_SNAKE_CASE__=None						):
 return [" ".join(x.split(' '						)[begin:end]						) for x in answers if len(x.split(' '						)						) > 1]
def       UpperCamelCase__     (  SCREAMING_SNAKE_CASE__			,       SCREAMING_SNAKE_CASE__=False						):
 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"
  __lowerCamelCase		:							List[Any]							=						(
      given_answers + get_sub_answers(SCREAMING_SNAKE_CASE__			,       begin=1						) + get_sub_answers(SCREAMING_SNAKE_CASE__			,       end=-1						)
  )
 __lowerCamelCase		:							List[Any]							=						[]
 for answer in given_answers:
  __lowerCamelCase		:							Union[str, Any]							=						answer.replace('_'			,       ' '						).lower()
  __lowerCamelCase		:							List[str]							=						''.join(c if c not in PUNCTUATION_SET_TO_EXCLUDE else ' ' for c in alias						)
  answers.append(' '.join(alias.split()						).strip()						)
 return set(SCREAMING_SNAKE_CASE__						)
def       UpperCamelCase__     (  SCREAMING_SNAKE_CASE__			,       SCREAMING_SNAKE_CASE__			,       SCREAMING_SNAKE_CASE__=1			,       SCREAMING_SNAKE_CASE__=100						):
 __lowerCamelCase					,     __lowerCamelCase		:							Optional[Any]							=						jax.lax.top_k(SCREAMING_SNAKE_CASE__			,       SCREAMING_SNAKE_CASE__						)
 __lowerCamelCase					,     __lowerCamelCase		:							Any							=						jax.lax.top_k(SCREAMING_SNAKE_CASE__			,       SCREAMING_SNAKE_CASE__						)

 __lowerCamelCase		:							List[str]							=						best_end_idx[:, None] - best_start_idx[None, :]
 __lowerCamelCase		:							List[Any]							=						jnp.logical_or(widths < 0			,       widths > max_size						)
 __lowerCamelCase		:							int							=						(best_end_scores[:, None] + best_start_scores[None, :]) - (1e8 * mask)
 __lowerCamelCase		:							str							=						jnp.argmax(SCREAMING_SNAKE_CASE__						).item()

 return best_start_idx[best_score % top_k], best_end_idx[best_score // top_k]
def       UpperCamelCase__     (  SCREAMING_SNAKE_CASE__						):
 __lowerCamelCase		:							Optional[int]							=						sample['question']['text']
 __lowerCamelCase		:							Dict							=						sample['document']['tokens']['token']
 __lowerCamelCase		:							int							=						sample['document']['tokens']['is_html']
 __lowerCamelCase		:							Union[str, Any]							=						sample['annotations']['long_answer']
 __lowerCamelCase		:							List[Any]							=						sample['annotations']['short_answers']

 __lowerCamelCase		:							List[str]							=						' '.join([context[i] for i in range(len(SCREAMING_SNAKE_CASE__						)						) 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",
   }

 __lowerCamelCase		:							Union[str, Any]							=						[]
 for s in short_answers:
  short_targets.extend(s['text']						)
 __lowerCamelCase		:							Optional[Any]							=						list(set(SCREAMING_SNAKE_CASE__						)						)

 __lowerCamelCase		:							int							=						[]
 for s in long_answers:
  if s["start_token"] == -1:
   continue
  __lowerCamelCase		:							int							=						context[s['start_token'] : s['end_token']]
  __lowerCamelCase		:							int							=						is_html[s['start_token'] : s['end_token']]
  __lowerCamelCase		:							List[str]							=						' '.join([answer[i] for i in range(len(SCREAMING_SNAKE_CASE__						)						) if not html[i]]						)
  if new_answer not in long_targets:
   long_targets.append(SCREAMING_SNAKE_CASE__						)

 __lowerCamelCase		:							List[Any]							=						'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       UpperCamelCase__     (  ):
 __lowerCamelCase		:							List[Any]							=						load_from_disk('natural-questions-validation'						)
 __lowerCamelCase		:							int							=						dataset.map(SCREAMING_SNAKE_CASE__						).remove_columns(['annotations', 'document', 'id']						)
 print(SCREAMING_SNAKE_CASE__						)

 __lowerCamelCase		:							Any							=						dataset.filter(lambda SCREAMING_SNAKE_CASE__						: (len(x['question']						) + len(x['context']						)) < 4 * 4_096						)
 __lowerCamelCase		:							int							=						short_validation_dataset.filter(lambda SCREAMING_SNAKE_CASE__						: x["category"] != "null"						)
 short_validation_dataset

 __lowerCamelCase		:							Union[str, Any]							=						'vasudevgupta/flax-bigbird-natural-questions'
 __lowerCamelCase		:							List[Any]							=						FlaxBigBirdForNaturalQuestions.from_pretrained(SCREAMING_SNAKE_CASE__						)
 __lowerCamelCase		:							Optional[Any]							=						BigBirdTokenizerFast.from_pretrained(SCREAMING_SNAKE_CASE__						)

 @jax.jit
 def forward(*SCREAMING_SNAKE_CASE__			,       **SCREAMING_SNAKE_CASE__						):
  __lowerCamelCase					,     __lowerCamelCase					,     __lowerCamelCase		:							List[Any]							=						model(*SCREAMING_SNAKE_CASE__			,       **SCREAMING_SNAKE_CASE__						)
  return start_logits, end_logits, jnp.argmax(SCREAMING_SNAKE_CASE__			,       axis=-1						)

 def evaluate(SCREAMING_SNAKE_CASE__						):
  # encode question and context so that they are separated by a tokenizer.sep_token and cut at max_length
  __lowerCamelCase		:							Optional[Any]							=						tokenizer(
      example['question']			,       example['context']			,       return_tensors='np'			,       max_length=4_096			,       padding='max_length'			,       truncation=SCREAMING_SNAKE_CASE__			,       )

  __lowerCamelCase					,     __lowerCamelCase					,     __lowerCamelCase		:							Union[str, Any]							=						forward(**SCREAMING_SNAKE_CASE__						)

  __lowerCamelCase		:							Dict							=						CATEGORY_MAPPING[category.item()]

  __lowerCamelCase		:							List[Any]							=						example['long'] + example['short']
  if example["category"] in ["yes", "no", "null"]:
   __lowerCamelCase		:							Dict							=						[example['category']]
  __lowerCamelCase		:							Tuple							=						example['category'] != 'null'
  # Now target can be: "yes", "no", "null", "list of long & short answers"

  if predicted_category in ["yes", "no", "null"]:
   __lowerCamelCase		:							str							=						[predicted_category]
   __lowerCamelCase		:							Optional[Any]							=						example['output'] == example['targets']
   __lowerCamelCase		:							List[str]							=						predicted_category != 'null'
   return example

  __lowerCamelCase		:							Union[str, Any]							=						38 if predicted_category == 'short' else 1_024
  __lowerCamelCase					,     __lowerCamelCase		:							Union[str, Any]							=						get_best_valid_start_end_idx(
      start_scores[0]			,       end_scores[0]			,       top_k=8			,       max_size=SCREAMING_SNAKE_CASE__						)

  __lowerCamelCase		:							Optional[Any]							=						inputs['input_ids'][0].tolist()
  __lowerCamelCase		:							Any							=						[tokenizer.decode(input_ids[start_score : end_score + 1]						)]

  __lowerCamelCase		:							Optional[Any]							=						expand_to_aliases(example['targets']			,       make_sub_answers=SCREAMING_SNAKE_CASE__						)
  __lowerCamelCase		:							Dict							=						expand_to_aliases(example['output']						)

  # some preprocessing to both prediction and answer
  __lowerCamelCase		:							Dict							=						{''.join(a.split()						) for a in answers}
  __lowerCamelCase		:							int							=						{''.join(p.split()						) for p in predictions}
  __lowerCamelCase		:							Tuple							=						{s for s in predictions if s not in ['``', '\'\'', '`', '\'']}

  # if there is a common element, it's a exact match
  __lowerCamelCase		:							Dict							=						len(list(answers & predictions						)						) > 0
  __lowerCamelCase		:							int							=						predicted_category != 'null' and len(SCREAMING_SNAKE_CASE__						) > 0

  return example

 __lowerCamelCase		:							Optional[Any]							=						short_validation_dataset.map(SCREAMING_SNAKE_CASE__						)

 __lowerCamelCase		:							Optional[int]							=						len(SCREAMING_SNAKE_CASE__						)
 __lowerCamelCase		:							Tuple							=						len(short_validation_dataset.filter(lambda SCREAMING_SNAKE_CASE__						: x["match"] == 1						)						)
 print('EM score:'			,       (matched / total) * 100			,       '%'						)


if __name__ == "__main__":
       main()





from __future__ import annotations
def       UpperCamelCase__     (  SCREAMING_SNAKE_CASE__			,       SCREAMING_SNAKE_CASE__			,       SCREAMING_SNAKE_CASE__			,       SCREAMING_SNAKE_CASE__						):
 __lowerCamelCase					,     __lowerCamelCase		:							str							=						m - 1, 0
 while i >= 0 and j < n:
  if key == mat[i][j]:
   print(f'Key {key} found at row- {i + 1} column- {j + 1}'						)
   return
  if key < mat[i][j]:
   i -= 1
  else:
   j += 1
 print(f'Key {key} not found'						)
def       UpperCamelCase__     (  ):
 __lowerCamelCase		:							List[Any]							=						[[2, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]]
 __lowerCamelCase		:							int							=						int(input('Enter the element to be searched:'						)						)
 print(SCREAMING_SNAKE_CASE__						)
 search_in_a_sorted_matrix(SCREAMING_SNAKE_CASE__			,       len(SCREAMING_SNAKE_CASE__						)			,       len(mat[0]						)			,       SCREAMING_SNAKE_CASE__						)


if __name__ == "__main__":
       import doctest

       doctest.testmod()
       main()

