




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







		'''simple docstring'''



		_snake_case   :   Union[str, Any]							=  MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
		_snake_case   :   Any							=  TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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



		@require_torch
		def  __UpperCAmelCase      (							self       )     ->							List[str]:
							UpperCAmelCase_   :				Tuple						=						pipeline(
							    task='text-classification'    ,		model='hf-internal-testing/tiny-random-distilbert'    ,		framework='pt'       )

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

							UpperCAmelCase_   :				List[Any]						=						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}]       )

							UpperCAmelCase_   :				int						=						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}],
							    ]    ,		)

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

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

							UpperCAmelCase_   :				Union[str, Any]						=						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}]]       )

							UpperCAmelCase_   :				str						=						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}],
							    ]    ,		)

							UpperCAmelCase_   :				Tuple						=						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  __UpperCAmelCase      (							self       )     ->							Any:
							import torch

							UpperCAmelCase_   :				Union[str, Any]						=						pipeline(
							    task='text-classification'    ,		model='hf-internal-testing/tiny-random-distilbert'    ,		framework='pt'    ,		device=torch.device('cpu'       )    ,		)

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



		@require_tf
		def  __UpperCAmelCase      (							self       )     ->							List[Any]:
							UpperCAmelCase_   :				Union[str, Any]						=						pipeline(
							    task='text-classification'    ,		model='hf-internal-testing/tiny-random-distilbert'    ,		framework='tf'       )

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



		@slow
		@require_torch
		def  __UpperCAmelCase      (							self       )     ->							Optional[int]:
							UpperCAmelCase_   :				List[Any]						=						pipeline('text-classification'       )

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



		@slow
		@require_tf
		def  __UpperCAmelCase      (							self       )     ->							Any:
							UpperCAmelCase_   :				Dict						=						pipeline('text-classification'    ,		framework='tf'       )

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



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase       )     ->							Union[str, Any]:
							UpperCAmelCase_   :				Optional[int]						=						TextClassificationPipeline(model=_UpperCamelCase    ,		tokenizer=_UpperCamelCase       )
							return text_classifier, ["HuggingFace is in", "This is another test"]



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase       )     ->							List[str]:
							UpperCAmelCase_   :				str						=						text_classifier.model
							# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
							UpperCAmelCase_   :				Tuple						=						'HuggingFace is in'
							UpperCAmelCase_   :				str						=						text_classifier(_UpperCamelCase       )

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

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

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














from typing import Any, Dict, List, Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
		from ..image_utils import load_image


if is_torch_available():
		import torch

		from ..models.auto.modeling_auto import MODEL_FOR_OBJECT_DETECTION_MAPPING, MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING

__UpperCAmelCase     							=						logging.get_logger(__name__)


__UpperCAmelCase     							=						Dict[str, Any]
__UpperCAmelCase     							=						List[Prediction]



