






'''simple docstring'''







import functools
import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

import huggingface_hub.utils as hf_hub_utils
from tqdm import auto as tqdm_lib


_lowercase       						=			threading.Lock()
_lowercase       						=			None

_lowercase       						=			{
    'debug': logging.DEBUG,
    'info': logging.INFO,
    'warning': logging.WARNING,
    'error': logging.ERROR,
    'critical': logging.CRITICAL,
}

_lowercase       						=			logging.WARNING

_lowercase       						=			True

def    __UpperCamelCase	(	) ->str:
 snake_case				=			os.getenv('''TRANSFORMERS_VERBOSITY'''					,   a     )
 if env_level_str:
  if env_level_str in log_levels:
   return log_levels[env_level_str]
  else:
   logging.getLogger().warning(
       f"""Unknown option TRANSFORMERS_VERBOSITY={env_level_str}, """
       f"""has to be one of: { ', '.join(log_levels.keys()     ) }"""     )
 return _default_log_level

def    __UpperCamelCase	(	) ->str:
 return __name__.split('''.'''     )[0]

def    __UpperCamelCase	(	) ->logging.Logger:
 return logging.getLogger(_get_library_name()     )

def    __UpperCamelCase	(	) ->None:
 global _default_handler

 with _lock:
  if _default_handler:
   # This library has already configured the library root logger.
   return
  snake_case				=			logging.StreamHandler()  # Set sys.stderr as stream.
  snake_case				=			sys.stderr.flush

  # Apply our default configuration to the library root logger.
  snake_case				=			_get_library_root_logger()
  library_root_logger.addHandler(_default_handler     )
  library_root_logger.setLevel(_get_default_logging_level()     )
  snake_case				=			False

def    __UpperCamelCase	(	) ->None:
 global _default_handler

 with _lock:
  if not _default_handler:
   return

  snake_case				=			_get_library_root_logger()
  library_root_logger.removeHandler(_default_handler     )
  library_root_logger.setLevel(logging.NOTSET     )
  snake_case				=			None

def    __UpperCamelCase	(	) ->str:
 return log_levels

def    __UpperCamelCase	(	a    : Optional[str] = None     ) ->logging.Logger:

 if name is None:
  snake_case				=			_get_library_name()

 _configure_library_root_logger()
 return logging.getLogger(a     )

def    __UpperCamelCase	(	) ->int:
 _configure_library_root_logger()
 return _get_library_root_logger().getEffectiveLevel()

def    __UpperCamelCase	(	a    : int     ) ->None:
 _configure_library_root_logger()
 _get_library_root_logger().setLevel(a     )

def    __UpperCamelCase	(	) ->Any:
 return set_verbosity(a     )

def    __UpperCamelCase	(	) ->int:
 return set_verbosity(a     )

def    __UpperCamelCase	(	) ->Tuple:
 return set_verbosity(a     )

def    __UpperCamelCase	(	) ->Union[str, Any]:
 return set_verbosity(a     )

def    __UpperCamelCase	(	) ->None:
 _configure_library_root_logger()

 assert _default_handler is not None
 _get_library_root_logger().removeHandler(_default_handler     )

def    __UpperCamelCase	(	) ->None:
 _configure_library_root_logger()

 assert _default_handler is not None
 _get_library_root_logger().addHandler(_default_handler     )

def    __UpperCamelCase	(	a    : logging.Handler     ) ->None:
 _configure_library_root_logger()

 assert handler is not None
 _get_library_root_logger().addHandler(a     )

def    __UpperCamelCase	(	a    : logging.Handler     ) ->None:
 _configure_library_root_logger()

 assert handler is not None and handler not in _get_library_root_logger().handlers
 _get_library_root_logger().removeHandler(a     )

def    __UpperCamelCase	(	) ->None:
 _configure_library_root_logger()
 snake_case				=			False

def    __UpperCamelCase	(	) ->None:
 _configure_library_root_logger()
 snake_case				=			True

