Ë
    S^(hhG  ã                   óŠ   — d Z ddl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y)	zZ
Sequence feature extraction class for common feature extractors to preprocess sequences.
é    )Ú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                 óª   •— || _         || _        || _        |j                  dd«      | _        |j                  dd«      | _        t        ‰| �  di |¤Ž y )NÚpadding_sideÚrightÚreturn_attention_maskT© )r   r   r   Úpopr   r   ÚsuperÚ__init__)Úselfr   r   r   ÚkwargsÚ	__class__s        €úl/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/feature_extraction_sequence_utils.pyr   z!SequenceFeatureExtractor.__init__*   sR   ø€ Ø(ˆÔØ*ˆÔØ*ˆÔà"ŸJ™J ~°wÓ?ˆÔØ%+§Z¡ZÐ0GÈÓ%NˆÔ"ä‰ÑÑ"˜6Ó"ó    NÚprocessed_featuresÚpaddingÚ
max_lengthÚ
truncationÚpad_to_multiple_ofr   Úreturn_tensorsÚreturnc           
      ó”  ‡ ‡— t        |t        t        f«      rMt        |d   t        t        f«      r4|d   j                  «       D ��	ci c]  }||D �	cg c]  }	|	|   ‘Œ	 c}	“Œ }}}	‰ j                  d   |vr5t        d‰ j                  d   › dt        |j                  «       «      › �«      ‚|‰ j                  d      }
|�|n‰ j                  }t        |
«      dk(  r	|rg |d<   |S |
d   }t        |t        t        f«      r@d}t        |
|   «      dk(  r|dz  }t        |
|   «      dk(  rŒ|t        |
«      k  r|
|   d   }|€it        |«      rd}n[t        |«      rd}nMt        |t        t        t        t        t        j                  f«      rd}nt        d	|› d
t!        |«      › d�«      ‚|j#                  «       D ]I  \  }}t        |d   t        t        f«      rt%        |«      ||<   Œ.|D �cg c]  }t%        |«      ‘Œ c}||<   ŒK ‰ j'                  ||¬«      }|‰ j                  d      }
t        |
«      Št)        ˆfd„|j+                  «       D «       «      st        d«      ‚g }t-        ‰«      D ]N  }|j#                  «       D ��ci c]  \  }}|||   “Œ }}}‰ j/                  ||||¬«      }|j1                  |«       ŒP |t2        j4                  k(  r$t7        ˆ fd„|D «       «      }t2        j8                  }i }t-        ‰«      D ]ž  }‰ j;                  ||   ||||¬«      }|j#                  «       D ]p  \  }}||vrg ||<   |j<                  t        j<                  t        j>                  «      u r|jA                  t        jB                  «      }||   j1                  |«       Œr Œ  t	        ||¬«      S c c}	w c c}	}w c c}w c c}}w )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   z�You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature` to this method that includes z, but you provided Úattention_maskr   ÚtfÚptÚnpztype of z
 unknown: zA. Should be one of a python, numpy, pytorch or tensorflow object.)r!   r"   c              3   ó:   •K  — | ]  }t        |«      ‰k(  –— Œ y ­w)N)Úlen)Ú.0ÚvÚ
batch_sizes     €r   ú	<genexpr>z/SequenceFeatureExtractor.pad.<locals>.<genexpr>º   s   øè ø€ ÒM¨A”3�q“6˜ZÕ'ÑMùs   ƒzLSome items in the output dictionary have a different batch size than others.)r"   r$   r#   c              3   óT   •K  — | ]  }t        |‰j                  d       «      –— Œ! y­w)r   N)r-   Úmodel_input_names)r.   Úinput_slicer   s     €r   r1   z/SequenceFeatureExtractor.pad.<locals>.<genexpr>Ë   s&   øè ø€ ÒmÈ[œS ¨T×-CÑ-CÀAÑ-FÑ!G×HÑmùs   ƒ%()r"   Úpadding_strategyr$   r   )Útensor_type)"Ú
isinstanceÚlistÚtupleÚdictr   Úkeysr3   Ú
ValueErrorr   r-   r
   r   ÚintÚfloatr+   Ú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%   ÚkeyÚexampleÚrequired_inputÚfirst_elementÚindexÚvaluer/   r5   Útruncated_inputsÚiÚkÚinputsÚinputs_sliceÚbatch_outputsÚoutputsr0   s   `                      @r   ÚpadzSequenceFeatureExtractor.pad4   s  ù€ ôR Ð(¬4´¨-Ô8¼ZÐHZÐ[\ÑH]Ô`dÔfrÐ_sÔ=tàQcÐdeÑQf×QkÑQkÓQm÷"ØJM�Ð2DÖE w�g˜c“lÒEÑEð"Ðñ "ð
 ×!Ñ! !Ñ$Ð,>Ñ>Üð1Ø15×1GÑ1GÈÑ1JÐ0Kð LÜÐ+×0Ñ0Ó2Ó3Ð4ð6óð ð ,¨D×,BÑ,BÀ1Ñ,EÑFˆà%:Ð%FÑ!ÈD×LfÑLfð 	ô ˆ~Ó !Ò#Ù$Ø79Ð"Ð#3Ñ4Ø%Ð%ð ' qÑ)ˆÜ�m¤d¬E ]Ô3àˆEÜ�n UÑ+Ó,°Ò1Ø˜‘
