






'''simple docstring'''





import logging
import os
import threading
import time


try:
			import warnings
except ImportError:
			_UpperCAmelCase			:		Optional[int]   				=    None

try:
			import msvcrt
except ImportError:
			_UpperCAmelCase			:		List[str]   				=    None

try:
			import fcntl
except ImportError:
			_UpperCAmelCase			:		str   				=    None


# Backward compatibility
# ------------------------------------------------
try:
			TimeoutError
except NameError:
			_UpperCAmelCase			:		Union[str, Any]   				=    OSError


# Data
# ------------------------------------------------
_UpperCAmelCase			:		Union[str, Any]   				=    [
    "Timeout",
    "BaseFileLock",
    "WindowsFileLock",
    "UnixFileLock",
    "SoftFileLock",
    "FileLock",
]

_UpperCAmelCase			:		Tuple   				=    "3.0.12"


_UpperCAmelCase			:		str   				=    None





def 	lowerCAmelCase_		()		->    Tuple:


				'''simple docstring'''




				global _logger
				lowerCAmelCase__      			=						_logger or logging.getLogger(__name__       )
				return _logger
class  lowerCAmelCase_       (				snake_case__					):




							def __init__(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   int	):
											lowerCAmelCase__      			=						lock_file
											return None




							def __str__(   self     :   List[Any]	):
											lowerCAmelCase__      			=						f'The file lock \'{self.lock_file}\' could not be acquired.'
											return temp



class  lowerCAmelCase_       :




							def __init__(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   List[str]	):
											lowerCAmelCase__      			=						lock
											return None




							def __enter__(   self     :   Any	):
											return self.lock




							def __exit__(   self     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Optional[int]	):
											self.lock.release()
											return None



class  lowerCAmelCase_       :




							def __init__(   self     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   Any=-1 ,							SCREAMING_SNAKE_CASE_     :   Any=None	):
											lowerCAmelCase__      			=						max_filename_length if max_filename_length is not None else 255
											# Hash the filename if it's too long
											lowerCAmelCase__      			=						self.hash_filename_if_too_long(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)
											# The path to the lock file.
											lowerCAmelCase__      			=						lock_file

											# The file descriptor for the *_lock_file* as it is returned by the
											# os.open() function.
											# This file lock is only NOT None, if the object currently holds the
											# lock.
											lowerCAmelCase__      			=						None

											# The default timeout value.
											lowerCAmelCase__      			=						timeout

											# We use this lock primarily for the lock counter.
											lowerCAmelCase__      			=						threading.Lock()

											# The lock counter is used for implementing the nested locking
											# mechanism. Whenever the lock is acquired, the counter is increased and
											# the lock is only released, when this value is 0 again.
											lowerCAmelCase__      			=						0
											return None




							@property
							def 						__snake_case					(   self     :   str	):
											return self._lock_file




							@property
							def 						__snake_case					(   self     :   Optional[int]	):
											return self._timeout




							@timeout.setter
							def 						__snake_case					(   self     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[int]	):
											lowerCAmelCase__      			=						float(SCREAMING_SNAKE_CASE_	)
											return None




							def 						__snake_case					(   self     :   List[Any]	):
											raise NotImplementedError()




							def 						__snake_case					(   self     :   Any	):
											raise NotImplementedError()




							@property
							def 						__snake_case					(   self     :   Union[str, Any]	):
											return self._lock_file_fd is not None




							def 						__snake_case					(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   int=None ,							SCREAMING_SNAKE_CASE_     :   str=0.05	):
											# Use the default timeout, if no timeout is provided.
											if timeout is None:
															lowerCAmelCase__      			=						self.timeout

											# Increment the number right at the beginning.
											# We can still undo it, if something fails.
											with self._thread_lock:
															self._lock_counter += 1

											lowerCAmelCase__      			=						id(self	)
											lowerCAmelCase__      			=						self._lock_file
											lowerCAmelCase__      			=						time.time()
											try:
															while True:
																			with self._thread_lock:
																							if not self.is_locked:
																											logger().debug(f'Attempting to acquire lock {lock_id} on {lock_filename}'	)
																											self._acquire()

																			if self.is_locked:
																							logger().debug(f'Lock {lock_id} acquired on {lock_filename}'	)
																							break
																			elif timeout >= 0 and time.time() - start_time > timeout:
																							logger().debug(f'Timeout on acquiring lock {lock_id} on {lock_filename}'	)
																							raise Timeout(self._lock_file	)
																			else:
																							logger().debug(
																							    f'Lock {lock_id} not acquired on {lock_filename}, waiting {poll_intervall} seconds ...'	)
																							time.sleep(SCREAMING_SNAKE_CASE_	)
											except:  # noqa
															# Something did go wrong, so decrement the counter.
															with self._thread_lock:
																			lowerCAmelCase__      			=						max(0 ,							self._lock_counter - 1	)

															raise
											return _Acquire_ReturnProxy(lock=self	)




							def 						__snake_case					(   self     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   Dict=False	):
											with self._thread_lock:
															if self.is_locked:
																			self._lock_counter -= 1

																			if self._lock_counter == 0 or force:
																							lowerCAmelCase__      			=						id(self	)
																							lowerCAmelCase__      			=						self._lock_file

																							logger().debug(f'Attempting to release lock {lock_id} on {lock_filename}'	)
																							self._release()
																							lowerCAmelCase__      			=						0
																							logger().debug(f'Lock {lock_id} released on {lock_filename}'	)

											return None




							def __enter__(   self     :   int	):
											self.acquire()
											return self




							def __exit__(   self     :   List[str] ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Tuple ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]	):
											self.release()
											return None




							def __del__(   self     :   Optional[int]	):
											self.release(force=SCREAMING_SNAKE_CASE_	)
											return None




							def 						__snake_case					(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Dict	):
											lowerCAmelCase__      			=						os.path.basename(SCREAMING_SNAKE_CASE_	)
											if len(SCREAMING_SNAKE_CASE_	) > max_length and max_length > 0:
															lowerCAmelCase__      			=						os.path.dirname(SCREAMING_SNAKE_CASE_	)
															lowerCAmelCase__      			=						str(hash(SCREAMING_SNAKE_CASE_	)	)
															lowerCAmelCase__      			=						filename[: max_length - len(SCREAMING_SNAKE_CASE_	) - 8] + '''...''' + hashed_filename + '''.lock'''
															return os.path.join(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)
											else:
															return path



