





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







@is_pipeline_test
class   _UpperCamelCase  (       unittest.TestCase  ):







					'''simple docstring'''

					lowerCamelCase     :     Union[str, Any]			    =       MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
					lowerCamelCase     :     Dict			    =       TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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





					@require_torch
					def        SCREAMING_SNAKE_CASE							(						self    :  str		):






						'''simple docstring'''







						UpperCAmelCase_								=     pipeline(
						    task="""text-classification""" ,					model="""hf-internal-testing/tiny-random-distilbert""" ,					framework="""pt"""		)

						UpperCAmelCase_								=     text_classifier("""This is great !"""		)
						self.assertEqual(nested_simplify(__lowercase		) ,					[{"""label""": """LABEL_0""", """score""": 0.504}]		)

						UpperCAmelCase_								=     text_classifier("""This is great !""" ,					top_k=2		)
						self.assertEqual(
						    nested_simplify(__lowercase		) ,					[{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}]		)

						UpperCAmelCase_								=     text_classifier(["""This is great !""", """This is bad"""] ,					top_k=2		)
						self.assertEqual(
						    nested_simplify(__lowercase		) ,					[
						        [{"""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_								=     text_classifier("""This is great !""" ,					top_k=1		)
						self.assertEqual(nested_simplify(__lowercase		) ,					[{"""label""": """LABEL_0""", """score""": 0.504}]		)

						# Legacy behavior
						UpperCAmelCase_								=     text_classifier("""This is great !""" ,					return_all_scores=__lowercase		)
						self.assertEqual(nested_simplify(__lowercase		) ,					[{"""label""": """LABEL_0""", """score""": 0.504}]		)

						UpperCAmelCase_								=     text_classifier("""This is great !""" ,					return_all_scores=__lowercase		)
						self.assertEqual(
						    nested_simplify(__lowercase		) ,					[[{"""label""": """LABEL_0""", """score""": 0.504}, {"""label""": """LABEL_1""", """score""": 0.496}]]		)

						UpperCAmelCase_								=     text_classifier(["""This is great !""", """Something else"""] ,					return_all_scores=__lowercase		)
						self.assertEqual(
						    nested_simplify(__lowercase		) ,					[
						        [{"""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_								=     text_classifier(["""This is great !""", """Something else"""] ,					return_all_scores=__lowercase		)
						self.assertEqual(
						    nested_simplify(__lowercase		) ,					[
						        {"""label""": """LABEL_0""", """score""": 0.504},
						        {"""label""": """LABEL_0""", """score""": 0.504},
						    ] ,					)





					@require_torch
					def        SCREAMING_SNAKE_CASE							(						self    :  Any		):






						'''simple docstring'''







						import torch

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

						UpperCAmelCase_								=     text_classifier("""This is great !"""		)
						self.assertEqual(nested_simplify(__lowercase		) ,					[{"""label""": """LABEL_0""", """score""": 0.504}]		)





					@require_tf
					def        SCREAMING_SNAKE_CASE							(						self    :  Tuple		):






						'''simple docstring'''







						UpperCAmelCase_								=     pipeline(
						    task="""text-classification""" ,					model="""hf-internal-testing/tiny-random-distilbert""" ,					framework="""tf"""		)

						UpperCAmelCase_								=     text_classifier("""This is great !"""		)
						self.assertEqual(nested_simplify(__lowercase		) ,					[{"""label""": """LABEL_0""", """score""": 0.504}]		)





					@slow
					@require_torch
					def        SCREAMING_SNAKE_CASE							(						self    :  Union[str, Any]		):






						'''simple docstring'''







						UpperCAmelCase_								=     pipeline("""text-classification"""		)

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





					@slow
					@require_tf
					def        SCREAMING_SNAKE_CASE							(						self    :  int		):






						'''simple docstring'''







						UpperCAmelCase_								=     pipeline("""text-classification""" ,					framework="""tf"""		)

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





					def        SCREAMING_SNAKE_CASE							(						self    :  Optional[int] ,					__lowercase    :  Tuple ,					__lowercase    :  Dict ,					__lowercase    :  Any		):






						'''simple docstring'''







						UpperCAmelCase_								=     TextClassificationPipeline(model=__lowercase ,					tokenizer=__lowercase		)
						return text_classifier, ["HuggingFace is in", "This is another test"]





					def        SCREAMING_SNAKE_CASE							(						self    :  Tuple ,					__lowercase    :  Optional[int] ,					__lowercase    :  int		):






						'''simple docstring'''







						UpperCAmelCase_								=     text_classifier.model
						# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
						UpperCAmelCase_								=     """HuggingFace is in"""
						UpperCAmelCase_								=     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_								=     ["""HuggingFace is in """, """Paris is in France"""]
						UpperCAmelCase_								=     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_								=     text_classifier(__lowercase ,					top_k=__lowercase		)
						UpperCAmelCase_								=     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_								=     {"""text""": """HuggingFace is in """, """text_pair""": """Paris is in France"""}
						UpperCAmelCase_								=     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_								=     [["""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_								=     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()		)









import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.text import TextDatasetReader

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases




def    A_(			A				,      A   ):
	assert isinstance(A				,      A   )
	assert dataset.num_rows == 4
	assert dataset.num_columns == 1
	assert dataset.column_names == ["text"]
	for feature, expected_dtype in expected_features.items():
		assert dataset.features[feature].dtype == expected_dtype




@pytest.mark.parametrize("""keep_in_memory"""				,      [False, True]   )
def    A_(			A				,      A				,      A   ):
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
		UpperCAmelCase_								=     TextDatasetReader(A				,      cache_dir=A				,      keep_in_memory=A   ).read()
	_check_text_dataset(A				,      A   )




@pytest.mark.parametrize(
    """features"""				,      [
        None,
        {"""text""": """string"""},
        {"""text""": """int32"""},
        {"""text""": """float32"""},
    ]				,      )
def    A_(			A				,      A				,      A   ):
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	UpperCAmelCase_								=     features.copy() if features else default_expected_features
	UpperCAmelCase_								=     (
	    Features({feature: Value(A   ) for feature, dtype in features.items()}   ) if features is not None else None
	)
	UpperCAmelCase_								=     TextDatasetReader(A				,      features=A				,      cache_dir=A   ).read()
	_check_text_dataset(A				,      A   )




@pytest.mark.parametrize("""split"""				,      [None, NamedSplit("""train"""   ), """train""", """test"""]   )
def    A_(			A				,      A				,      A   ):
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	UpperCAmelCase_								=     TextDatasetReader(A				,      cache_dir=A				,      split=A   ).read()
	_check_text_dataset(A				,      A   )
	assert dataset.split == split if split else "train"




@pytest.mark.parametrize("""path_type"""				,      [str, list]   )
def    A_(			A				,      A				,      A   ):
	if issubclass(A				,      A   ):
		UpperCAmelCase_								=     text_path
	elif issubclass(A				,      A   ):
		UpperCAmelCase_								=     [text_path]
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	UpperCAmelCase_								=     TextDatasetReader(A				,      cache_dir=A   ).read()
	_check_text_dataset(A				,      A   )




def    A_(			A				,      A				,      A=("train",)   ):
	assert isinstance(A				,      A   )
	for split in splits:
		UpperCAmelCase_								=     dataset_dict[split]
		assert dataset.num_rows == 4
		assert dataset.num_columns == 1
		assert dataset.column_names == ["text"]
		for feature, expected_dtype in expected_features.items():
			assert dataset.features[feature].dtype == expected_dtype




@pytest.mark.parametrize("""keep_in_memory"""				,      [False, True]   )
def    A_(			A				,      A				,      A   ):
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
		UpperCAmelCase_								=     TextDatasetReader({"""train""": text_path}				,      cache_dir=A				,      keep_in_memory=A   ).read()
	_check_text_datasetdict(A				,      A   )




@pytest.mark.parametrize(
    """features"""				,      [
        None,
        {"""text""": """string"""},
        {"""text""": """int32"""},
        {"""text""": """float32"""},
    ]				,      )
def    A_(			A				,      A				,      A   ):
	UpperCAmelCase_								=     tmp_path / """cache"""
	# CSV file loses col_1 string dtype information: default now is "int64" instead of "string"
	UpperCAmelCase_								=     {"""text""": """string"""}
	UpperCAmelCase_								=     features.copy() if features else default_expected_features
	UpperCAmelCase_								=     (
	    Features({feature: Value(A   ) for feature, dtype in features.items()}   ) if features is not None else None
	)
	UpperCAmelCase_								=     TextDatasetReader({"""train""": text_path}				,      features=A				,      cache_dir=A   ).read()
	_check_text_datasetdict(A				,      A   )




@pytest.mark.parametrize("""split"""				,      [None, NamedSplit("""train"""   ), """train""", """test"""]   )
def    A_(			A				,      A				,      A   ):
	if split:
		UpperCAmelCase_								=     {split: text_path}
	else:
		UpperCAmelCase_								=     """train"""
		UpperCAmelCase_								=     {"""train""": text_path, """test""": text_path}
	UpperCAmelCase_								=     tmp_path / """cache"""
	UpperCAmelCase_								=     {"""text""": """string"""}
	UpperCAmelCase_								=     TextDatasetReader(A				,      cache_dir=A   ).read()
	_check_text_datasetdict(A				,      A				,      splits=list(path.keys()   )   )
	assert all(dataset[split].split == split for split in path.keys()   )


