





# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import torch

from ..models.auto import AutoModelForSequenceClassification, AutoTokenizer
from .base import PipelineTool




class 	UpperCamelCase_							(  UpperCamelCase__     ):
							lowerCamelCase_							     =       "facebook/bart-large-mnli"
							lowerCamelCase_							     =       (
							    "This is a tool that classifies an English text using provided labels. It takes two inputs: `text`, which "
							    "should be the text to classify, and `labels`, which should be the list of labels to use for classification. "
							    "It returns the most likely label in the list of provided `labels` for the input text."
							)
							lowerCamelCase_							     =       "text_classifier"
							lowerCamelCase_							     =       AutoTokenizer
							lowerCamelCase_							     =       AutoModelForSequenceClassification

							lowerCamelCase_							     =       ["text", ["text"]]
							lowerCamelCase_							     =       ["text"]


							def   _snake_case	(				self     :Optional[Any]		) -> Optional[int]:



									"""simple docstring"""



									super().setup()
									SCREAMING_SNAKE_CASE__								=   self.model.config
									SCREAMING_SNAKE_CASE__								=   -1
									for idx, label in config.idalabel.items():
											if label.lower().startswith("""entail"""		):
													SCREAMING_SNAKE_CASE__								=   int(__A		)
									if self.entailment_id == -1:
											raise ValueError("""Could not determine the entailment ID from the model config, please pass it at init."""		)


							def   _snake_case	(				self     :Optional[Any]				,					__A     :Optional[Any]				,					__A     :Dict		) -> Any:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   labels
									return self.pre_processor(
									    [text] * len(__A		)				,					[f'''This example is {label}''' for label in labels]				,					return_tensors="""pt"""				,					padding="""max_length"""				,					)


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



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   outputs.logits
									SCREAMING_SNAKE_CASE__								=   torch.argmax(logits[:, 2]		).item()
									return self._labels[label_id]






import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()




@slow
@require_torch_gpu
class 	UpperCamelCase_							(  unittest.TestCase     ):


							def   _snake_case	(				self     :Union[str, Any]		) -> List[str]:



									"""simple docstring"""



									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()


							def   _snake_case	(				self     :Any		) -> Optional[int]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   StableDiffusionKDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe.to(__A		)
									sd_pipe.set_progress_bar_config(disable=__A		)

									sd_pipe.set_scheduler("""sample_euler"""		)

									SCREAMING_SNAKE_CASE__								=   """A painting of a squirrel eating a burger"""
									SCREAMING_SNAKE_CASE__								=   torch.manual_seed(0		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe([prompt]				,					generator=__A				,					guidance_scale=9.0				,					num_inference_steps=20				,					output_type="""np"""		)

									SCREAMING_SNAKE_CASE__								=   output.images

									SCREAMING_SNAKE_CASE__								=   image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									SCREAMING_SNAKE_CASE__								=   np.array([0.0_4_4_7, 0.0_4_9_2, 0.0_4_6_8, 0.0_4_0_8, 0.0_3_8_3, 0.0_4_0_8, 0.0_3_5_4, 0.0_3_8_0, 0.0_3_3_9]		)

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


							def   _snake_case	(				self     :str		) -> Union[str, Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe.to(__A		)
									sd_pipe.set_progress_bar_config(disable=__A		)

									sd_pipe.set_scheduler("""sample_euler"""		)

									SCREAMING_SNAKE_CASE__								=   """A painting of a squirrel eating a burger"""
									SCREAMING_SNAKE_CASE__								=   torch.manual_seed(0		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe([prompt]				,					generator=__A				,					guidance_scale=9.0				,					num_inference_steps=20				,					output_type="""np"""		)

									SCREAMING_SNAKE_CASE__								=   output.images

									SCREAMING_SNAKE_CASE__								=   image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									SCREAMING_SNAKE_CASE__								=   np.array([0.1_2_3_7, 0.1_3_2_0, 0.1_4_3_8, 0.1_3_5_9, 0.1_3_9_0, 0.1_1_3_2, 0.1_2_7_7, 0.1_1_7_5, 0.1_1_1_2]		)

									assert np.abs(image_slice.flatten() - expected_slice		).max() < 5E-1


							def   _snake_case	(				self     :Tuple		) -> Optional[Any]:



									"""simple docstring"""



									SCREAMING_SNAKE_CASE__								=   StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe.to(__A		)
									sd_pipe.set_progress_bar_config(disable=__A		)

									sd_pipe.set_scheduler("""sample_dpmpp_2m"""		)

									SCREAMING_SNAKE_CASE__								=   """A painting of a squirrel eating a burger"""
									SCREAMING_SNAKE_CASE__								=   torch.manual_seed(0		)
									SCREAMING_SNAKE_CASE__								=   sd_pipe(
									    [prompt]				,					generator=__A				,					guidance_scale=7.5				,					num_inference_steps=15				,					output_type="""np"""				,					use_karras_sigmas=__A				,					)

									SCREAMING_SNAKE_CASE__								=   output.images

									SCREAMING_SNAKE_CASE__								=   image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									SCREAMING_SNAKE_CASE__								=   np.array(
									    [0.1_1_3_8_1_6_8_9, 0.1_2_1_1_2_9_2_1, 0.1_3_8_9_4_5_7, 0.1_2_5_4_9_6_0_6, 0.1_2_4_4_9_6_4, 0.1_0_8_3_1_5_1_7, 0.1_1_5_6_2_8_6_6, 0.1_0_8_6_7_8_1_6, 0.1_0_4_9_9_0_4_8]		)

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