'''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.
UpperCamelCase__ :			Tuple												=				{'''LayoutLMv2Config''', '''LayoutLMv3Config'''}






@is_pipeline_test
class 		_UpperCamelCase						(							unittest.TestCase					):


			'''simple docstring'''

			_A   :	Union[str, Any]    					=	MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
			_A   :	Union[str, Any]    					=	TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

			if model_mapping is not None:
						_A   :	Union[str, Any]    					=	{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
			if tf_model_mapping is not None:
						_A   :	int    					=	{
						    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
						}

			@require_torch
			def UpperCamelCase__      (							self  :				Optional[int]		):





					"""simple docstring"""






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

					__SCREAMING_SNAKE_CASE :	str					 =	text_classifier("""This is great !"""		)
					self.assertEqual(nested_simplify(lowerCAmelCase__		)							,  [{"""label""": """LABEL_0""", """score""": 0.5_04}]		)

					__SCREAMING_SNAKE_CASE :	Union[str, Any]					 =	text_classifier("""This is great !"""							,  top_k=2		)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__		)							,  [{"""label""": """LABEL_0""", """score""": 0.5_04}, {"""label""": """LABEL_1""", """score""": 0.4_96}]		)

					__SCREAMING_SNAKE_CASE :	str					 =	text_classifier(["""This is great !""", """This is bad"""]							,  top_k=2		)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__		)							,  [
					        [{"""label""": """LABEL_0""", """score""": 0.5_04}, {"""label""": """LABEL_1""", """score""": 0.4_96}],
					        [{"""label""": """LABEL_0""", """score""": 0.5_04}, {"""label""": """LABEL_1""", """score""": 0.4_96}],
					    ]							,  )

					__SCREAMING_SNAKE_CASE :	List[str]					 =	text_classifier("""This is great !"""							,  top_k=1		)
					self.assertEqual(nested_simplify(lowerCAmelCase__		)							,  [{"""label""": """LABEL_0""", """score""": 0.5_04}]		)

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

					__SCREAMING_SNAKE_CASE :	Tuple					 =	text_classifier("""This is great !"""							,  return_all_scores=lowerCAmelCase__		)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__		)							,  [[{"""label""": """LABEL_0""", """score""": 0.5_04}, {"""label""": """LABEL_1""", """score""": 0.4_96}]]		)

					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	text_classifier(["""This is great !""", """Something else"""]							,  return_all_scores=lowerCAmelCase__		)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__		)							,  [
					        [{"""label""": """LABEL_0""", """score""": 0.5_04}, {"""label""": """LABEL_1""", """score""": 0.4_96}],
					        [{"""label""": """LABEL_0""", """score""": 0.5_04}, {"""label""": """LABEL_1""", """score""": 0.4_96}],
					    ]							,  )

					__SCREAMING_SNAKE_CASE :	Dict					 =	text_classifier(["""This is great !""", """Something else"""]							,  return_all_scores=lowerCAmelCase__		)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__		)							,  [
					        {"""label""": """LABEL_0""", """score""": 0.5_04},
					        {"""label""": """LABEL_0""", """score""": 0.5_04},
					    ]							,  )

			@require_torch
			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					import torch

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

					__SCREAMING_SNAKE_CASE :	List[str]					 =	text_classifier("""This is great !"""		)
					self.assertEqual(nested_simplify(lowerCAmelCase__		)							,  [{"""label""": """LABEL_0""", """score""": 0.5_04}]		)

			@require_tf
			def UpperCamelCase__      (							self  :				Tuple		):





					"""simple docstring"""






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

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	text_classifier("""This is great !"""		)
					self.assertEqual(nested_simplify(lowerCAmelCase__		)							,  [{"""label""": """LABEL_0""", """score""": 0.5_04}]		)

			@slow
			@require_torch
			def UpperCamelCase__      (							self  :				Union[str, Any]		):





					"""simple docstring"""






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

					__SCREAMING_SNAKE_CASE :	List[str]					 =	text_classifier("""This is great !"""		)
					self.assertEqual(nested_simplify(lowerCAmelCase__		)							,  [{"""label""": """POSITIVE""", """score""": 1.0}]		)
					__SCREAMING_SNAKE_CASE :	int					 =	text_classifier("""This is bad !"""		)
					self.assertEqual(nested_simplify(lowerCAmelCase__		)							,  [{"""label""": """NEGATIVE""", """score""": 1.0}]		)
					__SCREAMING_SNAKE_CASE :	int					 =	text_classifier("""Birds are a type of animal"""		)
					self.assertEqual(nested_simplify(lowerCAmelCase__		)							,  [{"""label""": """POSITIVE""", """score""": 0.9_88}]		)

			@slow
			@require_tf
			def UpperCamelCase__      (							self  :				List[Any]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	pipeline("""text-classification"""							,  framework="""tf"""		)

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

			def UpperCamelCase__      (							self  :				Union[str, Any]							,  lowerCAmelCase__  :				List[str]							,  lowerCAmelCase__  :				Union[str, Any]							,  lowerCAmelCase__  :				Dict		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Tuple					 =	TextClassificationPipeline(model=lowerCAmelCase__							,  tokenizer=lowerCAmelCase__		)
					return text_classifier, ["HuggingFace is in", "This is another test"]



			def UpperCamelCase__      (							self  :				List[Any]							,  lowerCAmelCase__  :				Optional[int]							,  lowerCAmelCase__  :				Optional[int]		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	Any					 =	text_classifier.model
					# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
					__SCREAMING_SNAKE_CASE :	Dict					 =	"""HuggingFace is in"""
					__SCREAMING_SNAKE_CASE :	int					 =	text_classifier(lowerCAmelCase__		)

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

					__SCREAMING_SNAKE_CASE :	List[Any]					 =	["""HuggingFace is in """, """Paris is in France"""]
					__SCREAMING_SNAKE_CASE :	int					 =	text_classifier(lowerCAmelCase__		)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__		)							,  [{"""label""": ANY(lowerCAmelCase__		), """score""": ANY(lowerCAmelCase__		)}, {"""label""": ANY(lowerCAmelCase__		), """score""": ANY(lowerCAmelCase__		)}]							,  )
					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
					__SCREAMING_SNAKE_CASE :	Any					 =	text_classifier(lowerCAmelCase__							,  top_k=lowerCAmelCase__		)
					__SCREAMING_SNAKE_CASE :	Optional[int]					 =	len(model.config.idalabel.values()		)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__		)							,  [[{"""label""": ANY(lowerCAmelCase__		), """score""": ANY(lowerCAmelCase__		)}] * N, [{"""label""": ANY(lowerCAmelCase__		), """score""": ANY(lowerCAmelCase__		)}] * N]							,  )

					__SCREAMING_SNAKE_CASE :	Any					 =	{"""text""": """HuggingFace is in """, """text_pair""": """Paris is in France"""}
					__SCREAMING_SNAKE_CASE :	int					 =	text_classifier(lowerCAmelCase__		)
					self.assertEqual(
					    nested_simplify(lowerCAmelCase__		)							,  {"""label""": ANY(lowerCAmelCase__		), """score""": ANY(lowerCAmelCase__		)}							,  )
					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.
					__SCREAMING_SNAKE_CASE :	Dict					 =	[["""HuggingFace is in """, """Paris is in France"""]]
					with self.assertRaises(lowerCAmelCase__		):
							text_classifier(lowerCAmelCase__		)

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




'''simple docstring'''




from sklearn.metrics import mean_squared_error

import datasets


UpperCamelCase__ :			Optional[Any]												=				'''\
@article{scikit-learn,
 title={Scikit-learn: Machine Learning in {P}ython},
 author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
 journal={Journal of Machine Learning Research},
 volume={12},
 pages={2825--2830},
 year={2011}
}
'''

UpperCamelCase__ :			List[str]												=				'''\
Mean Squared Error(MSE) is the average of the square of difference between the predicted
and actual values.
'''


UpperCamelCase__ :			Dict												=				'''
Args:
    predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)
        Estimated target values.
    references: array-like of shape (n_samples,) or (n_samples, n_outputs)
        Ground truth (correct) target values.
    sample_weight: array-like of shape (n_samples,), default=None
        Sample weights.
    multioutput: {"raw_values", "uniform_average"} or array-like of shape (n_outputs,), default="uniform_average"
        Defines aggregating of multiple output values. Array-like value defines weights used to average errors.

        "raw_values" : Returns a full set of errors in case of multioutput input.

        "uniform_average" : Errors of all outputs are averaged with uniform weight.

    squared : bool, default=True
        If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value.

