



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


lowercase__			   =  "\\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"

lowercase__			   =  "\\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"

lowercase__			   =  "\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      ):



						def     lowerCAmelCase__(self							):





													'''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     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase=None						,     _lowercase=True						,     _lowercase=False							):





													'''simple docstring'''

													if rouge_types is None:
																				__a		:		Optional[Any]									=       ["""rouge1""", """rouge2""", """rougeL""", """rougeLsum"""]

													__a		:		Optional[int]									=       rouge_scorer.RougeScorer(rouge_types=_lowercase						,     use_stemmer=_lowercase							)
													if use_aggregator:
																				__a		:		int									=       scoring.BootstrapAggregator()
													else:
																				__a		:		List[str]									=       []

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

													if use_aggregator:
																				__a		:		Tuple									=       aggregator.aggregate()
													else:
																				__a		:		Dict									=       {}
																				for key in scores[0]:
																											__a		:		Optional[int]									=       [score[key] for score in scores]

													return result






"""simple docstring"""

import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TextClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
lowercase__			   =  {"LayoutLMv2Config", "LayoutLMv3Config"}




@is_pipeline_test
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase      ):
						_lowerCAmelCase							=					MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
						_lowerCAmelCase							=					TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

						if model_mapping is not None:
												_lowerCAmelCase							=					{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
						if tf_model_mapping is not None:
												_lowerCAmelCase							=					{
												    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
												}



						@require_torch
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		int									=       pipeline(
													    task="""text-classification"""						,     model="""hf-internal-testing/tiny-random-distilbert"""						,     framework="""pt"""							)

													__a		:		Tuple									=       text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}]							)

													__a		:		Optional[Any]									=       text_classifier("""This is great !"""						,     top_k=2							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}]							)

													__a		:		int									=       text_classifier(["""This is great !""", """This is bad"""]						,     top_k=2							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													    ]						,     )

													__a		:		List[str]									=       text_classifier("""This is great !"""						,     top_k=1							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}]							)

													# Legacy behavior
													__a		:		Optional[int]									=       text_classifier("""This is great !"""						,     return_all_scores=_lowercase							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}]							)

													__a		:		Tuple									=       text_classifier("""This is great !"""						,     return_all_scores=_lowercase							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [[{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}]]							)

													__a		:		Any									=       text_classifier(["""This is great !""", """Something else"""]						,     return_all_scores=_lowercase							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													        [{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}],
													    ]						,     )

													__a		:		Union[str, Any]									=       text_classifier(["""This is great !""", """Something else"""]						,     return_all_scores=_lowercase							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [
													        {"""label""": """LABEL_0""", """score""": 0.504},
													        {"""label""": """LABEL_0""", """score""": 0.504},
													    ]						,     )



						@require_torch
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													import torch

													__a		:		Any									=       pipeline(
													    task="""text-classification"""						,     model="""hf-internal-testing/tiny-random-distilbert"""						,     framework="""pt"""						,     device=torch.device("""cpu"""							)						,     )

													__a		:		Optional[int]									=       text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}]							)



						@require_tf
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[Any]									=       pipeline(
													    task="""text-classification"""						,     model="""hf-internal-testing/tiny-random-distilbert"""						,     framework="""tf"""							)

													__a		:		List[str]									=       text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """LABEL_0""", """score""": 0.504}]							)



						@slow
						@require_torch
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Tuple									=       pipeline("""text-classification"""							)

													__a		:		Tuple									=       text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """POSITIVE""", """score""": 1.0}]							)
													__a		:		Optional[int]									=       text_classifier("""This is bad !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """NEGATIVE""", """score""": 1.0}]							)
													__a		:		Union[str, Any]									=       text_classifier("""Birds are a type of animal"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """POSITIVE""", """score""": 0.988}]							)



						@slow
						@require_tf
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[str]									=       pipeline("""text-classification"""						,     framework="""tf"""							)

													__a		:		str									=       text_classifier("""This is great !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """POSITIVE""", """score""": 1.0}]							)
													__a		:		Tuple									=       text_classifier("""This is bad !"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """NEGATIVE""", """score""": 1.0}]							)
													__a		:		str									=       text_classifier("""Birds are a type of animal"""							)
													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": """POSITIVE""", """score""": 0.988}]							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		Dict									=       TextClassificationPipeline(model=_lowercase						,     tokenizer=_lowercase							)
													return text_classifier, ["HuggingFace is in", "This is another test"]





						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		List[str]									=       text_classifier.model
													# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
													__a		:		Union[str, Any]									=       """HuggingFace is in"""
													__a		:		List[str]									=       text_classifier(_lowercase							)

													self.assertEqual(nested_simplify(_lowercase							)						,     [{"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}]							)
													self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()							)

													__a		:		Optional[int]									=       ["""HuggingFace is in """, """Paris is in France"""]
													__a		:		Dict									=       text_classifier(_lowercase							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [{"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}, {"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}]						,     )
													self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()							)
													self.assertTrue(outputs[1]["""label"""] in model.config.idalabel.values()							)

													# Forcing to get all results with `top_k=None`
													# This is NOT the legacy format
													__a		:		Dict									=       text_classifier(_lowercase						,     top_k=_lowercase							)
													__a		:		Dict									=       len(model.config.idalabel.values()							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [[{"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}] * N, [{"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}] * N]						,     )

													__a		:		Dict									=       {"""text""": """HuggingFace is in """, """text_pair""": """Paris is in France"""}
													__a		:		Any									=       text_classifier(_lowercase							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     {"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}						,     )
													self.assertTrue(outputs["""label"""] in model.config.idalabel.values()							)

													# This might be used a text pair, but tokenizer + pipe interaction
													# makes it hard to understand that it's not using the pair properly
													# https://github.com/huggingface/transformers/issues/17305
													# We disabled this usage instead as it was outputting wrong outputs.
													__a		:		Dict									=       [["""HuggingFace is in """, """Paris is in France"""]]
													with self.assertRaises(_lowercase							):
																				text_classifier(_lowercase							)

													# This used to be valid for doing text pairs
													# We're keeping it working because of backward compatibility
													__a		:		Optional[int]									=       text_classifier([[["""HuggingFace is in """, """Paris is in France"""]]]							)
													self.assertEqual(
													    nested_simplify(_lowercase							)						,     [{"""label""": ANY(_lowercase							), """score""": ANY(_lowercase							)}]						,     )
													self.assertTrue(outputs[0]["""label"""] in model.config.idalabel.values()							)

