
import inspect
import unittest

from transformers import RegNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
		import jax
		import jax.numpy as jnp

		from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor




class 				lowerCamelCase__							(       unittest.TestCase):




	'''simple docstring'''




	def __init__(      self	:Tuple  ,     a	:Dict  ,     a	:int=3  ,     a	:Optional[Any]=3_2  ,     a	:List[Any]=3  ,     a	:str=1_0  ,     a	:Union[str, Any]=[1_0, 2_0, 3_0, 4_0]  ,     a	:Optional[Any]=[1, 1, 2, 1]  ,     a	:Optional[int]=True  ,     a	:Dict=True  ,     a	:str="relu"  ,     a	:str=3  ,     a	:List[Any]=None  ,     )	->				Union[str, Any]:
				__UpperCamelCase			:     List[Any]         =      parent
				__UpperCamelCase			:     Tuple         =      batch_size
				__UpperCamelCase			:     Any         =      image_size
				__UpperCamelCase			:     Tuple         =      num_channels
				__UpperCamelCase			:     Optional[int]         =      embeddings_size
				__UpperCamelCase			:     Optional[int]         =      hidden_sizes
				__UpperCamelCase			:     int         =      depths
				__UpperCamelCase			:     Optional[int]         =      is_training
				__UpperCamelCase			:     Union[str, Any]         =      use_labels
				__UpperCamelCase			:     Tuple         =      hidden_act
				__UpperCamelCase			:     Tuple         =      num_labels
				__UpperCamelCase			:     str         =      scope
				__UpperCamelCase			:     int         =      len(lowerCamelCase_		)







	def      _lowerCamelCase       (      self	:List[Any]		)	->				Any:
				__UpperCamelCase			:     List[Any]         =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

				__UpperCamelCase			:     int         =      self.get_config()

				return config, pixel_values







	def      _lowerCamelCase       (      self	:int		)	->				int:
				return RegNetConfig(
				    num_channels=self.num_channels  ,     embeddings_size=self.embeddings_size  ,     hidden_sizes=self.hidden_sizes  ,     depths=self.depths  ,     hidden_act=self.hidden_act  ,     num_labels=self.num_labels  ,     image_size=self.image_size  ,     )







	def      _lowerCamelCase       (      self	:int  ,     a	:Union[str, Any]  ,     a	:Any		)	->				Optional[Any]:
				__UpperCamelCase			:     str         =      FlaxRegNetModel(config=lowerCamelCase_		)
				__UpperCamelCase			:     Any         =      model(lowerCamelCase_		)

				# Output shape (b, c, h, w)
				self.parent.assertEqual(
				    result.last_hidden_state.shape  ,     (self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2)  ,     )







	def      _lowerCamelCase       (      self	:str  ,     a	:int  ,     a	:Tuple		)	->				int:
				__UpperCamelCase			:     int         =      self.num_labels
				__UpperCamelCase			:     Optional[int]         =      FlaxRegNetForImageClassification(config=lowerCamelCase_		)
				__UpperCamelCase			:     Optional[int]         =      model(lowerCamelCase_		)
				self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.num_labels)		)







	def      _lowerCamelCase       (      self	:str		)	->				Dict:
				__UpperCamelCase			:     List[str]         =      self.prepare_config_and_inputs()
				__UpperCamelCase     ,		__UpperCamelCase			:     Optional[Any]         =      config_and_inputs
				__UpperCamelCase			:     List[str]         =      {"pixel_values": pixel_values}
				return config, inputs_dict




