§
    9²¸jÂ1  ã                   óþ   — d dl ZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZ  e
¦   «         rd dlZ ej        e¦  «        Zded	ed
edededej        fd„Z ed¬¦  «         G d„ de¦  «        ¦   «         ZdgZdS )é    Né   )Úhertz_to_mel)ÚSequenceFeatureExtractor)ÚBatchFeature)Ú
TensorTypeÚis_torch_availableÚlogging)ÚrequiresÚnum_mel_binsÚnum_spectrogram_binsÚsample_rateÚlower_edge_hertzÚupper_edge_hertzÚreturnc                 ó°  — t           j        }d}|dz  }t          j        d|||¬¦  «        |d…         }	t          |	d¬¦  «        dd…t           j        f         }
t          j        t          |d¬¦  «        t          |d¬¦  «        | dz   |¬¦  «        }|dd	…         t           j        dd…f         |dd
…         t           j        dd…f         |dd…         t           j        dd…f         }}}|
|z
  ||z
  z  }||
z
  ||z
  z  }t          j        dt          j        ||¦  «        ¦  «        }t          j        ||dgddgg¦  «                             |¦  «        S )z.NumPy-port of the JAX mel weight matrix logic.é   g       @ç        )ÚdtypeNÚkaldi)Ú	mel_scaleé   éþÿÿÿéÿÿÿÿr   )	ÚnpÚfloat64Úlinspacer   ÚnewaxisÚmaximumÚminimumÚpadÚastype)r   r   r   r   r   r   Úinternal_dtypeÚbands_to_zeroÚnyquist_hertzÚlinear_frequenciesÚspectrogram_bins_melÚedgesÚlower_edge_melÚ
center_melÚupper_edge_melÚlower_slopesÚupper_slopesÚmel_weights_matrixs                     ú€/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/lasr/feature_extraction_lasr.pyÚlinear_to_mel_weight_matrixr/       s|  € õ ”Z€Nð €MØ #Ñ%€MÝœ S¨-Ð9MÐUcÐdÑdÔdÐerÐesÐesÔtÐÝ'Ð(:ÀgÐNÑNÔNÈqÈqÈqÕRTÔR\È}Ô]ÐåŒKÝÐ%°Ð9Ñ9Ô9ÝÐ%°Ð9Ñ9Ô9Ø�qÑØð	ñ ô €Eð 	ˆcˆrˆcŒ
•2”:˜q˜q˜q�=Ô!Øˆa�ˆdŒ•B”J   �MÔ"ØˆaˆbˆbŒ	•"”*˜a˜a˜a�-Ô ð !/�J€Nð )¨>Ñ9¸jÈ>Ñ>YÑZ€LØ"Ð%9Ñ9¸nÈzÑ>YÑZ€LÝœ C­¬°LÀ,Ñ)OÔ)OÑPÔPÐÝŒ6Ð$¨°qÐ'9¸A¸q¸6Ð&BÑCÔC×JÒJÈ5ÑQÔQÐQó    )Útorch)Úbackendsc                   ó   ‡ — e Zd ZdZddgZ	 	 	 	 	 	 dˆ fd
„	Zd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dz  defd„Zˆ xZS ) ÚLasrFeatureExtractora7  
    Constructs a LASR 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 128):
                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.
            n_fft (`int`, *optional*, defaults to 512):
                Size of the Fourier transform.
            win_length (`int`, *optional*, defaults to 400):
                The window length for the STFT computation.
            padding_value (`float`, *optional*, defaults to 0.0):
                Padding value used to pad the audio. Should correspond to silences.
    Úinput_featuresÚattention_maské€   é€>  é    é   é�  r   c           
      óä   •—  t          ¦   «         j        d|||dœ|¤Ž || _        || _        || _        t          j        t          ||dz  dz   |ddt          j	        ¬¦  «        ¦  «        | _
        d S )N)Úfeature_sizeÚsampling_rateÚpadding_valuer   r   g     @_@g     L½@)r   r   r   r   r   r   © )ÚsuperÚ__init__Ú
