§
    9²¸j²B  ã                   ó¸   — d Z ddlZddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlmZmZ  e¦   «         rddlZ ej        e¦  «        Z G d	„ d
e
¦  «        Zd
gZdS )z%
Feature extractor class for Whisper
é    Né   )Úis_torch_available)Úmel_filter_bankÚspectrogramÚwindow_function)ÚSequenceFeatureExtractor)ÚBatchFeature)Ú
TensorTypeÚloggingc                   óÎ  ‡ — e Zd ZdZdgZ	 	 	 	 	 	 	 	 d"ˆ fd
„	Zdej        dedej        fd„Z	d#dej        dedej        fd„Z
e	 d$deej                 deej                 dedeej                 fd„¦   «         Z	 	 	 	 	 	 	 	 	 d%dej        ee         z  eej                 z  eee                  z  dededz  deez  dz  dedz  dedz  dedz  dedz  d edz  dedz  defd!„Zˆ xZS )&ÚWhisperFeatureExtractoraŸ  
    Constructs a Whisper feature extractor.

    This feature extractor inherits from [`~feature_extraction_sequence_utils.SequenceFeatureExtractor`] which contains
    most of the main methods. Users should refer to this superclass for more information regarding those methods.

    This class extracts mel-filter bank features from raw speech using a custom numpy implementation of the `Short Time
    Fourier Transform` which should match pytorch's `torch.stft` equivalent.

    Args:
        feature_size (`int`, *optional*, defaults to 80):
            The feature dimension of the extracted features.
        sampling_rate (`int`, *optional*, defaults to 16000):
            The sampling rate at which the audio files should be digitalized expressed in hertz (Hz).
        hop_length (`int`, *optional*, defaults to 160):
            Length of the overlapping windows for the STFT used to obtain the Mel Frequency coefficients.
        chunk_length (`int`, *optional*, defaults to 30):
            The maximum number of chunks of `sampling_rate` samples used to trim and pad longer or shorter audio
            sequences.
        n_fft (`int`, *optional*, defaults to 400):
            Size of the Fourier transform.
        padding_value (`float`, *optional*, defaults to 0.0):
            Padding value used to pad the audio. Should correspond to silences.
        dither (`float`, *optional*, defaults to 0.0):
            Adds dithering. In other words, adds a small Gaussian noise to each frame.
            E.g. use 0.0001 to add dithering with a normal distribution centered
            around 0.0 with standard deviation 0.0001 (assuming [-1,+1] range of raw_speech).
            The value 0.0 means no dithering.
            Dithering has similar effect as `spectrogram(mel_floor=...)`. It reduces
            the high log_mel_fbank values for signals with hard-zero sections,
            when VAD cutoff is present in the signal.
    Úinput_featureséP   é€>  é    é   é�  ç        Fc	           	      óþ   •—  t          ¦   «         j        d||||dœ|	¤Ž || _        || _        || _        ||z  | _        | j        |z  | _        || _        || _        t          d|dz  z   |dd|dd¬¦  «        | _
        d S )	N)Úfeature_sizeÚsampling_rateÚpadding_valueÚreturn_attention_maské   é   r   g     @¿@Úslaney)Únum_frequency_binsÚnum_mel_filtersÚmin_frequencyÚmax_frequencyr   ÚnormÚ	mel_scale© )ÚsuperÚ__init__Ún_fftÚ
hop_lengthÚchunk_lengthÚ	n_samplesÚnb_max_framesr   Úditherr   Úmel_filters)Úselfr   r   r'   r(   r&   r   r+   r   ÚkwargsÚ	__class__s             €ú†/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/whisper/feature_extraction_whisper.pyr%   z WhisperFeatureExtractor.__init__E   s¼   ø€ ð 	�‰ŒÔð 	
Ø%Ø'Ø'Ø"7ð		
ð 	
ð
 ð	