@require_flax
class 				lowerCamelCase__							(       a__  ,		unittest.TestCase):




	'''simple docstring'''




	_A			=  (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

	_A			=  False
	_A			=  False
	_A			=  False







	def      _lowerCamelCase       (      self	:Dict		)	->				None:
				__UpperCamelCase			:     str         =      FlaxRegNetModelTester(self		)
				__UpperCamelCase			:     Optional[int]         =      ConfigTester(self  ,     config_class=lowerCamelCase_  ,     has_text_modality=lowerCamelCase_		)







	def      _lowerCamelCase       (      self	:Any		)	->				Dict:
				self.create_and_test_config_common_properties()
				self.config_tester.create_and_test_config_to_json_string()
				self.config_tester.create_and_test_config_to_json_file()
				self.config_tester.create_and_test_config_from_and_save_pretrained()
				self.config_tester.create_and_test_config_with_num_labels()
				self.config_tester.check_config_can_be_init_without_params()
				self.config_tester.check_config_arguments_init()







	def      _lowerCamelCase       (      self	:Optional[Any]		)	->				Dict:
				return







	def      _lowerCamelCase       (      self	:Union[str, Any]		)	->				Union[str, Any]:
				__UpperCamelCase			:     List[Any]         =      self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*lowerCamelCase_		)







	def      _lowerCamelCase       (      self	:List[Any]		)	->				List[Any]:
				__UpperCamelCase			:     List[Any]         =      self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_		)







	@unittest.skip(reason="RegNet does not use inputs_embeds"		)
	def      _lowerCamelCase       (      self	:str		)	->				List[Any]:
				pass







	@unittest.skip(reason="RegNet does not support input and output embeddings"		)
	def      _lowerCamelCase       (      self	:Union[str, Any]		)	->				str:
				pass







	def      _lowerCamelCase       (      self	:List[Any]		)	->				Optional[int]:
				__UpperCamelCase     ,		__UpperCamelCase			:     List[Any]         =      self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							__UpperCamelCase			:     Optional[Any]         =      model_class(lowerCamelCase_		)
							__UpperCamelCase			:     int         =      inspect.signature(model.__call__		)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							__UpperCamelCase			:     List[Any]         =      [*signature.parameters.keys()]

							__UpperCamelCase			:     Union[str, Any]         =      ["pixel_values"]
							self.assertListEqual(arg_names[:1]  ,     lowerCamelCase_		)







	def      _lowerCamelCase       (      self	:int		)	->				Tuple:
				def check_hidden_states_output(a	:Optional[int]  ,     a	:Any  ,     a	:Any		):
							__UpperCamelCase			:     List[str]         =      model_class(lowerCamelCase_		)

							__UpperCamelCase			:     Optional[int]         =      model(**self._prepare_for_class(lowerCamelCase_  ,     lowerCamelCase_		)		)

							__UpperCamelCase			:     Any         =      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

							__UpperCamelCase			:     List[Any]         =      self.model_tester.num_stages
							self.assertEqual(len(lowerCamelCase_		)  ,     expected_num_stages + 1		)

				__UpperCamelCase     ,		__UpperCamelCase			:     Union[str, Any]         =      self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							__UpperCamelCase			:     Optional[int]         =      True
							check_hidden_states_output(lowerCamelCase_  ,     lowerCamelCase_  ,     lowerCamelCase_		)

							# check that output_hidden_states also work using config
							del inputs_dict["output_hidden_states"]
							__UpperCamelCase			:     str         =      True

							check_hidden_states_output(lowerCamelCase_  ,     lowerCamelCase_  ,     lowerCamelCase_		)







	def      _lowerCamelCase       (      self	:Dict		)	->				List[Any]:
				__UpperCamelCase     ,		__UpperCamelCase			:     List[str]         =      self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							with self.subTest(model_class.__name__		):
										__UpperCamelCase			:     Optional[Any]         =      self._prepare_for_class(lowerCamelCase_  ,     lowerCamelCase_		)
										__UpperCamelCase			:     Optional[Any]         =      model_class(lowerCamelCase_		)

										@jax.jit
										def model_jitted(a	:str  ,     **a	:Dict		):
													return model(pixel_values=lowerCamelCase_  ,     **lowerCamelCase_		)

										with self.subTest("JIT Enabled"		):
													__UpperCamelCase			:     Optional[Any]         =      model_jitted(**lowerCamelCase_		).to_tuple()

										with self.subTest("JIT Disabled"		):
													with jax.disable_jit():
																__UpperCamelCase			:     Tuple         =      model_jitted(**lowerCamelCase_		).to_tuple()

										self.assertEqual(len(lowerCamelCase_		)  ,     len(lowerCamelCase_		)		)
										for jitted_output, output in zip(lowerCamelCase_  ,     lowerCamelCase_		):
													self.assertEqual(jitted_output.shape  ,     output.shape		)




def 			_SCREAMING_SNAKE_CASE      (	)      ->					str:






			'''simple docstring'''





			__UpperCamelCase			:     int         =      Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
			return image




@require_flax
class 				lowerCamelCase__							(       unittest.TestCase):




	'''simple docstring'''




	@cached_property
	def      _lowerCamelCase       (      self	:Tuple		)	->				Optional[int]:
				return AutoImageProcessor.from_pretrained("facebook/regnet-y-040"		) if is_vision_available() else None







	@slow
	def      _lowerCamelCase       (      self	:Dict		)	->				List[Any]:
				__UpperCamelCase			:     Dict         =      FlaxRegNetForImageClassification.from_pretrained("facebook/regnet-y-040"		)

				__UpperCamelCase			:     int         =      self.default_image_processor
				__UpperCamelCase			:     Union[str, Any]         =      prepare_img()
				__UpperCamelCase			:     int         =      image_processor(images=lowerCamelCase_  ,     return_tensors="np"		)

				__UpperCamelCase			:     int         =      model(**lowerCamelCase_		)

				# verify the logits
				__UpperCamelCase			:     List[Any]         =      (1, 1_0_0_0)
				self.assertEqual(outputs.logits.shape  ,     lowerCamelCase_		)

				__UpperCamelCase			:     Any         =      jnp.array([-0.4180, -1.5051, -3.4836]		)

				self.assertTrue(jnp.allclose(outputs.logits[0, :3]  ,     lowerCamelCase_  ,     atol=1E-4		)		)

# 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 				lowerCamelCase__							(       __lowercase):




	'''simple docstring'''




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

	_A			=  ['text', ['text']]
	_A			=  ['text']







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







	def      _lowerCamelCase       (      self	:Any  ,     a	:Any		)	->				Optional[int]:
								__UpperCamelCase			:     Optional[int]         =      outputs.logits
								__UpperCamelCase			:     Dict         =      torch.argmax(logits[:, 2]		).item()
								return self._labels[label_id]