





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      lowerCAmelCase							(      unittest.TestCase    ):
 UpperCAmelCase__									=	MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
 UpperCAmelCase__									=	TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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

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

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

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

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

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

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

     lowerCamelCase__					:   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_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}],
         ]    ,  )

     lowerCamelCase__					:   str      				=	text_classifier(['This is great !', 'Something else']    ,  return_all_scores=lowercase_			)
     self.assertEqual(
         nested_simplify(lowercase_			)    ,  [
             {'label': 'LABEL_0', 'score': 0.5_0_4},
             {'label': 'LABEL_0', 'score': 0.5_0_4},
         ]    ,  )
 @require_torch
 def   A_			(   self						:   Dict			)    ->			Optional[int]:
     import torch

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

     lowerCamelCase__					:   List[str]      				=	text_classifier('This is great !'			)
     self.assertEqual(nested_simplify(lowercase_			)    ,  [{'label': 'LABEL_0', 'score': 0.5_0_4}]			)
 @require_tf
 def   A_			(   self						:   Dict			)    ->			Dict:
     lowerCamelCase__					:   Any      				=	pipeline(
         task='text-classification'    ,  model='hf-internal-testing/tiny-random-distilbert'    ,  framework='tf'			)

     lowerCamelCase__					:   List[str]      				=	text_classifier('This is great !'			)
     self.assertEqual(nested_simplify(lowercase_			)    ,  [{'label': 'LABEL_0', 'score': 0.5_0_4}]			)
 @slow
 @require_torch
 def   A_			(   self						:   Union[str, Any]			)    ->			Optional[int]:
     lowerCamelCase__					:   Tuple      				=	pipeline('text-classification'			)

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

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







 def   A_			(   self						:   str    ,  UpperCAmelCase						:   List[str]    ,  UpperCAmelCase						:   Union[str, Any]			)    ->			Dict:
     lowerCamelCase__					:   Tuple      				=	text_classifier.model
     # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
     lowerCamelCase__					:   Optional[Any]      				=	"""HuggingFace is in"""
     lowerCamelCase__					:   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()			)

     lowerCamelCase__					:   int      				=	["""HuggingFace is in """, """Paris is in France"""]
     lowerCamelCase__					:   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
     lowerCamelCase__					:   List[Any]      				=	text_classifier(lowercase_    ,  top_k=lowercase_			)
     lowerCamelCase__					:   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]    ,  )

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












import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available

