



import os
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
from datasets import Dataset

from transformers.models.realm.configuration_realm import RealmConfig
from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever
from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer





class 		UpperCAmelCase						(						snake_case_				):





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  tempfile.mkdtemp()
         _lowerCAmelCase 			=  5

         # Realm tok
         _lowerCAmelCase 			=  [
             '''[UNK]''',
             '''[CLS]''',
             '''[SEP]''',
             '''[PAD]''',
             '''[MASK]''',
             '''test''',
             '''question''',
             '''this''',
             '''is''',
             '''the''',
             '''first''',
             '''second''',
             '''third''',
             '''fourth''',
             '''fifth''',
             '''record''',
             '''want''',
             '''##want''',
             '''##ed''',
             '''wa''',
             '''un''',
             '''runn''',
             '''##ing''',
             ''',''',
             '''low''',
             '''lowest''',
         ]
         _lowerCAmelCase 			=  os.path.join(self.tmpdirname       ,     '''realm_tokenizer'''			)
         os.makedirs(_lowerCAmelCase       ,     exist_ok=_lowerCAmelCase			)
         _lowerCAmelCase 			=  os.path.join(_lowerCAmelCase       ,     VOCAB_FILES_NAMES['''vocab_file''']			)
         with open(self.vocab_file       ,     '''w'''       ,     encoding='''utf-8'''			) as vocab_writer:
               vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]			)			)

         _lowerCAmelCase 			=  os.path.join(self.tmpdirname       ,     '''realm_block_records'''			)
         os.makedirs(_lowerCAmelCase       ,     exist_ok=_lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self			):
         return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname       ,     '''realm_tokenizer'''			)			)





   def 	__lowerCAmelCase							(	self			):
         shutil.rmtree(self.tmpdirname			)





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  RealmConfig(num_block_records=self.num_block_records			)
         return config





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  Dataset.from_dict(
             {
                 '''id''': ['''0''', '''1'''],
                 '''question''': ['''foo''', '''bar'''],
                 '''answers''': [['''Foo''', '''Bar'''], ['''Bar''']],
             }			)
         return dataset





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  np.array(
             [
                 b'''This is the first record''',
                 b'''This is the second record''',
                 b'''This is the third record''',
                 b'''This is the fourth record''',
                 b'''This is the fifth record''',
                 b'''This is a longer longer longer record''',
             ]       ,     dtype=_lowerCAmelCase       ,     )
         return block_records





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  RealmRetriever(
             block_records=self.get_dummy_block_records()       ,     tokenizer=self.get_tokenizer()       ,     )
         return retriever





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  self.get_config()
         _lowerCAmelCase 			=  self.get_dummy_retriever()
         _lowerCAmelCase 			=  retriever.tokenizer

         _lowerCAmelCase 			=  np.array([0, 3]       ,     dtype='''long'''			)
         _lowerCAmelCase 			=  tokenizer(['''Test question''']			).input_ids
         _lowerCAmelCase 			=  tokenizer(
             ['''the fourth''']       ,     add_special_tokens=_lowerCAmelCase       ,     return_token_type_ids=_lowerCAmelCase       ,     return_attention_mask=_lowerCAmelCase       ,     ).input_ids
         _lowerCAmelCase 			=  config.reader_seq_len

         _lowerCAmelCase			,   _lowerCAmelCase			,   _lowerCAmelCase			,   _lowerCAmelCase 			=  retriever(
             _lowerCAmelCase       ,     _lowerCAmelCase       ,     answer_ids=_lowerCAmelCase       ,     max_length=_lowerCAmelCase       ,     return_tensors='''np'''			)

         self.assertEqual(len(_lowerCAmelCase			)       ,     2			)
         self.assertEqual(len(_lowerCAmelCase			)       ,     2			)
         self.assertEqual(len(_lowerCAmelCase			)       ,     2			)
         self.assertEqual(concat_inputs.input_ids.shape       ,     (2, 10)			)
         self.assertEqual(concat_inputs.attention_mask.shape       ,     (2, 10)			)
         self.assertEqual(concat_inputs.token_type_ids.shape       ,     (2, 10)			)
         self.assertEqual(concat_inputs.special_tokens_mask.shape       ,     (2, 10)			)
         self.assertEqual(
             tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0]			)       ,     ['''[CLS]''', '''test''', '''question''', '''[SEP]''', '''this''', '''is''', '''the''', '''first''', '''record''', '''[SEP]''']       ,     )
         self.assertEqual(
             tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1]			)       ,     ['''[CLS]''', '''test''', '''question''', '''[SEP]''', '''this''', '''is''', '''the''', '''fourth''', '''record''', '''[SEP]''']       ,     )





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  self.get_config()
         _lowerCAmelCase 			=  self.get_dummy_retriever()
         _lowerCAmelCase 			=  retriever.tokenizer

         _lowerCAmelCase 			=  np.array([0, 3, 5]       ,     dtype='''long'''			)
         _lowerCAmelCase 			=  tokenizer(['''Test question''']			).input_ids
         _lowerCAmelCase 			=  tokenizer(
             ['''the fourth''', '''longer longer''']       ,     add_special_tokens=_lowerCAmelCase       ,     return_token_type_ids=_lowerCAmelCase       ,     return_attention_mask=_lowerCAmelCase       ,     ).input_ids
         _lowerCAmelCase 			=  config.reader_seq_len

         _lowerCAmelCase			,   _lowerCAmelCase			,   _lowerCAmelCase			,   _lowerCAmelCase 			=  retriever(
             _lowerCAmelCase       ,     _lowerCAmelCase       ,     answer_ids=_lowerCAmelCase       ,     max_length=_lowerCAmelCase       ,     return_tensors='''np'''			)

         self.assertEqual([False, True, True]       ,     _lowerCAmelCase			)
         self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]]       ,     _lowerCAmelCase			)
         self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]]       ,     _lowerCAmelCase			)





   def 	__lowerCAmelCase							(	self			):
         _lowerCAmelCase 			=  self.get_dummy_retriever()
         retriever.save_pretrained(os.path.join(self.tmpdirname       ,     '''realm_block_records'''			)			)

         # Test local path
         _lowerCAmelCase 			=  retriever.from_pretrained(os.path.join(self.tmpdirname       ,     '''realm_block_records'''			)			)
         self.assertEqual(retriever.block_records[0]       ,     b'''This is the first record'''			)

         # Test mocked remote path
         with patch('''transformers.models.realm.retrieval_realm.hf_hub_download'''			) as mock_hf_hub_download:
               _lowerCAmelCase 			=  os.path.join(
                   os.path.join(self.tmpdirname       ,     '''realm_block_records'''			)       ,     _REALM_BLOCK_RECORDS_FILENAME			)
               _lowerCAmelCase 			=  RealmRetriever.from_pretrained('''google/realm-cc-news-pretrained-openqa'''			)

         self.assertEqual(retriever.block_records[0]       ,     b'''This is the first record'''			)




