§
    9²¸jL"  ã                   óD  — d dl Z ddlmZmZ ddlmZ ddlmZmZm	Z	 ddl
mZmZ ddlmZmZmZmZ ddlmZmZ  ej        e¦  «        Z e¦   «         rd dlZ e¦   «         rd dlZ G d	„ d
ed¬¦  «        Z ed¬¦  «        e G d„ de¦  «        ¦   «         ¦   «         ZdgZdS )é    Né   )Ú
AudioInputÚmake_list_of_audio)ÚBatchFeature)ÚProcessingKwargsÚProcessorMixinÚUnpack)ÚPreTokenizedInputÚ	TextInput)Úauto_docstringÚis_soundfile_availableÚis_torch_availableÚlogging)ÚrequiresÚrequires_backendsc                   ó   — e Zd ZdddiiZdS )ÚVibeVoiceProcessorKwargsÚaudio_kwargsÚsampling_rateiÀ]  N)Ú__name__Ú
__module__Ú__qualname__Ú	_defaults© ó    ú‚/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/vibevoice/processing_vibevoice.pyr   r   $   s"   € € € € € àØ˜Uð
ð€I€I€Ir   r   F)Útotal)Útorch)Úbackendsc                   óî   ‡ — e Zd ZeZ	 	 	 	 dˆ fd„	Zdefd„Zded	e	d
e
fd„Ze	 	 ddeez  ee         z  ee         z  dedz  dedz  dee         d
ef
ˆ fd„¦   «         Z	 ddede
dz  d
ee
         fd„Zˆ xZS )ÚVibeVoiceProcessorNú<|vision_start|>ú<|vision_end|>ú<|vision_pad|>c                 ó¦  •— || _         t          |dd¦  «        r|j        n|                     |¦  «        | _        || _        t          |dd¦  «        r|j        n|                     |¦  «        | _        || _        t          |dd¦  «        r|j        n|                     |¦  «        | _        t          ¦   «          	                    |||¬¦  «         dS )aÁ  
        audio_bos_token (`str`, *optional*, defaults to `"<|vision_start|>"`):
            The token used to indicate the beginning of audio generation.
        audio_eos_token (`str`, *optional*, defaults to `"<|vision_end|>"`):
            The token used to indicate the end of audio generation.
        audio_token (`str`, *optional*, defaults to `"<|vision_pad|>"`):
            The token used to indicate to continue generating audio.
        Úaudio_bos_token_idNÚaudio_eos_token_idÚaudio_token_id)Úchat_template)
Úaudio_bos_tokenÚgetattrr&   Úconvert_tokens_to_idsÚaudio_eos_tokenr'   Úaudio_tokenr(   ÚsuperÚ__init__)ÚselfÚfeature_extractorÚ	tokenizerr)   r*   r-   r.   Ú	__class__s          €r   r0   zVibeVoiceProcessor.__init__1   só   ø€ ð"  /ˆÔõ �yÐ"6¸Ñ=Ô=ðBˆIÔ(Ð(à×0Ò0°ÑAÔAð 	Ôð
  /ˆÔõ �yÐ"6¸Ñ=Ô=ðBˆIÔ(Ð(à×0Ò0°ÑAÔAð 	Ôð
 'ˆÔõ �yÐ"2°DÑ9Ô9ð>ˆIÔ$Ð$à×0Ò0°Ñ=Ô=ð 	Ôõ
 	‰Œ×ÒÐ*¨IÀ]ÐÑSÔSÐSÐSÐSr   Úaudioc                 ór  ‡ ‡—  ‰ j         |fi |¤ŽŠ|                     d¦  «        p‰ j         j        }t          j        ‰d                              d¬¦  «        |z  ¦  «                             ¦   «                              ¦   «         ‰ _        ˆˆ fd„t          t          |¦  «        ¦  «        D ¦   «         }‰|fS )NÚpad_to_multiple_ofÚpadding_maskéÿÿÿÿ)Údimc                 ó<   •— g | ]}‰                      ‰|¦  «        ‘ŒS r   )Úreplace_audio_token)Ú.0ÚidxÚprocessed_audior1   s     €€r   ú
