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

SCREAMING_SNAKE_CASE									=						logging.get_logger(__name__)



class  _lowerCamelCase			(enum.Enum     ):
				_snake_case     =  0
				_snake_case     =  1



@add_end_docstrings(__lowerCamelCase     )
class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "generated"







				def __init__( self    :       Any			,					*lowerCamelCase_    :       List[Any]			,					**lowerCamelCase_    :       Dict						):

					"""simple docstring"""






					super().__init__(*lowerCamelCase_			,					**lowerCamelCase_						)

					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    :       Union[str, Any]			,					lowerCamelCase_    :       Any=None			,					lowerCamelCase_    :       Tuple=None			,					lowerCamelCase_    :       Optional[int]=None			,					lowerCamelCase_    :       Optional[Any]=None			,					lowerCamelCase_    :       int=None			,					lowerCamelCase_    :       Optional[int]=None			,					**lowerCamelCase_    :       Optional[Any]			,					):

					"""simple docstring"""






					_lowercase :    str							=							{}
					if truncation is not None:
						_lowercase :    Union[str, Any]							=							truncation

					_lowercase :    Optional[Any]							=							generate_kwargs

					_lowercase :    Union[str, Any]							=							{}
					if return_tensors is not None and return_type is None:
						_lowercase :    Dict							=							ReturnType.TENSORS if return_tensors else ReturnType.TEXT
					if return_type is not None:
						_lowercase :    Tuple							=							return_type

					if clean_up_tokenization_spaces is not None:
						_lowercase :    Optional[int]							=							clean_up_tokenization_spaces

					if stop_sequence is not None:
						_lowercase :    str							=							self.tokenizer.encode(lowerCamelCase_			,					add_special_tokens=lowerCamelCase_						)
						if len(lowerCamelCase_						) > 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.'						)
						_lowercase :    List[Any]							=							stop_sequence_ids[0]

					return preprocess_params, forward_params, postprocess_params







				def 							__UpperCAmelCase					( self    :       int			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int						):

					"""simple docstring"""






					return True







				def 							__UpperCAmelCase					( self    :       int			,					*lowerCamelCase_    :       Any			,					lowerCamelCase_    :       Dict						):

					"""simple docstring"""






					_lowercase :    Union[str, Any]							=							self.model.config.prefix if self.model.config.prefix is not None else ''
					if isinstance(args[0]			,					lowerCamelCase_						):
						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'						)
						_lowercase :    Dict							=							([prefix + arg for arg in args[0]],)
						_lowercase :    Optional[int]							=							True

					elif isinstance(args[0]			,					lowerCamelCase_						):
						_lowercase :    Any							=							(prefix + args[0],)
						_lowercase :    str							=							False
					else:
						raise ValueError(
						    F''' `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`'''						)
					_lowercase :    List[str]							=							self.tokenizer(*lowerCamelCase_			,					padding=lowerCamelCase_			,					truncation=lowerCamelCase_			,					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    :       Dict			,					*lowerCamelCase_    :       str			,					**lowerCamelCase_    :       Optional[int]						):

					"""simple docstring"""






					_lowercase :    Union[str, Any]							=							super().__call__(*lowerCamelCase_			,					**lowerCamelCase_						)
					if (
					    isinstance(args[0]			,					lowerCamelCase_						)
					    and all(isinstance(lowerCamelCase_			,					lowerCamelCase_						) for el in args[0]						)
					    and all(len(lowerCamelCase_						) == 1 for res in result						)
					):
						return [res[0] for res in result]
					return result







				def 							__UpperCAmelCase					( self    :       Union[str, Any]			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       Union[str, Any]=TruncationStrategy.DO_NOT_TRUNCATE			,					**lowerCamelCase_    :       Optional[int]						):

					"""simple docstring"""






					_lowercase :    int							=							self._parse_and_tokenize(lowerCamelCase_			,					truncation=lowerCamelCase_			,					**lowerCamelCase_						)
					return inputs







				def 							__UpperCAmelCase					( self    :       str			,					lowerCamelCase_    :       Tuple			,					**lowerCamelCase_    :       List[str]						):

					"""simple docstring"""






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

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







				def 							__UpperCAmelCase					( self    :       Tuple			,					lowerCamelCase_    :       str			,					lowerCamelCase_    :       str=ReturnType.TEXT			,					lowerCamelCase_    :       Union[str, Any]=False						):

					"""simple docstring"""






					_lowercase :    Any							=							[]
					for output_ids in model_outputs["output_ids"][0]:
						if return_type == ReturnType.TENSORS:
							_lowercase :    List[str]							=							{F'''{self.return_name}_token_ids''': output_ids}
						elif return_type == ReturnType.TEXT:
							_lowercase :    Tuple							=							{
							    F'''{self.return_name}_text''': self.tokenizer.decode(
							        lowerCamelCase_			,					skip_special_tokens=lowerCamelCase_			,					clean_up_tokenization_spaces=lowerCamelCase_			,					)
							}
						records.append(lowerCamelCase_						)
					return records