def 							UpperCAmelCase__		(				_SCREAMING_SNAKE_CASE   :	Optional[Any]				)->Any:  # noqa: E741
      _lowerCAmelCase 			=  len(_SCREAMING_SNAKE_CASE				)
      _lowerCAmelCase 			=  0
      _lowerCAmelCase 			=  [0] * n
      _lowerCAmelCase 			=  [False] * n
      _lowerCAmelCase 			=  [False] * n

      def dfs(_SCREAMING_SNAKE_CASE   :	List[str]     ,				_SCREAMING_SNAKE_CASE   :	List[Any]     ,				_SCREAMING_SNAKE_CASE   :	Optional[int]     ,				_SCREAMING_SNAKE_CASE   :	int				):
            if parent == root:
                  out_edge_count += 1
            _lowerCAmelCase 			=  True
            _lowerCAmelCase 			=  at

            for to in l[at]:
                  if to == parent:
                        pass
                  elif not visited[to]:
                        _lowerCAmelCase 			=  dfs(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE				)
                        _lowerCAmelCase 			=  min(low[at]     ,				low[to]				)

                        # AP found via bridge
                        if at < low[to]:
                              _lowerCAmelCase 			=  True
                        # AP found via cycle
                        if at == low[to]:
                              _lowerCAmelCase 			=  True
                  else:
                        _lowerCAmelCase 			=  min(low[at]     ,				_SCREAMING_SNAKE_CASE				)
            return out_edge_count

      for i in range(_SCREAMING_SNAKE_CASE				):
            if not visited[i]:
                  _lowerCAmelCase 			=  0
                  _lowerCAmelCase 			=  dfs(_SCREAMING_SNAKE_CASE     ,				_SCREAMING_SNAKE_CASE     ,				-1     ,				_SCREAMING_SNAKE_CASE				)
                  _lowerCAmelCase 			=  out_edge_count > 1

      for x in range(len(_SCREAMING_SNAKE_CASE				)				):
            if is_art[x] is True:
                  print(_SCREAMING_SNAKE_CASE				)


# Adjacency list of graph
UpperCAmelCase_									=							{
    0: [1, 2],
    1: [0, 2],
    2: [0, 1, 3, 5],
    3: [2, 4],
    4: [3],
    5: [2, 6, 8],
    6: [5, 7],
    7: [6, 8],
    8: [5, 7],
}


compute_ap(data)