�ô �n UÑ+Ó,°Ó1à”s˜>Ó*Ò*Ø .¨uÑ 5°aÑ 8�àÐ!Ü˜MÔ*Ø!%‘Ü  Ô/Ø!%‘Ü˜M¬C´¼¼eÄRÇZÁZÐ+PÔQØ!%‘ä Ø˜}˜o¨Z¼¸]Ó8KÐ7Lð MVð Vóð ð
 -×2Ñ2Ó4ò 	G‰JˆC�Ü˜% ™(¤S¬% LÔ1Ü*2°5«/Ð" 3Ò'à@EÖ*F¸1¬8°A­;Ò*FÐ" 3Ò'ð		Gð  ×7Ñ7ÀÐT^Ð7Ó_Ðà+¨D×,BÑ,BÀ1Ñ,EÑFˆä˜Ó(ˆ
ÜÓMÐ1C×1JÑ1JÓ1LÔMÔMÜÐkÓlÐlàÐÜ�zÓ"ò 		2ˆAØ*<×*BÑ*BÓ*D×E¡$ ! Q�a˜˜1™‘gÐEˆFÑEàŸ>™>ØØ%Ø#5Ø%ð	 *ó ˆLð ×#Ñ# LÕ1ð		2ð œ×6Ñ6Ò6äÓmÐ\lÔmÓmˆJÜ.×9Ñ9ÐàˆÜ�zÓ"ò 	1ˆAà—i‘iØ  Ñ#Ø%Ø!1Ø#5Ø&;ð  ó ˆGð &Ÿm™m›oò 1‘
��UØ˜mÑ+Ø)+�M #Ñ&Ø—;‘;¤"§(¡(¬2¯:©:Ó"6Ñ6Ø!ŸL™L¬¯©Ó4�EØ˜cÑ"×)Ñ)¨%Õ0ñ1ð	1ô" ˜M°~ÔFÐFùòC Fùó"ùòh +Gùó Fs$   Á
N9ÁN4ÁN9Ç-N?ÊOÎ4N9r5   c                 ó   — || j                   d      }|t        j                  k(  rt        |«      }|�|�||z  dk7  r||z  dz   |z  }|t        j                  k7  xr t        |«      |k  }|r5d|vr1t        j                  t        |«      t
        j                  ¬«      |d<   |�r)|t        |«      z
  }| j                  dk(  rn|rt        j                  |d   d|f«      |d<   | j                  dkD  rd|fdfnd|f}	t        j                  ||	d| j                  ¬«      || j                   d   <   |S | j                  d	k(  rn|rt        j                  |d   |df«      |d<   | j                  dkD  r|dfdfn|df}	t        j                  ||	d| j                  ¬«      || j                   d   <   |S 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   r   r(   )rL   r   )r   r   Úconstant)Úconstant_valuesÚleftzInvalid padding strategy:)r3   r   rH   r-   Ú
