


'''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.
__A	:	Union[str, Any]     =     {'LayoutLMv2Config', 'LayoutLMv3Config'}
@is_pipeline_test
class 						__UpperCamelCase	(						unittest.TestCase							):
							lowercase			:		str   		=  MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
							lowercase			:		List[Any]   		=  TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

							if model_mapping is not None:
														lowercase			:		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:
														lowercase			:		List[Any]   		=  {
														    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
														}





							@require_torch
							def 		a__   (  self		:str			):
									snake_case_		:							Tuple			       =						pipeline(
									    task="""text-classification"""  ,model="""hf-internal-testing/tiny-random-distilbert"""  ,framework="""pt"""			)

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

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

									snake_case_		:							Tuple			       =						text_classifier(["""This is great !""", """This is bad"""]  ,top_k=2			)
									self.assertEqual(
									    nested_simplify(_UpperCamelCase			)  ,[
									        [{"""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}],
									    ]  ,)

									snake_case_		:							Any			       =						text_classifier("""This is great !"""  ,top_k=1			)
									self.assertEqual(nested_simplify(_UpperCamelCase			)  ,[{"""label""": """LABEL_0""", """score""": 0.5_04}]			)

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

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

									snake_case_		:							int			       =						text_classifier(["""This is great !""", """Something else"""]  ,return_all_scores=_UpperCamelCase			)
									self.assertEqual(
									    nested_simplify(_UpperCamelCase			)  ,[
									        [{"""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}],
									    ]  ,)

									snake_case_		:							Optional[Any]			       =						text_classifier(["""This is great !""", """Something else"""]  ,return_all_scores=_UpperCamelCase			)
									self.assertEqual(
									    nested_simplify(_UpperCamelCase			)  ,[
									        {"""label""": """LABEL_0""", """score""": 0.5_04},
									        {"""label""": """LABEL_0""", """score""": 0.5_04},
									    ]  ,)





							@require_torch
							def 		a__   (  self		:str			):
									import torch

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

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





							@require_tf
							def 		a__   (  self		:List[Any]			):
									snake_case_		:							List[str]			       =						pipeline(
									    task="""text-classification"""  ,model="""hf-internal-testing/tiny-random-distilbert"""  ,framework="""tf"""			)

									snake_case_		:							Optional[int]			       =						text_classifier("""This is great !"""			)
									self.assertEqual(nested_simplify(_UpperCamelCase			)  ,[{"""label""": """LABEL_0""", """score""": 0.5_04}]			)





							@slow
							@require_torch
							def 		a__   (  self		:Tuple			):
									snake_case_		:							int			       =						pipeline("""text-classification"""			)

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





							@slow
							@require_tf
							def 		a__   (  self		:Optional[int]			):
									snake_case_		:							Optional[int]			       =						pipeline("""text-classification"""  ,framework="""tf"""			)

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





							def 		a__   (  self		:Any  ,_UpperCamelCase		:List[Any]  ,_UpperCamelCase		:Union[str, Any]  ,_UpperCamelCase		:Optional[Any]			):
									snake_case_		:							Tuple			       =						TextClassificationPipeline(model=_UpperCamelCase  ,tokenizer=_UpperCamelCase			)
									return text_classifier, ["HuggingFace is in", "This is another test"]





							def 		a__   (  self		:str  ,_UpperCamelCase		:Any  ,_UpperCamelCase		:Optional[int]			):
									snake_case_		:							Optional[Any]			       =						text_classifier.model
									# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
									snake_case_		:							Dict			       =						"""HuggingFace is in"""
									snake_case_		:							int			       =						text_classifier(_UpperCamelCase			)

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

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

									snake_case_		:							int			       =						{"""text""": """HuggingFace is in """, """text_pair""": """Paris is in France"""}
									snake_case_		:							str			       =						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.
									snake_case_		:							str			       =						[["""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
									snake_case_		:							Any			       =						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'''






from ...configuration_utils import PretrainedConfig
from ...utils import logging


__A	:	Optional[int]     =     logging.get_logger(__name__)

__A	:	List[Any]     =     {
    'studio-ousia/luke-base': 'https://huggingface.co/studio-ousia/luke-base/resolve/main/config.json',
    'studio-ousia/luke-large': 'https://huggingface.co/studio-ousia/luke-large/resolve/main/config.json',
}
class 						__UpperCamelCase	(						lowercase__							):
							lowercase			:		Union[str, Any]   		=  'luke'





							def __init__(  self		:List[str]  ,_UpperCamelCase		:Tuple=5_0_2_6_7  ,_UpperCamelCase		:Tuple=5_0_0_0_0_0  ,_UpperCamelCase		:str=7_6_8  ,_UpperCamelCase		:List[Any]=2_5_6  ,_UpperCamelCase		:List[str]=1_2  ,_UpperCamelCase		:str=1_2  ,_UpperCamelCase		:Union[str, Any]=3_0_7_2  ,_UpperCamelCase		:int="gelu"  ,_UpperCamelCase		:int=0.1  ,_UpperCamelCase		:Dict=0.1  ,_UpperCamelCase		:Dict=5_1_2  ,_UpperCamelCase		:Optional[Any]=2  ,_UpperCamelCase		:int=0.02  ,_UpperCamelCase		:Any=1E-1_2  ,_UpperCamelCase		:Tuple=True  ,_UpperCamelCase		:Any=None  ,_UpperCamelCase		:Any=1  ,_UpperCamelCase		:int=0  ,_UpperCamelCase		:List[Any]=2  ,**_UpperCamelCase		:str  ,):
									super().__init__(pad_token_id=_UpperCamelCase  ,bos_token_id=_UpperCamelCase  ,eos_token_id=_UpperCamelCase  ,**_UpperCamelCase			)

									snake_case_		:							Any			       =						vocab_size
									snake_case_		:							List[Any]			       =						entity_vocab_size
									snake_case_		:							Optional[Any]			       =						hidden_size
									snake_case_		:							List[Any]			       =						entity_emb_size
									snake_case_		:							Dict			       =						num_hidden_layers
									snake_case_		:							Union[str, Any]			       =						num_attention_heads
									snake_case_		:							Dict			       =						hidden_act
									snake_case_		:							Dict			       =						intermediate_size
									snake_case_		:							Any			       =						hidden_dropout_prob
									snake_case_		:							List[Any]			       =						attention_probs_dropout_prob
									snake_case_		:							Optional[Any]			       =						max_position_embeddings
									snake_case_		:							Any			       =						type_vocab_size
									snake_case_		:							int			       =						initializer_range
									snake_case_		:							Union[str, Any]			       =						layer_norm_eps
									snake_case_		:							Tuple			       =						use_entity_aware_attention
									snake_case_		:							Tuple			       =						classifier_dropout