




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


@is_pipeline_test
class    lowerCamelCase    (		unittest.TestCase						):






   '''simple docstring'''





   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 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   Optional[Any]:
        __magic_name__							:	int    							=							pipeline(
            task='''text-classification'''       ,						model='''hf-internal-testing/tiny-random-distilbert'''       ,						framework='''pt'''  )

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

        __magic_name__							:	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}]  )

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

        __magic_name__							:	List[str]    							=							text_classifier('''This is great !'''       ,						top_k=1  )
        self.assertEqual(nested_simplify(_lowercase  )       ,						[{'''label''': '''LABEL_0''', '''score''': 0.5_0_4}]  )

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

        __magic_name__							:	Tuple    							=							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}]]  )

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

        __magic_name__							:	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_0''', '''score''': 0.5_0_4},
            ]       ,						)

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

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

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

   @require_tf
   def 							UpperCAmelCase__       ( self			:   Optional[int]  )						->   Optional[int]:
        __magic_name__							:	List[Any]    							=							pipeline(
            task='''text-classification'''       ,						model='''hf-internal-testing/tiny-random-distilbert'''       ,						framework='''tf'''  )

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

   @slow
   @require_torch
   def 							UpperCAmelCase__       ( self			:   Optional[Any]  )						->   Tuple:
        __magic_name__							:	Tuple    							=							pipeline('''text-classification'''  )

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

   @slow
   @require_tf
   def 							UpperCAmelCase__       ( self			:   str  )						->   Optional[int]:
        __magic_name__							:	List[str]    							=							pipeline('''text-classification'''       ,						framework='''tf'''  )

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

   def 							UpperCAmelCase__       ( self			:   int       ,						lowerCamelCase_			:   str       ,						lowerCamelCase_			:   Any       ,						lowerCamelCase_			:   int  )						->   Union[str, Any]:
        __magic_name__							:	Dict    							=							TextClassificationPipeline(model=_lowercase       ,						tokenizer=_lowercase  )
        return text_classifier, ["HuggingFace is in", "This is another test"]


   def 							UpperCAmelCase__       ( self			:   List[Any]       ,						lowerCamelCase_			:   int       ,						lowerCamelCase_			:   List[str]  )						->   str:
        __magic_name__							:	List[str]    							=							text_classifier.model
        # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
        __magic_name__							:	Union[str, Any]    							=							"""HuggingFace is in"""
        __magic_name__							:	List[str]    							=							text_classifier(_lowercase  )

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

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

        __magic_name__							:	Dict    							=							{"""text""": """HuggingFace is in """, """text_pair""": """Paris is in France"""}
        __magic_name__							:	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.
        __magic_name__							:	Dict    							=							[["""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
        __magic_name__							:	Optional[int]    							=							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 json
import os
import unittest

from transformers.models.blenderbot_small.tokenization_blenderbot_small import (
    VOCAB_FILES_NAMES,
    BlenderbotSmallTokenizer,
)

from ...test_tokenization_common import TokenizerTesterMixin


class    lowerCamelCase    (		_lowerCamelCase				,unittest.TestCase						):






			'''simple docstring'''





			UpperCamelCase__								=BlenderbotSmallTokenizer
			UpperCamelCase__								=False

			def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   str:
								super().setUp()

								__magic_name__							:	Union[str, Any]    							=							['''__start__''', '''adapt''', '''act''', '''ap@@''', '''te''', '''__end__''', '''__unk__''']
								__magic_name__							:	Tuple    							=							dict(zip(lowerCamelCase_       ,						range(len(lowerCamelCase_  )  )  )  )

								__magic_name__							:	Tuple    							=							['''#version: 0.2''', '''a p''', '''t e</w>''', '''ap t</w>''', '''a d''', '''ad apt</w>''', '''a c''', '''ac t</w>''', '''''']
								__magic_name__							:	List[Any]    							=							{'''unk_token''': '''__unk__''', '''bos_token''': '''__start__''', '''eos_token''': '''__end__'''}

								__magic_name__							:	str    							=							os.path.join(self.tmpdirname       ,						VOCAB_FILES_NAMES['''vocab_file''']  )
								__magic_name__							:	List[str]    							=							os.path.join(self.tmpdirname       ,						VOCAB_FILES_NAMES['''merges_file''']  )
								with open(self.vocab_file       ,						'''w'''       ,						encoding='''utf-8'''  ) as fp:
													fp.write(json.dumps(lowerCamelCase_  ) + '''\n'''  )
								with open(self.merges_file       ,						'''w'''       ,						encoding='''utf-8'''  ) as fp:
													fp.write('''\n'''.join(lowerCamelCase_  )  )

			def 							UpperCAmelCase__       ( self			:   List[str]       ,						**lowerCamelCase_			:   Optional[Any]  )						->   List[Any]:
								kwargs.update(self.special_tokens_map  )
								return BlenderbotSmallTokenizer.from_pretrained(self.tmpdirname       ,						**lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   Dict       ,						lowerCamelCase_			:   Union[str, Any]  )						->   Optional[int]:
								__magic_name__							:	Union[str, Any]    							=							'''adapt act apte'''
								__magic_name__							:	Dict    							=							'''adapt act apte'''
								return input_text, output_text

			def 							UpperCAmelCase__       ( self			:   Union[str, Any]  )						->   Any:
								__magic_name__							:	int    							=							BlenderbotSmallTokenizer(self.vocab_file       ,						self.merges_file       ,						**self.special_tokens_map  )
								__magic_name__							:	str    							=							'''adapt act apte'''
								__magic_name__							:	Any    							=							['''adapt''', '''act''', '''ap@@''', '''te''']
								__magic_name__							:	List[str]    							=							tokenizer.tokenize(lowerCamelCase_  )
								self.assertListEqual(lowerCamelCase_       ,						lowerCamelCase_  )

								__magic_name__							:	Union[str, Any]    							=							[tokenizer.bos_token] + tokens + [tokenizer.eos_token]

								__magic_name__							:	List[Any]    							=							[0, 1, 2, 3, 4, 5]
								self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCamelCase_  )       ,						lowerCamelCase_  )

			def 							UpperCAmelCase__       ( self			:   int  )						->   int:
								__magic_name__							:	Any    							=							BlenderbotSmallTokenizer.from_pretrained('''facebook/blenderbot-90M'''  )
								assert tok('''sam'''  ).input_ids == [1384]
								__magic_name__							:	Dict    							=							'''I am a small frog.'''
								__magic_name__							:	Tuple    							=							tok([src_text]       ,						padding=lowerCamelCase_       ,						truncation=lowerCamelCase_  )['''input_ids''']
								__magic_name__							:	Tuple    							=							tok.batch_decode(lowerCamelCase_       ,						skip_special_tokens=lowerCamelCase_       ,						clean_up_tokenization_spaces=lowerCamelCase_  )[0]
								assert src_text != decoded  # I wish it did!
								assert decoded == "i am a small frog ."


			def 							UpperCAmelCase__       ( self			:   Tuple  )						->   Dict:
								__magic_name__							:	Tuple    							=							BlenderbotSmallTokenizer.from_pretrained('''facebook/blenderbot-90M'''  )
								__magic_name__							:	Any    							=							'''I am a small frog .'''
								__magic_name__							:	List[str]    							=							'''.'''
								__magic_name__							:	Tuple    							=							tok(lowerCamelCase_  )['''input_ids''']
								__magic_name__							:	Optional[Any]    							=							tok(lowerCamelCase_  )['''input_ids''']

								assert encoded[-1] == encoded_dot[0]