class  lowerCAmelCase_       (				snake_case__					):




							def __init__(   self     :   Any ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]=-1 ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=None	):
											from .file_utils import relative_to_absolute_path

											super().__init__(SCREAMING_SNAKE_CASE_ ,							timeout=SCREAMING_SNAKE_CASE_ ,							max_filename_length=SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						'''\\\\?\\''' + relative_to_absolute_path(self.lock_file	)




							def 						__snake_case					(   self     :   Optional[int]	):
											lowerCAmelCase__      			=						os.O_RDWR | os.O_CREAT | os.O_TRUNC

											try:
															lowerCAmelCase__      			=						os.open(self._lock_file ,							SCREAMING_SNAKE_CASE_	)
											except OSError:
															pass
											else:
															try:
																			msvcrt.locking(SCREAMING_SNAKE_CASE_ ,							msvcrt.LK_NBLCK ,							1	)
															except OSError:
																			os.close(SCREAMING_SNAKE_CASE_	)
															else:
																			lowerCAmelCase__      			=						fd
											return None




							def 						__snake_case					(   self     :   Optional[Any]	):
											lowerCAmelCase__      			=						self._lock_file_fd
											lowerCAmelCase__      			=						None
											msvcrt.locking(SCREAMING_SNAKE_CASE_ ,							msvcrt.LK_UNLCK ,							1	)
											os.close(SCREAMING_SNAKE_CASE_	)

											try:
															os.remove(self._lock_file	)
											# Probably another instance of the application
											# that acquired the file lock.
											except OSError:
															pass
											return None



class  lowerCAmelCase_       (				snake_case__					):




							def __init__(   self     :   Any ,							SCREAMING_SNAKE_CASE_     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   List[str]=-1 ,							SCREAMING_SNAKE_CASE_     :   Dict=None	):
											lowerCAmelCase__      			=						os.statvfs(os.path.dirname(SCREAMING_SNAKE_CASE_	)	).f_namemax
											super().__init__(SCREAMING_SNAKE_CASE_ ,							timeout=SCREAMING_SNAKE_CASE_ ,							max_filename_length=SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Any	):
											lowerCAmelCase__      			=						os.O_RDWR | os.O_CREAT | os.O_TRUNC
											lowerCAmelCase__      			=						os.open(self._lock_file ,							SCREAMING_SNAKE_CASE_	)

											try:
															fcntl.flock(SCREAMING_SNAKE_CASE_ ,							fcntl.LOCK_EX | fcntl.LOCK_NB	)
											except OSError:
															os.close(SCREAMING_SNAKE_CASE_	)
											else:
															lowerCAmelCase__      			=						fd
											return None




							def 						__snake_case					(   self     :   Any	):
											# Do not remove the lockfile:
											#
											#   https://github.com/benediktschmitt/py-filelock/issues/31
											#   https://stackoverflow.com/questions/17708885/flock-removing-locked-file-without-race-condition
											lowerCAmelCase__      			=						self._lock_file_fd
											lowerCAmelCase__      			=						None
											fcntl.flock(SCREAMING_SNAKE_CASE_ ,							fcntl.LOCK_UN	)
											os.close(SCREAMING_SNAKE_CASE_	)
											return None



class  lowerCAmelCase_       (				snake_case__					):




							def 						__snake_case					(   self     :   Union[str, Any]	):
											lowerCAmelCase__      			=						os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_TRUNC
											try:
															lowerCAmelCase__      			=						os.open(self._lock_file ,							SCREAMING_SNAKE_CASE_	)
											except OSError:
															pass
											else:
															lowerCAmelCase__      			=						fd
											return None




							def 						__snake_case					(   self     :   Tuple	):
											os.close(self._lock_file_fd	)
											lowerCAmelCase__      			=						None

											try:
															os.remove(self._lock_file	)
											# The file is already deleted and that's what we want.
											except OSError:
															pass
											return None



_UpperCAmelCase			:		Optional[Any]   				=    None

if msvcrt:
			_UpperCAmelCase			:		int   				=    WindowsFileLock
elif fcntl:
			_UpperCAmelCase			:		Any   				=    UnixFileLock
else:
			_UpperCAmelCase			:		Dict   				=    SoftFileLock

			if warnings is not None:
						warnings.warn("only soft file lock is available")













import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class  lowerCAmelCase_       (				snake_case__					):
							UpperCamelCase_		:Tuple		      =    ['image_processor', 'tokenizer']
							UpperCamelCase_		:Tuple		      =    'ViTImageProcessor'
							UpperCamelCase_		:Dict		      =    ('CLIPTokenizer', 'CLIPTokenizerFast')




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

											lowerCAmelCase__      			=						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__(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)




							def __call__(   self     :   Dict ,							SCREAMING_SNAKE_CASE_     :   Any=None ,							SCREAMING_SNAKE_CASE_     :   Tuple=None ,							SCREAMING_SNAKE_CASE_     :   Dict=None ,							SCREAMING_SNAKE_CASE_     :   Optional[int]=None ,							**SCREAMING_SNAKE_CASE_     :   Optional[int]	):
											if text is None and visual_prompt is None and images is None:
															raise ValueError('''You have to specify either text, visual prompt or images.'''	)

											if text is not None and visual_prompt is not None:
															raise ValueError('''You have to specify exactly one type of prompt. Either text or visual prompt.'''	)

											if text is not None:
															lowerCAmelCase__      			=						self.tokenizer(SCREAMING_SNAKE_CASE_ ,							return_tensors=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)

											if visual_prompt is not None:
															lowerCAmelCase__      			=						self.image_processor(SCREAMING_SNAKE_CASE_ ,							return_tensors=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)

											if images is not None:
															lowerCAmelCase__      			=						self.image_processor(SCREAMING_SNAKE_CASE_ ,							return_tensors=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)

											if visual_prompt is not None and images is not None:
															lowerCAmelCase__      			=						{
															    '''pixel_values''': image_features.pixel_values,
															    '''conditional_pixel_values''': prompt_features.pixel_values,
															}
															return encoding
											elif text is not None and images is not None:
															lowerCAmelCase__      			=						image_features.pixel_values
															return encoding
											elif text is not None:
															return encoding
											elif visual_prompt is not None:
															lowerCAmelCase__      			=						{
															    '''conditional_pixel_values''': prompt_features.pixel_values,
															}
															return encoding
											else:
															return BatchEncoding(data=dict(**SCREAMING_SNAKE_CASE_	) ,							tensor_type=SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Union[str, Any] ,							*SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							**SCREAMING_SNAKE_CASE_     :   str	):
											return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Optional[int] ,							*SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							**SCREAMING_SNAKE_CASE_     :   Tuple	):
											return self.tokenizer.decode(*SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_	)




							@property
							def 						__snake_case					(   self     :   str	):
											warnings.warn(
											    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''' ,							SCREAMING_SNAKE_CASE_ ,							)
											return self.image_processor_class




							@property
							def 						__snake_case					(   self     :   Tuple	):
											warnings.warn(
											    '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''' ,							SCREAMING_SNAKE_CASE_ ,							)
											return self.image_processor





