






"""simple docstring"""





import argparse
import logging
import os
import sys
from collections import namedtuple

import torch
from modeling_bertabs import BertAbs, build_predictor
from torch.utils.data import DataLoader, SequentialSampler
from tqdm import tqdm

from transformers import BertTokenizer

from .utils_summarization import (
    CNNDMDataset,
    build_mask,
    compute_token_type_ids,
    encode_for_summarization,
    truncate_or_pad,
)


UpperCamelCase_						:					Any      				=			logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout, level=logging.INFO)


UpperCamelCase_						:					List[str]      				=			namedtuple("""Batch""", ["""document_names""", """batch_size""", """src""", """segs""", """mask_src""", """tgt_str"""])



def      __lowercase       (     a			:					Optional[int]			)       ->       Optional[int]:
      __snake_case   :     Tuple	      =BertTokenizer.from_pretrained('''bert-base-uncased'''							,		do_lower_case=lowerCAmelCase_			)
      __snake_case   :     Dict	      =BertAbs.from_pretrained('''remi/bertabs-finetuned-extractive-abstractive-summarization'''			)
      model.to(args.device			)
      model.eval()

      __snake_case   :     Union[str, Any]	      ={
          """BOS""": tokenizer.vocab["""[unused0]"""],
          """EOS""": tokenizer.vocab["""[unused1]"""],
          """PAD""": tokenizer.vocab["""[PAD]"""],
      }

      if args.compute_rouge:
            __snake_case   :     Optional[Any]	      =[]
            __snake_case   :     Optional[int]	      =[]

            import nltk
            import rouge

            nltk.download('''punkt'''			)
            __snake_case   :     Optional[Any]	      =rouge.Rouge(
                metrics=['''rouge-n''', '''rouge-l''']							,		max_n=2							,		limit_length=lowerCAmelCase_							,		length_limit=args.beam_size							,		length_limit_type='''words'''							,		apply_avg=lowerCAmelCase_							,		apply_best=lowerCAmelCase_							,		alpha=0.5							,		weight_factor=1.2							,		stemming=lowerCAmelCase_							,		)

      # these (unused) arguments are defined to keep the compatibility
      # with the legacy code and will be deleted in a next iteration.
      __snake_case   :     Optional[int]	      =""""""
      __snake_case   :     Any	      =""""""

      __snake_case   :     List[Any]	      =build_data_iterator(lowerCAmelCase_							,		lowerCAmelCase_			)
      __snake_case   :     Any	      =build_predictor(lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_			)

      logger.info('''***** Running evaluation *****'''			)
      logger.info('''  Number examples = %d'''							,		len(data_iterator.dataset			)			)
      logger.info('''  Batch size = %d'''							,		args.batch_size			)
      logger.info(''''''			)
      logger.info('''***** Beam Search parameters *****'''			)
      logger.info('''  Beam size = %d'''							,		args.beam_size			)
      logger.info('''  Minimum length = %d'''							,		args.min_length			)
      logger.info('''  Maximum length = %d'''							,		args.max_length			)
      logger.info('''  Alpha (length penalty) = %.2f'''							,		args.alpha			)
      logger.info('''  Trigrams %s be blocked'''							,		('''will''' if args.block_trigram else '''will NOT''')			)

      for batch in tqdm(lowerCAmelCase_			):
            __snake_case   :     Optional[int]	      =predictor.translate_batch(lowerCAmelCase_			)
            __snake_case   :     Dict	      =predictor.from_batch(lowerCAmelCase_			)
            __snake_case   :     Union[str, Any]	      =[format_summary(lowerCAmelCase_			) for t in translations]
            save_summaries(lowerCAmelCase_							,		args.summaries_output_dir							,		batch.document_names			)

            if args.compute_rouge:
                  reference_summaries += batch.tgt_str
                  generated_summaries += summaries

      if args.compute_rouge:
            __snake_case   :     Dict	      =rouge_evaluator.get_scores(lowerCAmelCase_							,		lowerCAmelCase_			)
            __snake_case   :     Optional[int]	      =format_rouge_scores(lowerCAmelCase_			)
            save_rouge_scores(lowerCAmelCase_			)
            print(lowerCAmelCase_			)



