






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.
lowerCamelCase      :       Dict    =						{'''LayoutLMv2Config''', '''LayoutLMv3Config'''}



@is_pipeline_test
class 				_UpperCamelCase      (unittest.TestCase      ):
      snake_case_	=				MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
      snake_case_	=				TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

      if model_mapping is not None:
            snake_case_	=				{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_	=				{
                config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
            }


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

           __lowerCAmelCase             =       text_classifier("This is great !"						)
           self.assertEqual(nested_simplify(__UpperCamelCase						)       ,    [{"label": "LABEL_0", "score": 0.5_0_4}]						)

           __lowerCAmelCase             =       text_classifier("This is great !"       ,    top_k=2						)
           self.assertEqual(
               nested_simplify(__UpperCamelCase						)       ,    [{"label": "LABEL_0", "score": 0.5_0_4}, {"label": "LABEL_1", "score": 0.4_9_6}]						)

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

           # Legacy behavior
           __lowerCAmelCase             =       text_classifier("This is great !"       ,    return_all_scores=__UpperCamelCase						)
           self.assertEqual(nested_simplify(__UpperCamelCase						)       ,    [{"label": "LABEL_0", "score": 0.5_0_4}]						)

           __lowerCAmelCase             =       text_classifier("This is great !"       ,    return_all_scores=__UpperCamelCase						)
           self.assertEqual(
               nested_simplify(__UpperCamelCase						)       ,    [[{"label": "LABEL_0", "score": 0.5_0_4}, {"label": "LABEL_1", "score": 0.4_9_6}]]						)

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


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

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

           __lowerCAmelCase             =       text_classifier("This is great !"						)
           self.assertEqual(nested_simplify(__UpperCamelCase						)       ,    [{"label": "LABEL_0", "score": 0.5_0_4}]						)


      @require_tf
      def 			__UpperCAmelCase		(						self						)->						Dict:
           __lowerCAmelCase             =       pipeline(
               task="text-classification"       ,    model="hf-internal-testing/tiny-random-distilbert"       ,    framework="tf"						)

           __lowerCAmelCase             =       text_classifier("This is great !"						)
           self.assertEqual(nested_simplify(__UpperCamelCase						)       ,    [{"label": "LABEL_0", "score": 0.5_0_4}]						)


      @slow
      @require_torch
      def 			__UpperCAmelCase		(						self						)->						Optional[Any]:
           __lowerCAmelCase             =       pipeline("text-classification"						)

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


      @slow
      @require_tf
      def 			__UpperCAmelCase		(						self						)->						Tuple:
           __lowerCAmelCase             =       pipeline("text-classification"       ,    framework="tf"						)

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


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase       ,    __UpperCamelCase						)->						Dict:
           __lowerCAmelCase             =       TextClassificationPipeline(model=__UpperCamelCase       ,    tokenizer=__UpperCamelCase						)
           return text_classifier, ["HuggingFace is in", "This is another test"]


      def 			__UpperCAmelCase		(						self       ,    __UpperCamelCase       ,    __UpperCamelCase						)->						Union[str, Any]:
           __lowerCAmelCase             =       text_classifier.model
           # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
           __lowerCAmelCase             =       "HuggingFace is in"
           __lowerCAmelCase             =       text_classifier(__UpperCamelCase						)

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

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

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














import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
 import torch

 from transformers.generation import DisjunctiveConstraint



