


import copy
from typing import Any, Dict, List, Optional, Union

import numpy as np
import torch

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


lowerCamelCase__							:						Optional[Any]							     =   logging.get_logger(__name__)
class 		__magic_name__						(A__				):





     '''simple docstring'''



     __lowercase				:							Dict	   =       ['input_features', 'is_longer']

     def __init__(					self:Dict     ,		_a:Any=64     ,		_a:Union[str, Any]=4_80_00     ,		_a:Dict=4_80     ,		_a:Dict=10     ,		_a:Dict=10_24     ,		_a:Optional[int]=0.0     ,		_a:Optional[Any]=False     ,		_a:Optional[int] = 0     ,		_a:int = 1_40_00     ,		_a:Dict = None     ,		_a:List[str] = "fusion"     ,		_a:int = "repeatpad"     ,		**_a:int     ,		):
       super().__init__(
           feature_size=snake_case_     ,		sampling_rate=snake_case_     ,		padding_value=snake_case_     ,		return_attention_mask=snake_case_     ,		**snake_case_     ,		)
       snake_case__						= top_db
       snake_case__						= truncation
       snake_case__						= padding
       snake_case__						= fft_window_size
       snake_case__						= (fft_window_size >> 1) + 1
       snake_case__						= hop_length
       snake_case__						= max_length_s
       snake_case__						= max_length_s * sampling_rate
       snake_case__						= sampling_rate
       snake_case__						= frequency_min
       snake_case__						= frequency_max
       snake_case__						= mel_filter_bank(
           num_frequency_bins=self.nb_frequency_bins     ,		num_mel_filters=snake_case_     ,		min_frequency=snake_case_     ,		max_frequency=snake_case_     ,		sampling_rate=snake_case_     ,		norm=snake_case_     ,		mel_scale='''htk'''     ,		)
       snake_case__						= mel_filter_bank(
           num_frequency_bins=self.nb_frequency_bins     ,		num_mel_filters=snake_case_     ,		min_frequency=snake_case_     ,		max_frequency=snake_case_     ,		sampling_rate=snake_case_     ,		norm='''slaney'''     ,		mel_scale='''slaney'''     ,		)

     def 	SCREAMING_SNAKE_CASE__						(					self:Optional[Any]   ):
       snake_case__						= copy.deepcopy(self.__dict__   )
       snake_case__						= self.__class__.__name__
       if "mel_filters" in output:
         del output["mel_filters"]
       if "mel_filters_slaney" in output:
         del output["mel_filters_slaney"]
       return output

     def 	SCREAMING_SNAKE_CASE__						(					self:List[str]     ,		_a:int     ,		_a:Tuple = None   ):
       snake_case__						= spectrogram(
           snake_case_     ,		window_function(self.fft_window_size     ,		'''hann'''   )     ,		frame_length=self.fft_window_size     ,		hop_length=self.hop_length     ,		power=2.0     ,		mel_filters=snake_case_     ,		log_mel='''dB'''     ,		)
       return log_mel_spectrogram.T

     def 	SCREAMING_SNAKE_CASE__						(					self:Tuple     ,		_a:Optional[Any]     ,		_a:Optional[int]     ,		_a:int   ):
       snake_case__						= np.array_split(list(range(0     ,		total_frames - chunk_frames + 1   )   )     ,		3   )
       if len(ranges[1]   ) == 0:
         # if the audio is too short, we just use the first chunk
         snake_case__						= [0]
       if len(ranges[2]   ) == 0:
         # if the audio is too short, we just use the first chunk
         snake_case__						= [0]
       # randomly choose index for each part
       snake_case__						= np.random.choice(ranges[0]   )
       snake_case__						= np.random.choice(ranges[1]   )
       snake_case__						= np.random.choice(ranges[2]   )

       snake_case__						= mel[idx_front : idx_front + chunk_frames, :]
       snake_case__						= mel[idx_middle : idx_middle + chunk_frames, :]
       snake_case__						= mel[idx_back : idx_back + chunk_frames, :]

       snake_case__						= torch.tensor(mel[None, None, :]   )
       snake_case__						= torch.nn.functional.interpolate(
           snake_case_     ,		size=[chunk_frames, 64]     ,		mode='''bilinear'''     ,		align_corners=snake_case_   )
       snake_case__						= mel_shrink[0][0].numpy()
       snake_case__						= np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back]     ,		axis=0   )
       return mel_fusion

     def 	SCREAMING_SNAKE_CASE__						(					self:Any     ,		_a:Tuple     ,		_a:Union[str, Any]     ,		_a:Any     ,		_a:List[str]   ):
       if waveform.shape[0] > max_length:
         if truncation == "rand_trunc":
           snake_case__						= True
           # random crop to max_length (for compatibility) -> this should be handled by self.pad
           snake_case__						= len(snake_case_   ) - max_length
           snake_case__						= np.random.randint(0     ,		overflow + 1   )
           snake_case__						= waveform[idx : idx + max_length]
           snake_case__						= self._np_extract_fbank_features(snake_case_     ,		self.mel_filters_slaney   )[None, :]
         elif truncation == "fusion":
           snake_case__						= self._np_extract_fbank_features(snake_case_     ,		self.mel_filters   )
           snake_case__						= max_length // self.hop_length + 1  # the +1 related to how the spectrogram is computed
           snake_case__						= mel.shape[0]
           if chunk_frames == total_frames:
             # there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length.
             # In this case, we just use the whole audio.
             snake_case__						= np.stack([mel, mel, mel, mel]     ,		axis=0   )
             snake_case__						= False
           else:
             snake_case__						= self._random_mel_fusion(snake_case_     ,		snake_case_     ,		snake_case_   )
             snake_case__						= True
         else:
           raise NotImplementedError(F"""data_truncating {truncation} not implemented"""   )

       else:
         snake_case__						= False
         # only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding
         if waveform.shape[0] < max_length:
           if padding == "repeat":
             snake_case__						= int(max_length / len(snake_case_   )   )
             snake_case__						= np.stack(np.tile(snake_case_     ,		n_repeat + 1   )   )[:max_length]
           if padding == "repeatpad":
             snake_case__						= int(max_length / len(snake_case_   )   )
             snake_case__						= np.stack(np.tile(snake_case_     ,		snake_case_   )   )
           snake_case__						= np.pad(snake_case_     ,		(0, max_length - waveform.shape[0])     ,		mode='''constant'''     ,		constant_values=0   )

         if truncation == "fusion":
           snake_case__						= self._np_extract_fbank_features(snake_case_     ,		self.mel_filters   )
           snake_case__						= np.stack([input_mel, input_mel, input_mel, input_mel]     ,		axis=0   )
         else:
           snake_case__						= self._np_extract_fbank_features(snake_case_     ,		self.mel_filters_slaney   )[None, :]

       return input_mel, longer







     def __call__(					self:Optional[Any]     ,		_a:Optional[Any]     ,		_a:List[Any] = None     ,		_a:Union[str, Any] = None     ,		_a:Dict = None     ,		_a:Optional[int] = None     ,		_a:List[str] = None     ,		**_a:Optional[Any]     ,		):
       snake_case__						= truncation if truncation is not None else self.truncation
       snake_case__						= padding if padding else self.padding

       if sampling_rate is not None:
         if sampling_rate != self.sampling_rate:
           raise ValueError(
               F"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a"""
               F""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input"""
               F""" was sampled with {self.sampling_rate} and not {sampling_rate}."""   )
       else:
         logger.warning(
             '''It is strongly recommended to pass the `sampling_rate` argument to this function. '''
             '''Failing to do so can result in silent errors that might be hard to debug.'''   )

       snake_case__						= isinstance(snake_case_     ,		np.ndarray   ) and len(raw_speech.shape   ) > 1
       if is_batched_numpy and len(raw_speech.shape   ) > 2:
         raise ValueError(F"""Only mono-channel audio is supported for input to {self}"""   )
       snake_case__						= is_batched_numpy or (
           isinstance(snake_case_     ,		(list, tuple)   ) and (isinstance(raw_speech[0]     ,		(np.ndarray, tuple, list)   ))
       )

       if is_batched:
         snake_case__						= [np.asarray(snake_case_     ,		dtype=np.floataa   ) for speech in raw_speech]
       elif not is_batched and not isinstance(snake_case_     ,		np.ndarray   ):
         snake_case__						= np.asarray(snake_case_     ,		dtype=np.floataa   )
       elif isinstance(snake_case_     ,		np.ndarray   ) and raw_speech.dtype is np.dtype(np.floataa   ):
         snake_case__						= raw_speech.astype(np.floataa   )

       # always return batch
       if not is_batched:
         snake_case__						= [np.asarray(snake_case_   )]

       # convert to mel spectrogram, truncate and pad if needed.
       snake_case__						= [
           self._get_input_mel(snake_case_     ,		max_length if max_length else self.nb_max_samples     ,		snake_case_     ,		snake_case_   )
           for waveform in raw_speech
       ]

       snake_case__						= []
       snake_case__						= []
       for mel, longer in padded_inputs:
         input_mel.append(snake_case_   )
         is_longer.append(snake_case_   )

       if truncation == "fusion" and sum(snake_case_   ) == 0:
         # if no audio is longer than 10s, then randomly select one audio to be longer
         snake_case__						= np.random.randint(0     ,		len(snake_case_   )   )
         snake_case__						= True

       if isinstance(input_mel[0]     ,		snake_case_   ):
         snake_case__						= [np.asarray(snake_case_     ,		dtype=np.floataa   ) for feature in input_mel]

       # is_longer is a list of bool
       snake_case__						= [[longer] for longer in is_longer]

       snake_case__						= {'''input_features''': input_mel, '''is_longer''': is_longer}
       snake_case__						= BatchFeature(snake_case_   )

       if return_tensors is not None:
         snake_case__						= input_features.convert_to_tensors(snake_case_   )

       return input_features



"""simple docstring"""
import enum
import warnings

from ..tokenization_utils import TruncationStrategy
from ..utils import add_end_docstrings, is_tf_available, is_torch_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_tf_available():
			import tensorflow as tf

			from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

if is_torch_available():
			from ..models.auto.modeling_auto import MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING

SCREAMING_SNAKE_CASE_					     =	logging.get_logger(__name__)
class       lowerCAmelCase_		(				enum.Enum ):



							'''simple docstring'''





							_snake_case						      =		0
							_snake_case						      =		1
@add_end_docstrings(A__ )
class       lowerCAmelCase_		(				A__ ):



							'''simple docstring'''





							_snake_case						      =		'''generated'''




							def __init__( self    ,				*snake_case_    ,				**snake_case_				)       ->  Optional[int]:
													super().__init__(*snake_case_    ,				**snake_case_				)

													self.check_model_type(
													    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
													    if self.framework == """tf"""
													    else MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING				)




							def    A__						( self    ,				snake_case_=None    ,				snake_case_=None    ,				snake_case_=None    ,				snake_case_=None    ,				snake_case_=None    ,				snake_case_=None    ,				**snake_case_    ,				)       ->  Union[str, Any]:
													__lowerCAmelCase     =  {}
													if truncation is not None:
																			__lowerCAmelCase     =  truncation

													__lowerCAmelCase     =  generate_kwargs

													__lowerCAmelCase     =  {}
													if return_tensors is not None and return_type is None:
																			__lowerCAmelCase     =  ReturnType.TENSORS if return_tensors else ReturnType.TEXT
													if return_type is not None:
																			__lowerCAmelCase     =  return_type

													if clean_up_tokenization_spaces is not None:
																			__lowerCAmelCase     =  clean_up_tokenization_spaces

													if stop_sequence is not None:
																			__lowerCAmelCase     =  self.tokenizer.encode(snake_case_    ,				add_special_tokens=snake_case_				)
																			if len(snake_case_				) > 1:
																									warnings.warn(
																									    """Stopping on a multiple token sequence is not yet supported on transformers. The first token of"""
																									    """ the stop sequence will be used as the stop sequence string in the interim."""				)
																			__lowerCAmelCase     =  stop_sequence_ids[0]

													return preprocess_params, forward_params, postprocess_params




							def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  Any:
													return True




							def    A__						( self    ,				*snake_case_    ,				snake_case_				)       ->  Dict:
													__lowerCAmelCase     =  self.model.config.prefix if self.model.config.prefix is not None else """"""
													if isinstance(args[0]    ,				snake_case_				):
																			if self.tokenizer.pad_token_id is None:
																									raise ValueError("""Please make sure that the tokenizer has a pad_token_id when using a batch input"""				)
																			__lowerCAmelCase     =  ([prefix + arg for arg in args[0]],)
																			__lowerCAmelCase     =  True

													elif isinstance(args[0]    ,				snake_case_				):
																			__lowerCAmelCase     =  (prefix + args[0],)
																			__lowerCAmelCase     =  False
													else:
																			raise ValueError(
																			    f""" `args[0]`: {args[0]} have the wrong format. The should be either of type `str` or type `list`"""				)
													__lowerCAmelCase     =  self.tokenizer(*snake_case_    ,				padding=snake_case_    ,				truncation=snake_case_    ,				return_tensors=self.framework				)
													# This is produced by tokenizers but is an invalid generate kwargs
													if "token_type_ids" in inputs:
																			del inputs["token_type_ids"]
													return inputs




							def __call__( self    ,				*snake_case_    ,				**snake_case_				)       ->  Dict:
													__lowerCAmelCase     =  super().__call__(*snake_case_    ,				**snake_case_				)
													if (
													    isinstance(args[0]    ,				snake_case_				)
													    and all(isinstance(snake_case_    ,				snake_case_				) for el in args[0]				)
													    and all(len(snake_case_				) == 1 for res in result				)
													):
																			return [res[0] for res in result]
													return result




							def    A__						( self    ,				snake_case_    ,				snake_case_=TruncationStrategy.DO_NOT_TRUNCATE    ,				**snake_case_				)       ->  Tuple:
													__lowerCAmelCase     =  self._parse_and_tokenize(snake_case_    ,				truncation=snake_case_    ,				**snake_case_				)
													return inputs




							def    A__						( self    ,				snake_case_    ,				**snake_case_				)       ->  Union[str, Any]:
													if self.framework == "pt":
																			__lowerCAmelCase   ,     __lowerCAmelCase     =  model_inputs["""input_ids"""].shape
													elif self.framework == "tf":
																			__lowerCAmelCase   ,     __lowerCAmelCase     =  tf.shape(model_inputs["""input_ids"""]				).numpy()

													__lowerCAmelCase     =  generate_kwargs.get("""min_length"""    ,				self.model.config.min_length				)
													__lowerCAmelCase     =  generate_kwargs.get("""max_length"""    ,				self.model.config.max_length				)
													self.check_inputs(snake_case_    ,				generate_kwargs["""min_length"""]    ,				generate_kwargs["""max_length"""]				)
													__lowerCAmelCase     =  self.model.generate(**snake_case_    ,				**snake_case_				)
													__lowerCAmelCase     =  output_ids.shape[0]
													if self.framework == "pt":
																			__lowerCAmelCase     =  output_ids.reshape(snake_case_    ,				out_b // in_b    ,				*output_ids.shape[1:]				)
													elif self.framework == "tf":
																			__lowerCAmelCase     =  tf.reshape(snake_case_    ,				(in_b, out_b // in_b, *output_ids.shape[1:])				)
													return {"output_ids": output_ids}




							def    A__						( self    ,				snake_case_    ,				snake_case_=ReturnType.TEXT    ,				snake_case_=False				)       ->  Dict:
													__lowerCAmelCase     =  []
													for output_ids in model_outputs["output_ids"][0]:
																			if return_type == ReturnType.TENSORS:
																									__lowerCAmelCase     =  {f"""{self.return_name}_token_ids""": output_ids}
																			elif return_type == ReturnType.TEXT:
																									__lowerCAmelCase     =  {
																									    f"""{self.return_name}_text""": self.tokenizer.decode(
																									        snake_case_    ,				skip_special_tokens=snake_case_    ,				clean_up_tokenization_spaces=snake_case_    ,				)
																									}
																			records.append(snake_case_				)
													return records
@add_end_docstrings(A__ )
class       lowerCAmelCase_		(				A__ ):



							'''simple docstring'''





							_snake_case						      =		'''summary'''




							def __call__( self    ,				*snake_case_    ,				**snake_case_				)       ->  Tuple:
													return super().__call__(*snake_case_    ,				**snake_case_				)




							def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  bool:
													if max_length < min_length:
																			logger.warning(f"""Your min_length={min_length} must be inferior than your max_length={max_length}."""				)

													if input_length < max_length:
																			logger.warning(
																			    f"""Your max_length is set to {max_length}, but your input_length is only {input_length}. Since this is """
																			    """a summarization task, where outputs shorter than the input are typically wanted, you might """
																			    f"""consider decreasing max_length manually, e.g. summarizer('...', max_length={input_length//2})"""				)
@add_end_docstrings(A__ )
class       lowerCAmelCase_		(				A__ ):



							'''simple docstring'''





							_snake_case						      =		'''translation'''




							def    A__						( self    ,				snake_case_    ,				snake_case_    ,				snake_case_				)       ->  Optional[int]:
													if input_length > 0.9 * max_length:
																			logger.warning(
																			    f"""Your input_length: {input_length} is bigger than 0.9 * max_length: {max_length}. You might consider """
																			    """increasing your max_length manually, e.g. translator('...', max_length=400)"""				)
													return True




							def    A__						( self    ,				*snake_case_    ,				snake_case_=TruncationStrategy.DO_NOT_TRUNCATE    ,				snake_case_=None    ,				snake_case_=None				)       ->  List[Any]:
													if getattr(self.tokenizer    ,				"""_build_translation_inputs"""    ,				snake_case_				):
																			return self.tokenizer._build_translation_inputs(
																			    *snake_case_    ,				return_tensors=self.framework    ,				truncation=snake_case_    ,				src_lang=snake_case_    ,				tgt_lang=snake_case_				)
													else:
																			return super()._parse_and_tokenize(*snake_case_    ,				truncation=snake_case_				)




							def    A__						( self    ,				snake_case_=None    ,				snake_case_=None    ,				**snake_case_				)       ->  List[Any]:
													__lowerCAmelCase   ,     __lowerCAmelCase   ,     __lowerCAmelCase     =  super()._sanitize_parameters(**snake_case_				)
													if src_lang is not None:
																			__lowerCAmelCase     =  src_lang
													if tgt_lang is not None:
																			__lowerCAmelCase     =  tgt_lang
													if src_lang is None and tgt_lang is None:
																			# Backward compatibility, direct arguments use is preferred.
																			__lowerCAmelCase     =  kwargs.get("""task"""    ,				self.task				)
																			__lowerCAmelCase     =  task.split("""_"""				)
																			if task and len(snake_case_				) == 4:
																									# translation, XX, to YY
																									__lowerCAmelCase     =  items[1]
																									__lowerCAmelCase     =  items[3]
													return preprocess_params, forward_params, postprocess_params




							def __call__( self    ,				*snake_case_    ,				**snake_case_				)       ->  List[Any]:
													return super().__call__(*snake_case_    ,				**snake_case_				)