hop_lengthÚn_fftÚ
win_lengthr1   Ú
from_numpyr/   r   r   Úmel_filters)	Úselfr=   r>   rC   rD   rE   r?   ÚkwargsÚ	__class__s	           €r.   rB   zLasrFeatureExtractor.__init__a   s‹   ø€ ð 	�‰ŒÔÐw lÀ-Ð_lÐwÐwÐpvÐwÐwÐwà$ˆŒØˆŒ
Ø$ˆŒÝ Ô+Ý'Ø)Ø%*¨a¡Z°!¡^Ø)Ø!&Ø!'Ý”jðñ ô ñ	
ô 	
ˆÔÐÐr0   Úcpuc                 óâ  — t          j        | j        d|t           j        ¬¦  «        }|                     t           j        ¦  «        }|                     d| j        | j        ¦  «        }t           j                             ||z  | j	        ¬¦  «        }t          j
        |¦  «        dz  }| j                             |¦  «        }t          j        ||z  d¬¦  «        }t          j        |¦  «        }|S )NF)ÚperiodicÚdevicer   r   )Únr   gñhãˆµøä>)Úmin)r1   Úhann_windowrE   r   ÚtoÚunfoldrC   ÚfftÚrfftrD   ÚabsrG   ÚclampÚlog)	rH   ÚwaveformrN   ÚwindowÚframesÚstftÚ
power_specrG   Úmel_specs	            r.   Ú_torch_extract_fbank_featuresz2LasrFeatureExtractor._torch_extract_fbank_features{   sÃ   € åÔ" 4¤?¸UÈ6ÕY^ÔYfÐgÑgÔgˆØ—;’;�uœ}Ñ-Ô-ˆð
 —’  T¤_°d´oÑFÔFˆÝŒy�~Š~˜f v™o°´ˆ~Ñ<Ô<ˆÝ”Y˜t‘_”_¨Ñ)ˆ
ð Ô&×)Ò)¨&Ñ1Ô1ˆÝ”;˜z¨KÑ7¸TÐBÑBÔBˆÝ”9˜XÑ&Ô&ˆàˆr0   FNÚlongestÚ
raw_speechÚ
truncationÚpad_to_multiple_ofÚreturn_tensorsÚreturn_attention_maskÚpaddingÚ
max_lengthr>   Údo_normalizerN   Úreturn_token_timestampsr   c                 ó  — |�<|| j         k    r0t          d| j        j        › d| j         › d| j         › d|› d�	¦  «        ‚n(t                               d| j        j        › d�¦  «         t          |t          j        ¦  «        rt          j
        |¦  «        }ndt          |t          t          f¦  «        rHt          |d	         t          t          j        f¦  «        rd
„ |D ¦   «         }nt          j
        |¦  «        }t          |t          j        ¦  «        ot          |j        ¦  «        dk    }|rUt          |j        ¦  «        dk    r=t                               d| j        j        › d�¦  «         |                     d¦  «        }t          |t          t          f¦  «        }|rZ|D ]W}t          |j        ¦  «        dk    r=t                               d| j        j        › d�¦  «         |                     d¦  «        }ŒX|s|rd„ |D ¦   «         }n*|dd…df                              t          j        ¦  «        g}t'          d|i¦  «        }|                      ||||||d¬¦  «        }|j                             d¦  «        }|                      ||
¦  «        }d|                     t          j        ¦  «        i}|rC|j        dd…| j        dz
  d| j        …f         }|                     t          j        ¦  «        |d<   t'          ||¬¦  «        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_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 Parakeet models, `attention_mask` should always be passed for batched inference, to avoid subtle
                bugs.

                </Tip>

            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.
            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.
            padding_value (`float`, *optional*, defaults to 0.0):
                The value that is used to fill the padding values / vectors.
            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")
            return_token_timestamps (`bool`, *optional*, defaults to `None`):
                Deprecated. Use `return_attention_mask` instead from which the number of frames can be inferred.

                Whether or not to return the number of frames of the input raw_speech.
                These num_frames can be used by the model to compute word level timestamps.
        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   c                 ó6   — g | ]}t          j        |¦  «        ‘ŒS r@   )r1   Útensor©Ú.0Úspeechs     r.   ú