def      __lowercase       (     a			:					List[Any]							,		a			:					str							,		a			:					Dict			)       ->       Any:


      for summary, document_name in zip(lowerCAmelCase_							,		lowerCAmelCase_			):
            # Prepare the summary file's name
            if "." in document_name:
                  __snake_case   :     Union[str, Any]	      =""".""".join(document_name.split('''.'''			)[:-1]			)
                  __snake_case   :     Tuple	      =document_name.split('''.'''			)[-1]
                  __snake_case   :     Dict	      =bare_document_name + """_summary.""" + extension
            else:
                  __snake_case   :     Optional[int]	      =document_name + """_summary"""

            __snake_case   :     Union[str, Any]	      =os.path.join(lowerCAmelCase_							,		lowerCAmelCase_			)
            with open(lowerCAmelCase_							,		'''w'''			) as output:
                  output.write(lowerCAmelCase_			)



def      __lowercase       (     a			:					int			)       ->       int:
      __snake_case   :     str	      =translation
      __snake_case   :     List[str]	      =(
          raw_summary.replace('''[unused0]'''							,		''''''			)
          .replace('''[unused3]'''							,		''''''			)
          .replace('''[PAD]'''							,		''''''			)
          .replace('''[unused1]'''							,		''''''			)
          .replace(R''' +'''							,		''' '''			)
          .replace(''' [unused2] '''							,		'''. '''			)
          .replace('''[unused2]'''							,		''''''			)
          .strip()
      )

      return summary



def      __lowercase       (     a			:					Dict			)       ->       Tuple:
      return """\n
****** ROUGE SCORES ******

** ROUGE 1
F1        >> {:.3f}
Precision >> {:.3f}
Recall    >> {:.3f}

** ROUGE 2
F1        >> {:.3f}
Precision >> {:.3f}
Recall    >> {:.3f}

** ROUGE L
F1        >> {:.3f}
Precision >> {:.3f}
Recall    >> {:.3f}""".format(
          scores['''rouge-1''']['''f''']							,		scores['''rouge-1''']['''p''']							,		scores['''rouge-1''']['''r''']							,		scores['''rouge-2''']['''f''']							,		scores['''rouge-2''']['''p''']							,		scores['''rouge-2''']['''r''']							,		scores['''rouge-l''']['''f''']							,		scores['''rouge-l''']['''p''']							,		scores['''rouge-l''']['''r''']							,		)



def      __lowercase       (     a			:					Tuple			)       ->       Any:


      with open('''rouge_scores.txt'''							,		'''w'''			) as output:
            output.write(lowerCAmelCase_			)



def      __lowercase       (     a			:					Optional[Any]							,		a			:					int			)       ->       Optional[int]:
      __snake_case   :     Dict	      =load_and_cache_examples(lowerCAmelCase_							,		lowerCAmelCase_			)
      __snake_case   :     Union[str, Any]	      =SequentialSampler(lowerCAmelCase_			)

      def collate_fn(a			:					int			):
            return collate(lowerCAmelCase_							,		lowerCAmelCase_							,		block_size=512							,		device=args.device			)

      __snake_case   :     Dict	      =DataLoader(
          lowerCAmelCase_							,		sampler=lowerCAmelCase_							,		batch_size=args.batch_size							,		collate_fn=lowerCAmelCase_							,		)

      return iterator



def      __lowercase       (     a			:					Any							,		a			:					Tuple			)       ->       Optional[int]:
      __snake_case   :     Dict	      =CNNDMDataset(args.documents_dir			)
      return dataset



