



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        UpperCAmelCase_  (       UpperCamelCase_	):

						'''simple docstring'''




						UpperCamelCase__      :			List[str]      =  '''char'''
						UpperCamelCase__      :			Optional[int]      =  '''bpe'''
						UpperCamelCase__      :			List[Any]      =  '''wp'''







lowerCAmelCase__		:					str							=(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)

class        UpperCAmelCase_  (       UpperCamelCase_	):

						'''simple docstring'''




						UpperCamelCase__      :			Optional[Any]      =  ['''image_processor''', '''char_tokenizer''']
						UpperCamelCase__      :			Optional[int]      =  '''ViTImageProcessor'''
						UpperCamelCase__      :			Optional[Any]      =  '''MgpstrTokenizer'''






						def __init__(   self				,							_A=None				,							_A=None				,							**_A		):







													'''simple docstring'''

													__SCREAMING_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				,							)
																				__SCREAMING_SNAKE_CASE									= kwargs.pop('feature_extractor'		)

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

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

													super().__init__(_A				,							_A		)






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







													'''simple docstring'''

													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:
																				__SCREAMING_SNAKE_CASE									= self.image_processor(_A				,							return_tensors=_A				,							**_A		)
													if text is not None:
																				__SCREAMING_SNAKE_CASE									= self.char_tokenizer(_A				,							return_tensors=_A				,							**_A		)

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






						def 	_A     (   self				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= sequences
													__SCREAMING_SNAKE_CASE									= char_preds.size(0		)

													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self._decode_helper(_A				,							'char'		)
													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self._decode_helper(_A				,							'bpe'		)
													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self._decode_helper(_A				,							'wp'		)

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

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






						def 	_A     (   self				,							_A				,							_A		):







													'''simple docstring'''

													if format == DecodeType.CHARACTER:
																				__SCREAMING_SNAKE_CASE									= self.char_decode
																				__SCREAMING_SNAKE_CASE									= 1
																				__SCREAMING_SNAKE_CASE									= '[s]'
													elif format == DecodeType.BPE:
																				__SCREAMING_SNAKE_CASE									= self.bpe_decode
																				__SCREAMING_SNAKE_CASE									= 2
																				__SCREAMING_SNAKE_CASE									= '#'
													elif format == DecodeType.WORDPIECE:
																				__SCREAMING_SNAKE_CASE									= self.wp_decode
																				__SCREAMING_SNAKE_CASE									= 102
																				__SCREAMING_SNAKE_CASE									= '[SEP]'
													else:
																				raise ValueError(f"""Format {format} is not supported."""		)

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

													for index in range(_A		):
																				__SCREAMING_SNAKE_CASE									= preds_str[index].find(_A		)
																				__SCREAMING_SNAKE_CASE									= preds_str[index][:pred_eos]
																				__SCREAMING_SNAKE_CASE									= preds_index[index].cpu().tolist()
																				__SCREAMING_SNAKE_CASE									= pred_index.index(_A		) if eos_token in pred_index else -1
																				__SCREAMING_SNAKE_CASE									= preds_max_prob[index][: pred_eos_index + 1]
																				__SCREAMING_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 	_A     (   self				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= [seq.replace(' '				,							''		) for seq in self.char_tokenizer.batch_decode(_A		)]
													return decode_strs






						def 	_A     (   self				,							_A		):







													'''simple docstring'''

													return self.bpe_tokenizer.batch_decode(_A		)






						def 	_A     (   self				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= [seq.replace(' '				,							''		) for seq in self.wp_tokenizer.batch_decode(_A		)]
													return decode_strs











import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin
from .feature_extraction_wavaveca import WavaVecaFeatureExtractor
from .tokenization_wavaveca import WavaVecaCTCTokenizer

class        UpperCAmelCase_  (       UpperCamelCase_	):

						'''simple docstring'''




						UpperCamelCase__      :			Union[str, Any]      =  '''Wav2Vec2FeatureExtractor'''
						UpperCamelCase__      :			Union[str, Any]      =  '''AutoTokenizer'''






						def __init__(   self				,							_A				,							_A		):







													'''simple docstring'''

													super().__init__(_A				,							_A		)
													__SCREAMING_SNAKE_CASE									= self.feature_extractor
													__SCREAMING_SNAKE_CASE									= False






						@classmethod
						def 	_A     (   cls				,							_A				,							**_A		):







													'''simple docstring'''

													try:
																				return super().from_pretrained(_A				,							**_A		)
													except OSError:
																				warnings.warn(
																				    f"""Loading a tokenizer inside {cls.__name__} from a config that does not"""
																				    ' include a `tokenizer_class` attribute is deprecated and will be '
																				    'removed in v5. Please add `\'tokenizer_class\': \'Wav2Vec2CTCTokenizer\'`'
																				    ' attribute to either your `config.json` or `tokenizer_config.json` '
																				    'file to suppress this warning: '				,							_A				,							)

																				__SCREAMING_SNAKE_CASE									= WavaVecaFeatureExtractor.from_pretrained(_A				,							**_A		)
																				__SCREAMING_SNAKE_CASE									= WavaVecaCTCTokenizer.from_pretrained(_A				,							**_A		)

																				return cls(feature_extractor=_A				,							tokenizer=_A		)






						def __call__(   self				,							*_A				,							**_A		):







													'''simple docstring'''

													if self._in_target_context_manager:
																				return self.current_processor(*_A				,							**_A		)

													if "raw_speech" in kwargs:
																				warnings.warn('Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead.'		)
																				__SCREAMING_SNAKE_CASE									= kwargs.pop('raw_speech'		)
													else:
																				__SCREAMING_SNAKE_CASE									= kwargs.pop('audio'				,							_A		)
													__SCREAMING_SNAKE_CASE									= kwargs.pop('sampling_rate'				,							_A		)
													__SCREAMING_SNAKE_CASE									= kwargs.pop('text'				,							_A		)
													if len(_A		) > 0:
																				__SCREAMING_SNAKE_CASE									= args[0]
																				__SCREAMING_SNAKE_CASE									= args[1:]

													if audio is None and text is None:
																				raise ValueError('You need to specify either an `audio` or `text` input to process.'		)

													if audio is not None:
																				__SCREAMING_SNAKE_CASE									= self.feature_extractor(_A				,							*_A				,							sampling_rate=_A				,							**_A		)
													if text is not None:
																				__SCREAMING_SNAKE_CASE									= self.tokenizer(_A				,							**_A		)

													if text is None:
																				return inputs
													elif audio is None:
																				return encodings
													else:
																				__SCREAMING_SNAKE_CASE									= encodings['input_ids']
																				return inputs






						def 	_A     (   self				,							*_A				,							**_A		):







													'''simple docstring'''

													if self._in_target_context_manager:
																				return self.current_processor.pad(*_A				,							**_A		)

													__SCREAMING_SNAKE_CASE									= kwargs.pop('input_features'				,							_A		)
													__SCREAMING_SNAKE_CASE									= kwargs.pop('labels'				,							_A		)
													if len(_A		) > 0:
																				__SCREAMING_SNAKE_CASE									= args[0]
																				__SCREAMING_SNAKE_CASE									= args[1:]

													if input_features is not None:
																				__SCREAMING_SNAKE_CASE									= self.feature_extractor.pad(_A				,							*_A				,							**_A		)
													if labels is not None:
																				__SCREAMING_SNAKE_CASE									= self.tokenizer.pad(_A				,							**_A		)

													if labels is None:
																				return input_features
													elif input_features is None:
																				return labels
													else:
																				__SCREAMING_SNAKE_CASE									= labels['input_ids']
																				return input_features






						def 	_A     (   self				,							*_A				,							**_A		):







													'''simple docstring'''

													return self.tokenizer.batch_decode(*_A				,							**_A		)






						def 	_A     (   self				,							*_A				,							**_A		):







													'''simple docstring'''

													return self.tokenizer.decode(*_A				,							**_A		)






						@contextmanager
						def 	_A     (   self		):







													'''simple docstring'''

													warnings.warn(
													    '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '
													    'labels by using the argument `text` of the regular `__call__` method (either in the same call as '
													    'your audio inputs, or in a separate call.'		)
													__SCREAMING_SNAKE_CASE									= True
													__SCREAMING_SNAKE_CASE									= self.tokenizer
													yield
													__SCREAMING_SNAKE_CASE									= self.feature_extractor
													__SCREAMING_SNAKE_CASE									= False






