




'''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

lowerCAmelCase_     						=				logging.get_logger(__name__)

class 	_snake_case(			enum.Enum      ):
 __snake_case:							Tuple						=					0
 __snake_case:							int						=					1




@add_end_docstrings(UpperCAmelCase      )
class 	_snake_case(			UpperCAmelCase      ):
 __snake_case:							str						=					'''generated'''






 def __init__(self       :  Dict    ,		*a       :  str    ,		**a       :  List[str]    )       ->   List[Any]:






    """simple docstring"""


    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 						_UpperCamelCase (self       :  str    ,		a       :  Dict=None    ,		a       :  List[str]=None    ,		a       :  Any=None    ,		a       :  str=None    ,		a       :  Union[str, Any]=None    ,		a       :  List[str]=None    ,		**a       :  Any    ,		)       ->   List[Any]:






    """simple docstring"""


    A__       =     {}
    if truncation is not None:
       A__       =     truncation

    A__       =     generate_kwargs

    A__       =     {}
    if return_tensors is not None and return_type is None:
       A__       =     ReturnType.TENSORS if return_tensors else ReturnType.TEXT
    if return_type is not None:
       A__       =     return_type

    if clean_up_tokenization_spaces is not None:
       A__       =     clean_up_tokenization_spaces

    if stop_sequence is not None:
       A__       =     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.'    )
       A__       =     stop_sequence_ids[0]

    return preprocess_params, forward_params, postprocess_params






 def 						_UpperCamelCase (self       :  int    ,		a       :  int    ,		a       :  int    ,		a       :  int    )       ->   Union[str, Any]:






    """simple docstring"""


    return True






 def 						_UpperCamelCase (self       :  Any    ,		*a       :  Any    ,		a       :  Any    )       ->   Union[str, Any]:






    """simple docstring"""


    A__       =     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'    )
       A__       =     ([prefix + arg for arg in args[0]],)
       A__       =     True

    elif isinstance(args[0]    ,		a    ):
       A__       =     (prefix + args[0],)
       A__       =     False
    else:
       raise ValueError(
           f""" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`"""    )
    A__       =     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       :  str    ,		*a       :  Optional[int]    ,		**a       :  str    )       ->   List[str]:






    """simple docstring"""


    A__       =     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 						_UpperCamelCase (self       :  Dict    ,		a       :  Any    ,		a       :  Dict=TruncationStrategy.DO_NOT_TRUNCATE    ,		**a       :  Optional[int]    )       ->   str:






    """simple docstring"""


    A__       =     self._parse_and_tokenize(a    ,		truncation=a    ,		**a    )
    return inputs






 def 						_UpperCamelCase (self       :  Tuple    ,		a       :  str    ,		**a       :  List[Any]    )       ->   List[Any]:






    """simple docstring"""


    if self.framework == "pt":
       A__  ,			A__       =     model_inputs['input_ids'].shape
    elif self.framework == "tf":
       A__  ,			A__       =     tf.shape(model_inputs['input_ids']    ).numpy()

    A__       =     generate_kwargs.get('min_length'    ,		self.model.config.min_length    )
    A__       =     generate_kwargs.get('max_length'    ,		self.model.config.max_length    )
    self.check_inputs(a    ,		generate_kwargs['min_length']    ,		generate_kwargs['max_length']    )
    A__       =     self.model.generate(**a    ,		**a    )
    A__       =     output_ids.shape[0]
    if self.framework == "pt":
       A__       =     output_ids.reshape(a    ,		out_b // in_b    ,		*output_ids.shape[1:]    )
    elif self.framework == "tf":
       A__       =     tf.reshape(a    ,		(in_b, out_b // in_b, *output_ids.shape[1:])    )
    return {"output_ids": output_ids}






 def 						_UpperCamelCase (self       :  Dict    ,		a       :  str    ,		a       :  List[Any]=ReturnType.TEXT    ,		a       :  int=False    )       ->   str:






    """simple docstring"""


    A__       =     []
    for output_ids in model_outputs["output_ids"][0]:
       if return_type == ReturnType.TENSORS:
          A__       =     {f"""{self.return_name}_token_ids""": output_ids}
       elif return_type == ReturnType.TEXT:
          A__       =     {
              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(UpperCAmelCase      )
class 	_snake_case(			UpperCAmelCase      ):
 __snake_case:							Dict						=					'''summary'''






 def __call__(self       :  Optional[Any]    ,		*a       :  Optional[int]    ,		**a       :  Optional[int]    )       ->   int:






    """simple docstring"""


    return super().__call__(*a    ,		**a    )






 def 						_UpperCamelCase (self       :  int    ,		a       :  int    ,		a       :  int    ,		a       :  int    )       ->   bool:






    """simple docstring"""


    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(UpperCAmelCase      )
class 	_snake_case(			UpperCAmelCase      ):
 __snake_case:							List[str]						=					'''translation'''






 def 						_UpperCamelCase (self       :  Tuple    ,		a       :  int    ,		a       :  int    ,		a       :  int    )       ->   Union[str, Any]:






    """simple docstring"""


    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 						_UpperCamelCase (self       :  List[str]    ,		*a       :  Optional[Any]    ,		a       :  int=TruncationStrategy.DO_NOT_TRUNCATE    ,		a       :  Optional[int]=None    ,		a       :  Optional[Any]=None    )       ->   Union[str, Any]:






    """simple docstring"""


    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 						_UpperCamelCase (self       :  List[Any]    ,		a       :  List[Any]=None    ,		a       :  List[Any]=None    ,		**a       :  Optional[int]    )       ->   List[Any]:






    """simple docstring"""


    A__  ,			A__  ,			A__       =     super()._sanitize_parameters(**a    )
    if src_lang is not None:
       A__       =     src_lang
    if tgt_lang is not None:
       A__       =     tgt_lang
    if src_lang is None and tgt_lang is None:
       # Backward compatibility, direct arguments use is preferred.
       A__       =     kwargs.get('task'    ,		self.task    )
       A__       =     task.split('_'    )
       if task and len(a    ) == 4:
          # translation, XX, to YY
          A__       =     items[1]
          A__       =     items[3]
    return preprocess_params, forward_params, postprocess_params






 def __call__(self       :  Optional[int]    ,		*a       :  List[str]    ,		**a       :  Dict    )       ->   List[str]:






    """simple docstring"""


    return super().__call__(*a    ,		**a    )








'''simple docstring'''







import re
import string

import numpy as np

import datasets


lowerCAmelCase_     						=				'''
Returns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.
'''

lowerCAmelCase_     						=				'''
Args:
    predictions: List of predicted texts.
    references: List of reference texts.
    regexes_to_ignore: List, defaults to None. Regex expressions of characters to
        ignore when calculating the exact matches. Note: these regexes are removed
        from the input data before the changes based on the options below (e.g. ignore_case,
        ignore_punctuation, ignore_numbers) are applied.
    ignore_case: Boolean, defaults to False. If true, turns everything
        to lowercase so that capitalization differences are ignored.
    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before
        comparing predictions and references.
    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before
        comparing predictions and references.
