






"""simple docstring"""
import os
import unittest

from huggingface_hub.utils import are_progress_bars_disabled

import transformers.models.bart.tokenization_bart
from transformers import logging
from transformers.testing_utils import CaptureLogger, mockenv, mockenv_context
from transformers.utils.logging import disable_progress_bar, enable_progress_bar



class      SCREAMING_SNAKE_CASE  (				unittest.TestCase						):





	"""simple docstring"""




	def    lowerCamelCase(self							):
		A_		:						Optional[Any]	    =   logging.get_logger()

		# the current default level is logging.WARNING
		A_		:						Optional[Any]	    =   logging.get_verbosity()

		logging.set_verbosity_error()
		self.assertEqual(logger.getEffectiveLevel()     ,     logging.get_verbosity()							)

		logging.set_verbosity_warning()
		self.assertEqual(logger.getEffectiveLevel()     ,     logging.get_verbosity()							)

		logging.set_verbosity_info()
		self.assertEqual(logger.getEffectiveLevel()     ,     logging.get_verbosity()							)

		logging.set_verbosity_debug()
		self.assertEqual(logger.getEffectiveLevel()     ,     logging.get_verbosity()							)

		# restore to the original level
		logging.set_verbosity(lowerCAmelCase_							)





	def    lowerCamelCase(self							):
		A_		:						List[str]	    =   logging.get_verbosity()

		A_		:						Dict	    =   logging.get_logger("""transformers.models.bart.tokenization_bart"""							)
		A_		:						Tuple	    =   """Testing 1, 2, 3"""

		# should be able to log warnings (if default settings weren't overridden by `pytest --log-level-all`)
		if level_origin <= logging.WARNING:
			with CaptureLogger(lowerCAmelCase_							) as cl:
				logger.warning(lowerCAmelCase_							)
			self.assertEqual(cl.out     ,     msg + """\n"""							)

		# this is setting the level for all of `transformers.*` loggers
		logging.set_verbosity_error()

		# should not be able to log warnings
		with CaptureLogger(lowerCAmelCase_							) as cl:
			logger.warning(lowerCAmelCase_							)
		self.assertEqual(cl.out     ,     """"""							)

		# should be able to log warnings again
		logging.set_verbosity_warning()
		with CaptureLogger(lowerCAmelCase_							) as cl:
			logger.warning(lowerCAmelCase_							)
		self.assertEqual(cl.out     ,     msg + """\n"""							)

		# restore to the original level
		logging.set_verbosity(lowerCAmelCase_							)





	@mockenv(TRANSFORMERS_VERBOSITY="""error"""							)
	def    lowerCamelCase(self							):
		# reset for the env var to take effect, next time some logger call is made
		transformers.utils.logging._reset_library_root_logger()
		# this action activates the env var
		A_		:						Optional[int]	    =   logging.get_logger("""transformers.models.bart.tokenization_bart"""							)

		A_		:						Optional[Any]	    =   os.getenv("""TRANSFORMERS_VERBOSITY"""     ,     lowerCAmelCase_							)
		A_		:						str	    =   logging.log_levels[env_level_str]

		A_		:						str	    =   logging.get_verbosity()
		self.assertEqual(
		    lowerCAmelCase_     ,     lowerCAmelCase_     ,     f"""TRANSFORMERS_VERBOSITY={env_level_str}/{env_level}, but internal verbosity is {current_level}"""     ,     )

		# restore to the original level
		A_		:						List[str]	    =   """"""
		transformers.utils.logging._reset_library_root_logger()





	@mockenv(TRANSFORMERS_VERBOSITY="""super-error"""							)
	def    lowerCamelCase(self							):
		# reset for the env var to take effect, next time some logger call is made
		transformers.utils.logging._reset_library_root_logger()
		A_		:						Tuple	    =   logging.logging.getLogger()
		with CaptureLogger(lowerCAmelCase_							) as cl:
			# this action activates the env var
			logging.get_logger("""transformers.models.bart.tokenization_bart"""							)
		self.assertIn("""Unknown option TRANSFORMERS_VERBOSITY=super-error"""     ,     cl.out							)

		# no need to restore as nothing was changed





	def    lowerCamelCase(self							):
		# testing `logger.warning_advice()`
		transformers.utils.logging._reset_library_root_logger()

		A_		:						Tuple	    =   logging.get_logger("""transformers.models.bart.tokenization_bart"""							)
		A_		:						List[Any]	    =   """Testing 1, 2, 3"""

		with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS="""1"""							):
			# nothing should be logged as env var disables this method
			with CaptureLogger(lowerCAmelCase_							) as cl:
				logger.warning_advice(lowerCAmelCase_							)
			self.assertEqual(cl.out     ,     """"""							)

		with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS=""""""							):
			# should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset
			with CaptureLogger(lowerCAmelCase_							) as cl:
				logger.warning_advice(lowerCAmelCase_							)
			self.assertEqual(cl.out     ,     msg + """\n"""							)






def 			__UpperCamelCase	(		):
	disable_progress_bar()
	assert are_progress_bars_disabled()

	enable_progress_bar()
	assert not are_progress_bars_disabled()









"""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  (				_SCREAMING_SNAKE_CASE						):





	"""simple docstring"""




	_A       :				Optional[int]    =				"""facebook/bart-large-mnli"""
	_A       :				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."""
	)
	_A       :				List[str]    =				"""text_classifier"""
	_A       :				Optional[int]    =				AutoTokenizer
	_A       :				Optional[Any]    =				AutoModelForSequenceClassification

	_A       :				List[str]    =				["""text""", ["""text"""]]
	_A       :				Dict    =				["""text"""]





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





	def    lowerCamelCase(self     ,     lowerCAmelCase_							):
		A_		:						str	    =   outputs.logits
		A_		:						Optional[int]	    =   torch.argmax(logits[:, 2]							).item()
		return self._labels[label_id]