<listcomp>z1LasrFeatureExtractor.__call__.<locals>.<listcomp>ä   s"   € ÐLÐLÐL°v�eœl¨6Ñ2Ô2ÐLÐLÐLr0   r   r   z2Only mono-channel audio is supported for input to z;. We will take the mean of the channels to convert to mono.r   c                 ó`   — g | ]+}|d d …d f                               t          j        ¦  «        ‘Œ,S )N)rR   r1   Úfloat32rn   s     r.   rq   z1LasrFeatureExtractor.__call__.<locals>.<listcomp>û   s4   € ÐUÐUÐUÀ˜&    D œ/×,Ò,­U¬]Ñ;Ô;ÐUÐUÐUr0   r5   Úpt)rf   rg   rb   rc   re   rd   r6   )ÚdataÚtensor_type)r>   Ú
ValueErrorrJ   Ú__name__ÚloggerÚwarningÚ
isinstancer   Úndarrayr1   rm   ÚlistÚtupleÚTensorÚlenÚshapeÚmeanrR   rs   r   r    r5   Úsqueezer_   r6   rE   rC   Úbool)rH   ra   rb   rc   rd   re   rf   rg   r>   rh   rN   ri   rI   Úis_batched_torchÚis_batched_sequencerp   Úbatched_speechÚpadded_inputsr5   ru   r6   s                        r.   Ú__call__zLasrFeatureExtractor.__call__Ž   s„  € ðH Ð$Ø Ô 2Ò2Ð2Ý ðWÈ$Ì.ÔJað Wð WØ)-Ô);ðWð Wà)-Ô);ðWð WàFSðWð Wð Wñô ð ð 3õ �NŠNð\ÐW[ÔWeÔWnð \ð \ð \ñô ð õ �j¥"¤*Ñ-Ô-ð 	6Ýœ jÑ1Ô1ˆJˆJÝ˜
¥T­5 MÑ2Ô2ð 	6Ý˜* Qœ-­$µ´
Ð);Ñ<Ô<ð 6ØLÐLÀÐLÑLÔL�
�
å"œ\¨*Ñ5Ô5�
å% jµ%´,Ñ?Ô?Ð]ÅCÈ
ÔHXÑDYÔDYÐ\]ÒD]ÐØð 	-¥ JÔ$4Ñ 5Ô 5¸Ò 9Ð 9Ý�NŠNðLÀTÄ^ÔE\ð Lð Lð Lñô ð ð $Ÿš¨Ñ,Ô,ˆJå(¨µd½E°]ÑCÔCÐØð 	-Ø$ð -ð -�Ý�v”|Ñ$Ô$ qÒ(Ð(Ý—N’NðTÈTÌ^ÔMdð Tð Tð Tñô ð ð $Ÿ[š[¨™_œ_�Føàð 	AÐ2ð 	AØUÐUÈ*ÐUÑUÔUˆJˆJà$ Q Q Q¨ WÔ-×0Ò0µ´Ñ?Ô?Ð@ˆJå%Ð'7¸Ð&DÑEÔEˆØŸšØØØ!Ø!Ø1Ø"7Øð !ñ 
ô 
ˆð 'Ô5×=Ò=¸bÑAÔAˆØ×;Ò;¸NÈFÑSÔSˆà˜n×/Ò/µ´Ñ>Ô>ð
ˆð !ð 	CØ*Ô9¸!¸!¸!¸T¼_ÈqÑ=PÐ=cÐTXÔTcÐ=cÐ:cÔdˆNØ%3×%6Ò%6µu´zÑ%BÔ%BˆDÐ!Ñ"å °>ÐBÑBÔBÐBr0   )r7   r8   r9   r:   r;   r   )rK   )
FNNNr`   NNNrK   N)rx   Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesrB   r_   r   r|   r}   Úfloatr„   ÚintÚstrr   r   r‰   Ú__classcell__)rJ   s   @r.   r4   r4   E   s¥  ø€ € € € € ðð ð0 *Ð+;Ð<Ðð ØØØØØð
ð 
ð 
ð 
ð 
ð 
ð4ð ð ð ð, !Ø)-Ø26Ø-1Ø'Ø!%Ø$(Ø$(Ø"Ø/3ðECð ECà”J  e¤Ñ,¨t°B´JÔ/?Ñ?À$ÀtÈEÄ{ÔBSÑSðECð ðECð   $™Jð	ECð
 ˜jÑ(¨4Ñ/ðECð  $ d™{ðECð �t‘ðECð ˜$‘JðECð ˜T‘zðECð ˜T‘kðECð �d‘
ðECð "&¨¡ðECð 
ðECð ECð ECð ECð ECð ECð ECð ECr0   r4   )Únumpyr   Úaudio_utilsr   Ú!feature_extraction_sequence_utilsr   Úfeature_extraction_utilsr   Úutilsr   r   r	   Úutils.import_utilsr
   r1   Ú
get_loggerrx   ry   r�   rŽ   r|   r/   r4   Ú__all__r@   r0   r.   ú<module>rš      sf  ðð Ð Ð Ð à 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø IÐ IÐ IÐ IÐ IÐ IØ 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ð <Ø *Ð *Ð *Ð *Ð *Ð *ð ÐÑÔð Ø€L€L€Lð 
ˆÔ	˜HÑ	%Ô	%€ð"RØð"Ràð"Rð ð"Rð ð	"Rð
 ð"Rð „Zð"Rð "Rð "Rð "RðJ 
€�:ÐÑÔðMCð MCð MCð MCð MCÐ3ñ MCô MCñ ÔðMCð` "Ð
"€€€r0   