"""simple docstring"""





import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
			import torch

			from transformers.generation import DisjunctiveConstraint




@require_torch
class  _snake_case		(    unittest.TestCase ):







				"""simple docstring"""






				def 		_lowerCAmelCase		(      self							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						int			  =    [[1, 2, 4], [1, 2, 3, 4]]
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    DisjunctiveConstraint(_A)
											self.assertTrue(isinstance(dc.token_ids      ,    _A))

											with self.assertRaises(_A):
																		DisjunctiveConstraint(torch.LongTensor([[1, 2, 4], [1, 2, 3]]))

											with self.assertRaises(_A):
																		DisjunctiveConstraint([torch.LongTensor([1, 2, 4]), torch.LongTensor([1, 2, 3, 4, 5])])





				def 		_lowerCAmelCase		(      self							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    [[1, 2], [1, 2, 3, 4]]

											with self.assertRaises(_A):
																		DisjunctiveConstraint(_A)  # fails here





				def 		_lowerCAmelCase		(      self							:							Dict):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    [[1, 2, 3], [1, 2, 4]]

											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    DisjunctiveConstraint(_A)

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Any			  =    dc.update(1)
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    stepped is True and completed is False and reset is False
											self.assertTrue(_A)
											self.assertTrue(not dc.completed)
											self.assertTrue(dc.current_seq == [1])

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						List[str]			  =    dc.update(2)
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    stepped is True and completed is False and reset is False
											self.assertTrue(_A)
											self.assertTrue(not dc.completed)
											self.assertTrue(dc.current_seq == [1, 2])

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Dict			  =    dc.update(3)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    stepped is True and completed is True and reset is False
											self.assertTrue(_A)
											self.assertTrue(dc.completed)  # Completed!
											self.assertTrue(dc.current_seq == [1, 2, 3])





				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    [[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

											_SCREAMING_SNAKE_CASE		:						Dict			  =    DisjunctiveConstraint(_A)

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    dc.update(1)
											self.assertTrue(not dc.completed)
											self.assertTrue(dc.current_seq == [1])

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    dc.update(2)
											self.assertTrue(not dc.completed)
											self.assertTrue(dc.current_seq == [1, 2])

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    dc.update(4)
											self.assertTrue(not dc.completed)
											self.assertTrue(dc.current_seq == [1, 2, 4])

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						int			  =    dc.update(5)
											self.assertTrue(dc.completed)  # Completed!
											self.assertTrue(dc.current_seq == [1, 2, 4, 5])

											dc.reset()

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						List[str]			  =    dc.update(1)
											self.assertTrue(not dc.completed)
											self.assertTrue(dc.remaining() == 3)
											self.assertTrue(dc.current_seq == [1])

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    dc.update(2)
											self.assertTrue(not dc.completed)
											self.assertTrue(dc.remaining() == 2)
											self.assertTrue(dc.current_seq == [1, 2])

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    dc.update(5)
											self.assertTrue(dc.completed)  # Completed!
											self.assertTrue(dc.remaining() == 0)
											self.assertTrue(dc.current_seq == [1, 2, 5])



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







				"""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							:							int):



											"""simple docstring"""







											super().setup()
											_SCREAMING_SNAKE_CASE		:						Any			  =    self.model.config
											_SCREAMING_SNAKE_CASE		:						Any			  =    -1
											for idx, label in config.idalabel.items():
																		if label.lower().startswith("""entail"""):
																									_SCREAMING_SNAKE_CASE		:						List[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[Any]      ,    _A							:							Tuple      ,    _A							:							List[str]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    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							:							Tuple      ,    _A							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    outputs.logits
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    torch.argmax(logits[:, 2]).item()
											return self._labels[label_id]


