


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     a		(_lowerCAmelCase				):




							"""simple docstring"""


							__UpperCAmelCase   :							Union[str, Any]					  =  "char"
							__UpperCAmelCase   :							int					  =  "bpe"
							__UpperCAmelCase   :							Union[str, Any]					  =  "wp"
_snake_case					:		List[Any]      =    (DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)

class     a		(_lowerCAmelCase				):




							"""simple docstring"""


							__UpperCAmelCase   :							List[Any]					  =  ["image_processor", "char_tokenizer"]
							__UpperCAmelCase   :							Union[str, Any]					  =  "ViTImageProcessor"
							__UpperCAmelCase   :							List[Any]					  =  "MgpstrTokenizer"

							def __init__(							self	:  Dict   ,     lowerCamelCase	:  List[Any]=None   ,     lowerCamelCase	:  Optional[Any]=None   ,     **lowerCamelCase	:  List[str]    )     ->   Optional[int]:
														__snake_case		:      List[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."   ,     lowerCamelCase   ,     )
																					__snake_case		:      Union[str, Any]										=       kwargs.pop("feature_extractor"    )

														__snake_case		:      str										=       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		:      int										=       tokenizer
														__snake_case		:      Optional[int]										=       AutoTokenizer.from_pretrained("gpt2"    )
														__snake_case		:      Dict										=       AutoTokenizer.from_pretrained("bert-base-uncased"    )

														super().__init__(lowerCamelCase   ,     lowerCamelCase    )

							def __call__(							self	:  Optional[int]   ,     lowerCamelCase	:  Optional[int]=None   ,     lowerCamelCase	:  Optional[int]=None   ,     lowerCamelCase	:  Tuple=None   ,     **lowerCamelCase	:  Optional[Any]    )     ->   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:
																					__snake_case		:      Dict										=       self.image_processor(lowerCamelCase   ,     return_tensors=lowerCamelCase   ,     **lowerCamelCase    )
														if text is not None:
																					__snake_case		:      Any										=       self.char_tokenizer(lowerCamelCase   ,     return_tensors=lowerCamelCase   ,     **lowerCamelCase    )

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

							def    __snake_case (							self	:  str   ,     lowerCamelCase	:  List[str]    )     ->   str:
														__snake_case		:      int										=       sequences
														__snake_case		:      int										=       char_preds.size(0    )

														__snake_case		:      Any										=       self._decode_helper(lowerCamelCase   ,     "char"    )
														__snake_case		:      Dict										=       self._decode_helper(lowerCamelCase   ,     "bpe"    )
														__snake_case		:      Optional[int]										=       self._decode_helper(lowerCamelCase   ,     "wp"    )

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

														__snake_case		:      Union[str, Any]										=       {}
														__snake_case		:      Union[str, Any]										=       final_strs
														__snake_case		:      Union[str, Any]										=       final_scores
														__snake_case		:      Dict										=       char_strs
														__snake_case		:      Union[str, Any]										=       bpe_strs
														__snake_case		:      Optional[Any]										=       wp_strs
														return out

							def    __snake_case (							self	:  int   ,     lowerCamelCase	:  Dict   ,     lowerCamelCase	:  Optional[Any]    )     ->   Any:
														if format == DecodeType.CHARACTER:
																					__snake_case		:      Tuple										=       self.char_decode
																					__snake_case		:      Optional[Any]										=       1
																					__snake_case		:      Tuple										=       "[s]"
														elif format == DecodeType.BPE:
																					__snake_case		:      Optional[int]										=       self.bpe_decode
																					__snake_case		:      Optional[int]										=       2
																					__snake_case		:      Optional[Any]										=       "#"
														elif format == DecodeType.WORDPIECE:
																					__snake_case		:      List[str]										=       self.wp_decode
																					__snake_case		:      Any										=       102
																					__snake_case		:      Optional[int]										=       "[SEP]"
														else:
																					raise ValueError(F'Format {format} is not supported.'    )

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

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

														return dec_strs, conf_scores

							def    __snake_case (							self	:  Dict   ,     lowerCamelCase	:  Optional[Any]    )     ->   str:
														__snake_case		:      Any										=       [seq.replace(" "   ,     ""    ) for seq in self.char_tokenizer.batch_decode(lowerCamelCase    )]
														return decode_strs

							def    __snake_case (							self	:  List[Any]   ,     lowerCamelCase	:  List[Any]    )     ->   List[Any]:
														return self.bpe_tokenizer.batch_decode(lowerCamelCase    )






							def    __snake_case (							self	:  Union[str, Any]   ,     lowerCamelCase	:  Tuple    )     ->   Union[str, Any]:
														__snake_case		:      Any										=       [seq.replace(" "   ,     ""    ) for seq in self.wp_tokenizer.batch_decode(lowerCamelCase    )]
														return decode_strs





from ...configuration_utils import PretrainedConfig
from ...utils import logging


_snake_case					:		Any      =    logging.get_logger(__name__)

_snake_case					:		Dict      =    {
    "facebook/s2t-wav2vec2-large-en-de": (
        "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}

class     a		(_lowerCAmelCase				):




							"""simple docstring"""


							__UpperCAmelCase   :							Union[str, Any]					  =  "speech_to_text_2"
							__UpperCAmelCase   :							Dict					  =  ["past_key_values"]
							__UpperCAmelCase   :							str					  =  {"num_attention_heads": "decoder_attention_heads", "hidden_size": "d_model"}

							def __init__(							self	:  str   ,     lowerCamelCase	:  List[Any]=10000   ,     lowerCamelCase	:  Tuple=6   ,     lowerCamelCase	:  Optional[Any]=2048   ,     lowerCamelCase	:  Optional[int]=4   ,     lowerCamelCase	:  Optional[int]=0.0   ,     lowerCamelCase	:  str=True   ,     lowerCamelCase	:  str="relu"   ,     lowerCamelCase	:  Tuple=256   ,     lowerCamelCase	:  Union[str, Any]=0.1   ,     lowerCamelCase	:  Optional[int]=0.0   ,     lowerCamelCase	:  Any=0.0   ,     lowerCamelCase	:  Optional[int]=0.02   ,     lowerCamelCase	:  Dict=2   ,     lowerCamelCase	:  List[str]=True   ,     lowerCamelCase	:  str=1   ,     lowerCamelCase	:  Any=0   ,     lowerCamelCase	:  List[Any]=2   ,     lowerCamelCase	:  List[Any]=1024   ,     **lowerCamelCase	:  List[str]   ,     )     ->   Any:
														__snake_case		:      Tuple										=       vocab_size
														__snake_case		:      int										=       d_model
														__snake_case		:      List[str]										=       decoder_ffn_dim
														__snake_case		:      Union[str, Any]										=       decoder_layers
														__snake_case		:      Dict										=       decoder_attention_heads
														__snake_case		:      Any										=       dropout
														__snake_case		:      List[str]										=       attention_dropout
														__snake_case		:      Optional[Any]										=       activation_dropout
														__snake_case		:      Union[str, Any]										=       activation_function
														__snake_case		:      Union[str, Any]										=       init_std
														__snake_case		:      Union[str, Any]										=       decoder_layerdrop
														__snake_case		:      Optional[int]										=       use_cache
														__snake_case		:      Optional[int]										=       decoder_layers
														__snake_case		:      str										=       scale_embedding  # scale factor will be sqrt(d_model) if True
														__snake_case		:      Dict										=       max_target_positions

														super().__init__(
														    pad_token_id=lowerCamelCase   ,     bos_token_id=lowerCamelCase   ,     eos_token_id=lowerCamelCase   ,     decoder_start_token_id=lowerCamelCase   ,     **lowerCamelCase   ,     )

