


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 : Union[str, Any] 			=				{"LayoutLMv2Config", "LayoutLMv3Config"}



@is_pipeline_test
class    UpperCAmelCase_	(							unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase      :     List[Any]							=					MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
       __UpperCamelCase      :     Optional[Any]							=					TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

       if model_mapping is not None:
              __UpperCamelCase      :     Tuple							=					{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
       if tf_model_mapping is not None:
              __UpperCamelCase      :     List[str]							=					{
                  config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
              }






       @require_torch
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Union[str, Any]   	=		pipeline(
                  task='''text-classification''' ,					model='''hf-internal-testing/tiny-random-distilbert''' ,					framework='''pt'''						)

              UpperCamelCase    :	Optional[int]   	=		text_classifier('''This is great !'''						)
              self.assertEqual(nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[{'''label''': '''LABEL_0''', '''score''': 0.504}]						)

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

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

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

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

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

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

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






       @require_torch
       def  _lowercase   (     self						):




              """simple docstring"""




              import torch

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

              UpperCamelCase    :	List[Any]   	=		text_classifier('''This is great !'''						)
              self.assertEqual(nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[{'''label''': '''LABEL_0''', '''score''': 0.504}]						)






       @require_tf
       def  _lowercase   (     self						):




              """simple docstring"""




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

              UpperCamelCase    :	Optional[int]   	=		text_classifier('''This is great !'''						)
              self.assertEqual(nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[{'''label''': '''LABEL_0''', '''score''': 0.504}]						)






       @slow
       @require_torch
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Optional[int]   	=		pipeline('''text-classification'''						)

              UpperCamelCase    :	Tuple   	=		text_classifier('''This is great !'''						)
              self.assertEqual(nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[{'''label''': '''POSITIVE''', '''score''': 1.0}]						)
              UpperCamelCase    :	int   	=		text_classifier('''This is bad !'''						)
              self.assertEqual(nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[{'''label''': '''NEGATIVE''', '''score''': 1.0}]						)
              UpperCamelCase    :	Tuple   	=		text_classifier('''Birds are a type of animal'''						)
              self.assertEqual(nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[{'''label''': '''POSITIVE''', '''score''': 0.988}]						)






       @slow
       @require_tf
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	List[str]   	=		pipeline('''text-classification''' ,					framework='''tf'''						)

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






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		TextClassificationPipeline(model=__SCREAMING_SNAKE_CASE ,					tokenizer=__SCREAMING_SNAKE_CASE						)
              return text_classifier, ["HuggingFace is in", "This is another test"]






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		text_classifier.model
              # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
              UpperCamelCase    :	Tuple   	=		'''HuggingFace is in'''
              UpperCamelCase    :	Tuple   	=		text_classifier(__SCREAMING_SNAKE_CASE						)

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

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

              UpperCamelCase    :	Tuple   	=		{'''text''': '''HuggingFace is in ''', '''text_pair''': '''Paris is in France'''}
              UpperCamelCase    :	Union[str, Any]   	=		text_classifier(__SCREAMING_SNAKE_CASE						)
              self.assertEqual(
                  nested_simplify(__SCREAMING_SNAKE_CASE						) ,					{'''label''': ANY(__SCREAMING_SNAKE_CASE						), '''score''': ANY(__SCREAMING_SNAKE_CASE						)} ,					)
              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.
              UpperCamelCase    :	str   	=		[['''HuggingFace is in ''', '''Paris is in France''']]
              with self.assertRaises(__SCREAMING_SNAKE_CASE						):
                     text_classifier(__SCREAMING_SNAKE_CASE						)

              # This used to be valid for doing text pairs
              # We're keeping it working because of backward compatibility
              UpperCamelCase    :	str   	=		text_classifier([[['''HuggingFace is in ''', '''Paris is in France''']]]						)
              self.assertEqual(
                  nested_simplify(__SCREAMING_SNAKE_CASE						) ,					[{'''label''': ANY(__SCREAMING_SNAKE_CASE						), '''score''': ANY(__SCREAMING_SNAKE_CASE						)}] ,					)
              self.assertTrue(outputs[0]['''label'''] in model.config.idalabel.values()						)





import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionImageVariationPipeline
from diffusers.utils.testing_utils import load_image, require_torch_gpu, slow, torch_device


__UpperCAmelCase : Dict 			=				False



class    UpperCAmelCase_	(							unittest.TestCase):
       '''simple docstring'''
       pass




@slow
@require_torch_gpu
class    UpperCAmelCase_	(							unittest.TestCase):
       '''simple docstring'''






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Dict   	=		VersatileDiffusionImageVariationPipeline.from_pretrained('''shi-labs/versatile-diffusion'''						)
              pipe.to(__SCREAMING_SNAKE_CASE						)
              pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE						)

              UpperCamelCase    :	Union[str, Any]   	=		load_image(
                  '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''						)
              UpperCamelCase    :	str   	=		torch.manual_seed(0						)
              UpperCamelCase    :	Union[str, Any]   	=		pipe(
                  image=__SCREAMING_SNAKE_CASE ,					generator=__SCREAMING_SNAKE_CASE ,					guidance_scale=7.5 ,					num_inference_steps=50 ,					output_type='''numpy''' ,					).images

              UpperCamelCase    :	List[Any]   	=		image[0, 253:256, 253:256, -1]

              assert image.shape == (1, 512, 512, 3)
              UpperCamelCase    :	Dict   	=		np.array([0.0_441, 0.0_469, 0.0_507, 0.0_575, 0.0_632, 0.0_650, 0.0_865, 0.0_909, 0.0_945]						)

              assert np.abs(image_slice.flatten() - expected_slice						).max() < 1e-2