def    __UpperCamelCase	(	) ->None:
 snake_case				=			_get_library_root_logger().handlers

 for handler in handlers:
  snake_case				=			logging.Formatter('''[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s'''     )
  handler.setFormatter(a     )

def    __UpperCamelCase	(	) ->None:
 snake_case				=			_get_library_root_logger().handlers

 for handler in handlers:
  handler.setFormatter(a     )

def    __UpperCamelCase	(	self    : List[str]					,   *a    : List[Any]					,   **a    : Union[str, Any]     ) ->Optional[Any]:
 snake_case				=			os.getenv('''TRANSFORMERS_NO_ADVISORY_WARNINGS'''					,   a     )
 if no_advisory_warnings:
  return
 self.warning(*a					,   **a     )


_lowercase       						=			warning_advice

@functools.lru_cache(a     )
def    __UpperCamelCase	(	self    : Optional[Any]					,   *a    : str					,   **a    : Dict     ) ->List[str]:
 self.warning(*a					,   **a     )


_lowercase       						=			warning_once
class 			_lowercase						:







     def __init__( self    ,   *A__    ,   **A__    ) ->	List[Any]:  # pylint: disable=unused-argument
      snake_case				=			args[0] if args else None







     def __iter__( self    ) ->	int:
      return iter(self._iterator    )







     def __getattr__( self    ,   A__    ) ->	Union[str, Any]:

      def empty_fn(*A__    ,   **A__    ):  # pylint: disable=unused-argument
       return

      return empty_fn







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







     def __exit__( self    ,   A__    ,   A__    ,   A__    ) ->	Dict:
      return




class 			_lowercase						:







     def __call__( self    ,   *A__    ,   **A__    ) ->	int:
      if _tqdm_active:
       return tqdm_lib.tqdm(*A__    ,   **A__    )
      else:
       return EmptyTqdm(*A__    ,   **A__    )







     def     UpperCamelCase			( self    ,   *A__    ,   **A__    ) ->	List[Any]:
      snake_case				=			None
      if _tqdm_active:
       return tqdm_lib.tqdm.set_lock(*A__    ,   **A__    )







     def     UpperCamelCase			( self    ) ->	Optional[Any]:
      if _tqdm_active:
       return tqdm_lib.tqdm.get_lock()




_lowercase       						=			_tqdm_cls()

def    __UpperCamelCase	(	) ->bool:
 global _tqdm_active
 return bool(_tqdm_active     )

def    __UpperCamelCase	(	) ->List[Any]:
 global _tqdm_active
 snake_case				=			True
 hf_hub_utils.enable_progress_bars()






def    __UpperCamelCase	(	) ->Any:
 global _tqdm_active
 snake_case				=			False
 hf_hub_utils.disable_progress_bars()








'''simple docstring'''







import warnings

from transformers import AutoTokenizer
from transformers.utils import is_torch_available
from transformers.utils.generic import ExplicitEnum

from ...processing_utils import ProcessorMixin


if is_torch_available():
   import torch
class 			_lowercase						(       __a					):
     _UpperCAmelCase										=					'''char'''
     _UpperCAmelCase										=					'''bpe'''
     _UpperCAmelCase										=					'''wp'''