@add_end_docstrings(_snake_case      )
class   lowerCamelCase				(_snake_case      ):







		'''simple docstring'''



		def __init__(							self    ,		*_UpperCamelCase    ,		**_UpperCamelCase       )     ->							Optional[int]:
							super().__init__(*_UpperCamelCase    ,		**_UpperCamelCase       )

							if self.framework == "tf":
												raise ValueError(f"The {self.__class__} is only available in PyTorch."       )

							requires_backends(self    ,		'vision'       )
							self.check_model_type(
							    dict(MODEL_FOR_OBJECT_DETECTION_MAPPING.items() + MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.items()       )       )



		def  __UpperCAmelCase      (							self    ,		**_UpperCamelCase       )     ->							Tuple:
							UpperCAmelCase_   :				str						=						{}
							if "threshold" in kwargs:
												UpperCAmelCase_   :				Tuple						=						kwargs['threshold']
							return {}, {}, postprocess_kwargs



		def __call__(							self    ,		*_UpperCamelCase    ,		**_UpperCamelCase       )     ->							Union[Predictions, List[Prediction]]:
							return super().__call__(*_UpperCamelCase    ,		**_UpperCamelCase       )



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Dict:
							UpperCAmelCase_   :				Any						=						load_image(_UpperCamelCase       )
							UpperCAmelCase_   :				Any						=						torch.IntTensor([[image.height, image.width]]       )
							UpperCAmelCase_   :				str						=						self.image_processor(images=[image]    ,		return_tensors='pt'       )
							if self.tokenizer is not None:
												UpperCAmelCase_   :				List[str]						=						self.tokenizer(text=inputs['words']    ,		boxes=inputs['boxes']    ,		return_tensors='pt'       )
							UpperCAmelCase_   :				Any						=						target_size
							return inputs



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							List[Any]:
							UpperCAmelCase_   :				Any						=						model_inputs.pop('target_size'       )
							UpperCAmelCase_   :				Optional[Any]						=						self.model(**_UpperCamelCase       )
							UpperCAmelCase_   :				Dict						=						outputs.__class__({'target_size': target_size, **outputs}       )
							if self.tokenizer is not None:
												UpperCAmelCase_   :				List[str]						=						model_inputs['bbox']
							return model_outputs



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase    ,		_UpperCamelCase=0.9       )     ->							List[str]:
							UpperCAmelCase_   :				List[Any]						=						model_outputs['target_size']
							if self.tokenizer is not None:
												# This is a LayoutLMForTokenClassification variant.
												# The OCR got the boxes and the model classified the words.
												UpperCAmelCase_	,  UpperCAmelCase_   :				str						=						target_size[0].tolist()

												def unnormalize(_UpperCamelCase       ):
																	return self._get_bounding_box(
																	    torch.Tensor(
																	        [
																	            (width * bbox[0] / 1_0_0_0),
																	            (height * bbox[1] / 1_0_0_0),
																	            (width * bbox[2] / 1_0_0_0),
																	            (height * bbox[3] / 1_0_0_0),
																	        ]       )       )

												UpperCAmelCase_	,  UpperCAmelCase_   :				List[Any]						=						model_outputs['logits'].squeeze(0       ).softmax(dim=-1       ).max(dim=-1       )
												UpperCAmelCase_   :				Union[str, Any]						=						[self.model.config.idalabel[prediction] for prediction in classes.tolist()]
												UpperCAmelCase_   :				Tuple						=						[unnormalize(_UpperCamelCase       ) for bbox in model_outputs['bbox'].squeeze(0       )]
												UpperCAmelCase_   :				List[str]						=						['score', 'label', 'box']
												UpperCAmelCase_   :				Any						=						[dict(zip(_UpperCamelCase    ,		_UpperCamelCase       )       ) for vals in zip(scores.tolist()    ,		_UpperCamelCase    ,		_UpperCamelCase       ) if vals[0] > threshold]
							else:
												# This is a regular ForObjectDetectionModel
												UpperCAmelCase_   :				Union[str, Any]						=						self.image_processor.post_process_object_detection(_UpperCamelCase    ,		_UpperCamelCase    ,		_UpperCamelCase       )
												UpperCAmelCase_   :				str						=						raw_annotations[0]
												UpperCAmelCase_   :				str						=						raw_annotation['scores']
												UpperCAmelCase_   :				Tuple						=						raw_annotation['labels']
												UpperCAmelCase_   :				List[Any]						=						raw_annotation['boxes']

												UpperCAmelCase_   :				Union[str, Any]						=						scores.tolist()
												UpperCAmelCase_   :				int						=						[self.model.config.idalabel[label.item()] for label in labels]
												UpperCAmelCase_   :				Any						=						[self._get_bounding_box(_UpperCamelCase       ) for box in boxes]

												# {"scores": [...], ...} --> [{"score":x, ...}, ...]
												UpperCAmelCase_   :				int						=						['score', 'label', 'box']
												UpperCAmelCase_   :				Dict						=						[
												    dict(zip(_UpperCamelCase    ,		_UpperCamelCase       )       )
												    for vals in zip(raw_annotation['scores']    ,		raw_annotation['labels']    ,		raw_annotation['boxes']       )
												]

							return annotation



		def  __UpperCAmelCase      (							self    ,		_UpperCamelCase       )     ->							Dict[str, int]:
							if self.framework != "pt":
												raise ValueError('The ObjectDetectionPipeline is only available in PyTorch.'       )
							UpperCAmelCase_	,  UpperCAmelCase_	,  UpperCAmelCase_	,  UpperCAmelCase_   :				Dict						=						box.int().tolist()
							UpperCAmelCase_   :				str						=						{
							    'xmin': xmin,
							    'ymin': ymin,
							    'xmax': xmax,
							    'ymax': ymax,
							}
							return bbox