ð 	
ð 	
ð ˆŒ
Ø$ˆŒØ(ˆÔØ%¨Ñ5ˆŒØ!œ^¨zÑ9ˆÔØ*ˆÔØˆŒÝ*Ø  5¨A¡:™~Ø(ØØ Ø'ØØð
ñ 
ô 
ˆÔÐÐó    Úwaveform_batchÚdeviceÚreturnc                 ó”  — |dk    rt          d|› d�¦  «        ‚g }|D ]•}t          |t          | j        d¦  «        | j        | j        d| j        | j        d¬¦  «        }|dd…dd	…f         }t          j        || 	                    ¦   «         d
z
  ¦  «        }|dz   dz  }| 
                    |¦  «         Œ–t          j        |¦  «        }|S )z¦
        Compute the log-mel spectrogram of the provided audio, gives similar results to Whisper's original torch
        implementation with 1e-5 tolerance.
        ÚcpuzGot device `zñ` for feature extraction, but feature extraction on CUDA accelerator devices requires torch, which is not installed. Either set `device='cpu'`, or install torch according to the official instructions: https://pytorch.org/get-started/locally/Úhanng       @Úlog10)Úframe_lengthr'   Úpowerr+   r,   Úlog_melNéÿÿÿÿç       @ç      @)Ú
ValueErrorr   r   r&   r'   r+   r,   ÚnpÚmaximumÚmaxÚappendÚarray)r-   r2   r3   Úlog_spec_batchÚwaveformÚlog_specs         r0   Ú_np_extract_fbank_featuresz2WhisperFeatureExtractor._np_extract_fbank_featuresi   s   € ð
 �UŠ?ˆ?Ýðq˜vð qð qð qñô ð ð
 ˆØ&ð 	,ð 	,ˆHÝ"ØÝ ¤
¨FÑ3Ô3Ø!œZØœ?ØØ”{Ø Ô,Øð	ñ 	ô 	ˆHð      3 B 3 Ô'ˆHÝ”z (¨H¯LªL©N¬N¸SÑ,@ÑAÔAˆHØ  3™¨#Ñ-ˆHØ×!Ò! (Ñ+Ô+Ð+Ð+Ýœ .Ñ1Ô1ˆØÐr1   r6   rF   c                 óH  — t          j        |¦  «                             |t           j        ¦  «        }t          j        | j        |¬¦  «        }| j        dk    r1|| j        t          j        |j        |j	        |j
        ¬¦  «        z  z  }t          j        || j        | j        |d¬¦  «        }|ddd…f                              ¦   «         d	z                       ¦   «         }t          j        | j        ¦  «                             |t           j        ¦  «        }|j        |z  }t          j        |d
¬¦  «                             ¦   «         }|                     ¦   «         d	k    rQ|                     d	d¬¦  «        d                              dd¬¦  «        d         }	t          j        ||	dz
  ¦  «        }n*t          j        ||                     ¦   «         dz
  ¦  «        }|dz   dz  }|dk    r&|                     ¦   «                              ¦   «         }|                     ¦   «         S )zÈ
        Compute the log-mel spectrogram of the audio using PyTorch's GPU-accelerated STFT implementation with batching,
        yielding results similar to cpu computing with 1e-5 tolerance.
        )r3   r   )Údtyper3   T)ÚwindowÚreturn_complex.Nr<   r   g»½×Ùß|Û=)Úmin)ÚdimÚkeepdimr   r   r=   r>   r6   )ÚtorchÚ
from_numpyÚtoÚfloat32Úhann_windowr&   r+   ÚrandnÚshaperJ   r3   Ústftr'   ÚabsÚ
contiguousr,   ÚTÚclampr8   rN   rB   rA   Údetachr6   Únumpy)
r-   rF   r3   rK   rW   Ú
magnitudesr,   Úmel_specrG   Úmax_vals
             r0   Ú_torch_extract_fbank_featuresz5WhisperFeatureExtractor._torch_extract_fbank_features‡   sÓ  € õ
 Ô# HÑ-Ô-×0Ò0°½¼ÑGÔGˆÝÔ" 4¤:°fÐ=Ñ=Ô=ˆð
 Œ;˜#ÒÐØ˜œ¥e¤k°(´.ÈÌÐ_gÔ_nÐ&oÑ&oÔ&oÑoÑoˆHåŒz˜( D¤J°´ÈÐ_cÐdÑdÔdˆð
 ˜3   ˜8”n×(Ò(Ñ*Ô*¨aÑ/×;Ò;Ñ=Ô=ˆ
åÔ& tÔ'7Ñ8Ô8×;Ò;¸FÅEÄMÑRÔRˆØ”= :Ñ-ˆå”;˜x¨UÐ3Ñ3Ô3×9Ò9Ñ;Ô;ˆØ�<Š<‰>Œ>˜QÒÐØ—l’l q°$�lÑ7Ô7¸Ô:×>Ò>À1ÈdÐ>ÑSÔSÐTUÔVˆGÝ”} X¨w¸©}Ñ=Ô=ˆHˆHå”} X¨x¯|ª|©~¬~ÀÑ/CÑDÔDˆHØ˜s‘N cÑ)ˆØ�UŠ?ˆ?Ø—’Ñ(Ô(×,Ò,Ñ.Ô.ˆHØ�~Š~ÑÔÐr1   Úinput_valuesÚattention_maskr   c                 ó´  — |�Ét          j        |t           j        ¦  «        }g }t          | |                     d¦  «        ¦  «        D ]ƒ\  }}||d|…                              ¦   «         z
  t          j        |d|…                              ¦   «         dz   ¦  «        z  }||j        d         k     r|||d…<   | 	                    |¦  «         Œ„nd„ | D ¦   «         }|S )z[
        Every array in the list is normalized to have zero mean and unit variance
        Nr<   çH¯¼šò×z>r   c                 ó�   — g | ]C}||                      ¦   «         z
  t          j        |                     ¦   «         d z   ¦  «        z  ‘ŒDS )re   )Úmeanr@   ÚsqrtÚvar)Ú.0Úxs     r0   ú