DO_NOT_PADr+   ÚonesÚint32r   r]   r   r   r<   Ústr)
r   r    r"   r5   r$   r   rR   Úneeds_to_be_paddedÚ
differenceÚpadding_shapes
             r   rK   zSequenceFeatureExtractor._padâ   s  € ðD ,¨D×,BÑ,BÀ1Ñ,EÑFˆàœ×6Ñ6Ò6Ü˜^Ó,ˆJàÐ!Ð&8Ð&DÈ*ÐWiÑJiÐmnÒJnØ%Ð);Ñ;¸qÑ@ÐDVÑVˆJà-´×1KÑ1KÑKÒpÔPSÐTbÓPcÐfpÑPpÐá Ð%5Ð=OÑ%OÜ35·7±7¼3¸~Ó;NÔVX×V^ÑV^Ô3_ÐÐ/Ñ0âØ#¤c¨.Ó&9Ñ9ˆJØ× Ñ  GÒ+Ù(Ü;=¿6¹6Ø*Ð+;Ñ<¸qÀ*¸oó<Ð&Ð'7Ñ8ð >B×=NÑ=NÐQRÒ=R ! Z °&Ñ 9ÐYZÐ\fÐXg�Ü@BÇÁØ" M°:Èt×OaÑOaôAÐ" 4×#9Ñ#9¸!Ñ#<Ñ=ð "Ð!ð ×"Ñ" fÒ,Ù(Ü;=¿6¹6Ø*Ð+;Ñ<¸zÈ1¸oó<Ð&Ð'7Ñ8ð >B×=NÑ=NÐQRÒ=R *¨a °&Ñ 9ÐYcÐefÐXg�Ü@BÇÁØ" M°:Èt×OaÑOaôAÐ" 4×#9Ñ#9¸!Ñ#<Ñ=ð "Ð!ô !Ð!<¼sÀ4×CTÑCTÓ?UÑ!UÓVÐVà!Ð!r   c                 ó  — |s|S |r|€t        d«      ‚|| j                  d      }|�|�||z  dk7  r||z  dz   |z  }t        |«      |kD  }|r4|| 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   r(   )r<   r3   r-   )r   r    r"   r$   r#   rR   Úneeds_to_be_truncateds          r   rF   z"SequenceFeatureExtractor._truncate*  sÕ   € ñ. Ø%Ð%Ù˜JÐ.ÜÐjÓkÐkà+¨D×,BÑ,BÀ1Ñ,EÑFˆð Ð!Ð&8Ð&DÈ*ÐWiÑJiÐmnÒJnØ%Ð);Ñ;¸qÑ@ÐDVÑVˆJä # NÓ 3°jÑ @Ðá Ø<NÈt×OeÑOeÐfgÑOhÑ<iÐjuÐkuÐ<vÐ˜t×5Ñ5°aÑ8Ñ9ØÐ#5Ñ5Ø7IÐJZÑ7[Ð\gÐ]gÐ7hÐ"Ð#3Ñ4à!Ð!r   c                 ón  — |durD|du rt         j                  }n?t        |t         «      st        |«      }n#t        |t         «      r|}nt         j                  }|€0t         j                  k(  rt        dt         j                  › d�«      ‚t         j                  k7  r| j                  €t        d«      ‚|S )z3
        Find the correct padding strategy
        FTzWhen setting ``padding=z(``, make sure that max_length is definedz­Asking 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   rH   r7   rb   rJ   r<   r   )r   r!   r"   r5   s       r   rB   z0SequenceFeatureExtractor._get_padding_strategiesU  s½   € ð ˜%ÑØ˜$‰Ü#2×#:Ñ#:Ñ Ü ¬Ô9Ü#2°7Ó#;Ñ Ü˜G¤_Ô5Ø#*Ñ ä.×9Ñ9Ðð ÐØ¤?×#=Ñ#=Ò=Ü Ø-¬o×.HÑ.HÐ-IÐIqÐróð ð
 œ×9Ñ9Ò9¸t×?QÑ?QÐ?YÜð]óð ð
  Ðr   )TNFNNN)NNN)FN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r=   r>   r   r   r   r8   r:   re   Úboolr   r   r	   r]   rb   r+   r?   rK   rF   rB   Ú__classcell__)r   s   @r   r   r      så  ø„ ñ
ð# Sð #¸ð #ÈUõ #ð& 6:Ø$(Ø Ø,0Ø04Ø;?ñlGà!ØØ�ÑØ��lÐ"Ñ#Ø��d˜<Ñ(Ð(Ñ)Ø��c˜<Ð'Ñ(Ñ)ð	+ñ
ðlGð �t˜S /Ð1Ñ2ðlGð ˜S‘MðlGð ðlGð % S™MðlGð  (¨™~ðlGð !  s¨J Ñ!7Ñ8ðlGð 
ólGðb %)Ø,;×,FÑ,FØ,0Ø04ñF"à! $ s¨B¯J©J Ñ"7¸Ð"EÑFðF"ð ˜S‘MðF"ð *ð	F"ð
 % S™MðF"ð  (¨™~ðF"ð 
óF"ðV %)Ø,0Ø%)ñ)"à! $ s¨B¯J©J Ñ"7¸Ð"EÑFð)"ð ˜S‘Mð)"ð % S™Mð	)"ð
 ˜T‘Nó)"÷V r   r   )ro   Útypingr   r   Únumpyr+   Úfeature_extraction_utilsr   r   Úutilsr   r	   r
   r   r   r   Ú
get_loggerrl   Úloggerr   r   r   r   ú<module>rx      s>   ðñ÷ #ã ç Jß `× `ð 
ˆ×	Ñ	˜HÓ	%€ôV Ð5õ V r   