Returns:
    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.
Examples:
    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds)
    >>> print(round(results["exact_match"], 1))
    25.0

    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)
    >>> print(round(results["exact_match"], 1))
    50.0


    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)
    >>> print(round(results["exact_match"], 1))
    75.0

    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["the cat", "theater", "YELLING", "agent007"]
    >>> preds = ["cat?", "theater", "yelling", "agent"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)
    >>> print(round(results["exact_match"], 1))
    100.0

    >>> exact_match = datasets.load_metric("exact_match")
    >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]
    >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]
    >>> results = exact_match.compute(references=refs, predictions=preds)
    >>> print(round(results["exact_match"], 1))
    33.3

'''

lowerCAmelCase_     						=				'''
'''

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






 def 						_UpperCamelCase (self       :  Optional[Any]    )       ->   Dict:






    """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'    ),
            }    )    ,		reference_urls=[]    ,		)






 def 						_UpperCamelCase (self       :  List[str]    ,		a       :  str    ,		a       :  Tuple    ,		a       :  Tuple=None    ,		a       :  Dict=False    ,		a       :  Dict=False    ,		a       :  List[str]=False    ,		)       ->   int:






    """simple docstring"""


    if regexes_to_ignore is not None:
       for s in regexes_to_ignore:
          A__       =     np.array([re.sub(a    ,		''    ,		a    ) for x in predictions]    )
          A__       =     np.array([re.sub(a    ,		''    ,		a    ) for x in references]    )
    else:
       A__       =     np.asarray(a    )
       A__       =     np.asarray(a    )

    if ignore_case:
       A__       =     np.char.lower(a    )
       A__       =     np.char.lower(a    )

    if ignore_punctuation:
       A__       =     string.punctuation.maketrans(''    ,		''    ,		string.punctuation    )
       A__       =     np.char.translate(a    ,		table=a    )
       A__       =     np.char.translate(a    ,		table=a    )

    if ignore_numbers:
       A__       =     string.digits.maketrans(''    ,		''    ,		string.digits    )
       A__       =     np.char.translate(a    ,		table=a    )
       A__       =     np.char.translate(a    ,		table=a    )

    A__       =     predictions == references

    return {"exact_match": np.mean(a    ) * 1_00}