Returns:
    mse : mean squared error.
Examples:

    >>> mse_metric = datasets.load_metric("mse")
    >>> predictions = [2.5, 0.0, 2, 8]
    >>> references = [3, -0.5, 2, 7]
    >>> results = mse_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'mse\': 0.375}
    >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False)
    >>> print(rmse_result)
    {\'mse\': 0.6123724356957945}

    If you\'re using multi-dimensional lists, then set the config as follows :

    >>> mse_metric = datasets.load_metric("mse", "multilist")
    >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]
    >>> references = [[0, 2], [-1, 2], [8, -5]]
    >>> results = mse_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'mse\': 0.7083333333333334}
    >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput=\'raw_values\')
    >>> print(results) # doctest: +NORMALIZE_WHITESPACE
    {\'mse\': array([0.41666667, 1. ])}
'''






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


			'''simple docstring'''

			def UpperCamelCase__      (							self  :				str		):





					"""simple docstring"""






					return datasets.MetricInfo(
					    description=_DESCRIPTION							,  citation=_CITATION							,  inputs_description=_KWARGS_DESCRIPTION							,  features=datasets.Features(self._get_feature_types()		)							,  reference_urls=[
					        """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html"""
					    ]							,  )

			def UpperCamelCase__      (							self  :				Optional[Any]		):





					"""simple docstring"""






					if self.config_name == "multilist":
							return {
							    "predictions": datasets.Sequence(datasets.Value("""float"""		)		),
							    "references": datasets.Sequence(datasets.Value("""float"""		)		),
							}
					else:
							return {
							    "predictions": datasets.Value("""float"""		),
							    "references": datasets.Value("""float"""		),
							}



			def UpperCamelCase__      (							self  :				List[Any]							,  lowerCAmelCase__  :				Any							,  lowerCAmelCase__  :				str							,  lowerCAmelCase__  :				List[Any]=None							,  lowerCAmelCase__  :				Optional[int]="uniform_average"							,  lowerCAmelCase__  :				int=True		):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE :	str					 =	mean_squared_error(
					    lowerCAmelCase__							,  lowerCAmelCase__							,  sample_weight=lowerCAmelCase__							,  multioutput=lowerCAmelCase__							,  squared=lowerCAmelCase__		)

					return {"mse": mse}