def      __lowercase       (     a			:					List[Any]							,		a			:					Tuple							,		a			:					int							,		a			:					str			)       ->       List[str]:
      __snake_case   :     List[str]	      =[x for x in data if not len(x[1]			) == 0]  # remove empty_files
      __snake_case   :     str	      =[name for name, _, _ in data]
      __snake_case   :     List[str]	      =[""" """.join(lowerCAmelCase_			) for _, _, summary_list in data]

      __snake_case   :     Optional[int]	      =[encode_for_summarization(lowerCAmelCase_							,		lowerCAmelCase_							,		lowerCAmelCase_			) for _, story, summary in data]
      __snake_case   :     Union[str, Any]	      =torch.tensor(
          [truncate_or_pad(lowerCAmelCase_							,		lowerCAmelCase_							,		tokenizer.pad_token_id			) for story, _ in encoded_text]			)
      __snake_case   :     Tuple	      =compute_token_type_ids(lowerCAmelCase_							,		tokenizer.cls_token_id			)
      __snake_case   :     Tuple	      =build_mask(lowerCAmelCase_							,		tokenizer.pad_token_id			)

      __snake_case   :     List[Any]	      =Batch(
          document_names=lowerCAmelCase_							,		batch_size=len(lowerCAmelCase_			)							,		src=encoded_stories.to(lowerCAmelCase_			)							,		segs=encoder_token_type_ids.to(lowerCAmelCase_			)							,		mask_src=encoder_mask.to(lowerCAmelCase_			)							,		tgt_str=lowerCAmelCase_							,		)

      return batch



def      __lowercase       (     a			:					List[Any]							,		a			:					str			)       ->       int:
      __snake_case   :     Optional[Any]	      =summary_tokens.to('''cpu'''			).numpy()
      __snake_case   :     Optional[Any]	      =tokenizer.decode(lowerCAmelCase_			)
      __snake_case   :     Union[str, Any]	      =summary.split('''.'''			)
      __snake_case   :     Optional[int]	      =[s + """.""" for s in sentences]
      return sentences



def      __lowercase       (     )       ->       Any:
      __snake_case   :     int	      =argparse.ArgumentParser()
      parser.add_argument(
          '''--documents_dir'''							,		default=lowerCAmelCase_							,		type=lowerCAmelCase_							,		required=lowerCAmelCase_							,		help='''The folder where the documents to summarize are located.'''							,		)
      parser.add_argument(
          '''--summaries_output_dir'''							,		default=lowerCAmelCase_							,		type=lowerCAmelCase_							,		required=lowerCAmelCase_							,		help='''The folder in wich the summaries should be written. Defaults to the folder where the documents are'''							,		)
      parser.add_argument(
          '''--compute_rouge'''							,		default=lowerCAmelCase_							,		type=lowerCAmelCase_							,		required=lowerCAmelCase_							,		help='''Compute the ROUGE metrics during evaluation. Only available for the CNN/DailyMail dataset.'''							,		)
      # EVALUATION options
      parser.add_argument(
          '''--no_cuda'''							,		default=lowerCAmelCase_							,		type=lowerCAmelCase_							,		help='''Whether to force the execution on CPU.'''							,		)
      parser.add_argument(
          '''--batch_size'''							,		default=4							,		type=lowerCAmelCase_							,		help='''Batch size per GPU/CPU for training.'''							,		)
      # BEAM SEARCH arguments
      parser.add_argument(
          '''--min_length'''							,		default=50							,		type=lowerCAmelCase_							,		help='''Minimum number of tokens for the summaries.'''							,		)
      parser.add_argument(
          '''--max_length'''							,		default=200							,		type=lowerCAmelCase_							,		help='''Maixmum number of tokens for the summaries.'''							,		)
      parser.add_argument(
          '''--beam_size'''							,		default=5							,		type=lowerCAmelCase_							,		help='''The number of beams to start with for each example.'''							,		)
      parser.add_argument(
          '''--alpha'''							,		default=0.9_5							,		type=lowerCAmelCase_							,		help='''The value of alpha for the length penalty in the beam search.'''							,		)
      parser.add_argument(
          '''--block_trigram'''							,		default=lowerCAmelCase_							,		type=lowerCAmelCase_							,		help='''Whether to block the existence of repeating trigrams in the text generated by beam search.'''							,		)
      __snake_case   :     Any	      =parser.parse_args()

      # Select device (distibuted not available)
      __snake_case   :     Optional[int]	      =torch.device('''cuda''' if torch.cuda.is_available() and not args.no_cuda else '''cpu'''			)

      # Check the existence of directories
      if not args.summaries_output_dir:
            __snake_case   :     Optional[Any]	      =args.documents_dir

      if not documents_dir_is_valid(args.documents_dir			):
            raise FileNotFoundError(
                '''We could not find the directory you specified for the documents to summarize, or it was empty. Please'''
                ''' specify a valid path.'''			)
      os.makedirs(args.summaries_output_dir							,		exist_ok=lowerCAmelCase_			)

      evaluate(lowerCAmelCase_			)



