
'''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 						__UpperCAmelCase      (					_lowerCamelCase						):
							__lowercase      		=       """char"""
							__lowercase      		=       """bpe"""
							__lowercase      		=       """wp"""
lowercase    :							List[Any]      =					(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)


class 						__UpperCAmelCase      (					_lowerCamelCase						):
							__lowercase      		=       ["""image_processor""", """char_tokenizer"""]
							__lowercase      		=       """ViTImageProcessor"""
							__lowercase      		=       """MgpstrTokenizer"""


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





								"""simple docstring"""






								_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.'    ,   lowerCAmelCase_    ,   )
									_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__(lowerCAmelCase_    ,   lowerCAmelCase_  )


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





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

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


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






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

								_snake_case						, _snake_case   						=		self._decode_helper(lowerCAmelCase_    ,   'char'  )
								_snake_case						, _snake_case   						=		self._decode_helper(lowerCAmelCase_    ,   'bpe'  )
								_snake_case						, _snake_case   						=		self._decode_helper(lowerCAmelCase_    ,   'wp'  )

								_snake_case   						=		[]
								_snake_case   						=		[]
								for i in range(lowerCAmelCase_  ):
									_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(lowerCAmelCase_  )  )
									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  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								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=lowerCAmelCase_    ,   sorted=lowerCAmelCase_  )
								_snake_case   						=		preds_index.view(-1    ,   lowerCAmelCase_  )[:, 1:]
								_snake_case   						=		decoder(lowerCAmelCase_  )
								_snake_case						, _snake_case   						=		torch.nn.functional.softmax(lowerCAmelCase_    ,   dim=2  ).max(dim=2  )
								_snake_case   						=		preds_max_prob[:, 1:]

								for index in range(lowerCAmelCase_  ):
									_snake_case   						=		preds_str[index].find(lowerCAmelCase_  )
									_snake_case   						=		preds_str[index][:pred_eos]
									_snake_case   						=		preds_index[index].cpu().tolist()
									_snake_case   						=		pred_index.index(lowerCAmelCase_  ) 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(lowerCAmelCase_  )
									conf_scores.append(lowerCAmelCase_  )

								return dec_strs, conf_scores


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






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


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






								return self.bpe_tokenizer.batch_decode(lowerCAmelCase_  )




							def  lowerCamelCase				(       self    ,   lowerCAmelCase_  ):





								"""simple docstring"""






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



"""simple docstring"""




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


UpperCAmelCase__      :    Optional[Any]    							= logging.get_logger(__name__)

UpperCAmelCase__      :    Dict    							= {
    'MIT/ast-finetuned-audioset-10-10-0.4593': (
        'https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json'
    ),
}



class 	lowerCAmelCase_						(a__  ):


				"""simple docstring"""



				__UpperCamelCase      :						Optional[int]					       =   '''audio-spectrogram-transformer'''



				def __init__(self   ,       SCREAMING_SNAKE_CASE__=7_68   ,       SCREAMING_SNAKE_CASE__=12   ,       SCREAMING_SNAKE_CASE__=12   ,       SCREAMING_SNAKE_CASE__=30_72   ,       SCREAMING_SNAKE_CASE__="gelu"   ,       SCREAMING_SNAKE_CASE__=0.0   ,       SCREAMING_SNAKE_CASE__=0.0   ,       SCREAMING_SNAKE_CASE__=0.02   ,       SCREAMING_SNAKE_CASE__=1E-12   ,       SCREAMING_SNAKE_CASE__=16   ,       SCREAMING_SNAKE_CASE__=True   ,       SCREAMING_SNAKE_CASE__=10   ,       SCREAMING_SNAKE_CASE__=10   ,       SCREAMING_SNAKE_CASE__=10_24   ,       SCREAMING_SNAKE_CASE__=1_28   ,       **SCREAMING_SNAKE_CASE__   ,       )					->		Tuple:



											"""simple docstring"""




											super().__init__(**SCREAMING_SNAKE_CASE__    )

											SCREAMING_SNAKE_CASE__						:       Optional[Any]           =   hidden_size
											SCREAMING_SNAKE_CASE__						:       str           =   num_hidden_layers
											SCREAMING_SNAKE_CASE__						:       int           =   num_attention_heads
											SCREAMING_SNAKE_CASE__						:       Tuple           =   intermediate_size
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   hidden_act
											SCREAMING_SNAKE_CASE__						:       Any           =   hidden_dropout_prob
											SCREAMING_SNAKE_CASE__						:       List[Any]           =   attention_probs_dropout_prob
											SCREAMING_SNAKE_CASE__						:       int           =   initializer_range
											SCREAMING_SNAKE_CASE__						:       int           =   layer_norm_eps
											SCREAMING_SNAKE_CASE__						:       Dict           =   patch_size
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   qkv_bias
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   frequency_stride
											SCREAMING_SNAKE_CASE__						:       Any           =   time_stride
											SCREAMING_SNAKE_CASE__						:       Optional[int]           =   max_length
											SCREAMING_SNAKE_CASE__						:       Any           =   num_mel_bins




