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





@is_pipeline_test
class     lowerCamelCase      (       unittest.TestCase					):
	UpperCamelCase_  :					Optional[Any]    		=	MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
	UpperCamelCase_  :					Optional[int]    		=	TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

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







	@require_torch
	def  snake_case__     (     self      :Dict			)		->      Union[str, Any]:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= pipeline(
				    task='''text-classification'''   ,     model='''hf-internal-testing/tiny-random-distilbert'''   ,     framework='''pt'''			)

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

				SCREAMING_SNAKE_CASE     						= text_classifier('''This is great !'''   ,     top_k=2			)
				self.assertEqual(
				    nested_simplify(lowercase			)   ,     [{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}]			)

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

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

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

				SCREAMING_SNAKE_CASE     						= text_classifier('''This is great !'''   ,     return_all_scores=lowercase			)
				self.assertEqual(
				    nested_simplify(lowercase			)   ,     [[{'''label''': '''LABEL_0''', '''score''': 0.5_04}, {'''label''': '''LABEL_1''', '''score''': 0.4_96}]]			)

				SCREAMING_SNAKE_CASE     						= text_classifier(['''This is great !''', '''Something else''']   ,     return_all_scores=lowercase			)
				self.assertEqual(
				    nested_simplify(lowercase			)   ,     [
				        [{'''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}],
				    ]   ,     )

				SCREAMING_SNAKE_CASE     						= text_classifier(['''This is great !''', '''Something else''']   ,     return_all_scores=lowercase			)
				self.assertEqual(
				    nested_simplify(lowercase			)   ,     [
				        {'''label''': '''LABEL_0''', '''score''': 0.5_04},
				        {'''label''': '''LABEL_0''', '''score''': 0.5_04},
				    ]   ,     )







	@require_torch
	def  snake_case__     (     self      :List[str]			)		->      Optional[int]:







				"""simple docstring"""






				import torch

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

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







	@require_tf
	def  snake_case__     (     self      :Dict			)		->      Any:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= pipeline(
				    task='''text-classification'''   ,     model='''hf-internal-testing/tiny-random-distilbert'''   ,     framework='''tf'''			)

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







	@slow
	@require_torch
	def  snake_case__     (     self      :Optional[int]			)		->      str:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= pipeline('''text-classification'''			)

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







	@slow
	@require_tf
	def  snake_case__     (     self      :Any			)		->      List[str]:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= pipeline('''text-classification'''   ,     framework='''tf'''			)

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







	def  snake_case__     (     self      :Optional[Any]   ,     lowercase      :Union[str, Any]   ,     lowercase      :Optional[int]   ,     lowercase      :Optional[Any]			)		->      int:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= TextClassificationPipeline(model=lowercase   ,     tokenizer=lowercase			)
				return text_classifier, ["HuggingFace is in", "This is another test"]







	def  snake_case__     (     self      :List[Any]   ,     lowercase      :int   ,     lowercase      :List[str]			)		->      Tuple:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= text_classifier.model
				# Small inputs because BartTokenizer tiny has maximum position embeddings = 22
				SCREAMING_SNAKE_CASE     						= '''HuggingFace is in'''
				SCREAMING_SNAKE_CASE     						= text_classifier(lowercase			)

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

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

				SCREAMING_SNAKE_CASE     						= {'''text''': '''HuggingFace is in ''', '''text_pair''': '''Paris is in France'''}
				SCREAMING_SNAKE_CASE     						= 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.
				SCREAMING_SNAKE_CASE     						= [['''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
				SCREAMING_SNAKE_CASE     						= 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 tempfile
import unittest

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.testing_utils import (
    is_torch_available,
    require_optimum,
    require_torch,
    slow,
)


if is_torch_available():
							import torch





@require_torch
@require_optimum
@slow
class     lowerCamelCase      (       unittest.TestCase					):







	def  snake_case__     (     self      :List[Any]			)		->      Optional[Any]:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= '''hf-internal-testing/tiny-random-t5'''
				SCREAMING_SNAKE_CASE     						= AutoTokenizer.from_pretrained(lowercase			)
				SCREAMING_SNAKE_CASE     						= AutoModelForSeqaSeqLM.from_pretrained(lowercase			)

				SCREAMING_SNAKE_CASE     						= tokenizer('''This is me'''   ,     return_tensors='''pt'''			)

				SCREAMING_SNAKE_CASE     						= model.to_bettertransformer()

				self.assertTrue(any('''BetterTransformer''' in mod.__class__.__name__ for _, mod in model.named_modules()			)			)

				SCREAMING_SNAKE_CASE     						= model.generate(**lowercase			)

				SCREAMING_SNAKE_CASE     						= model.reverse_bettertransformer()

				self.assertFalse(any('''BetterTransformer''' in mod.__class__.__name__ for _, mod in model.named_modules()			)			)

				with tempfile.TemporaryDirectory() as tmpdirname:
							model.save_pretrained(lowercase			)

							SCREAMING_SNAKE_CASE     						= AutoModelForSeqaSeqLM.from_pretrained(lowercase			)

							self.assertFalse(
							    any('''BetterTransformer''' in mod.__class__.__name__ for _, mod in model_reloaded.named_modules()			)			)

							SCREAMING_SNAKE_CASE     						= model_reloaded.generate(**lowercase			)
							self.assertTrue(torch.allclose(lowercase   ,     lowercase			)			)







	def  snake_case__     (     self      :Union[str, Any]			)		->      Optional[int]:







				"""simple docstring"""






				SCREAMING_SNAKE_CASE     						= '''hf-internal-testing/tiny-random-t5'''
				SCREAMING_SNAKE_CASE     						= AutoModelForSeqaSeqLM.from_pretrained(lowercase			)

				SCREAMING_SNAKE_CASE     						= model.to_bettertransformer()

				with tempfile.TemporaryDirectory() as tmpdirname:
							with self.assertRaises(lowercase			):
										model.save_pretrained(lowercase			)

							SCREAMING_SNAKE_CASE     						= model.reverse_bettertransformer()
							model.save_pretrained(lowercase			)