






'''simple docstring'''
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 LevitImageProcessor






class        __SCREAMING_SNAKE_CASE (     unittest.TestCase			):




 def __init__( self		:							Union[str, Any]   , __lowercase		:							List[Any]   , __lowercase		:							Any=7   , __lowercase		:							Tuple=3   , __lowercase		:							Any=18   , __lowercase		:							Optional[Any]=30   , __lowercase		:							List[Any]=4_00   , __lowercase		:							Optional[int]=True   , __lowercase		:							List[str]=None   , __lowercase		:							Union[str, Any]=True   , __lowercase		:							Union[str, Any]=None   , __lowercase		:							str=True   , __lowercase		:							Optional[int]=[0.5, 0.5, 0.5]   , __lowercase		:							Tuple=[0.5, 0.5, 0.5]   , )		->  Any:
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=size if size is not None else {'''shortest_edge''': 18}
       SCREAMING_SNAKE_CASE__       :			List[str]													=crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}
       SCREAMING_SNAKE_CASE__       :			List[Any]													=parent
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=batch_size
       SCREAMING_SNAKE_CASE__       :			str													=num_channels
       SCREAMING_SNAKE_CASE__       :			Any													=image_size
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=min_resolution
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=max_resolution
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=do_resize
       SCREAMING_SNAKE_CASE__       :			Optional[Any]													=size
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=do_center_crop
       SCREAMING_SNAKE_CASE__       :			Union[str, Any]													=crop_size
       SCREAMING_SNAKE_CASE__       :			str													=do_normalize
       SCREAMING_SNAKE_CASE__       :			Tuple													=image_mean
       SCREAMING_SNAKE_CASE__       :			Dict													=image_std




 def        __magic_name__		( self		:							Optional[Any] )		->  str:
       return {
           "image_mean": self.image_mean,
           "image_std": self.image_std,
           "do_normalize": self.do_normalize,
           "do_resize": self.do_resize,
           "do_center_crop": self.do_center_crop,
           "size": self.size,
           "crop_size": self.crop_size,
       }






