


'''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 						a__					(					unittest.TestCase   ):





			'''simple docstring'''

			A						:				Optional[int]					      =			MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
			A						:				List[Any]					      =			TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

			if model_mapping is not None:
						A						:				int					      =			{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
			if tf_model_mapping is not None:
						A						:				str					      =			{
						    config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
						}
			@require_torch
			def 					lowerCAmelCase     (  self							:			List[Any]	)      ->		Tuple:
						__A=	pipeline(
						    task='text-classification'			,    model='hf-internal-testing/tiny-random-distilbert'			,    framework='pt'	)

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

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

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

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

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

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

						__A=	text_classifier(['This is great !', 'Something else']			,    return_all_scores=lowerCAmelCase_	)
						self.assertEqual(
						    nested_simplify(lowerCAmelCase_	)			,    [
						        [{'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}],
						    ]			,    )

						__A=	text_classifier(['This is great !', 'Something else']			,    return_all_scores=lowerCAmelCase_	)
						self.assertEqual(
						    nested_simplify(lowerCAmelCase_	)			,    [
						        {'label': 'LABEL_0', 'score': 0.5_04},
						        {'label': 'LABEL_0', 'score': 0.5_04},
						    ]			,    )
			@require_torch
			def 					lowerCAmelCase     (  self							:			List[str]	)      ->		List[str]:
						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(lowerCAmelCase_	)			,    [{'label': 'LABEL_0', 'score': 0.5_04}]	)
			@require_tf
			def 					lowerCAmelCase     (  self							:			str	)      ->		Optional[int]:
						__A=	pipeline(
						    task='text-classification'			,    model='hf-internal-testing/tiny-random-distilbert'			,    framework='tf'	)

						__A=	text_classifier('This is great !'	)
						self.assertEqual(nested_simplify(lowerCAmelCase_	)			,    [{'label': 'LABEL_0', 'score': 0.5_04}]	)
			@slow
			@require_torch
			def 					lowerCAmelCase     (  self							:			Union[str, Any]	)      ->		List[Any]:
						__A=	pipeline('text-classification'	)

						__A=	text_classifier('This is great !'	)
						self.assertEqual(nested_simplify(lowerCAmelCase_	)			,    [{'label': 'POSITIVE', 'score': 1.0}]	)
						__A=	text_classifier('This is bad !'	)
						self.assertEqual(nested_simplify(lowerCAmelCase_	)			,    [{'label': 'NEGATIVE', 'score': 1.0}]	)
						__A=	text_classifier('Birds are a type of animal'	)
						self.assertEqual(nested_simplify(lowerCAmelCase_	)			,    [{'label': 'POSITIVE', 'score': 0.9_88}]	)
			@slow
			@require_tf
			def 					lowerCAmelCase     (  self							:			List[str]	)      ->		Optional[int]:
						__A=	pipeline('text-classification'			,    framework='tf'	)

						__A=	text_classifier('This is great !'	)
						self.assertEqual(nested_simplify(lowerCAmelCase_	)			,    [{'label': 'POSITIVE', 'score': 1.0}]	)
						__A=	text_classifier('This is bad !'	)
						self.assertEqual(nested_simplify(lowerCAmelCase_	)			,    [{'label': 'NEGATIVE', 'score': 1.0}]	)
						__A=	text_classifier('Birds are a type of animal'	)
						self.assertEqual(nested_simplify(lowerCAmelCase_	)			,    [{'label': 'POSITIVE', 'score': 0.9_88}]	)
			def 					lowerCAmelCase     (  self							:			Optional[Any]			,    lowerCAmelCase_							:			str			,    lowerCAmelCase_							:			List[str]			,    lowerCAmelCase_							:			Union[str, Any]	)      ->		Dict:
						__A=	TextClassificationPipeline(model=lowerCAmelCase_			,    tokenizer=lowerCAmelCase_	)
						return text_classifier, ["HuggingFace is in", "This is another test"]

			def 					lowerCAmelCase     (  self							:			Dict			,    lowerCAmelCase_							:			List[Any]			,    lowerCAmelCase_							:			Union[str, Any]	)      ->		List[Any]:
						__A=	text_classifier.model
						# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
						__A=	'HuggingFace is in'
						__A=	text_classifier(lowerCAmelCase_	)

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

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

						__A=	{'text': 'HuggingFace is in ', 'text_pair': 'Paris is in France'}
						__A=	text_classifier(lowerCAmelCase_	)
						self.assertEqual(
						    nested_simplify(lowerCAmelCase_	)			,    {'label': ANY(lowerCAmelCase_	), 'score': ANY(lowerCAmelCase_	)}			,    )
						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(lowerCAmelCase_	):
									text_classifier(lowerCAmelCase_	)

						# 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(lowerCAmelCase_	)			,    [{'label': ANY(lowerCAmelCase_	), 'score': ANY(lowerCAmelCase_	)}]			,    )
						self.assertTrue(outputs[0]['label'] in model.config.idalabel.values()	)







'''simple docstring'''





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase__  			=				logging.get_logger(__name__)

UpperCAmelCase__  			=				{
    '''roberta-base''': '''https://huggingface.co/roberta-base/resolve/main/config.json''',
    '''roberta-large''': '''https://huggingface.co/roberta-large/resolve/main/config.json''',
    '''roberta-large-mnli''': '''https://huggingface.co/roberta-large-mnli/resolve/main/config.json''',
    '''distilroberta-base''': '''https://huggingface.co/distilroberta-base/resolve/main/config.json''',
    '''roberta-base-openai-detector''': '''https://huggingface.co/roberta-base-openai-detector/resolve/main/config.json''',
    '''roberta-large-openai-detector''': '''https://huggingface.co/roberta-large-openai-detector/resolve/main/config.json''',
}
class 						a__					(					a_   ):





			'''simple docstring'''

			A						:				Union[str, Any]					      =			'''roberta'''
			def __init__(  self							:			Any			,    lowerCAmelCase_							:			int=50_265			,    lowerCAmelCase_							:			int=768			,    lowerCAmelCase_							:			Optional[Any]=12			,    lowerCAmelCase_							:			Tuple=12			,    lowerCAmelCase_							:			Optional[int]=3_072			,    lowerCAmelCase_							:			List[str]="gelu"			,    lowerCAmelCase_							:			List[str]=0.1			,    lowerCAmelCase_							:			Tuple=0.1			,    lowerCAmelCase_							:			List[Any]=512			,    lowerCAmelCase_							:			Union[str, Any]=2			,    lowerCAmelCase_							:			List[Any]=0.02			,    lowerCAmelCase_							:			Optional[int]=1E-12			,    lowerCAmelCase_							:			str=1			,    lowerCAmelCase_							:			Dict=0			,    lowerCAmelCase_							:			Any=2			,    lowerCAmelCase_							:			List[str]="absolute"			,    lowerCAmelCase_							:			Any=True			,    lowerCAmelCase_							:			Dict=None			,    **lowerCAmelCase_							:			Any			,    )      ->		int:
						super().__init__(pad_token_id=lowerCAmelCase_			,    bos_token_id=lowerCAmelCase_			,    eos_token_id=lowerCAmelCase_			,    **lowerCAmelCase_	)

						__A=	vocab_size
						__A=	hidden_size
						__A=	num_hidden_layers
						__A=	num_attention_heads
						__A=	hidden_act
						__A=	intermediate_size
						__A=	hidden_dropout_prob
						__A=	attention_probs_dropout_prob
						__A=	max_position_embeddings
						__A=	type_vocab_size
						__A=	initializer_range
						__A=	layer_norm_eps
						__A=	position_embedding_type
						__A=	use_cache
						__A=	classifier_dropout


class 						a__					(					a_   ):





			'''simple docstring'''

			@property
			def 					lowerCAmelCase     (  self							:			Any	)      ->		Mapping[str, Mapping[int, str]]:
						if self.task == "multiple-choice":
									__A=	{0: 'batch', 1: 'choice', 2: 'sequence'}
						else:
									__A=	{0: 'batch', 1: 'sequence'}
						return OrderedDict(
						    [
						        ('input_ids', dynamic_axis),
						        ('attention_mask', dynamic_axis),
						    ]	)