class      lowerCAmelCase							(      __UpperCamelCase    ):
 UpperCAmelCase__									=	["""image_processor""", """tokenizer"""]
 UpperCAmelCase__									=	"""OwlViTImageProcessor"""
 UpperCAmelCase__									=	("""CLIPTokenizer""", """CLIPTokenizerFast""")
 def __init__(   self						:   Optional[int]    ,  UpperCAmelCase						:   str=None    ,  UpperCAmelCase						:   Any=None    ,  **UpperCAmelCase						:   Optional[Any]			)    ->			Dict:
     lowerCamelCase__					:   Any      				=	None
     if "feature_extractor" in kwargs:
         warnings.warn(
             'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
             ' instead.'    ,  UpperCAmelCase    ,  )
         lowerCamelCase__					:   Optional[Any]      				=	kwargs.pop('feature_extractor'			)

     lowerCamelCase__					:   Dict      				=	image_processor if image_processor is not None else feature_extractor
     if image_processor is None:
         raise ValueError('You need to specify an `image_processor`.'			)
     if tokenizer is None:
         raise ValueError('You need to specify a `tokenizer`.'			)

     super().__init__(UpperCAmelCase    ,  UpperCAmelCase			)
 def __call__(   self						:   List[Any]    ,  UpperCAmelCase						:   List[Any]=None    ,  UpperCAmelCase						:   Tuple=None    ,  UpperCAmelCase						:   int=None    ,  UpperCAmelCase						:   Optional[int]="max_length"    ,  UpperCAmelCase						:   Dict="np"    ,  **UpperCAmelCase						:   Union[str, Any]			)    ->			Union[str, Any]:

     if text is None and query_images is None and images is None:
         raise ValueError(
             'You have to specify at least one text or query image or image. All three cannot be none.'			)

     if text is not None:
         if isinstance(UpperCAmelCase    ,  UpperCAmelCase			) or (isinstance(UpperCAmelCase    ,  UpperCAmelCase			) and not isinstance(text[0]    ,  UpperCAmelCase			)):
             lowerCamelCase__					:   Any      				=	[self.tokenizer(UpperCAmelCase    ,  padding=UpperCAmelCase    ,  return_tensors=UpperCAmelCase    ,  **UpperCAmelCase			)]

         elif isinstance(UpperCAmelCase    ,  UpperCAmelCase			) and isinstance(text[0]    ,  UpperCAmelCase			):
             lowerCamelCase__					:   List[str]      				=	[]

             # Maximum number of queries across batch
             lowerCamelCase__					:   List[Any]      				=	max([len(UpperCAmelCase			) for t in text]			)

             # Pad all batch samples to max number of text queries
             for t in text:
                 if len(UpperCAmelCase			) != max_num_queries:
                     lowerCamelCase__					:   int      				=	t + [' '] * (max_num_queries - len(UpperCAmelCase			))

                 lowerCamelCase__					:   str      				=	self.tokenizer(UpperCAmelCase    ,  padding=UpperCAmelCase    ,  return_tensors=UpperCAmelCase    ,  **UpperCAmelCase			)
                 encodings.append(UpperCAmelCase			)
         else:
             raise TypeError('Input text should be a string, a list of strings or a nested list of strings'			)

         if return_tensors == "np":
             lowerCamelCase__					:   Optional[Any]      				=	np.concatenate([encoding['input_ids'] for encoding in encodings]    ,  axis=0			)
             lowerCamelCase__					:   int      				=	np.concatenate([encoding['attention_mask'] for encoding in encodings]    ,  axis=0			)

         elif return_tensors == "jax" and is_flax_available():
             import jax.numpy as jnp

             lowerCamelCase__					:   Optional[int]      				=	jnp.concatenate([encoding['input_ids'] for encoding in encodings]    ,  axis=0			)
             lowerCamelCase__					:   Any      				=	jnp.concatenate([encoding['attention_mask'] for encoding in encodings]    ,  axis=0			)

         elif return_tensors == "pt" and is_torch_available():
             import torch

             lowerCamelCase__					:   Union[str, Any]      				=	torch.cat([encoding['input_ids'] for encoding in encodings]    ,  dim=0			)
             lowerCamelCase__					:   Dict      				=	torch.cat([encoding['attention_mask'] for encoding in encodings]    ,  dim=0			)

         elif return_tensors == "tf" and is_tf_available():
             import tensorflow as tf

             lowerCamelCase__					:   Any      				=	tf.stack([encoding['input_ids'] for encoding in encodings]    ,  axis=0			)
             lowerCamelCase__					:   Optional[int]      				=	tf.stack([encoding['attention_mask'] for encoding in encodings]    ,  axis=0			)

         else:
             raise ValueError('Target return tensor type could not be returned'			)

         lowerCamelCase__					:   str      				=	BatchEncoding()
         lowerCamelCase__					:   int      				=	input_ids
         lowerCamelCase__					:   Any      				=	attention_mask

     if query_images is not None:
         lowerCamelCase__					:   Union[str, Any]      				=	BatchEncoding()
         lowerCamelCase__					:   Optional[Any]      				=	self.image_processor(
             UpperCAmelCase    ,  return_tensors=UpperCAmelCase    ,  **UpperCAmelCase			).pixel_values
         lowerCamelCase__					:   List[Any]      				=	query_pixel_values

     if images is not None:
         lowerCamelCase__					:   List[Any]      				=	self.image_processor(UpperCAmelCase    ,  return_tensors=UpperCAmelCase    ,  **UpperCAmelCase			)

     if text is not None and images is not None:
         lowerCamelCase__					:   Tuple      				=	image_features.pixel_values
         return encoding
     elif query_images is not None and images is not None:
         lowerCamelCase__					:   Dict      				=	image_features.pixel_values
         return encoding
     elif text is not None or query_images is not None:
         return encoding
     else:
         return BatchEncoding(data=dict(**UpperCAmelCase			)    ,  tensor_type=UpperCAmelCase			)
 def   A_			(   self						:   str    ,  *UpperCAmelCase						:   Dict    ,  **UpperCAmelCase						:   List[Any]			)    ->			Any:
     return self.image_processor.post_process(*UpperCAmelCase    ,  **UpperCAmelCase			)
 def   A_			(   self						:   int    ,  *UpperCAmelCase						:   List[Any]    ,  **UpperCAmelCase						:   Tuple			)    ->			str:
     return self.image_processor.post_process_object_detection(*UpperCAmelCase    ,  **UpperCAmelCase			)
 def   A_			(   self						:   Tuple    ,  *UpperCAmelCase						:   Dict    ,  **UpperCAmelCase						:   Optional[int]			)    ->			Optional[Any]:
     return self.image_processor.post_process_image_guided_detection(*UpperCAmelCase    ,  **UpperCAmelCase			)
 def   A_			(   self						:   Optional[Any]    ,  *UpperCAmelCase						:   Tuple    ,  **UpperCAmelCase						:   List[Any]			)    ->			Tuple:
     return self.tokenizer.batch_decode(*UpperCAmelCase    ,  **UpperCAmelCase			)
 def   A_			(   self						:   Union[str, Any]    ,  *UpperCAmelCase						:   List[Any]    ,  **UpperCAmelCase						:   List[Any]			)    ->			List[str]:
     return self.tokenizer.decode(*UpperCAmelCase    ,  **UpperCAmelCase			)
 @property
 def   A_			(   self						:   List[str]			)    ->			Optional[Any]:
     warnings.warn(
         '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'    ,  UpperCAmelCase    ,  )
     return self.image_processor_class







 @property
 def   A_			(   self						:   int			)    ->			int:
     warnings.warn(
         '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'    ,  UpperCAmelCase    ,  )
     return self.image_processor