@require_torch
@require_vision
class        __SCREAMING_SNAKE_CASE (     lowerCamelCase		,			unittest.TestCase			):
 snake_case_												=							LevitImageProcessor if is_vision_available() else None




 def        __magic_name__		( self		:							Union[str, Any] )		->  int:
       SCREAMING_SNAKE_CASE__       :			str													=LevitImageProcessingTester(self )




 @property
 def        __magic_name__		( self		:							Optional[Any] )		->  Tuple:
       return self.image_processor_tester.prepare_image_processor_dict()




 def        __magic_name__		( self		:							List[Any] )		->  List[str]:
       SCREAMING_SNAKE_CASE__       :			Tuple													=self.image_processing_class(**self.image_processor_dict )
       self.assertTrue(hasattr(__lowercase   , '''image_mean''' ) )
       self.assertTrue(hasattr(__lowercase   , '''image_std''' ) )
       self.assertTrue(hasattr(__lowercase   , '''do_normalize''' ) )
       self.assertTrue(hasattr(__lowercase   , '''do_resize''' ) )
       self.assertTrue(hasattr(__lowercase   , '''do_center_crop''' ) )
       self.assertTrue(hasattr(__lowercase   , '''size''' ) )




 def        __magic_name__		( self		:							int )		->  int:
       SCREAMING_SNAKE_CASE__       :			Tuple													=self.image_processing_class.from_dict(self.image_processor_dict )
       self.assertEqual(image_processor.size   , {'''shortest_edge''': 18} )
       self.assertEqual(image_processor.crop_size   , {'''height''': 18, '''width''': 18} )

       SCREAMING_SNAKE_CASE__       :			List[Any]													=self.image_processing_class.from_dict(self.image_processor_dict   , size=42   , crop_size=84 )
       self.assertEqual(image_processor.size   , {'''shortest_edge''': 42} )
       self.assertEqual(image_processor.crop_size   , {'''height''': 84, '''width''': 84} )




 def        __magic_name__		( self		:							List[str] )		->  int:
       pass




 def        __magic_name__		( self		:							List[Any] )		->  Optional[int]:
       # Initialize image_processing
       SCREAMING_SNAKE_CASE__       :			int													=self.image_processing_class(**self.image_processor_dict )
       # create random PIL images
       SCREAMING_SNAKE_CASE__       :			List[str]													=prepare_image_inputs(self.image_processor_tester   , equal_resolution=__lowercase )
       for image in image_inputs:
             self.assertIsInstance(__lowercase   , Image.Image )

       # Test not batched input
       SCREAMING_SNAKE_CASE__       :			List[Any]													=image_processing(image_inputs[0]   , return_tensors='''pt''' ).pixel_values
       self.assertEqual(
           encoded_images.shape   , (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )   , )

       # Test batched
       SCREAMING_SNAKE_CASE__       :			str													=image_processing(__lowercase   , return_tensors='''pt''' ).pixel_values
       self.assertEqual(
           encoded_images.shape   , (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )   , )




 def        __magic_name__		( self		:							Dict )		->  Any:
       # Initialize image_processing
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=self.image_processing_class(**self.image_processor_dict )
       # create random numpy tensors
       SCREAMING_SNAKE_CASE__       :			List[Any]													=prepare_image_inputs(self.image_processor_tester   , equal_resolution=__lowercase   , numpify=__lowercase )
       for image in image_inputs:
             self.assertIsInstance(__lowercase   , np.ndarray )

       # Test not batched input
       SCREAMING_SNAKE_CASE__       :			List[str]													=image_processing(image_inputs[0]   , return_tensors='''pt''' ).pixel_values
       self.assertEqual(
           encoded_images.shape   , (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )   , )

       # Test batched
       SCREAMING_SNAKE_CASE__       :			str													=image_processing(__lowercase   , return_tensors='''pt''' ).pixel_values
       self.assertEqual(
           encoded_images.shape   , (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )   , )




 def        __magic_name__		( self		:							Tuple )		->  Tuple:
       # Initialize image_processing
       SCREAMING_SNAKE_CASE__       :			int													=self.image_processing_class(**self.image_processor_dict )
       # create random PyTorch tensors
       SCREAMING_SNAKE_CASE__       :			Dict													=prepare_image_inputs(self.image_processor_tester   , equal_resolution=__lowercase   , torchify=__lowercase )
       for image in image_inputs:
             self.assertIsInstance(__lowercase   , torch.Tensor )

       # Test not batched input
       SCREAMING_SNAKE_CASE__       :			Optional[int]													=image_processing(image_inputs[0]   , return_tensors='''pt''' ).pixel_values
       self.assertEqual(
           encoded_images.shape   , (
               1,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )   , )

       # Test batched
       SCREAMING_SNAKE_CASE__       :			Dict													=image_processing(__lowercase   , return_tensors='''pt''' ).pixel_values
       self.assertEqual(
           encoded_images.shape   , (
               self.image_processor_tester.batch_size,
               self.image_processor_tester.num_channels,
               self.image_processor_tester.crop_size['''height'''],
               self.image_processor_tester.crop_size['''width'''],
           )   , )







'''simple docstring'''
# 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 (     lowerCamelCase			):
 snake_case_												=							"""facebook/bart-large-mnli"""
 snake_case_												=							(
     """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."""
 )
 snake_case_												=							"""text_classifier"""
 snake_case_												=							AutoTokenizer
 snake_case_												=							AutoModelForSequenceClassification

 snake_case_												=							["""text""", ["""text"""]]
 snake_case_												=							["""text"""]




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




 def        __magic_name__		( self		:							List[Any]   , __lowercase		:							str )		->  List[Any]:
       SCREAMING_SNAKE_CASE__       :			int													=outputs.logits
       SCREAMING_SNAKE_CASE__       :			Any													=torch.argmax(logits[:, 2] ).item()
       return self._labels[label_id]