





'''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__:     List[Any]										=    {"LayoutLMv2Config", "LayoutLMv3Config"}

@is_pipeline_test
class        SCREAMING_SNAKE_CASE(							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   A			(		self    :  List[Any]	)    ->	Tuple:
								UpperCAmelCase				: Any											=	pipeline(
								    task='''text-classification'''	,    model='''hf-internal-testing/tiny-random-distilbert'''	,    framework='''pt'''	)

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

								UpperCAmelCase				: Optional[Any]											=	text_classifier('''This is great !'''	,    top_k=2	)
								self.assertEqual(
								    nested_simplify(lowercase_	)	,    [{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}]	)

								UpperCAmelCase				: Any											=	text_classifier(['''This is great !''', '''This is bad''']	,    top_k=2	)
								self.assertEqual(
								    nested_simplify(lowercase_	)	,    [
								        [{'''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}],
								    ]	,    )

								UpperCAmelCase				: Optional[int]											=	text_classifier('''This is great !'''	,    top_k=1	)
								self.assertEqual(nested_simplify(lowercase_	)	,    [{'''label''': '''LABEL_0''', '''score''': 0.5_04}]	)

								# Legacy behavior
								UpperCAmelCase				: Optional[int]											=	text_classifier('''This is great !'''	,    return_all_scores=lowercase_	)
								self.assertEqual(nested_simplify(lowercase_	)	,    [{'''label''': '''LABEL_0''', '''score''': 0.5_04}]	)

								UpperCAmelCase				: List[str]											=	text_classifier('''This is great !'''	,    return_all_scores=lowercase_	)
								self.assertEqual(
								    nested_simplify(lowercase_	)	,    [[{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}]]	)

								UpperCAmelCase				: Union[str, Any]											=	text_classifier(['''This is great !''', '''Something else''']	,    return_all_scores=lowercase_	)
								self.assertEqual(
								    nested_simplify(lowercase_	)	,    [
								        [{'''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}],
								    ]	,    )

								UpperCAmelCase				: str											=	text_classifier(['''This is great !''', '''Something else''']	,    return_all_scores=lowercase_	)
								self.assertEqual(
								    nested_simplify(lowercase_	)	,    [
								        {'''label''': '''LABEL_0''', '''score''': 0.5_04},
								        {'''label''': '''LABEL_0''', '''score''': 0.5_04},
								    ]	,    )







	@require_torch
	def   A			(		self    :  Dict	)    ->	Tuple:
								import torch

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

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







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

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







	@slow
	@require_torch
	def   A			(		self    :  Union[str, Any]	)    ->	int:
								UpperCAmelCase				: Tuple											=	pipeline('''text-classification'''	)

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







	@slow
	@require_tf
	def   A			(		self    :  Optional[Any]	)    ->	str:
								UpperCAmelCase				: List[str]											=	pipeline('''text-classification'''	,    framework='''tf'''	)

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







	def   A			(		self    :  Dict	,    __snake_case    :  List[Any]	,    __snake_case    :  Union[str, Any]	,    __snake_case    :  Optional[int]	)    ->	List[str]:
								UpperCAmelCase				: Dict											=	TextClassificationPipeline(model=lowercase_	,    tokenizer=lowercase_	)
								return text_classifier, ["HuggingFace is in", "This is another test"]







	def   A			(		self    :  str	,    __snake_case    :  List[str]	,    __snake_case    :  Union[str, Any]	)    ->	Any:
								UpperCAmelCase				: Tuple											=	text_classifier.model
								# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
								UpperCAmelCase				: Optional[Any]											=	"""HuggingFace is in"""
								UpperCAmelCase				: Union[str, Any]											=	text_classifier(lowercase_	)

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

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

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






import pytest

from datasets.splits import SplitDict, SplitInfo
from datasets.utils.py_utils import asdict


@pytest.mark.parametrize(
    '''split_dict'''     ,   [
        SplitDict(),
        SplitDict({'''train''': SplitInfo(name='''train'''     ,   num_bytes=1337     ,   num_examples=42     ,   dataset_name='''my_dataset'''						)}						),
        SplitDict({'''train''': SplitInfo(name='''train'''     ,   num_bytes=1337     ,   num_examples=42						)}						),
        SplitDict({'''train''': SplitInfo()}						),
    ]     ,   )
def 				snake_case_  (      _lowerCAmelCase						:		SplitDict						)   ->     List[Any]:
							UpperCAmelCase				: str											=	split_dict._to_yaml_list()
							assert len(_lowerCAmelCase						) == len(_lowerCAmelCase						)
							UpperCAmelCase				: Optional[int]											=	SplitDict._from_yaml_list(_lowerCAmelCase						)
							for split_name, split_info in split_dict.items():
														# dataset_name field is deprecated, and is therefore not part of the YAML dump
														UpperCAmelCase				: Optional[Any]											=	None
														# the split name of split_dict takes over the name of the split info object
														UpperCAmelCase				: Dict											=	split_name
							assert split_dict == reloaded




@pytest.mark.parametrize(
    '''split_info'''     ,   [SplitInfo(), SplitInfo(dataset_name=_lowerCAmelCase						), SplitInfo(dataset_name='''my_dataset'''						)]						)
def 				snake_case_  (      _lowerCAmelCase						:		List[Any]						)   ->     Optional[Any]:
							# For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name"
							# field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files
							UpperCAmelCase				: Any											=	asdict(SplitDict({'''train''': split_info}						)						)
							assert "dataset_name" in split_dict_asdict["train"]
							assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name