<listcomp>z5VibeVoiceProcessor._process_audio.<locals>.<listcomp>\   s*   ø€ ÐjÐjÐjÐQT˜d×6Ò6°ÈÑLÔLÐjÐjÐjr   )r2   Úgetr7   r   ÚceilÚsumÚintÚtolistÚ_num_audio_tokensÚrangeÚlen)r1   r5   Úkwargsr7   Úaudio_replacementsr?   s   `    @r   Ú_process_audioz!VibeVoiceProcessor._process_audioV   sÄ   øø€ Ø0˜$Ô0°ÐAÐA¸&ÐAÐAˆØ#ŸZšZÐ(<Ñ=Ô=ÐjÀÔAWÔAjÐåŒJ� ~Ô6×:Ò:¸rÐ:ÑBÔBÐEWÑWÑXÔX×\Ò\Ñ^Ô^×eÒeÑgÔgð 	Ôð kÐjÐjÐjÐjÕX]Õ^aÐbgÑ^hÔ^hÑXiÔXiÐjÑjÔjÐØÐ 2Ð2Ð2r   Úaudio_inputsÚ	audio_idxÚreturnc                 ó,   — | j         | j        |         z  S ©N)r.   rF   )r1   rL   rM   s      r   r<   z&VibeVoiceProcessor.replace_audio_token_   s   € ØÔ $Ô"8¸Ô"CÑCÐCr   FÚtextÚoutput_labelsrI   c                 óž  •— d|v r(|d         dk    rt          | j        j        › d�¦  «        ‚ t          ¦   «         j        d||dœ|¤Ž}|r�|d                              ¦   «         }d|v rd||d         dk    <   d||| j        k    <   ||d	<   t          j        |d         t          j	        ¬
¦  «        }|�d||d         | j        k    <   ||d<   |S )a<  
        Main method to process text inputs with optional voice samples.

        This method processes text inputs (typically prepared by apply_chat_template) and optional voice samples for
        voice cloning. It expands audio diffusion tokens based on the actual audio length.

        Args:
            text (`str`, `List[str]`):
                The input text(s) to process, typically prepared by apply_chat_template with audio token placeholders.
            audio (`List[Union[str, np.ndarray]]`, *optional*):
                Audio samples for speaker voice cloning. Should match the number of audio token placeholders in text.
            output_labels (bool, *optional*, default=False):
                Whether to return labels for training.
            **kwargs:
                Additional keyword arguments passed to the tokenizer and feature extractor.

        Returns:
            [`BatchFeature`]: A [`BatchFeature`] with the following fields:
            - **input_ids** -- List of token ids to be fed to the model.
            - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
              `return_attention_mask=True`).
            - **input_values** -- List of audio values to be fed to the model. Returned when `audio` is not `None`.
            - **padding_mask** -- List of indices specifying which audio frames should be attended to by the model.
              Returned when `audio` is not `None`.
            - **labels** -- Labels for language model training. Padding and audio diffusion tokens are masked with
              -100 (audio diffusion token embeddings are replaced by audio features, so their token identity is not a
              meaningful target). Audio bos/eos and text eos tokens are kept as targets so the LM learns when to start
              and stop generating audio. Returned when `output_labels=True`.
            - **acoustic_loss_mask** -- Boolean mask for positions where diffusion loss is computed. True at audio
              diffusion token positions. Returned when `output_labels=True`.
        Úreturn_tensorsÚptz% only supports `return_tensors='pt'`.)rQ   r5   Ú	input_idsÚattention_maskiœÿÿÿr   Úlabels)ÚdtypeNTÚacoustic_loss_maskr   )
Ú
ValueErrorr4   r   r/   Ú__call__Úcloner(   r   Ú
zeros_likeÚbool)	r1   rQ   r5   rR   rI   ÚdatarX   rZ   r4   s	           €r   r\   zVibeVoiceProcessor.__call__b   s  ø€ ðN ˜vÐ%Ð%¨&Ð1AÔ*BÀdÒ*JÐ*JÝ ¤Ô 7Ð^Ð^Ð^Ñ_Ô_Ð_à�u‰wŒwÔÐA T°ÐAÐA¸&ÐAÐAˆØð 	<Ø˜+Ô&×,Ò,Ñ.Ô.ˆFà 4Ð'Ð'Ø6:��tÐ,Ô-°Ò2Ñ3ð 59ˆF�6˜TÔ0Ò0Ñ1Ø#ˆD�‰Nå!&Ô!1°$°{Ô2CÍ5Ì:Ð!VÑ!VÔ!VÐØÐ ØOSÐ" 4¨Ô#4¸Ô8KÒ#KÑLØ);ˆDÐ%Ñ&àˆr   Úoutput_pathc                 óÂ  — t          | dg¦  «         t          |¦  «        }t          |¦  «        D ]d\  }}|                     ¦   «                              ¦   «                              ¦   «                              ¦   «                              ¦   «         ||<   Œet          |¦  «        dk    r-|€d}t          j
        ||d         | j        j        ¦  «         |gS |€d}t          j        |d¬¦  «         g }t          |¦  «        D ]^\  }}t          j                             |d	|› d
�¦  «        }t          j
        ||| j        j        ¦  «         |                     |¦  «         Œ_|S )aI  
        Save audio data to WAV file(s).
        TODO eventually move to AudioProcessor base class.

        Args:
            audio: Audio output from the model to be saved
            output_path: Output file path or directory for multiple files

        Returns:
            List[str]: Paths to the saved audio files.
        Ú	soundfileé   Nzvibevoice_output.wavr   Úvibevoice_outputsT)Úexist_okÚaudio_z.wav)r   r   Ú	enumerateÚdetachÚcpuÚfloatÚnumpyÚsqueezerH   ÚsfÚwriter2   r   ÚosÚmakedirsÚpathÚjoinÚappend)	r1   r5   ra   r>   ÚitemÚsaved_pathsÚiÚaudio_arrayÚ	file_paths	            r   Ú
save_audiozVibeVoiceProcessor.save_audiož   s[  € õ" 	˜$  Ñ.Ô.Ð.å" 5Ñ)Ô)ˆÝ" 5Ñ)Ô)ð 	Gð 	G‰IˆC�ØŸš™œ×*Ò*Ñ,Ô,×2Ò2Ñ4Ô4×:Ò:Ñ<Ô<×DÒDÑFÔFˆE�#‰JˆJåˆu‰:Œ:˜Š?ˆ?ØÐ"Ø4�ÝŒH�[ %¨¤(¨DÔ,BÔ,PÑQÔQÐQØ�=Ð àÐ"Ø1�ÝŒK˜¨dÐ3Ñ3Ô3Ð3ØˆKÝ"+¨EÑ"2Ô"2ð .ð .‘��;ÝœGŸLšL¨Ð6F¸qÐ6FÐ6FÐ6FÑGÔG�	Ý”˜ K°Ô1GÔ1UÑVÔVÐVØ×"Ò" 9Ñ-Ô-Ð-Ð-ØÐr   )Nr"   r#   r$   )NFrP   )r   r   r   r   Úvalid_processor_kwargsr0   r   rK   ÚdictrD   Ústrr<   r   r   r
   Úlistr_   r	   r   r\   rz   Ú__classcell__)r4   s   @r   r!   r!   ,   sx  ø€ € € € € ð 6Ðð Ø*Ø(Ø$ð#Tð #Tð #Tð #Tð #Tð #TðJ3 Jð 3ð 3ð 3ð 3ðD°ð DÀð DÈð Dð Dð Dð Dð ð $(Ø%*ð	9ð 9àÐ+Ñ+¨d°9¬oÑ=ÀÐEVÔ@WÑWð9ð ˜DÑ ð9ð ˜d‘{ð	9ð
 Ð1Ô2ð9ð 
ð9ð 9ð 9ð 9ð 9ñ „^ð9ð| #'ð%ð %àð%ð ˜4‘Zð%ð 
ˆcŒð	%ð %ð %ð %ð %ð %ð %ð %r   r!   )rp   Úaudio_utilsr   r   Úfeature_extraction_utilsr   Úprocessing_utilsr   r   r	   Útokenization_utils_baser
   r   Úutilsr   r   r   r   Úutils.import_utilsr   r   Ú
get_loggerr   Úloggerr   rc   rn   r   r!   Ú__all__r   r   r   ú<module>r‰      s�  ðð 
€	€	€	à 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HÐ HØ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CØ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XØ =Ð =Ð =Ð =Ð =Ð =Ð =Ð =ð 
ˆÔ	˜HÑ	%Ô	%€ð ÐÑÔð Ø€L€L€LàÐÑÔð ØÐÐÐðð ð ð ð Ð/°uð ñ ô ð ð 
€�:ÐÑÔØðUð Uð Uð Uð U˜ñ Uô Uñ „ñ ÔðUðp  Ð
 €€€r   