






"""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.
_snake_case          =   {'LayoutLMv2Config', 'LayoutLMv3Config'}






@is_pipeline_test
class        UpperCamelCase			(	unittest.TestCase      ):
 UpperCamelCase					:       int			=					MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
 UpperCamelCase					:       Optional[Any]			=					TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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


 @require_torch
 def _lowercase	(     self		:						List[Any]							)					->       Optional[Any]:
   _a  :       Union[str, Any]             =	pipeline(
       task="""text-classification"""							,						model="""hf-internal-testing/tiny-random-distilbert"""							,						framework="""pt"""							)

   _a  :       Dict             =	text_classifier("""This is great !"""							)
   self.assertEqual(nested_simplify(lowercase_							)							,						[{"""label""": """LABEL_0""", """score""": 0.5_0_4}]							)

   _a  :       Dict             =	text_classifier("""This is great !"""							,						top_k=2							)
   self.assertEqual(
       nested_simplify(lowercase_							)							,						[{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}]							)

   _a  :       int             =	text_classifier(["""This is great !""", """This is bad"""]							,						top_k=2							)
   self.assertEqual(
       nested_simplify(lowercase_							)							,						[
           [{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}],
           [{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}],
       ]							,						)

   _a  :       List[Any]             =	text_classifier("""This is great !"""							,						top_k=1							)
   self.assertEqual(nested_simplify(lowercase_							)							,						[{"""label""": """LABEL_0""", """score""": 0.5_0_4}]							)

   # Legacy behavior
   _a  :       Union[str, Any]             =	text_classifier("""This is great !"""							,						return_all_scores=lowercase_							)
   self.assertEqual(nested_simplify(lowercase_							)							,						[{"""label""": """LABEL_0""", """score""": 0.5_0_4}]							)

   _a  :       Optional[Any]             =	text_classifier("""This is great !"""							,						return_all_scores=lowercase_							)
   self.assertEqual(
       nested_simplify(lowercase_							)							,						[[{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}]]							)

   _a  :       Optional[int]             =	text_classifier(["""This is great !""", """Something else"""]							,						return_all_scores=lowercase_							)
   self.assertEqual(
       nested_simplify(lowercase_							)							,						[
           [{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}],
           [{"""label""": """LABEL_0""", """score""": 0.5_0_4}, {"""label""": """LABEL_1""", """score""": 0.4_9_6}],
       ]							,						)

   _a  :       Optional[int]             =	text_classifier(["""This is great !""", """Something else"""]							,						return_all_scores=lowercase_							)
   self.assertEqual(
       nested_simplify(lowercase_							)							,						[
           {"""label""": """LABEL_0""", """score""": 0.5_0_4},
           {"""label""": """LABEL_0""", """score""": 0.5_0_4},
       ]							,						)


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

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

   _a  :       Dict             =	text_classifier("""This is great !"""							)
   self.assertEqual(nested_simplify(lowercase_							)							,						[{"""label""": """LABEL_0""", """score""": 0.5_0_4}]							)


 @require_tf
 def _lowercase	(     self		:						Any							)					->       Optional[int]:
   _a  :       Optional[Any]             =	pipeline(
       task="""text-classification"""							,						model="""hf-internal-testing/tiny-random-distilbert"""							,						framework="""tf"""							)

   _a  :       Optional[Any]             =	text_classifier("""This is great !"""							)
   self.assertEqual(nested_simplify(lowercase_							)							,						[{"""label""": """LABEL_0""", """score""": 0.5_0_4}]							)


 @slow
 @require_torch
 def _lowercase	(     self		:						List[Any]							)					->       Dict:
   _a  :       Any             =	pipeline("""text-classification"""							)

   _a  :       Tuple             =	text_classifier("""This is great !"""							)
   self.assertEqual(nested_simplify(lowercase_							)							,						[{"""label""": """POSITIVE""", """score""": 1.0}]							)
   _a  :       int             =	text_classifier("""This is bad !"""							)
   self.assertEqual(nested_simplify(lowercase_							)							,						[{"""label""": """NEGATIVE""", """score""": 1.0}]							)
   _a  :       Optional[Any]             =	text_classifier("""Birds are a type of animal"""							)
   self.assertEqual(nested_simplify(lowercase_							)							,						[{"""label""": """POSITIVE""", """score""": 0.9_8_8}]							)


 @slow
 @require_tf
 def _lowercase	(     self		:						Union[str, Any]							)					->       Dict:
   _a  :       Optional[int]             =	pipeline("""text-classification"""							,						framework="""tf"""							)

   _a  :       int             =	text_classifier("""This is great !"""							)
   self.assertEqual(nested_simplify(lowercase_							)							,						[{"""label""": """POSITIVE""", """score""": 1.0}]							)
   _a  :       List[Any]             =	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.9_8_8}]							)


 def _lowercase	(     self		:						Any							,						UpperCAmelCase__		:						Optional[int]							,						UpperCAmelCase__		:						Any							,						UpperCAmelCase__		:						Optional[int]							)					->       str:
   _a  :       Any             =	TextClassificationPipeline(model=lowercase_							,						tokenizer=lowercase_							)
   return text_classifier, ["HuggingFace is in", "This is another test"]







 def _lowercase	(     self		:						str							,						UpperCAmelCase__		:						str							,						UpperCAmelCase__		:						Tuple							)					->       Dict:
   _a  :       List[Any]             =	text_classifier.model
   # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
   _a  :       str             =	"HuggingFace is in"
   _a  :       Dict             =	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  :       Any             =	["HuggingFace is in ", "Paris is in France"]
   _a  :       List[Any]             =	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  :       Optional[int]             =	text_classifier(lowercase_							,						top_k=lowercase_							)
   _a  :       int             =	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  :       List[Any]             =	{"text": "HuggingFace is in ", "text_pair": "Paris is in France"}
   _a  :       int             =	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()							)



"""simple docstring"""





from diffusers.utils.testing_utils import require_onnxruntime


@require_onnxruntime
class A_	:




  '''simple docstring'''
  pass