<listcomp>zCWhisperFeatureExtractor.zero_mean_unit_var_norm.<locals>.<listcomp>½   s@   € Ð"bÐ"bÐ"bÐPQ A¨¯ª©¬¡LµB´G¸A¿EºE¹G¼GÀd¹NÑ4KÔ4KÑ#KÐ"bÐ"bÐ"br1   )
r@   rD   Úint32ÚzipÚsumrg   rh   ri   rV   rC   )rb   rc   r   Únormed_input_valuesÚvectorÚlengthÚnormed_slices          r0   Úzero_mean_unit_var_normz/WhisperFeatureExtractor.zero_mean_unit_var_normª   só   € ð Ð%ÝœX nµb´hÑ?Ô?ˆNØ"$Ðå"% l°N×4FÒ4FÀrÑ4JÔ4JÑ"KÔ"Kð 9ð 9‘�˜Ø &¨°°°¬×)=Ò)=Ñ)?Ô)?Ñ ?Å2Ä7È6ÐRYÐSYÐRYÌ?×K^ÒK^ÑK`ÔK`ÐcgÑKgÑChÔChÑh�Ø˜LÔ.¨qÔ1Ò1Ð1Ø,9�L   Ñ)à#×*Ò*¨<Ñ8Ô8Ð8Ð8ð9ð #cÐ"bÐUaÐ"bÑ"bÔ"bÐà"Ð"r1   TNÚ
max_lengthÚ
raw_speechÚ
truncationÚpad_to_multiple_ofÚreturn_tensorsr   Úpaddingr   Údo_normalizec                 ó”  — |�<|| j         k    r0t          d| j        j        › d| j         › d| j         › d|› d�	¦  «        ‚n(t                               d| j        j        › d�¦  «         t          |t          j        ¦  «        ot          |j
        ¦  «        d	k    }|r*t          |j
        ¦  «        d
