


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



@is_pipeline_test
class    UpperCAmelCase_	(							unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase      :     List[Any]							=					MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
       __UpperCamelCase      :     Optional[Any]							=					TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

       if model_mapping is not None:
              __UpperCamelCase      :     Tuple							=					{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
       if tf_model_mapping is not None:
              __UpperCamelCase      :     List[str]							=					{
                  config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
              }






       @require_torch
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Union[str, Any]   	=		pipeline(
                  task='''text-classification''' ,					model='''hf-internal-testing/tiny-random-distilbert''' ,					framework='''pt'''						)

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

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

              UpperCamelCase    :	List[Any]   	=		text_classifier(['''This is great !''', '''This is bad'''] ,					top_k=2						)
              self.assertEqual(
                  nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[
                      [{'''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    :	Optional[Any]   	=		text_classifier('''This is great !''' ,					top_k=1						)
              self.assertEqual(nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[{'''label''': '''LABEL_0''', '''score''': 0.504}]						)

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

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

              UpperCamelCase    :	str   	=		text_classifier(['''This is great !''', '''Something else'''] ,					return_all_scores=__SCREAMING_SNAKE_CASE						)
              self.assertEqual(
                  nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[
                      [{'''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    :	List[str]   	=		text_classifier(['''This is great !''', '''Something else'''] ,					return_all_scores=__SCREAMING_SNAKE_CASE						)
              self.assertEqual(
                  nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[
                      {'''label''': '''LABEL_0''', '''score''': 0.504},
                      {'''label''': '''LABEL_0''', '''score''': 0.504},
                  ] ,					)






       @require_torch
       def  _lowercase   (     self						):




              """simple docstring"""




              import torch

              UpperCamelCase    :	Optional[int]   	=		pipeline(
                  task='''text-classification''' ,					model='''hf-internal-testing/tiny-random-distilbert''' ,					framework='''pt''' ,					device=torch.device('''cpu'''						) ,					)

              UpperCamelCase    :	List[Any]   	=		text_classifier('''This is great !'''						)
              self.assertEqual(nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[{'''label''': '''LABEL_0''', '''score''': 0.504}]						)






       @require_tf
       def  _lowercase   (     self						):




              """simple docstring"""




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

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






       @slow
       @require_torch
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[int]   	=		pipeline('''text-classification'''						)

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






       @slow
       @require_tf
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	List[str]   	=		pipeline('''text-classification''' ,					framework='''tf'''						)

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






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




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






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		text_classifier.model
              # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
              UpperCamelCase    :	Tuple   	=		'''HuggingFace is in'''
              UpperCamelCase    :	Tuple   	=		text_classifier(__SCREAMING_SNAKE_CASE						)

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

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

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

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





import re
from pathlib import Path
from unittest import TestCase

import pytest



@pytest.mark.integration
class    UpperCAmelCase_	(							_a):
       '''simple docstring'''






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              with open(__SCREAMING_SNAKE_CASE ,					encoding='''utf-8'''						) as input_file:
                     UpperCamelCase    :	str   	=		re.compile(R'''(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)'''						)
                     UpperCamelCase    :	Optional[int]   	=		input_file.read()
                     UpperCamelCase    :	Union[str, Any]   	=		regexp.search(__SCREAMING_SNAKE_CASE						)

              return match






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              with open(__SCREAMING_SNAKE_CASE ,					encoding='''utf-8'''						) as input_file:
                     UpperCamelCase    :	Optional[int]   	=		re.compile(R'''#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()''' ,					re.DOTALL						)
                     UpperCamelCase    :	Tuple   	=		input_file.read()
                     # use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search`
                     UpperCamelCase    :	Dict   	=		regexp.finditer(__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	int   	=		[match for match in matches if match is not None and match.group(1						) is not None]
              return matches[0] if matches else None






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	int   	=		Path('''./datasets'''						)
              UpperCamelCase    :	Tuple   	=		list(dataset_paths.absolute().glob('''**/*.py'''						)						)

              for dataset in dataset_files:
                     if self._no_encoding_on_file_open(str(__SCREAMING_SNAKE_CASE						)						):
                            raise AssertionError(f"""open(...) must use utf-8 encoding in {dataset}"""						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[int]   	=		Path('''./datasets'''						)
              UpperCamelCase    :	Tuple   	=		list(dataset_paths.absolute().glob('''**/*.py'''						)						)

              for dataset in dataset_files:
                     if self._no_print_statements(str(__SCREAMING_SNAKE_CASE						)						):
                            raise AssertionError(f"""print statement found in {dataset}. Use datasets.logger/logging instead."""						)