@require_torch
class 				_UpperCamelCase      (unittest.TestCase      ):


      def 			__UpperCAmelCase		(						self						)->						List[Any]:
           # For consistency across different places the DisjunctiveConstraint is called,
           # dc.token_ids is a list of integers. It is also initialized only by integers.

           __lowerCAmelCase             =       [[1, 2, 4], [1, 2, 3, 4]]
           __lowerCAmelCase             =       DisjunctiveConstraint(__UpperCamelCase						)
           self.assertTrue(isinstance(dc.token_ids       ,    __UpperCamelCase						)						)

           with self.assertRaises(__UpperCamelCase						):
                DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]						)						)

           with self.assertRaises(__UpperCamelCase						):
                DisjunctiveConstraint([torch.LongTensor([1, 2, 4]						), torch.LongTensor([1, 2, 3, 4, 5]						)]						)


      def 			__UpperCAmelCase		(						self						)->						Tuple:
           # We can't have constraints that are complete subsets of another. This leads to a preverse
           # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint?
           # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially
           # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm
           # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it).
           __lowerCAmelCase             =       [[1, 2], [1, 2, 3, 4]]

           with self.assertRaises(__UpperCamelCase						):
                DisjunctiveConstraint(__UpperCamelCase						)  # fails here


      def 			__UpperCAmelCase		(						self						)->						List[Any]:
           __lowerCAmelCase             =       [[1, 2, 3], [1, 2, 4]]

           __lowerCAmelCase             =       DisjunctiveConstraint(__UpperCamelCase						)

           __lowerCAmelCase      ,			__lowerCAmelCase      ,			__lowerCAmelCase             =       dc.update(1						)
           __lowerCAmelCase             =       stepped is True and completed is False and reset is False
           self.assertTrue(__UpperCamelCase						)
           self.assertTrue(not dc.completed						)
           self.assertTrue(dc.current_seq == [1]						)

           __lowerCAmelCase      ,			__lowerCAmelCase      ,			__lowerCAmelCase             =       dc.update(2						)
           __lowerCAmelCase             =       stepped is True and completed is False and reset is False
           self.assertTrue(__UpperCamelCase						)
           self.assertTrue(not dc.completed						)
           self.assertTrue(dc.current_seq == [1, 2]						)

           __lowerCAmelCase      ,			__lowerCAmelCase      ,			__lowerCAmelCase             =       dc.update(3						)
           __lowerCAmelCase             =       stepped is True and completed is True and reset is False
           self.assertTrue(__UpperCamelCase						)
           self.assertTrue(dc.completed						)  # Completed!
           self.assertTrue(dc.current_seq == [1, 2, 3]						)


      def 			__UpperCAmelCase		(						self						)->						int:
           __lowerCAmelCase             =       [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

           __lowerCAmelCase             =       DisjunctiveConstraint(__UpperCamelCase						)

           __lowerCAmelCase      ,			__lowerCAmelCase      ,			__lowerCAmelCase             =       dc.update(1						)
           self.assertTrue(not dc.completed						)
           self.assertTrue(dc.current_seq == [1]						)

           __lowerCAmelCase      ,			__lowerCAmelCase      ,			__lowerCAmelCase             =       dc.update(2						)
           self.assertTrue(not dc.completed						)
           self.assertTrue(dc.current_seq == [1, 2]						)

           __lowerCAmelCase      ,			__lowerCAmelCase      ,			__lowerCAmelCase             =       dc.update(4						)
           self.assertTrue(not dc.completed						)
           self.assertTrue(dc.current_seq == [1, 2, 4]						)

           __lowerCAmelCase      ,			__lowerCAmelCase      ,			__lowerCAmelCase             =       dc.update(5						)
           self.assertTrue(dc.completed						)  # Completed!
           self.assertTrue(dc.current_seq == [1, 2, 4, 5]						)

           dc.reset()

           __lowerCAmelCase      ,			__lowerCAmelCase      ,			__lowerCAmelCase             =       dc.update(1						)
           self.assertTrue(not dc.completed						)
           self.assertTrue(dc.remaining() == 3						)
           self.assertTrue(dc.current_seq == [1]						)

           __lowerCAmelCase      ,			__lowerCAmelCase      ,			__lowerCAmelCase             =       dc.update(2						)
           self.assertTrue(not dc.completed						)
           self.assertTrue(dc.remaining() == 2						)
           self.assertTrue(dc.current_seq == [1, 2]						)

           __lowerCAmelCase      ,			__lowerCAmelCase      ,			__lowerCAmelCase             =       dc.update(5						)
           self.assertTrue(dc.completed						)  # Completed!
           self.assertTrue(dc.remaining() == 0						)
           self.assertTrue(dc.current_seq == [1, 2, 5]						)






