



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.
a_								=       {"""LayoutLMv2Config""", """LayoutLMv3Config"""}



@is_pipeline_test
class 			__lowerCAmelCase  (						unittest.TestCase ):
 lowerCAmelCase__							       =	MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
 lowerCAmelCase__							       =	TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

 if model_mapping is not None:
  lowerCAmelCase__							       =	{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
 if tf_model_mapping is not None:
  lowerCAmelCase__							       =	{
      config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
  }




 @require_torch
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __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.504}]					)

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

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

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

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

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

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

    __lowerCamelCase    				=     text_classifier(['''This is great !''', '''Something else''']						,					return_all_scores=UpperCAmelCase_					)
    self.assertEqual(
        nested_simplify(UpperCAmelCase_					)						,					[
            {'''label''': '''LABEL_0''', '''score''': 0.504},
            {'''label''': '''LABEL_0''', '''score''': 0.504},
        ]						,					)




 @require_torch
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    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.504}]					)




 @require_tf
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __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.504}]					)




 @slow
 @require_torch
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __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.988}]					)




 @slow
 @require_tf
 def 							lowerCamelCase   (    self					):



    '''simple docstring'''
    __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.988}]					)




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					__UpperCAmelCase						,					__UpperCAmelCase					):



    '''simple docstring'''
    __lowerCamelCase    				=     TextClassificationPipeline(model=UpperCAmelCase_						,					tokenizer=UpperCAmelCase_					)
    return text_classifier, ["HuggingFace is in", "This is another test"]




 def 							lowerCamelCase   (    self						,					__UpperCAmelCase						,					__UpperCAmelCase					):



    '''simple docstring'''
    __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()					)











'''simple docstring'''





from __future__ import annotations




def 							lowerCamelCase	(					_snake_case	:    int  ,_snake_case	:    int							):





    '''simple docstring'''


    if b == 0:
        return (1, 0)
    ((lowercase__)							,							(lowercase__))									=    extended_euclid(_snake_case  ,a % b							)
    lowercase__									=    a // b
    return (y, x - k * y)




def 							lowerCamelCase	(					_snake_case	:    int  ,_snake_case	:    int  ,_snake_case	:    int  ,_snake_case	:    int							):





    '''simple docstring'''


    ((lowercase__)							,							(lowercase__))									=    extended_euclid(_snake_case  ,_snake_case							)
    lowercase__									=    na * na
    lowercase__									=    ra * x * na + ra * y * na
    return (n % m + m) % m




def 							lowerCamelCase	(					_snake_case	:    int  ,_snake_case	:    int							):





    '''simple docstring'''


    ((lowercase__)							,							(lowercase__))									=    extended_euclid(_snake_case  ,_snake_case							)
    if b < 0:
        lowercase__									=    (b % n + n) % n
    return b




def 							lowerCamelCase	(					_snake_case	:    int  ,_snake_case	:    int  ,_snake_case	:    int  ,_snake_case	:    int							):





    '''simple docstring'''


    lowercase__							,							lowercase__									=    invert_modulo(_snake_case  ,_snake_case							), invert_modulo(_snake_case  ,_snake_case							)
    lowercase__									=    na * na
    lowercase__									=    ra * x * na + ra * y * na
    return (n % m + m) % m


if __name__ == "__main__":
 from doctest import testmod

 testmod(name="chinese_remainder_theorem", verbose=True)
 testmod(name="chinese_remainder_theorem2", verbose=True)
 testmod(name="invert_modulo", verbose=True)
 testmod(name="extended_euclid", verbose=True)




