

# 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     snake_case_			(				_a							):


							"""simple docstring"""





							__UpperCAmelCase       ="""facebook/bart-large-mnli"""
							__UpperCAmelCase       =(
							    """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."""
							)
							__UpperCAmelCase       ="""text_classifier"""
							__UpperCAmelCase       =AutoTokenizer
							__UpperCAmelCase       =AutoModelForSequenceClassification

							__UpperCAmelCase       =["""text""", ["""text"""]]
							__UpperCAmelCase       =["""text"""]




							def        A__  ( self							):
												super().setup()
												__lowerCAmelCase					=							self.model.config
												__lowerCAmelCase					=							-1
												for idx, label in config.idalabel.items():
																	if label.lower().startswith('entail'							):
																						__lowerCAmelCase					=							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        A__  ( self					,						_A					,						_A							):
												__lowerCAmelCase					=							labels
												return self.pre_processor(
												    [text] * len(_A							)					,						[F"""This example is {label}""" for label in labels]					,						return_tensors='pt'					,						padding='max_length'					,						)




							def        A__  ( self					,						_A							):
												__lowerCAmelCase					=							outputs.logits
												__lowerCAmelCase					=							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 ViTImageProcessor





class     snake_case_			(				unittest.TestCase							):


							"""simple docstring"""





							def __init__( self					,						_A					,						_A=1_3					,						_A=3					,						_A=2_2_4					,						_A=3_0					,						_A=4_0_0					,						_A=True					,						_A=None					,						_A=True					,						_A=[0.5, 0.5, 0.5]					,						_A=[0.5, 0.5, 0.5]					,						):
												__lowerCAmelCase					=							size if size is not None else {'height': 1_8, 'width': 1_8}
												__lowerCAmelCase					=							parent
												__lowerCAmelCase					=							batch_size
												__lowerCAmelCase					=							num_channels
												__lowerCAmelCase					=							image_size
												__lowerCAmelCase					=							min_resolution
												__lowerCAmelCase					=							max_resolution
												__lowerCAmelCase					=							do_resize
												__lowerCAmelCase					=							size
												__lowerCAmelCase					=							do_normalize
												__lowerCAmelCase					=							image_mean
												__lowerCAmelCase					=							image_std




							def        A__  ( self							):
												return {
												    "image_mean": self.image_mean,
												    "image_std": self.image_std,
												    "do_normalize": self.do_normalize,
												    "do_resize": self.do_resize,
												    "size": self.size,
												}





@require_torch
@require_vision
class     snake_case_			(				_a			, unittest.TestCase							):


							"""simple docstring"""





							__UpperCAmelCase       =ViTImageProcessor if is_vision_available() else None




							def        A__  ( self							):
												__lowerCAmelCase					=							EfficientFormerImageProcessorTester(self							)




							@property
							def        A__  ( self							):
												return self.image_proc_tester.prepare_image_processor_dict()




							def        A__  ( self							):
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												self.assertTrue(hasattr(_A					,						'image_mean'							)							)
												self.assertTrue(hasattr(_A					,						'image_std'							)							)
												self.assertTrue(hasattr(_A					,						'do_normalize'							)							)
												self.assertTrue(hasattr(_A					,						'do_resize'							)							)
												self.assertTrue(hasattr(_A					,						'size'							)							)




							def        A__  ( self							):
												pass




							def        A__  ( self							):
												# Initialize image_processor
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												# create random PIL images
												__lowerCAmelCase					=							prepare_image_inputs(self.image_proc_tester					,						equal_resolution=_A							)
												for image in image_inputs:
																	self.assertIsInstance(_A					,						Image.Image							)

												# Test not batched input
												__lowerCAmelCase					=							image_processor(image_inputs[0]					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        1,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)

												# Test batched
												__lowerCAmelCase					=							image_processor(_A					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        self.image_proc_tester.batch_size,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)




							def        A__  ( self							):
												# Initialize image_processor
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												# create random numpy tensors
												__lowerCAmelCase					=							prepare_image_inputs(self.image_proc_tester					,						equal_resolution=_A					,						numpify=_A							)
												for image in image_inputs:
																	self.assertIsInstance(_A					,						np.ndarray							)

												# Test not batched input
												__lowerCAmelCase					=							image_processor(image_inputs[0]					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        1,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)

												# Test batched
												__lowerCAmelCase					=							image_processor(_A					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        self.image_proc_tester.batch_size,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)




							def        A__  ( self							):
												# Initialize image_processor
												__lowerCAmelCase					=							self.image_processing_class(**self.image_processor_dict							)
												# create random PyTorch tensors
												__lowerCAmelCase					=							prepare_image_inputs(self.image_proc_tester					,						equal_resolution=_A					,						torchify=_A							)
												for image in image_inputs:
																	self.assertIsInstance(_A					,						torch.Tensor							)

												# Test not batched input
												__lowerCAmelCase					=							image_processor(image_inputs[0]					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        1,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)

												# Test batched
												__lowerCAmelCase					=							image_processor(_A					,						return_tensors='pt'							).pixel_values
												self.assertEqual(
												    encoded_images.shape					,						(
												        self.image_proc_tester.batch_size,
												        self.image_proc_tester.num_channels,
												        self.image_proc_tester.size['height'],
												        self.image_proc_tester.size['width'],
												    )					,						)