@add_end_docstrings(__lowerCamelCase     )
class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "summary"







				def __call__( self    :       Union[str, Any]			,					*lowerCamelCase_    :       Optional[Any]			,					**lowerCamelCase_    :       Tuple						):

					"""simple docstring"""






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







				def 							__UpperCAmelCase					( self    :       Dict			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int						):

					"""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(__lowerCamelCase     )
class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "translation"







				def 							__UpperCAmelCase					( self    :       Union[str, Any]			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int						):

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

					"""simple docstring"""






					if getattr(self.tokenizer			,					'_build_translation_inputs'			,					lowerCamelCase_						):
						return self.tokenizer._build_translation_inputs(
						    *lowerCamelCase_			,					return_tensors=self.framework			,					truncation=lowerCamelCase_			,					src_lang=lowerCamelCase_			,					tgt_lang=lowerCamelCase_						)
					else:
						return super()._parse_and_tokenize(*lowerCamelCase_			,					truncation=lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       Union[str, Any]			,					lowerCamelCase_    :       Any=None			,					lowerCamelCase_    :       Optional[Any]=None			,					**lowerCamelCase_    :       Dict						):

					"""simple docstring"""






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







				def __call__( self    :       List[Any]			,					*lowerCamelCase_    :       List[str]			,					**lowerCamelCase_    :       Union[str, Any]						):

					"""simple docstring"""






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




"""simple docstring"""







import doctest
import glob
import importlib
import inspect
import os
import re
from contextlib import contextmanager
from functools import wraps
from unittest.mock import patch

import numpy as np
import pytest
from absl.testing import parameterized

import datasets
from datasets import load_metric

from .utils import for_all_test_methods, local, slow


# mark all tests as integration
SCREAMING_SNAKE_CASE									=						pytest.mark.integration


SCREAMING_SNAKE_CASE									=						{'comet'}
SCREAMING_SNAKE_CASE									=						importlib.util.find_spec('fairseq') is not None

SCREAMING_SNAKE_CASE									=						{'code_eval'}
SCREAMING_SNAKE_CASE									=						os.name == 'nt'

SCREAMING_SNAKE_CASE									=						{'bertscore', 'frugalscore', 'perplexity'}
SCREAMING_SNAKE_CASE									=						importlib.util.find_spec('transformers') is not None


def      __lowerCAmelCase(		__UpperCAmelCase			):






	"""simple docstring"""





	@wraps(__UpperCAmelCase			)
	def wrapper(self     ,__UpperCAmelCase			):
		if not _has_fairseq and metric_name in REQUIRE_FAIRSEQ:
			self.skipTest('"test requires Fairseq"'			)
		else:
			test_case(self     ,__UpperCAmelCase			)

	return wrapper


def      __lowerCAmelCase(		__UpperCAmelCase			):






	"""simple docstring"""





	@wraps(__UpperCAmelCase			)
	def wrapper(self     ,__UpperCAmelCase			):
		if not _has_transformers and metric_name in REQUIRE_TRANSFORMERS:
			self.skipTest('"test requires transformers"'			)
		else:
			test_case(self     ,__UpperCAmelCase			)

	return wrapper


def      __lowerCAmelCase(		__UpperCAmelCase			):






	"""simple docstring"""





	@wraps(__UpperCAmelCase			)
	def wrapper(self     ,__UpperCAmelCase			):
		if _on_windows and metric_name in UNSUPPORTED_ON_WINDOWS:
			self.skipTest('"test not supported on Windows"'			)
		else:
			test_case(self     ,__UpperCAmelCase			)

	return wrapper


def      __lowerCAmelCase(		):






	"""simple docstring"""





	_lowercase :    int							=							[metric_dir.split(os.sep			)[-2] for metric_dir in glob.glob('./metrics/*/'			)]
	return [{"testcase_name": x, "metric_name": x} for x in metrics if x != "gleu"]  # gleu is unfinished



@parameterized.named_parameters(get_local_metric_names()     )
@for_all_test_methods(
    __lowerCamelCase						,							__lowerCamelCase						,							__lowerCamelCase     )
@local
class  _lowerCamelCase			(parameterized.TestCase     ):
				_snake_case     =  {}
				_snake_case     =  None







				@pytest.mark.filterwarnings('ignore:metric_module_factory is deprecated:FutureWarning'						)
				@pytest.mark.filterwarnings('ignore:load_metric is deprecated:FutureWarning'						)
				def 							__UpperCAmelCase					( self    :       str			,					lowerCamelCase_    :       List[str]						):

					"""simple docstring"""






					_lowercase :    Optional[Any]							=							'[...]'
					_lowercase :    str							=							importlib.import_module(
					    datasets.load.metric_module_factory(os.path.join('metrics'			,					lowerCamelCase_						)						).module_path						)
					_lowercase :    Dict							=							datasets.load.import_main_class(metric_module.__name__			,					dataset=lowerCamelCase_						)
					# check parameters
					_lowercase :    Optional[int]							=							inspect.signature(metric._compute						).parameters
					self.assertTrue(all(p.kind != p.VAR_KEYWORD for p in parameters.values()						)						)  # no **kwargs
					# run doctest
					with self.patch_intensive_calls(lowerCamelCase_			,					metric_module.__name__						):
						with self.use_local_metrics():
							try:
								_lowercase :    Optional[Any]							=							doctest.testmod(lowerCamelCase_			,					verbose=lowerCamelCase_			,					raise_on_error=lowerCamelCase_						)
							except doctest.UnexpectedException as e:
								raise e.exc_info[1]  # raise the exception that doctest caught
					self.assertEqual(results.failed			,					0						)
					self.assertGreater(results.attempted			,					1						)







				@slow
				def 							__UpperCAmelCase					( self    :       Any			,					lowerCamelCase_    :       Dict						):

					"""simple docstring"""






					_lowercase :    Optional[Any]							=							'[...]'
					_lowercase :    Dict							=							importlib.import_module(
					    datasets.load.metric_module_factory(os.path.join('metrics'			,					lowerCamelCase_						)						).module_path						)
					# run doctest
					with self.use_local_metrics():
						_lowercase :    str							=							doctest.testmod(lowerCamelCase_			,					verbose=lowerCamelCase_			,					raise_on_error=lowerCamelCase_						)
					self.assertEqual(results.failed			,					0						)
					self.assertGreater(results.attempted			,					1						)







				@contextmanager
				def 							__UpperCAmelCase					( self    :       Tuple			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       str						):

					"""simple docstring"""






					if metric_name in self.INTENSIVE_CALLS_PATCHER:
						with self.INTENSIVE_CALLS_PATCHER[metric_name](lowerCamelCase_						):
							yield
					else:
						yield







				@contextmanager
				def 							__UpperCAmelCase					( self    :       Dict						):

					"""simple docstring"""






					def load_local_metric(lowerCamelCase_    :       Optional[Any]			,					*lowerCamelCase_    :       Any			,					**lowerCamelCase_    :       Optional[int]						):
						return load_metric(os.path.join('metrics'			,					lowerCamelCase_						)			,					*lowerCamelCase_			,					**lowerCamelCase_						)

					with patch('datasets.load_metric'						) as mock_load_metric:
						_lowercase :    str							=							load_local_metric
						yield







				@classmethod
				def 							__UpperCAmelCase					( cls    :       Tuple			,					lowerCamelCase_    :       Tuple						):

					"""simple docstring"""






					def wrapper(lowerCamelCase_    :       int						):
						_lowercase :    Any							=							contextmanager(lowerCamelCase_						)
						_lowercase :    Any							=							patcher
						return patcher

					return wrapper



@LocalMetricTest.register_intensive_calls_patcher('bleurt'			)
def      __lowerCAmelCase(		__UpperCAmelCase			):






	"""simple docstring"""





	import tensorflow.compat.va as tf
	from bleurt.score import Predictor

	tf.flags.DEFINE_string('sv'     ,''     ,''			)  # handle pytest cli flags



	class  _lowerCamelCase			(__lowerCamelCase     ):







					def 							__UpperCAmelCase					( self    :       Optional[Any]			,					lowerCamelCase_    :       str						):

						"""simple docstring"""






						assert len(input_dict['input_ids']						) == 2
						return np.array([1.03, 1.04]						)

    # mock predict_fn which is supposed to do a forward pass with a bleurt model
	with patch('bleurt.score._create_predictor'			) as mock_create_predictor:
		_lowercase :    Dict							=							MockedPredictor()
		yield


@LocalMetricTest.register_intensive_calls_patcher('bertscore'			)
def      __lowerCAmelCase(		__UpperCAmelCase			):






	"""simple docstring"""





	import torch

	def bert_cos_score_idf(__UpperCAmelCase     ,__UpperCAmelCase     ,*__UpperCAmelCase     ,**__UpperCAmelCase			):
		return torch.tensor([[1.0, 1.0, 1.0]] * len(__UpperCAmelCase			)			)

	# mock get_model which is supposed to do download a bert model
	# mock bert_cos_score_idf which is supposed to do a forward pass with a bert model
	with patch('bert_score.scorer.get_model'			), patch(
	    'bert_score.scorer.bert_cos_score_idf'			) as mock_bert_cos_score_idf:
		_lowercase :    Tuple							=							bert_cos_score_idf
		yield


@LocalMetricTest.register_intensive_calls_patcher('comet'			)
def      __lowerCAmelCase(		__UpperCAmelCase			):






	"""simple docstring"""





	def load_from_checkpoint(__UpperCAmelCase			):



		class  _lowerCamelCase			:







						def 							__UpperCAmelCase					( self    :       Tuple			,					lowerCamelCase_    :       str			,					*lowerCamelCase_    :       List[Any]			,					**lowerCamelCase_    :       List[str]						):

							"""simple docstring"""






							assert len(lowerCamelCase_						) == 2
							_lowercase :    Union[str, Any]							=							[0.19, 0.92]
							return scores, sum(lowerCamelCase_						) / len(lowerCamelCase_						)



		return Model()

	# mock load_from_checkpoint which is supposed to do download a bert model
	# mock load_from_checkpoint which is supposed to do download a bert model
	with patch('comet.download_model'			) as mock_download_model:
		_lowercase :    Dict							=							None
		with patch('comet.load_from_checkpoint'			) as mock_load_from_checkpoint:
			_lowercase :    str							=							load_from_checkpoint
			yield







def      __lowerCAmelCase(		):






	"""simple docstring"""





	_lowercase :    Tuple							=							load_metric(os.path.join('metrics'     ,'seqeval'			)			)
	_lowercase :    int							=							'ERROR'
	_lowercase :    Union[str, Any]							=							F'''Scheme should be one of [IOB1, IOB2, IOE1, IOE2, IOBES, BILOU], got {wrong_scheme}'''
	with pytest.raises(__UpperCAmelCase     ,match=re.escape(__UpperCAmelCase			)			):
		metric.compute(predictions=[]     ,references=[]     ,scheme=__UpperCAmelCase			)



