





'''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.
__lowerCamelCase    :			Union[str, Any]		= {'LayoutLMv2Config', 'LayoutLMv3Config'}







@is_pipeline_test
class 		UpperCAmelCase (					unittest.TestCase):


  """simple docstring"""







  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 	UpperCamelCase__   (					self     :       Optional[int]	)  ->    Tuple:
    _UpperCamelCase							      =pipeline(
        task='''text-classification'''				,						model='''hf-internal-testing/tiny-random-distilbert'''				,						framework='''pt'''	)

    _UpperCamelCase							      =text_classifier('''This is great !'''	)
    self.assertEqual(nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''LABEL_0''', '''score''': 0.504}]	)

    _UpperCamelCase							      =text_classifier('''This is great !'''				,						top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}]	)

    _UpperCamelCase							      =text_classifier(['''This is great !''', '''This is bad''']				,						top_k=2	)
    self.assertEqual(
        nested_simplify(UpperCamelCase__	)				,						[
            [{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}],
            [{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}],
        ]				,						)

    _UpperCamelCase							      =text_classifier('''This is great !'''				,						top_k=1	)
    self.assertEqual(nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''LABEL_0''', '''score''': 0.504}]	)

    # Legacy behavior
    _UpperCamelCase							      =text_classifier('''This is great !'''				,						return_all_scores=UpperCamelCase__	)
    self.assertEqual(nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''LABEL_0''', '''score''': 0.504}]	)

    _UpperCamelCase							      =text_classifier('''This is great !'''				,						return_all_scores=UpperCamelCase__	)
    self.assertEqual(
        nested_simplify(UpperCamelCase__	)				,						[[{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}]]	)

    _UpperCamelCase							      =text_classifier(['''This is great !''', '''Something else''']				,						return_all_scores=UpperCamelCase__	)
    self.assertEqual(
        nested_simplify(UpperCamelCase__	)				,						[
            [{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}],
            [{'''label''': '''LABEL_0''', '''score''': 0.504}, {'''label''': '''LABEL_1''', '''score''': 0.496}],
        ]				,						)

    _UpperCamelCase							      =text_classifier(['''This is great !''', '''Something else''']				,						return_all_scores=UpperCamelCase__	)
    self.assertEqual(
        nested_simplify(UpperCamelCase__	)				,						[
            {'''label''': '''LABEL_0''', '''score''': 0.504},
            {'''label''': '''LABEL_0''', '''score''': 0.504},
        ]				,						)


  @require_torch
  def 	UpperCamelCase__   (					self     :       Union[str, Any]	)  ->    int:
    import torch

    _UpperCamelCase							      =pipeline(
        task='''text-classification'''				,						model='''hf-internal-testing/tiny-random-distilbert'''				,						framework='''pt'''				,						device=torch.device('''cpu'''	)				,						)

    _UpperCamelCase							      =text_classifier('''This is great !'''	)
    self.assertEqual(nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''LABEL_0''', '''score''': 0.504}]	)


  @require_tf
  def 	UpperCamelCase__   (					self     :       Tuple	)  ->    str:
    _UpperCamelCase							      =pipeline(
        task='''text-classification'''				,						model='''hf-internal-testing/tiny-random-distilbert'''				,						framework='''tf'''	)

    _UpperCamelCase							      =text_classifier('''This is great !'''	)
    self.assertEqual(nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''LABEL_0''', '''score''': 0.504}]	)


  @slow
  @require_torch
  def 	UpperCamelCase__   (					self     :       List[Any]	)  ->    Dict:
    _UpperCamelCase							      =pipeline('''text-classification'''	)

    _UpperCamelCase							      =text_classifier('''This is great !'''	)
    self.assertEqual(nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''POSITIVE''', '''score''': 1.0}]	)
    _UpperCamelCase							      =text_classifier('''This is bad !'''	)
    self.assertEqual(nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''NEGATIVE''', '''score''': 1.0}]	)
    _UpperCamelCase							      =text_classifier('''Birds are a type of animal'''	)
    self.assertEqual(nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''POSITIVE''', '''score''': 0.988}]	)


  @slow
  @require_tf
  def 	UpperCamelCase__   (					self     :       int	)  ->    str:
    _UpperCamelCase							      =pipeline('''text-classification'''				,						framework='''tf'''	)

    _UpperCamelCase							      =text_classifier('''This is great !'''	)
    self.assertEqual(nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''POSITIVE''', '''score''': 1.0}]	)
    _UpperCamelCase							      =text_classifier('''This is bad !'''	)
    self.assertEqual(nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''NEGATIVE''', '''score''': 1.0}]	)
    _UpperCamelCase							      =text_classifier('''Birds are a type of animal'''	)
    self.assertEqual(nested_simplify(UpperCamelCase__	)				,						[{'''label''': '''POSITIVE''', '''score''': 0.988}]	)


  def 	UpperCamelCase__   (					self     :       Dict				,						UpperCamelCase__     :       Any				,						UpperCamelCase__     :       Tuple				,						UpperCamelCase__     :       Any	)  ->    Optional[Any]:
    _UpperCamelCase							      =TextClassificationPipeline(model=UpperCamelCase__				,						tokenizer=UpperCamelCase__	)
    return text_classifier, ["HuggingFace is in", "This is another test"]




  def 	UpperCamelCase__   (					self     :       List[str]				,						UpperCamelCase__     :       Optional[Any]				,						UpperCamelCase__     :       Any	)  ->    List[Any]:
    _UpperCamelCase							      =text_classifier.model
    # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
    _UpperCamelCase							      ='''HuggingFace is in'''
    _UpperCamelCase							      =text_classifier(UpperCamelCase__	)

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

    _UpperCamelCase							      =['''HuggingFace is in ''', '''Paris is in France''']
    _UpperCamelCase							      =text_classifier(UpperCamelCase__	)
    self.assertEqual(
        nested_simplify(UpperCamelCase__	)				,						[{'''label''': ANY(UpperCamelCase__	), '''score''': ANY(UpperCamelCase__	)}, {'''label''': ANY(UpperCamelCase__	), '''score''': ANY(UpperCamelCase__	)}]				,						)
    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
    _UpperCamelCase							      =text_classifier(UpperCamelCase__				,						top_k=UpperCamelCase__	)
    _UpperCamelCase							      =len(model.config.idalabel.values()	)
    self.assertEqual(
        nested_simplify(UpperCamelCase__	)				,						[[{'''label''': ANY(UpperCamelCase__	), '''score''': ANY(UpperCamelCase__	)}] * N, [{'''label''': ANY(UpperCamelCase__	), '''score''': ANY(UpperCamelCase__	)}] * N]				,						)

    _UpperCamelCase							      ={'''text''': '''HuggingFace is in ''', '''text_pair''': '''Paris is in France'''}
    _UpperCamelCase							      =text_classifier(UpperCamelCase__	)
    self.assertEqual(
        nested_simplify(UpperCamelCase__	)				,						{'''label''': ANY(UpperCamelCase__	), '''score''': ANY(UpperCamelCase__	)}				,						)
    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.
    _UpperCamelCase							      =[['''HuggingFace is in ''', '''Paris is in France''']]
    with self.assertRaises(UpperCamelCase__	):
      text_classifier(UpperCamelCase__	)

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












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