_lowercase       						=			(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)
class 			_lowercase						(       __a					):
     _UpperCAmelCase										=					['''image_processor''', '''char_tokenizer''']
     _UpperCAmelCase										=					'''ViTImageProcessor'''
     _UpperCAmelCase										=					'''MgpstrTokenizer'''







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

      snake_case				=			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`.'''    )

      snake_case				=			tokenizer
      snake_case				=			AutoTokenizer.from_pretrained('''gpt2'''    )
      snake_case				=			AutoTokenizer.from_pretrained('''bert-base-uncased'''    )

      super().__init__(A__    ,   A__    )







     def __call__( self    ,   A__=None    ,   A__=None    ,   A__=None    ,   **A__    ) ->	List[str]:
      if images is None and text is None:
       raise ValueError('''You need to specify either an `images` or `text` input to process.'''    )

      if images is not None:
       snake_case				=			self.image_processor(A__    ,   return_tensors=A__    ,   **A__    )
      if text is not None:
       snake_case				=			self.char_tokenizer(A__    ,   return_tensors=A__    ,   **A__    )

      if text is None:
       return inputs
      elif images is None:
       return encodings
      else:
       snake_case				=			encodings['''input_ids''']
       return inputs







     def     UpperCamelCase			( self    ,   A__    ) ->	Dict:
      snake_case				,		snake_case				,		snake_case				=			sequences
      snake_case				=			char_preds.size(0    )

      snake_case				,		snake_case				=			self._decode_helper(A__    ,   '''char'''    )
      snake_case				,		snake_case				=			self._decode_helper(A__    ,   '''bpe'''    )
      snake_case				,		snake_case				=			self._decode_helper(A__    ,   '''wp'''    )

      snake_case				=			[]
      snake_case				=			[]
      for i in range(A__    ):
       snake_case				=			[char_scores[i], bpe_scores[i], wp_scores[i]]
       snake_case				=			[char_strs[i], bpe_strs[i], wp_strs[i]]
       snake_case				=			scores.index(max(A__    )    )
       final_strs.append(strs[max_score_index]    )
       final_scores.append(scores[max_score_index]    )

      snake_case				=			{}
      snake_case				=			final_strs
      snake_case				=			final_scores
      snake_case				=			char_strs
      snake_case				=			bpe_strs
      snake_case				=			wp_strs
      return out







     def     UpperCamelCase			( self    ,   A__    ,   A__    ) ->	Optional[Any]:
      if format == DecodeType.CHARACTER:
       snake_case				=			self.char_decode
       snake_case				=			1
       snake_case				=			'''[s]'''
      elif format == DecodeType.BPE:
       snake_case				=			self.bpe_decode
       snake_case				=			2
       snake_case				=			'''#'''
      elif format == DecodeType.WORDPIECE:
       snake_case				=			self.wp_decode
       snake_case				=			1_02
       snake_case				=			'''[SEP]'''
      else:
       raise ValueError(F"""Format {format} is not supported."""    )

      snake_case				,		snake_case				=			[], []
      snake_case				=			pred_logits.size(0    )
      snake_case				=			pred_logits.size(1    )
      snake_case				,		snake_case				=			pred_logits.topk(1    ,   dim=-1    ,   largest=A__    ,   sorted=A__    )
      snake_case				=			preds_index.view(-1    ,   A__    )[:, 1:]
      snake_case				=			decoder(A__    )
      snake_case				,		snake_case				=			torch.nn.functional.softmax(A__    ,   dim=2    ).max(dim=2    )
      snake_case				=			preds_max_prob[:, 1:]

      for index in range(A__    ):
       snake_case				=			preds_str[index].find(A__    )
       snake_case				=			preds_str[index][:pred_eos]
       snake_case				=			preds_index[index].cpu().tolist()
       snake_case				=			pred_index.index(A__    ) if eos_token in pred_index else -1
       snake_case				=			preds_max_prob[index][: pred_eos_index + 1]
       snake_case				=			pred_max_prob.cumprod(dim=0    )[-1] if pred_max_prob.nelement() != 0 else 0.0
       dec_strs.append(A__    )
       conf_scores.append(A__    )

      return dec_strs, conf_scores







     def     UpperCamelCase			( self    ,   A__    ) ->	int:
      snake_case				=			[seq.replace(''' '''    ,   ''''''    ) for seq in self.char_tokenizer.batch_decode(A__    )]
      return decode_strs







     def     UpperCamelCase			( self    ,   A__    ) ->	List[str]:
      return self.bpe_tokenizer.batch_decode(A__    )







     def     UpperCamelCase			( self    ,   A__    ) ->	Union[str, Any]:
      snake_case				=			[seq.replace(''' '''    ,   ''''''    ) for seq in self.wp_tokenizer.batch_decode(A__    )]
      return decode_strs

