






"""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__      (							snake_case__       ):
			_UpperCAmelCase		:Tuple             =			"char"
			_UpperCAmelCase		:Optional[Any]             =			"bpe"
			_UpperCAmelCase		:Optional[int]             =			"wp"


A__	:    Any			 = (DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)







class        lowercase__      (							snake_case__       ):
			_UpperCAmelCase		:Union[str, Any]             =			["image_processor", "char_tokenizer"]
			_UpperCAmelCase		:int             =			"ViTImageProcessor"
			_UpperCAmelCase		:List[str]             =			"MgpstrTokenizer"


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

										lowerCamelCase_	:							Dict									=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`." )

										lowerCamelCase_	:							List[str]									=tokenizer
										lowerCamelCase_	:							str									=AutoTokenizer.from_pretrained("gpt2" )
										lowerCamelCase_	:							Tuple									=AutoTokenizer.from_pretrained("bert-base-uncased" )

										super().__init__(snake_case__       ,	snake_case__ )


			def __call__(				self					:  Tuple       ,	snake_case__					:  Dict=None       ,	snake_case__					:  Tuple=None       ,	snake_case__					:  Union[str, Any]=None       ,	**snake_case__					:  List[Any] ):
										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:
																	lowerCamelCase_	:							Any									=self.image_processor(snake_case__       ,	return_tensors=snake_case__       ,	**snake_case__ )
										if text is not None:
																	lowerCamelCase_	:							Dict									=self.char_tokenizer(snake_case__       ,	return_tensors=snake_case__       ,	**snake_case__ )

										if text is None:
																	return inputs
										elif images is None:
																	return encodings
										else:
																	lowerCamelCase_	:							List[Any]									=encodings["input_ids"]
																	return inputs


			def      UpperCAmelCase__						(				self					:  int       ,	snake_case__					:  Any ):
										lowerCamelCase_							,						lowerCamelCase_							,						lowerCamelCase_	:							int									=sequences
										lowerCamelCase_	:							Optional[int]									=char_preds.size(0 )

										lowerCamelCase_							,						lowerCamelCase_	:							List[Any]									=self._decode_helper(snake_case__       ,	"char" )
										lowerCamelCase_							,						lowerCamelCase_	:							Union[str, Any]									=self._decode_helper(snake_case__       ,	"bpe" )
										lowerCamelCase_							,						lowerCamelCase_	:							Tuple									=self._decode_helper(snake_case__       ,	"wp" )

										lowerCamelCase_	:							List[Any]									=[]
										lowerCamelCase_	:							List[str]									=[]
										for i in range(snake_case__ ):
																	lowerCamelCase_	:							Optional[int]									=[char_scores[i], bpe_scores[i], wp_scores[i]]
																	lowerCamelCase_	:							Dict									=[char_strs[i], bpe_strs[i], wp_strs[i]]
																	lowerCamelCase_	:							List[Any]									=scores.index(max(snake_case__ ) )
																	final_strs.append(strs[max_score_index] )
																	final_scores.append(scores[max_score_index] )

										lowerCamelCase_	:							Dict									={}
										lowerCamelCase_	:							List[str]									=final_strs
										lowerCamelCase_	:							Optional[Any]									=final_scores
										lowerCamelCase_	:							List[str]									=char_strs
										lowerCamelCase_	:							Optional[Any]									=bpe_strs
										lowerCamelCase_	:							Tuple									=wp_strs
										return out


			def      UpperCAmelCase__						(				self					:  Optional[int]       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  List[str] ):
										if format == DecodeType.CHARACTER:
																	lowerCamelCase_	:							List[str]									=self.char_decode
																	lowerCamelCase_	:							Union[str, Any]									=1
																	lowerCamelCase_	:							Any									="[s]"
										elif format == DecodeType.BPE:
																	lowerCamelCase_	:							List[Any]									=self.bpe_decode
																	lowerCamelCase_	:							str									=2
																	lowerCamelCase_	:							Any									="#"
										elif format == DecodeType.WORDPIECE:
																	lowerCamelCase_	:							List[str]									=self.wp_decode
																	lowerCamelCase_	:							Optional[Any]									=102
																	lowerCamelCase_	:							Optional[Any]									="[SEP]"
										else:
																	raise ValueError(F"""Format {format} is not supported.""" )

										lowerCamelCase_							,						lowerCamelCase_	:							Any									=[], []
										lowerCamelCase_	:							str									=pred_logits.size(0 )
										lowerCamelCase_	:							List[Any]									=pred_logits.size(1 )
										lowerCamelCase_							,						lowerCamelCase_	:							Optional[Any]									=pred_logits.topk(1       ,	dim=-1       ,	largest=snake_case__       ,	sorted=snake_case__ )
										lowerCamelCase_	:							List[str]									=preds_index.view(-1       ,	snake_case__ )[:, 1:]
										lowerCamelCase_	:							Optional[int]									=decoder(snake_case__ )
										lowerCamelCase_							,						lowerCamelCase_	:							Union[str, Any]									=torch.nn.functional.softmax(snake_case__       ,	dim=2 ).max(dim=2 )
										lowerCamelCase_	:							List[str]									=preds_max_prob[:, 1:]

										for index in range(snake_case__ ):
																	lowerCamelCase_	:							Dict									=preds_str[index].find(snake_case__ )
																	lowerCamelCase_	:							Optional[int]									=preds_str[index][:pred_eos]
																	lowerCamelCase_	:							Tuple									=preds_index[index].cpu().tolist()
																	lowerCamelCase_	:							Any									=pred_index.index(snake_case__ ) if eos_token in pred_index else -1
																	lowerCamelCase_	:							List[Any]									=preds_max_prob[index][: pred_eos_index + 1]
																	lowerCamelCase_	:							Any									=pred_max_prob.cumprod(dim=0 )[-1] if pred_max_prob.nelement() != 0 else 0.0
																	dec_strs.append(snake_case__ )
																	conf_scores.append(snake_case__ )

										return dec_strs, conf_scores


			def      UpperCAmelCase__						(				self					:  int       ,	snake_case__					:  Union[str, Any] ):
										lowerCamelCase_	:							int									=[seq.replace(" "       ,	"" ) for seq in self.char_tokenizer.batch_decode(snake_case__ )]
										return decode_strs


			def      UpperCAmelCase__						(				self					:  str       ,	snake_case__					:  List[Any] ):
										return self.bpe_tokenizer.batch_decode(snake_case__ )


			def      UpperCAmelCase__						(				self					:  List[str]       ,	snake_case__					:  Union[str, Any] ):
										lowerCamelCase_	:							List[str]									=[seq.replace(" "       ,	"" ) for seq in self.wp_tokenizer.batch_decode(snake_case__ )]
										return decode_strs











"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_tf_available,
    is_torch_available,
)


A__	:    Optional[int]			 = {
    'configuration_speech_to_text': ['SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Speech2TextConfig'],
    'processing_speech_to_text': ['Speech2TextProcessor'],
}

try:
						if not is_sentencepiece_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A__	:    Dict			 = ['Speech2TextTokenizer']

try:
						if not is_speech_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A__	:    Optional[Any]			 = ['Speech2TextFeatureExtractor']

try:
						if not is_tf_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A__	:    Dict			 = [
						    'TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'TFSpeech2TextForConditionalGeneration',
						    'TFSpeech2TextModel',
						    'TFSpeech2TextPreTrainedModel',
						]

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						A__	:    Optional[int]			 = [
						    'SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'Speech2TextForConditionalGeneration',
						    'Speech2TextModel',
						    'Speech2TextPreTrainedModel',
						]


if TYPE_CHECKING:
						from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextConfig
						from .processing_speech_to_text import SpeechaTextProcessor

						try:
												if not is_sentencepiece_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_speech_to_text import SpeechaTextTokenizer

						try:
												if not is_speech_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .feature_extraction_speech_to_text import SpeechaTextFeatureExtractor

						try:
												if not is_tf_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_tf_speech_to_text import (
												    TF_SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    TFSpeechaTextForConditionalGeneration,
												    TFSpeechaTextModel,
												    TFSpeechaTextPreTrainedModel,
												)

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_speech_to_text import (
												    SPEECH_TO_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
												    SpeechaTextForConditionalGeneration,
												    SpeechaTextModel,
												    SpeechaTextPreTrainedModel,
												)

else:
						import sys

						A__	:    List[Any]			 = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



