



# 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       SCREAMING_SNAKE_CASE	(							_a    ):
							__lowerCamelCase		:		List[Any]			='''facebook/bart-large-mnli'''
							__lowerCamelCase		:		str			=(
							    '''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		:		Optional[int]			='''text_classifier'''
							__lowerCamelCase		:		Optional[Any]			=AutoTokenizer
							__lowerCamelCase		:		List[str]			=AutoModelForSequenceClassification

							__lowerCamelCase		:		List[Any]			=['''text''', ['''text''']]
							__lowerCamelCase		:		Union[str, Any]			=['''text''']





							def   UpperCamelCase_    (      self						:						Union[str, Any]		):

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





							def   UpperCamelCase_    (      self						:						List[str]					, __lowercase						:						Optional[Any]					, __lowercase						:						Any		):

											'''simple docstring'''
											__a        =						labels
											return self.pre_processor(
											    [text] * len(__lowercase		)					, [F"This example is {label}" for label in labels]					, return_tensors="""pt"""					, padding="""max_length"""					, )







							def   UpperCamelCase_    (      self						:						Optional[int]					, __lowercase						:						str		):

											'''simple docstring'''
											__a        =						outputs.logits
											__a        =						torch.argmax(logits[:, 2]		).item()
											return self._labels[label_id]







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import GLPNImageProcessor



class 					__lowerCAmelCase       (			unittest.TestCase     ):






			def __init__(self					,							__magic_name__					,							__magic_name__=7					,							__magic_name__=3					,							__magic_name__=18					,							__magic_name__=30					,							__magic_name__=400					,							__magic_name__=True					,							__magic_name__=32					,							__magic_name__=True					,							)     ->   Dict:


							'''simple docstring'''



							snake_case_						:    Tuple				=  parent
							snake_case_						:    Union[str, Any]				=  batch_size
							snake_case_						:    Union[str, Any]				=  num_channels
							snake_case_						:    Optional[Any]				=  image_size
							snake_case_						:    int				=  min_resolution
							snake_case_						:    Any				=  max_resolution
							snake_case_						:    Tuple				=  do_resize
							snake_case_						:    str				=  size_divisor
							snake_case_						:    Optional[Any]				=  do_rescale






			def 			lowerCamelCase  (self    )     ->   Dict:


							'''simple docstring'''



							return {
							    "do_resize": self.do_resize,
							    "size_divisor": self.size_divisor,
							    "do_rescale": self.do_rescale,
							}




@require_torch
@require_vision
class 					__lowerCAmelCase       (			_a,     unittest.TestCase     ):
			lowerCamelCase_     :     Optional[Any]       			=      GLPNImageProcessor if is_vision_available() else None






			def 			lowerCamelCase  (self    )     ->   Optional[Any]:


							'''simple docstring'''



							snake_case_						:    str				=  GLPNImageProcessingTester(self    )






			@property
			def 			lowerCamelCase  (self    )     ->   Union[str, Any]:


							'''simple docstring'''



							return self.image_processor_tester.prepare_image_processor_dict()






			def 			lowerCamelCase  (self    )     ->   List[str]:


							'''simple docstring'''



							snake_case_						:    Optional[int]				=  self.image_processing_class(**self.image_processor_dict    )
							self.assertTrue(hasattr(__magic_name__					,							'''do_resize'''    )    )
							self.assertTrue(hasattr(__magic_name__					,							'''size_divisor'''    )    )
							self.assertTrue(hasattr(__magic_name__					,							'''resample'''    )    )
							self.assertTrue(hasattr(__magic_name__					,							'''do_rescale'''    )    )






			def 			lowerCamelCase  (self    )     ->   List[Any]:


							'''simple docstring'''



							pass






			def 			lowerCamelCase  (self    )     ->   int:


							'''simple docstring'''



							snake_case_						:    Optional[Any]				=  self.image_processing_class(**self.image_processor_dict    )
							# create random PIL images
							snake_case_						:    Union[str, Any]				=  prepare_image_inputs(self.image_processor_tester					,							equal_resolution=__magic_name__    )
							for image in image_inputs:
											self.assertIsInstance(__magic_name__					,							Image.Image    )

							# Test not batched input (GLPNImageProcessor doesn't support batching)
							snake_case_						:    Optional[int]				=  image_processing(image_inputs[0]					,							return_tensors='''pt'''    ).pixel_values
							self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0    )
							self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0    )






			def 			lowerCamelCase  (self    )     ->   Dict:


							'''simple docstring'''



							snake_case_						:    str				=  self.image_processing_class(**self.image_processor_dict    )
							# create random numpy tensors
							snake_case_						:    List[str]				=  prepare_image_inputs(self.image_processor_tester					,							equal_resolution=__magic_name__					,							numpify=__magic_name__    )
							for image in image_inputs:
											self.assertIsInstance(__magic_name__					,							np.ndarray    )

							# Test not batched input (GLPNImageProcessor doesn't support batching)
							snake_case_						:    Any				=  image_processing(image_inputs[0]					,							return_tensors='''pt'''    ).pixel_values
							self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0    )
							self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0    )






			def 			lowerCamelCase  (self    )     ->   Optional[Any]:


							'''simple docstring'''



							snake_case_						:    Dict				=  self.image_processing_class(**self.image_processor_dict    )
							# create random PyTorch tensors
							snake_case_						:    Optional[Any]				=  prepare_image_inputs(self.image_processor_tester					,							equal_resolution=__magic_name__					,							torchify=__magic_name__    )
							for image in image_inputs:
											self.assertIsInstance(__magic_name__					,							torch.Tensor    )

							# Test not batched input (GLPNImageProcessor doesn't support batching)
							snake_case_						:    Dict				=  image_processing(image_inputs[0]					,							return_tensors='''pt'''    ).pixel_values
							self.assertTrue(encoded_images.shape[-1] % self.image_processor_tester.size_divisor == 0    )
							self.assertTrue(encoded_images.shape[-2] % self.image_processor_tester.size_divisor == 0    )
