



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






@is_pipeline_test
class       UpperCamelCase__			(   unittest.TestCase      ):

			"""simple docstring"""


			A__	:					Union[str, Any]							  =     MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
			A__	:					List[str]							  =     TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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


			@require_torch
			def   snake_case__	(      self		)      ->	Any:
				A__											=					pipeline(
				    task="text-classification"						,	model="hf-internal-testing/tiny-random-distilbert"						,	framework="pt"		)

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

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

				A__											=					text_classifier(["This is great !", "This is bad"]						,	top_k=2		)
				self.assertEqual(
				    nested_simplify(SCREAMING_SNAKE_CASE__		)						,	[
				        [{"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__											=					text_classifier("This is great !"						,	top_k=1		)
				self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE__		)						,	[{"label": "LABEL_0", "score": 0.5_0_4}]		)

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

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

				A__											=					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.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__											=					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.5_0_4},
				        {"label": "LABEL_0", "score": 0.5_0_4},
				    ]						,	)


			@require_torch
			def   snake_case__	(      self		)      ->	List[Any]:
				import torch

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

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


			@require_tf
			def   snake_case__	(      self		)      ->	Tuple:
				A__											=					pipeline(
				    task="text-classification"						,	model="hf-internal-testing/tiny-random-distilbert"						,	framework="tf"		)

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


			@slow
			@require_torch
			def   snake_case__	(      self		)      ->	Union[str, Any]:
				A__											=					pipeline("text-classification"		)

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


			@slow
			@require_tf
			def   snake_case__	(      self		)      ->	List[str]:
				A__											=					pipeline("text-classification"						,	framework="tf"		)

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


			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)      ->	Optional[int]:
				A__											=					TextClassificationPipeline(model=SCREAMING_SNAKE_CASE__						,	tokenizer=SCREAMING_SNAKE_CASE__		)
				return text_classifier, ["HuggingFace is in", "This is another test"]







			def   snake_case__	(      self						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__		)      ->	Union[str, Any]:
				A__											=					text_classifier.model
				# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
				A__											=					"HuggingFace is in"
				A__											=					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()		)

				A__											=					["HuggingFace is in ", "Paris is in France"]
				A__											=					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
				A__											=					text_classifier(SCREAMING_SNAKE_CASE__						,	top_k=SCREAMING_SNAKE_CASE__		)
				A__											=					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]						,	)

				A__											=					{"text": "HuggingFace is in ", "text_pair": "Paris is in France"}
				A__											=					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.
				A__											=					[["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
				A__											=					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()		)







"""simple docstring"""





def     __SCREAMING_SNAKE_CASE    ( A_		,	A_					):
	if number < 0 or shift_amount < 0:
		raise ValueError('''both inputs must be positive integers'''					)

	lowerCAmelCase__      :		int    							=      str(bin(A_					)					)
	binary_number += "0" * shift_amount
	return binary_number

def     __SCREAMING_SNAKE_CASE    ( A_		,	A_					):
	if number < 0 or shift_amount < 0:
		raise ValueError('''both inputs must be positive integers'''					)

	lowerCAmelCase__      :		Any    							=      str(bin(A_					)					)[2:]
	if shift_amount >= len(A_					):
		return "0b0"
	lowerCAmelCase__      :		Tuple    							=      binary_number[: len(A_					) - shift_amount]
	return "0b" + shifted_binary_number

def     __SCREAMING_SNAKE_CASE    ( A_		,	A_					):
	if number >= 0:  # Get binary representation of positive number
		lowerCAmelCase__      :		Union[str, Any]    							=      '''0''' + str(bin(A_					)					).strip('''-'''					)[2:]
	else:  # Get binary (2's complement) representation of negative number
		lowerCAmelCase__      :		Optional[Any]    							=      len(bin(A_					)[3:]					)  # Find 2's complement of number
		lowerCAmelCase__      :		Optional[int]    							=      bin(abs(A_					) - (1 << binary_number_length)					)[3:]
		lowerCAmelCase__      :		int    							=      (
		    '''1''' + '''0''' * (binary_number_length - len(A_					)) + binary_number
		)

	if shift_amount >= len(A_					):
		return "0b" + binary_number[0] * len(A_					)
	return (
	    "0b"
	    + binary_number[0] * shift_amount
	    + binary_number[: len(A_					) - shift_amount]
	)


if __name__ == "__main__":
							import doctest

							doctest.testmod()