k    rt          d| › �¦  «        ‚|pHt          |t          t          f¦  «        o,t          |d         t          j        t          t          f¦  «        }|rd„ |D ¦   «         }n›|s;t          |t          j        ¦  «        s!t          j        |t          j        ¬¦  «        }n^t          |t          j        ¦  «        rD|j        t          j        t          j        ¦  «        u r|                     t          j        ¦  «        }|st          j        |g¦  «        j        g}t'          d|i¦  «        }|                      |||r|n| j        |||p|	¬¦  «        }|	rK|                      |d         |d         | j        ¬¦  «        |d<   t          j        |d         d¬¦  «        |d<   |                     d¦  «                             d
dd	¦  «        }t7          ¦   «         r| j        n| j        } ||d         |
¦  «        }t          |d         t          ¦  «        rd„ |D ¦   «         |d<   n||d<   |rL|d         dd…dd| j        …f         }|d         j
        d	         | j        z  dk    r|dd…dd…f         }||d<   |�|                     |¦  «        }|S )a]  Main method to featurize and prepare for the model one or several sequence(s). Implementation uses PyTorch
        for the STFT computation if available, otherwise a slower NumPy based one.

        Args:
            raw_speech (`np.ndarray`, `list[float]`, `list[np.ndarray]`, `list[list[float]]`):
                The sequence or batch of sequences to be padded. Each sequence can be a numpy array, a list of float
                values, a list of numpy arrays or a list of list of float values. Must be mono channel audio, not
                stereo, i.e. single float per timestep.
            truncation (`bool`, *optional*, default to `True`):
                Activates truncation to cut input sequences longer than *max_length* to *max_length*.
            pad_to_multiple_of (`int`, *optional*, defaults to None):
                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_tensors (`str` or [`~utils.TensorType`], *optional*):
                If set, will return tensors instead of list of python integers. Acceptable values are:

                - `'pt'`: Return PyTorch `torch.Tensor` objects.
                - `'np'`: Return Numpy `np.ndarray` objects.
            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)

                <Tip>

                For Whisper models, `attention_mask` should always be passed for batched inference, to avoid subtle
                bugs.

                </Tip>
            padding (`str` or [`~utils.PaddingStrategy`], *optional*, defaults to `'max_length'`):
                Activates and controls padding. Accepts the following values:

                - `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single sequence is
                  provided).
                - `'max_length'` (default): 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.
                - `'do_not_pad'`: No padding (i.e., can output a batch with sequences of different lengths).
            max_length (`int`, *optional*):
                Controls the maximum length to use by one of the truncation/padding parameters.

                If left unset or set to `None`, this will use the predefined model maximum length if a maximum length
                is required by one of the truncation/padding parameters. If the model has no specific maximum input
                length (like XLNet) truncation/padding to a maximum length will be deactivated.
            sampling_rate (`int`, *optional*):
                The sampling rate at which the `raw_speech` input was sampled. It is strongly recommended to pass
                `sampling_rate` at the forward call to prevent silent errors and allow automatic speech recognition
                pipeline.
            do_normalize (`bool`, *optional*, defaults to `False`):
                Whether or not to zero-mean unit-variance normalize the input. Normalizing can help to significantly
                improve the performance of the model.
            device (`str`, *optional*, defaults to `'cpu'`):
                Specifies the device for computation of the log-mel spectrogram of audio signals in the
                `_torch_extract_fbank_features` method. (e.g., "cpu", "cuda")
            **kwargs: Not supported by WhisperFeatureExtractor.__call__() and ignored.
        Nz3The model corresponding to this feature extractor: z& was trained using a sampling rate of zI. Please make sure that the provided `raw_speech` input was sampled with z	 and not ú.zDIt is strongly recommended to pass the `sampling_rate` argument to `zN()`. Failing to do so can result in silent errors that might be hard to debug.r   r   z2Only mono-channel audio is supported for input to r   c                 óZ   — g | ](}t          j        |gt           j        ¬ ¦  «        j        ‘Œ)S ©©rJ   )r@   ÚasarrayrS   rZ   )rj   Úspeechs     r0   rl   z4WhisperFeatureExtractor.__call__.<locals>.<listcomp>  s.   € Ð\Ð\Ð\Àv�"œ* f XµR´ZÐ@Ñ@Ô@ÔBÐ\Ð\Ð\r1   r€   r   )rz   ru   rw   rx   r   rc   )rc   r   )Úaxisc                 óN   — g | ]"}t          j        |t           j        ¬ ¦  «        ‘Œ#S r   )r@   r�   rS   )rj   Úfeatures     r0   rl   z4WhisperFeatureExtractor.__call__.<locals>.<listcomp>G  s*   € Ð.sÐ.sÐ.sÐY`­r¬z¸'ÍÌÐ/TÑ/TÔ/TÐ.sÐ.sÐ.sr1   r<   ) r   r?   r/   Ú__name__ÚloggerÚwarningÚ