def      __lowercase       (     a			:					Optional[int]			)       ->       Union[str, Any]:


      if not os.path.exists(lowerCAmelCase_			):
            return False

      __snake_case   :     Any	      =os.listdir(lowerCAmelCase_			)
      if len(lowerCAmelCase_			) == 0:
            return False

      return True


if __name__ == "__main__":
    main()







'''simple docstring'''






import qiskit
from qiskit.providers import Backend




def        snake_case_   (	lowerCAmelCase_			,  lowerCAmelCase_						)->						tuple[qiskit.QuantumCircuit, str, str]:





       '''simple docstring'''


       _UpperCAmelCase	,_UpperCAmelCase    :		Optional[Any]     	=					bin(lowerCAmelCase_						)[2:], bin(lowerCAmelCase_						)[2:]  # Remove leading '0b'

       # Ensure that both strings are of the same length
       if len(lowerCAmelCase_						) > len(lowerCAmelCase_						):
              _UpperCAmelCase    :		str     	=					y.zfill(len(lowerCAmelCase_						)						)
       else:
              _UpperCAmelCase    :		int     	=					x.zfill(len(lowerCAmelCase_						)						)

       # We need (3 * number of bits in the larger number)+1 qBits
       # The second parameter is the number of classical registers, to measure the result
       _UpperCAmelCase    :		Any     	=					qiskit.QuantumCircuit((len(lowerCAmelCase_						) * 3) + 1			,  len(lowerCAmelCase_						) + 1						)

       # We are essentially "not-ing" the bits that are 1
       # Reversed because it's easier to perform ops on more significant bits
       for i in range(len(lowerCAmelCase_						)						):
              if x[::-1][i] == "1":
                     circuit.x(lowerCAmelCase_						)
       for j in range(len(lowerCAmelCase_						)						):
              if y[::-1][j] == "1":
                     circuit.x(len(lowerCAmelCase_						) + j						)

       return circuit, x, y




def        snake_case_   (	lowerCAmelCase_			,  lowerCAmelCase_			,  lowerCAmelCase_			,  lowerCAmelCase_			,  lowerCAmelCase_			,  )->						Dict:





       '''simple docstring'''


       circuit.ccx(lowerCAmelCase_			,  lowerCAmelCase_			,  lowerCAmelCase_						)
       circuit.cx(lowerCAmelCase_			,  lowerCAmelCase_						)
       circuit.ccx(lowerCAmelCase_			,  lowerCAmelCase_			,  lowerCAmelCase_						)
       circuit.cx(lowerCAmelCase_			,  lowerCAmelCase_						)
       circuit.cx(lowerCAmelCase_			,  lowerCAmelCase_						)




def        snake_case_   (	lowerCAmelCase_			,  lowerCAmelCase_			,  lowerCAmelCase_ = qiskit.Aer.get_backend("""aer_simulator"""						)			,  )->						int:





       '''simple docstring'''


       if vala < 0 or vala < 0:
              raise ValueError("""Both Integers must be positive!"""						)

       # Store the Integers
       _UpperCAmelCase	,_UpperCAmelCase	,_UpperCAmelCase    :		Union[str, Any]     	=					store_two_classics(lowerCAmelCase_			,  lowerCAmelCase_						)
       for i in range(len(lowerCAmelCase_						)						):
              full_adder(lowerCAmelCase_			,  lowerCAmelCase_			,  len(lowerCAmelCase_						) + i			,  len(lowerCAmelCase_						) + len(lowerCAmelCase_						) + i			,  len(lowerCAmelCase_						) + len(lowerCAmelCase_						) + i + 1						)
              circuit.barrier()  # Optional, just for aesthetics

       # Measure the resultant qBits
       for i in range(len(lowerCAmelCase_						) + 1						):
              circuit.measure([(len(lowerCAmelCase_						) * 2) + i]			,  [i]						)

       _UpperCAmelCase    :		Optional[int]     	=					qiskit.execute(lowerCAmelCase_			,  lowerCAmelCase_			,  shots=1						).result()

       # The result is in binary. Convert it back to int
       return int(list(res.get_counts()						)[0]			,  2						)


if __name__ == "__main__":
   import doctest

   doctest.testmod()



