





"""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 			__lowercase     (     unittest.TestCase							):







     '''simple docstring'''




     def 	_lowerCamelCase						(  self       ):
         # For consistency across different places the DisjunctiveConstraint is called,
         # dc.token_ids is a list of integers. It is also initialized only by integers.

         __a	:  Union[str, Any]         =					[[1, 2, 4], [1, 2, 3, 4]]
         __a	:  Any         =					DisjunctiveConstraint(_UpperCAmelCase       )
         self.assertTrue(isinstance(dc.token_ids		,     _UpperCAmelCase       )       )

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

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







     def 	_lowerCamelCase						(  self       ):
         # We can't have constraints that are complete subsets of another. This leads to a preverse
         # interpretation of "constraint fulfillment": does generating [1,2,3] fulfill the constraint?
         # It would mean that it generated [1,2] which fulfills it, but it's in the middle of potentially
         # fulfilling [1,2,3,4]. If we believe that [1,2,3] does fulfill the constraint, then the algorithm
         # will necessarily never reach [1,2,3,4], giving users a false sense of control (better to just not allow it).
         __a	:  Dict         =					[[1, 2], [1, 2, 3, 4]]

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







     def 	_lowerCamelCase						(  self       ):
         __a	:  List[Any]         =					[[1, 2, 3], [1, 2, 4]]

         __a	:  Any         =					DisjunctiveConstraint(_UpperCAmelCase       )

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

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

         __a    ,  __a    ,  __a	:  str         =					dc.update(3       )
         __a	:  Optional[Any]         =					stepped is True and completed is True and reset is False
         self.assertTrue(_UpperCAmelCase       )
         self.assertTrue(dc.completed       )  # Completed!
         self.assertTrue(dc.current_seq == [1, 2, 3]       )







     def 	_lowerCamelCase						(  self       ):
         __a	:  List[Any]         =					[[1, 2, 3], [1, 2, 4, 5], [1, 2, 5]]

         __a	:  Union[str, Any]         =					DisjunctiveConstraint(_UpperCAmelCase       )

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

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

         __a    ,  __a    ,  __a	:  Tuple         =					dc.update(4       )
         self.assertTrue(not dc.completed       )
         self.assertTrue(dc.current_seq == [1, 2, 4]       )

         __a    ,  __a    ,  __a	:  Union[str, Any]         =					dc.update(5       )
         self.assertTrue(dc.completed       )  # Completed!
         self.assertTrue(dc.current_seq == [1, 2, 4, 5]       )

         dc.reset()

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

         __a    ,  __a    ,  __a	:  List[Any]         =					dc.update(2       )
         self.assertTrue(not dc.completed       )
         self.assertTrue(dc.remaining() == 2       )
         self.assertTrue(dc.current_seq == [1, 2]       )

         __a    ,  __a    ,  __a	:  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"""






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding




class 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					['''image_processor''', '''tokenizer''']
					__lowerCAmelCase					=					'''CLIPImageProcessor'''
					__lowerCAmelCase					=					('''XLMRobertaTokenizer''', '''XLMRobertaTokenizerFast''')







					def __init__(  self		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     **_UpperCAmelCase       ):
									__a	:  Optional[Any]         =					None
									if "feature_extractor" in kwargs:
													warnings.warn(
													    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
													    ''' instead.'''		,     _UpperCAmelCase		,     )
													__a	:  Any         =					kwargs.pop('''feature_extractor'''       )

									__a	:  Optional[Any]         =					image_processor if image_processor is not None else feature_extractor
									if image_processor is None:
													raise ValueError('''You need to specify an `image_processor`.'''       )
									if tokenizer is None:
													raise ValueError('''You need to specify a `tokenizer`.'''       )

									super().__init__(_UpperCAmelCase		,     _UpperCAmelCase       )







					def __call__(  self		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     _UpperCAmelCase=None		,     **_UpperCAmelCase       ):

									if text is None and images is None:
													raise ValueError('''You have to specify either text or images. Both cannot be none.'''       )

									if text is not None:
													__a	:  Any         =					self.tokenizer(_UpperCAmelCase		,     return_tensors=_UpperCAmelCase		,     **_UpperCAmelCase       )

									if images is not None:
													__a	:  List[str]         =					self.image_processor(_UpperCAmelCase		,     return_tensors=_UpperCAmelCase		,     **_UpperCAmelCase       )

									if text is not None and images is not None:
													__a	:  Optional[int]         =					image_features.pixel_values
													return encoding
									elif text is not None:
													return encoding
									else:
													return BatchEncoding(data=dict(**_UpperCAmelCase       )		,     tensor_type=_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self		,     *_UpperCAmelCase		,     **_UpperCAmelCase       ):
									return self.tokenizer.batch_decode(*_UpperCAmelCase		,     **_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self		,     *_UpperCAmelCase		,     **_UpperCAmelCase       ):
									return self.tokenizer.decode(*_UpperCAmelCase		,     **_UpperCAmelCase       )







					@property
					def 	_lowerCamelCase						(  self       ):
									__a	:  Union[str, Any]         =					self.tokenizer.model_input_names
									__a	:  Dict         =					self.image_processor.model_input_names
									return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names       )       )