Ë
    ÛÓ^jêK  ã                   ó†   — d Z ddlZddlmZmZ ddlmZmZ ddl	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.
é    Né   )Úis_valid_audioÚ
load_audio)ÚBatchFeatureÚFeatureExtractionMixin)ÚPaddingStrategyÚ
TensorTypeÚ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   z  e	e
ef   z  e	e
ee   f   z  ee	e
ef      z  dee
z  ez  d	edz  d
ededz  dedz  de
ez  dz  defd„Zdej                  ddfde	e
ej"                  f   ez  d	edz  dededz  dedz  de	fd„Z	 	 	 dde	e
ej"                  f   ez  d	edz  dedz  d
edz  fd„Zdd„Zdde
ee
   z  eee
      z  dedz  f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        €úy/var/www/ramen.bs-engineer-server.com/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   }|€[t        |«      rd}nMt        |t        t        t        t        t        j                  f«      rd}nt        d|› d	t        |«      › d
�«      ‚|j!                  «       D ]d  \  }}t        |d   t        t        f«      rt#        |«      ||<   Œ.t        |t        j                  «      rŒI|D �cg c]  }t#        |«      ‘Œ c}||<   Œf ‰ j%                  ||¬«      }|‰ j                  d      }
t        |
«      Št'        ˆfd„|j)                  «       D «       «      st        d«      ‚g }t+        ‰«      D ]N  }|j!                  «       D ��ci c]  \  }}|||   “Œ }}}‰ j-                  ||||¬«      }|j/                  |«       ŒP |t0        j2                  k(  r$t5        ˆ fd„|D «       «      }t0        j6                  }i }t+        ‰«      D ]ž  }‰ j9                  ||   ||||¬«      }|j!                  «       D ]p  \  }}||vrg ||<   |j:                  t        j:                  t        j<                  «      u r|j?                  t        j@                  «      }||   j/                  |«       Œr Œ  t	        ||¬«      S c c}	w c c}	}w c c}w c c}}w )a2  
        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 or PyTorch 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 or PyTorch 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:

                - `'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   ÚptÚnpztype of z
 unknown: z6. Should be one of a python, numpy, or pytorch 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   r0   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   Úkeysr2   Ú
ValueErrorr   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.   r4   Útruncated_inputsÚiÚkÚinputsÚinputs_sliceÚbatch_outputsÚoutputsr/   s   `                      @r   ÚpadzSequenceFeatureExtractor.pad3   s  ù€ ôL Ð(¬4´¨-Ô8¼ZÐHZÐ[\ÑH]Ô`dÔfrÐ_sÔ=tð RdÐ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¬C´¼¼eÄRÇZÁZÐ+PÔQØ!%‘ä Ø˜}˜o¨Z¼¸]Ó8KÐ7Lð MKð Kóð ð
 -×2Ñ2Ó4ò 	G‰JˆC�Ü˜% ™(¤S¬% LÔ1Ü*2°5«/Ð" 3Ò'Ü ¤r§z¡zÕ2ð AFÖ*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ùòE Fùó"ùòj +Gùó Fs$   Á
OÁOÁOÇ:OÊOÏOr4   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'   )rK   r   )r   r   Úconstant)Úconstant_valuesÚleftzInvalid padding strategy:)r2   r   rG   r,   Ú
DO_NOT_PADr)   ÚonesÚint32r   r\   r   r   r;   Ústr)
r   r   r!   r4   r#   r   rQ   Úneeds_to_be_paddedÚ
differenceÚpadding_shapes
             r   rJ   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;   r2   r,   )r   r   r!   r#   r"   rQ   Úneeds_to_be_truncateds          r   rE   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   rG   r6   ra   rI   r;   r   )r   r    r!   r4   s       r   rA   z0SequenceFeatureExtractor._get_padding_strategiesS  s½   € ð ˜%ÑØ˜$‰Ü#2×#:Ñ#:Ñ Ü ¬Ô9Ü#2°7Ó#;Ñ Ü˜G¤_Ô5Ø#*Ñ ä.×9Ñ9Ðð ÐØ¤?×#=Ñ#=Ò=Ü Ø-¬o×.HÑ.HÐ-IÐIqÐróð ð
 œ×9Ñ9Ò9¸t×?QÑ?QÐ?YÜð]óð ð
  Ðr   Úaudio_url_or_urlsc                 ó6  — |r|n| j                   }t        |t        «      r4t        |d   t        «      s!|D �cg c]  }| j	                  ||¬«      ‘Œ c}S t        |t
        «      rt        ||¬«      S t        |«      r|S t        dt        |«      › �«      ‚c c}w )zê
        Convert a single or a list of urls into the corresponding `np.ndarray` objects.

        If a single url is passed, the return value will be a single object. If a list is passed a list of objects is
        returned.
        r   )r   z=only a single or a list of entries is supported but got type=)
r   r6   r7   r=   Úfetch_audiord   r   r   Ú	TypeErrorr?   )r   rk   r   Úxs       r   rm   z$SequenceFeatureExtractor.fetch_audios  s™   € ñ *7™¸D×<NÑ<NˆÜÐ'¬Ô.´zÐBSÐTUÑBVÔX]Ô7^ØN_Ö`È�D×$Ñ$ Q°mÐ$ÕDÒ`Ð`ÜÐ)¬3Ô/ÜÐ/¸}ÔMÐMÜÐ-Ô.Ø$Ð$äÐ[Ô\`ÐarÓ\sÐ[tÐuÓvÐvùò as   ¸B)TNFNNN)NNN)FNr+   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r<   r=   r   r   r7   r9   rd   Úboolr   r	   r\   ra   r)   r>   rJ   rE   rA   rm   Ú__classcell__)r   s   @r   r   r      s  ø„ ñ
ð# Sð #¸ð #ÈUõ #ð" 15Ø!%Ø Ø)-Ø-1Ø26ñkGà(Ø
ˆ|Ñ
ñà
ˆs�LÐ Ñ
!ñ"ð ˆs�D˜Ñ&Ð&Ñ
'ñ(ð ˆt�C˜Ð%Ñ&Ñ
'ñ	(ðkGð ˜‘˜oÑ-ðkGð ˜$‘JðkGð ðkGð   $™JðkGð  $ d™{ðkGð ˜jÑ(¨4Ñ/ðkGð 
ókGð` "&Ø,;×,FÑ,FØ)-Ø-1ñF"à   b§j¡j Ñ1°LÑ@ðF"ð ˜$‘JðF"ð *ð	F"ð
   $™JðF"ð  $ d™{ðF"ð 
óF"ðV "&Ø)-Ø"&ñ)"à   b§j¡j Ñ1°LÑ@ð)"ð ˜$‘Jð)"ð   $™Jð	)"ð
 ˜4‘Kó)"óV ñ@w¨S°4¸±9©_¸tÀDÈÁI¹Ñ-Nð wÐ_bÐeiÑ_i÷ wr   r   )rs   Únumpyr)   Úaudio_utilsr   r   Úfeature_extraction_utilsr   r   Úutilsr   r	   r
   r   r   Ú
get_loggerrp   Úloggerr   r   r   r   ú<module>r|      s@   ðñó ç 3ß Jß RÕ Rð 
ˆ×	Ñ	˜HÓ	%€ôgwÐ5õ gwr   