
    gG                         d Z ddlmZmZmZmZ ddlZddlm	Z	m
Z
 ddlmZmZmZmZmZmZ  ej        e          Z G d de
          ZdS )	zZ
Sequence feature extraction class for common feature extractors to preprocess sequences.
    )DictListOptionalUnionN   )BatchFeatureFeatureExtractionMixin)PaddingStrategy
TensorTypeis_tf_tensoris_torch_tensorloggingto_numpyc                       e Zd ZdZdededef fdZ	 	 	 	 	 	 dd	eee	e         e
eef         e
ee	e         f         e	e
eef                  f         d
eeeef         dee         dedee         dee         deeeef                  defdZdej        ddfd	ee
eej        f         ef         dee         dedee         dee         defdZ	 	 	 dd	ee
eej        f         ef         dee         dee         dee         fdZddZ xZS )SequenceFeatureExtractora  
    This is a general feature extraction class for speech recognition.

    Args:
        feature_size (`int`):
            The feature dimension of the extracted features.
        sampling_rate (`int`):
            The sampling rate at which the audio files should be digitalized expressed in hertz (Hz).
        padding_value (`float`):
            The value that is used to fill the padding values / vectors.
    feature_sizesampling_ratepadding_valuec                     || _         || _        || _        |                    dd          | _        |                    dd          | _         t                      j        di | d S )Npadding_siderightreturn_attention_maskT )r   r   r   popr   r   super__init__)selfr   r   r   kwargs	__class__s        j/var/www/html/ai-engine/env/lib/python3.11/site-packages/transformers/feature_extraction_sequence_utils.pyr   z!SequenceFeatureExtractor.__init__+   sk    (**"JJ~w??%+ZZ0G%N%N"""6"""""    TNFprocessed_featurespadding
max_length
truncationpad_to_multiple_ofr   return_tensorsreturnc           	      B    t          t          t          f          rHt          d         t          t          f          r&fdd                                         D              j        d         vr?t          d j        d          dt                                                                j        d                  }||n j        }t          |          dk    r	|rg d<   S |d         }	t          |	t          t          f          rZd}
t          ||
                   dk    r|
dz  }
t          ||
                   dk    |
t          |          k     r||
         d         }	|}t          |	          rd}nkt          |	          rd	}nYt          |	t          t          t          t          t          j        f          rd
}n#t          d|	 dt!          |	           d                                          D ]I\  }}t          |d         t          t          f          rt%          |          |<   :d |D             |<   J                     ||          } j        d                  }t          |          t)          fd                                D                       st          d          g }t-                    D ]Pfd                                D             }                     ||||          }|                    |           Q|t2          j        k    r't7           fd|D                       }t2          j        }i }t-                    D ]                     |         ||||          }|                                D ]m\  }}||vrg ||<   |j        t          j        t          j                  u r|                     t          j!                  }||                             |           nt	          ||          S )a  
        Pad input values / input vectors or a batch of input values / input vectors up to predefined length or to the
        max sequence length in the batch.

        Padding side (left/right) padding values are defined at the feature extractor level (with `self.padding_side`,
        `self.padding_value`)

        <Tip>

        If the `processed_features` passed are dictionary of numpy arrays, PyTorch tensors or TensorFlow tensors, the
        result will use the same type unless you provide a different tensor type with `return_tensors`. In the case of
        PyTorch tensors, you will lose the specific device of your tensors however.

        </Tip>

        Args:
            processed_features ([`BatchFeature`], list of [`BatchFeature`], `Dict[str, List[float]]`, `Dict[str, List[List[float]]` or `List[Dict[str, List[float]]]`):
                Processed inputs. Can represent one input ([`BatchFeature`] or `Dict[str, List[float]]`) or a batch of
                input values / vectors (list of [`BatchFeature`], *Dict[str, List[List[float]]]* or *List[Dict[str,
                List[float]]]*) so you can use this method during preprocessing as well as in a PyTorch Dataloader
                collate function.

                Instead of `List[float]` you can have tensors (numpy arrays, PyTorch tensors or TensorFlow tensors),
                see the note above for the return type.
            padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `True`):
                Select a strategy to pad the returned sequences (according to the model's padding side and padding
                index) among:

                - `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
                  sequence if provided).
                - `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum
                  acceptable input length for the model if that argument is not provided.
                - `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different
                  lengths).
            max_length (`int`, *optional*):
                Maximum length of the returned list and optionally padding length (see above).
            truncation (`bool`):
                Activates truncation to cut input sequences longer than `max_length` to `max_length`.
            pad_to_multiple_of (`int`, *optional*):
                If set will pad the sequence to a multiple of the provided value.

                This is especially useful to enable the use of Tensor Cores on NVIDIA hardware with compute capability
                `>= 7.5` (Volta), or on TPUs which benefit from having sequence lengths be a multiple of 128.
            return_attention_mask (`bool`, *optional*):
                Whether to return the attention mask. If left to the default, will return the attention mask according
                to the specific feature_extractor's default.

                [What are attention masks?](../glossary#attention-mask)
            return_tensors (`str` or [`~utils.TensorType`], *optional*):
                If set, will return tensors instead of list of python integers. Acceptable values are:

                - `'tf'`: Return TensorFlow `tf.constant` objects.
                - `'pt'`: Return PyTorch `torch.Tensor` objects.
                - `'np'`: Return Numpy `np.ndarray` objects.
        r   c                 0    i | ]fd D             S )c                      g | ]
}|         S r   r   ).0examplekeys     r    
<listcomp>z;SequenceFeatureExtractor.pad.<locals>.<dictcomp>.<listcomp>   s    EEEwgclEEEr!   r   )r,   r.   r"   s    @r    
<dictcomp>z0SequenceFeatureExtractor.pad.<locals>.<dictcomp>   s?     " " "JMEEEE2DEEE" " "r!   zYou should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature` to this method that includes z, but you provided Nattention_maskr   tfptnpztype of z
 unknown: zA. Should be one of a python, numpy, pytorch or tensorflow object.c                 ,    g | ]}t          |          S r   )r   )r,   vs     r    r/   z0SequenceFeatureExtractor.pad.<locals>.<listcomp>   s    *F*F*F18A;;*F*F*Fr!   )r#   r$   c              3   >   K   | ]}t          |          k    V  d S )N)len)r,   r6   
batch_sizes     r    	<genexpr>z/SequenceFeatureExtractor.pad.<locals>.<genexpr>   s.      MMA3q66Z'MMMMMMr!   zLSome items in the output dictionary have a different batch size than others.c                 (    i | ]\  }}||         S r   r   )r,   kr6   is      r    r0   z0SequenceFeatureExtractor.pad.<locals>.<dictcomp>   s#    EEE$!Qa1EEEr!   )r$   r&   r%   c              3   X   K   | ]$}t          |j        d                             V  %dS )r   N)r8   model_input_names)r,   input_slicer   s     r    r:   z/SequenceFeatureExtractor.pad.<locals>.<genexpr>   s8      mm[ST-CA-F!GHHmmmmmmr!   )r$   padding_strategyr&   r   )tensor_type)"
isinstancelisttupledictr   keysr?   
ValueErrorr   r8   r   r   intfloatr4   ndarraytypeitemsr   _get_padding_strategiesallvaluesrange	_truncateappendr
   LONGESTmax
MAX_LENGTH_paddtypefloat64astypefloat32)r   r"   r#   r$   r%   r&   r   r'   required_inputfirst_elementindexr.   valuerA   truncated_inputsinputsinputs_slicebatch_outputsoutputsr9   r=   s   ``                 @@r    padzSequenceFeatureExtractor.pad5   s   R (4-88 	ZHZ[\H]`dfr_s=t=t 	" " " "QcdeQfQkQkQmQm" " "
 !!$,>>>6151G1J6 6+0022336 6   ,D,B1,EF%:%F!!DLf 	 ~!##$ :79"#34%% 'q)mdE]33 	9EnU+,,11
 nU+,,11s>**** .u 5a 8!M** 
!% // !%MCeRZ+PQQ !% V} V V]8K8K V V V  
 -2244 	G 	GJC%(S%L11 G*25//"3''*F*F*F*F*F"3''  77T^7__+D,B1,EF((
MMMM1C1J1J1L1LMMMMM 	mklllz"" 		2 		2AEEEE*<*B*B*D*DEEEF>>%#5%	 *  L ##L1111666mmmm\lmmmmmJ.9z"" 	1 	1Aii #%!1#5&;    G &mmoo 1 1
Um++)+M#&;"(2:"6"666!LL44Ec"))%00001 M~FFFFr!   rA   c                 v   || j         d                  }|t          j        k    rt          |          }||||z  dk    r||z  dz   |z  }|t          j        k    ot          |          |k     }|r4d|vr0t          j        t          |          t
          j                  |d<   |r|t          |          z
  }| j        dk    rc|r t          j	        |d         d|f          |d<   | j
        dk    rd|fdfnd|f}	t          j	        ||	d| j        	          || j         d         <   n| j        d
k    rc|r t          j	        |d         |df          |d<   | j
        dk    r|dfdfn|df}	t          j	        ||	d| j        	          || j         d         <   n$t          dt          | j                  z             |S )a  
        Pad inputs (on left/right and up to predefined length or max length in the batch)

        Args:
            processed_features (`Union[Dict[str, np.ndarray], BatchFeature]`):
                Dictionary of input values (`np.ndarray[float]`) / input vectors (`List[np.ndarray[float]]`) or batch
                of inputs values (`List[np.ndarray[int]]`) / input vectors (`List[np.ndarray[int]]`)
            max_length (`int`, *optional*):
                Maximum length of the returned list and optionally padding length (see below)
            padding_strategy (`PaddingStrategy`, *optional*, default to `PaddingStrategy.DO_NOT_PAD`):
                PaddingStrategy to use for padding.

                - PaddingStrategy.LONGEST Pad to the longest sequence in the batch
                - PaddingStrategy.MAX_LENGTH: Pad to the max length (default)
                - PaddingStrategy.DO_NOT_PAD: Do not pad
                The feature_extractor padding sides are defined in self.padding_side:

                    - 'left': pads on the left of the sequences
                    - 'right': pads on the right of the sequences
            pad_to_multiple_of (`int`, *optional*):
                Integer if set will pad the sequence to a multiple of the provided value. This is especially useful to
                enable the use of Tensor Core on NVIDIA hardware with compute capability `>= 7.5` (Volta), or on TPUs
                which benefit from having sequence lengths be a multiple of 128.
            return_attention_mask (`bool`, *optional*):
                Set to False to avoid returning attention mask (default: set to model specifics)
        r   Nr   r1   )rX   r   )r   r   constant)constant_valuesleftzInvalid padding strategy:)r?   r
   rT   r8   
DO_NOT_PADr4   onesint32r   re   r   r   rH   str)
r   r"   r$   rA   r&   r   r\   needs_to_be_padded
differencepadding_shapes
             r    rW   zSequenceFeatureExtractor._pad   sD   D ,D,B1,EF666^,,J!&8&D*WiJimnJnJn%);;q@DVVJ-1KKpPSTbPcPcfpPp  	`%5=O%O%O3573~;N;NVXV^3_3_3_/0 	W#c.&9&99J G++( ;=6*+;<q*o< <&'78 >B=NQR=R=R!Z& 9 9YZ\fXg@B"M:tOaA A A"4#9!#<== "f,,( ;=6*+;<z1o< <&'78 >B=NQR=R=R*a& 9 9YcefXg@B"M:tOaA A A"4#9!#<== !!<s4CT?U?U!UVVV!!r!   c                 4   |s|S |r|t          d          || j        d                  }||||z  dk    r||z  dz   |z  }t          |          |k    }|r@|| j        d                  d|         || j        d         <   d|v r|d         d|         |d<   |S )a  
        Truncate inputs to predefined length or max length in the batch

        Args:
            processed_features(`Union[Dict[str, np.ndarray], BatchFeature]`):
                Dictionary of input values (`np.ndarray[float]`) / input vectors (`List[np.ndarray[float]]`) or batch
                of inputs values (`List[np.ndarray[int]]`) / input vectors (`List[np.ndarray[int]]`)
            max_length (`int`, *optional*):
                maximum length of the returned list and optionally padding length (see below)
            pad_to_multiple_of (`int`, *optional*) :
                Integer if set will pad the sequence to a multiple of the provided value. This is especially useful to
                enable the use of Tensor Core on NVIDIA hardware with compute capability `>= 7.5` (Volta), or on TPUs
                which benefit from having sequence lengths be a multiple of 128.
            truncation (`bool`, *optional*):
                Activates truncation to cut input sequences longer than `max_length` to `max_length`.
        NzKWhen setting ``truncation=True``, make sure that ``max_length`` is defined.r   r   r1   )rH   r?   r8   )r   r"   r$   r&   r%   r\   needs_to_be_truncateds          r    rR   z"SequenceFeatureExtractor._truncate+  s    .  	l%% 	lJ.jkkk+D,B1,EF !&8&D*WiJimnJnJn%);;q@DVVJ #N 3 3j @  	i<NtOefgOh<ijukuju<vt5a89#5557IJZ7[\g]g\g7h"#34!!r!   c                 l   |durN|du rt           j        }nIt          |t                     st          |          }n$t          |t                     r|}nt           j        }|-|t           j        k    rt          dt           j         d          |t           j        k    r| j        t          d          |S )z3
        Find the correct padding strategy
        FTNzWhen setting ``padding=z(``, make sure that max_length is definedzAsking to pad but the feature_extractor does not have a padding value. Please select a value to use as `padding_value`. For example: `feature_extractor.padding_value = 0.0`.)r
   rT   rC   rj   rV   rH   r   )r   r#   r$   rA   s       r    rN   z0SequenceFeatureExtractor._get_padding_strategiesV  s     %$#2#:  99 +#27#;#;  G_55 +#* .9 ?#=== ro.Hrrr  
 999t?Q?Y]  
  r!   )TNFNNN)NNN)FN)__name__
__module____qualname____doc__rI   rJ   r   r   r   r   r   rm   boolr
   r   r   re   rj   r4   rK   rF   rW   rR   rN   __classcell__)r   s   @r    r   r      so       
 
#S # #U # # # # # #& 6:$( ,004;?lG lG!l"#d<(()c<'()	+
lG tS/12lG SMlG lG %SMlG  (~lG !sJ!78lG 
lG lG lG lGb %),;,F,004F" F"!$sBJ"7"EFF" SMF" *	F"
 %SMF"  (~F" 
F" F" F" F"V %),0%))" )"!$sBJ"7"EF)" SM)" %SM	)"
 TN)" )" )" )"V               r!   r   )rw   typingr   r   r   r   numpyr4   feature_extraction_utilsr   r	   utilsr
   r   r   r   r   r   
get_loggerrt   loggerr   r   r!   r    <module>r      s     / . . . . . . . . . . .     J J J J J J J J ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` 
	H	%	%V  V  V  V  V 5 V  V  V  V  V r!   