
"""simple docstring"""






import enum
import warnings

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
				import tensorflow as tf

				from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

if is_torch_available():
				from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

__UpperCAmelCase       =  logging.get_logger(__name__)




class 			_SCREAMING_SNAKE_CASE					(			enum.Enum							):
			UpperCAmelCase_  :List[Any]				 =						0
			UpperCAmelCase_  :int				 =						1




@add_end_docstrings(A__							)
class 			_SCREAMING_SNAKE_CASE					(			A__							):
			UpperCAmelCase_  :Union[str, Any]				 =						"generated"






			def __init__(					self       ,   *__A       ,   **__A       )			->     int:
						super().__init__(*__A       ,   **__A       )

						self.check_model_type(
						    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
						    if self.framework == """tf"""
						    else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING       )






			def  __lowerCAmelCase (					self       ,   __A=None       ,   __A=None       ,   __A=None       ,   __A=None       ,   __A=None       ,   __A=None       ,   **__A       ,   )			->     str:
						lowerCAmelCase_    :List[str]			     =  {}
						if truncation is not None:
									lowerCAmelCase_    :List[Any]			     =  truncation

						lowerCAmelCase_    :Optional[int]			     =  generate_kwargs

						lowerCAmelCase_    :Optional[int]			     =  {}
						if return_tensors is not None and return_type is None:
									lowerCAmelCase_    :Optional[int]			     =  ReturnType.TENSORS if return_tensors else ReturnType.TEXT
						if return_type is not None:
									lowerCAmelCase_    :Any			     =  return_type

						if clean_up_tokenization_spaces is not None:
									lowerCAmelCase_    :str			     =  clean_up_tokenization_spaces

						if stop_sequence is not None:
									lowerCAmelCase_    :int			     =  self.tokenizer.encode(__A       ,   add_special_tokens=__A       )
									if len(__A       ) > 1:
												warnings.warn(
												    """Stopping on a multiple token sequence is not yet supported on transformers. The first token of"""
												    """ the stop sequence will be used as the stop sequence string in the interim."""       )
									lowerCAmelCase_    :Optional[Any]			     =  stop_sequence_ids[0]

						return preprocess_params, forward_params, postprocess_params






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       )			->     Optional[Any]:
						return True






			def  __lowerCAmelCase (					self       ,   *__A       ,   __A       )			->     List[Any]:
						lowerCAmelCase_    :List[str]			     =  self.model.config.prefix if self.model.config.prefix is not None else """"""
						if isinstance(args[0]       ,   __A       ):
									if self.tokenizer.pad_token_id is None:
												raise ValueError("""Please make sure that the tokenizer has a pad_token_id when using a batch input"""       )
									lowerCAmelCase_    :Dict			     =  ([prefix + arg for arg in args[0]],)
									lowerCAmelCase_    :Optional[int]			     =  True

						elif isinstance(args[0]       ,   __A       ):
									lowerCAmelCase_    :Any			     =  (prefix + args[0],)
									lowerCAmelCase_    :Optional[Any]			     =  False
						else:
									raise ValueError(
									    f""" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`"""       )
						lowerCAmelCase_    :List[str]			     =  self.tokenizer(*__A       ,   padding=__A       ,   truncation=__A       ,   return_tensors=self.framework       )
						# This is produced by tokenizers but is an invalid generate kwargs
						if "token_type_ids" in inputs:
									del inputs["token_type_ids"]
						return inputs






			def __call__(					self       ,   *__A       ,   **__A       )			->     Optional[int]:
						lowerCAmelCase_    :Any			     =  super().__call__(*__A       ,   **__A       )
						if (
						    isinstance(args[0]       ,   __A       )
						    and all(isinstance(__A       ,   __A       ) for el in args[0]       )
						    and all(len(__A       ) == 1 for res in result       )
						):
									return [res[0] for res in result]
						return result






			def  __lowerCAmelCase (					self       ,   __A       ,   __A=TruncationStrategy.DO_NOT_TRUNCATE       ,   **__A       )			->     Tuple:
						lowerCAmelCase_    :Union[str, Any]			     =  self._parse_and_tokenize(__A       ,   truncation=__A       ,   **__A       )
						return inputs






			def  __lowerCAmelCase (					self       ,   __A       ,   **__A       )			->     str:
						if self.framework == "pt":
									lowerCAmelCase_     ,       lowerCAmelCase_    :List[str]			     =  model_inputs["""input_ids"""].shape
						elif self.framework == "tf":
									lowerCAmelCase_     ,       lowerCAmelCase_    :Optional[int]			     =  tf.shape(model_inputs["""input_ids"""]       ).numpy()

						lowerCAmelCase_    :Optional[Any]			     =  generate_kwargs.get("""min_length"""       ,   self.model.config.min_length       )
						lowerCAmelCase_    :Union[str, Any]			     =  generate_kwargs.get("""max_length"""       ,   self.model.config.max_length       )
						self.check_inputs(__A       ,   generate_kwargs["""min_length"""]       ,   generate_kwargs["""max_length"""]       )
						lowerCAmelCase_    :Optional[Any]			     =  self.model.generate(**__A       ,   **__A       )
						lowerCAmelCase_    :Optional[Any]			     =  output_ids.shape[0]
						if self.framework == "pt":
									lowerCAmelCase_    :str			     =  output_ids.reshape(__A       ,   out_b // in_b       ,   *output_ids.shape[1:]       )
						elif self.framework == "tf":
									lowerCAmelCase_    :Tuple			     =  tf.reshape(__A       ,   (in_b, out_b // in_b, *output_ids.shape[1:])       )
						return {"output_ids": output_ids}






			def  __lowerCAmelCase (					self       ,   __A       ,   __A=ReturnType.TEXT       ,   __A=False       )			->     List[str]:
						lowerCAmelCase_    :int			     =  []
						for output_ids in model_outputs["output_ids"][0]:
									if return_type == ReturnType.TENSORS:
												lowerCAmelCase_    :Optional[Any]			     =  {f"""{self.return_name}_token_ids""": output_ids}
									elif return_type == ReturnType.TEXT:
												lowerCAmelCase_    :str			     =  {
												    f"""{self.return_name}_text""": self.tokenizer.decode(
												        __A       ,   skip_special_tokens=__A       ,   clean_up_tokenization_spaces=__A       ,   )
												}
									records.append(__A       )
						return records




@add_end_docstrings(A__							)
class 			_SCREAMING_SNAKE_CASE					(			A__							):
			UpperCAmelCase_  :Any				 =						"summary"






			def __call__(					self       ,   *__A       ,   **__A       )			->     Tuple:
						return super().__call__(*__A       ,   **__A       )






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       )			->     bool:
						if max_length < min_length:
									logger.warning(f"""Your min_length={min_length} must be inferior than your max_length={max_length}."""       )

						if input_length < max_length:
									logger.warning(
									    f"""Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is """
									    """a summarization task, where outputs shorter than the input are typically wanted, you might """
									    f"""consider decreasing max_length manually, e.g. summarizer('...', max_length={input_length//2})"""       )




@add_end_docstrings(A__							)
class 			_SCREAMING_SNAKE_CASE					(			A__							):
			UpperCAmelCase_  :List[str]				 =						"translation"






			def  __lowerCAmelCase (					self       ,   __A       ,   __A       ,   __A       )			->     Dict:
						if input_length > 0.9 * max_length:
									logger.warning(
									    f"""Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider """
									    """increasing your max_length manually, e.g. translator('...', max_length=400)"""       )
						return True






			def  __lowerCAmelCase (					self       ,   *__A       ,   __A=TruncationStrategy.DO_NOT_TRUNCATE       ,   __A=None       ,   __A=None       )			->     Union[str, Any]:
						if getattr(self.tokenizer       ,   """_build_translation_inputs"""       ,   __A       ):
									return self.tokenizer._build_translation_inputs(
									    *__A       ,   return_tensors=self.framework       ,   truncation=__A       ,   src_lang=__A       ,   tgt_lang=__A       )
						else:
									return super()._parse_and_tokenize(*__A       ,   truncation=__A       )






			def  __lowerCAmelCase (					self       ,   __A=None       ,   __A=None       ,   **__A       )			->     Dict:
						lowerCAmelCase_     ,       lowerCAmelCase_     ,       lowerCAmelCase_    :Union[str, Any]			     =  super()._sanitize_parameters(**__A       )
						if src_lang is not None:
									lowerCAmelCase_    :List[str]			     =  src_lang
						if tgt_lang is not None:
									lowerCAmelCase_    :Tuple			     =  tgt_lang
						if src_lang is None and tgt_lang is None:
									# Backward compatibility, direct arguments use is preferred.
									lowerCAmelCase_    :Dict			     =  kwargs.get("""task"""       ,   self.task       )
									lowerCAmelCase_    :Any			     =  task.split("""_"""       )
									if task and len(__A       ) == 4:
												# translation, XX, to YY
												lowerCAmelCase_    :Optional[int]			     =  items[1]
												lowerCAmelCase_    :Optional[Any]			     =  items[3]
						return preprocess_params, forward_params, postprocess_params






			def __call__(					self       ,   *__A       ,   **__A       )			->     str:
						return super().__call__(*__A       ,   **__A       )











'''simple docstring'''


import absl  # noqa: F401 # Here to have a nice missing dependency error message early on
import nltk  # noqa: F401 # Here to have a nice missing dependency error message early on
import numpy  # noqa: F401 # Here to have a nice missing dependency error message early on
import six  # noqa: F401 # Here to have a nice missing dependency error message early on
from rouge_score import rouge_scorer, scoring

import datasets


_lowerCamelCase	:       List[str]      	=	"\\n@inproceedings{lin-2004-rouge,\n    title = \"{ROUGE}: A Package for Automatic Evaluation of Summaries\",\n    author = \"Lin, Chin-Yew\",\n    booktitle = \"Text Summarization Branches Out\",\n    month = jul,\n    year = \"2004\",\n    address = \"Barcelona, Spain\",\n    publisher = \"Association for Computational Linguistics\",\n    url = \"https://www.aclweb.org/anthology/W04-1013\",\n    pages = \"74--81\",\n}\n"

_lowerCamelCase	:       Optional[int]      	=	"\\nROUGE, or Recall-Oriented Understudy for Gisting Evaluation, is a set of metrics and a software package used for\nevaluating automatic summarization and machine translation software in natural language processing.\nThe metrics compare an automatically produced summary or translation against a reference or a set of references (human-produced) summary or translation.\n\nNote that ROUGE is case insensitive, meaning that upper case letters are treated the same way as lower case letters.\n\nThis metrics is a wrapper around Google Research reimplementation of ROUGE:\nhttps://github.com/google-research/google-research/tree/master/rouge\n"

_lowerCamelCase	:       str      	=	"\nCalculates average rouge scores for a list of hypotheses and references\nArgs:\n    predictions: list of predictions to score. Each prediction\n        should be a string with tokens separated by spaces.\n    references: list of reference for each prediction. Each\n        reference should be a string with tokens separated by spaces.\n    rouge_types: A list of rouge types to calculate.\n        Valid names:\n        `\"rouge{n}\"` (e.g. `\"rouge1\"`, `\"rouge2\"`) where: {n} is the n-gram based scoring,\n        `\"rougeL\"`: Longest common subsequence based scoring.\n        `\"rougeLSum\"`: rougeLsum splits text using `\"\n\"`.\n        See details in https://github.com/huggingface/datasets/issues/617\n    use_stemmer: Bool indicating whether Porter stemmer should be used to strip word suffixes.\n    use_aggregator: Return aggregates if this is set to True\nReturns:\n    rouge1: rouge_1 (precision, recall, f1),\n    rouge2: rouge_2 (precision, recall, f1),\n    rougeL: rouge_l (precision, recall, f1),\n    rougeLsum: rouge_lsum (precision, recall, f1)\nExamples:\n\n    >>> rouge = datasets.load_metric('rouge')\n    >>> predictions = [\"hello there\", \"general kenobi\"]\n    >>> references = [\"hello there\", \"general kenobi\"]\n    >>> results = rouge.compute(predictions=predictions, references=references)\n    >>> print(list(results.keys()))\n    ['rouge1', 'rouge2', 'rougeL', 'rougeLsum']\n    >>> print(results[\"rouge1\"])\n    AggregateScore(low=Score(precision=1.0, recall=1.0, fmeasure=1.0), mid=Score(precision=1.0, recall=1.0, fmeasure=1.0), high=Score(precision=1.0, recall=1.0, fmeasure=1.0))\n    >>> print(results[\"rouge1\"].mid.fmeasure)\n    1.0\n"

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION   ,						_KWARGS_DESCRIPTION		)
class  SCREAMING_SNAKE_CASE (  datasets.Metric		):






    """simple docstring"""





    def  A       (  self      :					Union[str, Any] ):


       """simple docstring"""







       return datasets.MetricInfo(
           description=_DESCRIPTION  ,						citation=_CITATION  ,						inputs_description=_KWARGS_DESCRIPTION  ,						features=datasets.Features(
               {
                   'predictions': datasets.Value('string'  ,						id='sequence' ),
                   'references': datasets.Value('string'  ,						id='sequence' ),
               } )  ,						codebase_urls=['https://github.com/google-research/google-research/tree/master/rouge']  ,						reference_urls=[
               'https://en.wikipedia.org/wiki/ROUGE_(metric)',
               'https://github.com/google-research/google-research/tree/master/rouge',
           ]  ,						)






    def  A       (  self      :					Tuple  ,						UpperCamelCase__      :					Tuple  ,						UpperCamelCase__      :					Dict  ,						UpperCamelCase__      :					List[str]=None  ,						UpperCamelCase__      :					List[Any]=True  ,						UpperCamelCase__      :					Optional[Any]=False ):


       """simple docstring"""







       if rouge_types is None:
          UpperCamelCase					=  ['rouge1', 'rouge2', 'rougeL', 'rougeLsum']

       UpperCamelCase					=  rouge_scorer.RougeScorer(rouge_types=UpperCamelCase__  ,						use_stemmer=UpperCamelCase__ )
       if use_aggregator:
          UpperCamelCase					=  scoring.BootstrapAggregator()
       else:
          UpperCamelCase					=  []

       for ref, pred in zip(UpperCamelCase__  ,						UpperCamelCase__ ):
          UpperCamelCase					=  scorer.score(UpperCamelCase__  ,						UpperCamelCase__ )
          if use_aggregator:
             aggregator.add_scores(UpperCamelCase__ )
          else:
             scores.append(UpperCamelCase__ )

       if use_aggregator:
          UpperCamelCase					=  aggregator.aggregate()
       else:
          UpperCamelCase					=  {}
          for key in scores[0]:
             UpperCamelCase					=  [score[key] for score in scores]

       return result