isinstancer@   ÚndarrayÚlenrV   ÚlistÚtupler�   rS   rJ   Úfloat64ÚastyperZ   r	   Úpadr)   rt   r   ÚstackÚgetÚ	transposer   ra   rH   r'   Úconvert_to_tensors)r-   rv   rw   rx   ry   r   rz   ru   r   r{   r3   r.   Úis_batched_numpyÚ
is_batchedÚbatched_speechÚpadded_inputsr   Úextract_fbank_featuresÚrescaled_attention_masks                      r0   Ú__call__z WhisperFeatureExtractor.__call__Á   sû  € ðP Ð$Ø Ô 2Ò2Ð2Ý ðWÈ$Ì.ÔJað Wð WØ)-Ô);ðWð Wà)-Ô);ðWð WàFSðWð Wð Wñô ð ð 3õ �NŠNð\ÐW[ÔWeÔWnð \ð \ð \ñô ð õ
 & jµ"´*Ñ=Ô=Ð[Å#ÀjÔFVÑBWÔBWÐZ[ÒB[ÐØð 	Z¥ JÔ$4Ñ 5Ô 5¸Ò 9Ð 9ÝÐXÐRVÐXÐXÑYÔYÐYØ%ð 
Ý�z¥D­% =Ñ1Ô1ÐlµzÀ*ÈQÄ-ÕRTÔR\Õ^cÕeiÐQjÑ7kÔ7kð 	ð ð 	7Ø\Ð\ÐQ[Ð\Ñ\Ô\ˆJˆJØð 	7¥J¨z½2¼:Ñ$FÔ$Fð 	7Ýœ Jµb´jÐAÑAÔAˆJˆJÝ˜
¥B¤JÑ/Ô/ð 	7°JÔ4DÍÌÕQSÔQ[ÑH\ÔH\Ð4\Ð4\Ø#×*Ò*­2¬:Ñ6Ô6ˆJð ð 	6Ýœ* j \Ñ2Ô2Ô4Ð5ˆJå%Ð'7¸Ð&DÑEÔEˆð ŸšØØØ%/ÐC�z�z°T´^Ø!Ø1Ø"7Ð"G¸<ð !ñ 
ô 
ˆð ð 	`Ø.2×.JÒ.JØÐ.Ô/Ø,Ð-=Ô>Ø"Ô0ð /Kñ /ô /ˆMÐ*Ñ+õ
 /1¬h°}ÐEUÔ7VÐ]^Ð._Ñ._Ô._ˆMÐ*Ñ+ð '×*Ò*Ð+;Ñ<Ô<×FÒFÀqÈ!ÈQÑOÔOˆõ 3EÑ2FÔ2FÐkˆDÔ.Ð.ÈDÔLkð 	ð 0Ð/°¸qÔ0AÀ6ÑJÔJˆå�n QÔ'­Ñ.Ô.ð 	=Ø.sÐ.sÐdrÐ.sÑ.sÔ.sˆMÐ*Ñ+Ð+ð /=ˆMÐ*Ñ+à ð 		Fà&3Ð4DÔ&EÀaÀaÀaÐI[ÐI[ÈDÌOÐI[ÐF[Ô&\Ð#ð
 Ð-Ô.Ô4°QÔ7¸$¼/ÑIÈQÒNÐNØ*AÀ!À!À!ÀSÀbÀSÀ&Ô*IÐ'Ø.EˆMÐ*Ñ+àÐ%Ø)×<Ò<¸^ÑLÔLˆMàÐr1   )r   r   r   r   r   r   r   F)r6   )r   )	TNNNru   NNNr6   )r†   Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesr%   r@   rŠ   ÚstrrH   ra   ÚstaticmethodrŒ   Úfloatrt   ÚboolÚintr
   r	   r›   Ú__classcell__)r/   s   @r0   r   r   !   s4  ø€ € € € € ðð ðB *Ð*Ðð ØØØØØØØ#ð"
ð "
ð "
ð "
ð "
ð "
ðH¸¼ð ÈSð ÐUWÔU_ð ð ð ð ð<! ð ! °b´jð ! È#ð ! ÐZ\ÔZdð ! ð ! ð ! ð ! ðF ð beð#ð #Ø˜2œ:Ô&ð#Ø8<¸R¼ZÔ8Hð#ØY^ð#à	ˆbŒjÔ	ð#ð #ð #ñ „\ð#ð0  Ø)-Ø26Ø-1Ø*Ø!%Ø$(Ø$(Ø"ðYð Yà”J  e¤Ñ,¨t°B´JÔ/?Ñ?À$ÀtÈEÄ{ÔBSÑSðYð ðYð   $™Jð	Yð
 ˜jÑ(¨4Ñ/ðYð  $ d™{ðYð �t‘ðYð ˜$‘JðYð ˜T‘zðYð ˜T‘kðYð �d‘
ðYð 
ðYð Yð Yð Yð Yð Yð Yð Yr1   r   )rž   r]   r@   Ú r   Úaudio_utilsr   r   r   Ú!feature_extraction_sequence_utilsr   Úfeature_extraction_utilsr	   Úutilsr
   r   rP   Ú
get_loggerr†   r‡   r   Ú__all__r#   r1   r0   ú<module>r­      sù   ððð ð Ð Ð Ð à "Ð "Ð "Ð "Ð "Ð "Ø HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HØ IÐ IÐ IÐ IÐ IÐ IØ 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (ð ÐÑÔð Ø€L€L€Là	ˆÔ	˜HÑ	%Ô	%€ðyð yð yð yð yÐ6ñ yô yð yðx	 %Ð
%€€€r1   