§
    ‚Štj±<  ã                   óÀ   — d Z ddlZddlZ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 ddlmZ d	d
lmZ  ej        e¦  «        Ze G d„ de¦  «        ¦   «         ZdgZdS )z
Processor class for Bark
é    Né   )ÚBatchFeature)ÚProcessorMixin)ÚBatchEncoding)Úauto_docstringÚlogging)Úcached_fileé   )ÚAutoTokenizerc                   ó   ‡ — e Zd ZddddœZdˆ fd„	Ze	 dd„¦   «         Z	 	 	 dd
efˆ fd„Ze	de
de
de
fd„¦   «         Zdde
dz  fd„Zddedz  fd„Zedefd„¦   «         Zddefd„Ze	 	 	 	 	 	 	 ddefd„¦   «         Zˆ xZS ) ÚBarkProcessoré   r
   ©Úsemantic_promptÚcoarse_promptÚfine_promptNc                 óX   •— t          ¦   «                              |¦  «         || _        dS )a*  
        speaker_embeddings (`dict[dict[str]]`, *optional*):
            Optional nested speaker embeddings dictionary. The first level contains voice preset names (e.g
            `"en_speaker_4"`). The second level contains `"semantic_prompt"`, `"coarse_prompt"` and `"fine_prompt"`
            embeddings. The values correspond to the path of the corresponding `np.ndarray`. See
            [here](https://suno-ai.notion.site/8b8e8749ed514b0cbf3f699013548683?v=bc67cff786b04b50b3ceb756fd05f68c) for
            a list of `voice_preset_names`.
        N)ÚsuperÚ__init__Úspeaker_embeddings)ÚselfÚ	tokenizerr   Ú	__class__s      €úf/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/bark/processing_bark.pyr   zBarkProcessor.__init__*   s+   ø€ õ 	‰Œ×Ò˜Ñ#Ô#Ð#à"4ˆÔÐÐó    úspeaker_embeddings_path.jsonc                 óª  — |                      d¦  «        }|��t          |||                     dd¦  «        |                     dd¦  «        |                     dd¦  «        |                     dd¦  «        |                     dd¦  «        ||                     d	d¦  «        ddd¬
¦  «        }|€?t                               dt
          j                             ||¦  «        › d�¦  «         d}n>t          |¦  «        5 }t          j
        |¦  «        }ddd¦  «         n# 1 swxY w Y   nd}|�	d|v r||d<   t          j        |fi |¤Ž} | ||¬¦  «        S )aÛ  
        Instantiate a Bark processor associated with a pretrained model.

        Args:
            pretrained_model_name_or_path (`str` or `os.PathLike`):
                This can be either:

                - a string, the *model id* of a pretrained [`BarkProcessor`] hosted inside a model repo on
                  huggingface.co.
                - a path to a *directory* containing a processor saved using the [`~BarkProcessor.save_pretrained`]
                  method, e.g., `./my_model_directory/`.
            speaker_embeddings_dict_path (`str`, *optional*, defaults to `"speaker_embeddings_path.json"`):
                The name of the `.json` file containing the speaker_embeddings dictionary located in
                `pretrained_model_name_or_path`. If `None`, no speaker_embeddings is loaded.
            **kwargs
                Additional keyword arguments passed along to both
                [`~tokenization_utils_base.PreTrainedTokenizer.from_pretrained`].
        ÚtokenNÚ	subfolderÚ	cache_dirÚforce_downloadFÚproxiesÚlocal_files_onlyÚrevision©
r   r    r!   r"   r#   r   r$   Ú _raise_exceptions_for_gated_repoÚ%_raise_exceptions_for_missing_entriesÚ'_raise_exceptions_for_connection_errorsú`zã` does not exists
                    , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json
                    dictionary if wanted, otherwise set `speaker_embeddings_dict_path=None`.Úrepo_or_path)r   r   )Úgetr	   ÚpopÚloggerÚwarningÚosÚpathÚjoinÚopenÚjsonÚloadr   Úfrom_pretrained)	ÚclsÚ!pretrained_processor_name_or_pathÚspeaker_embeddings_dict_pathÚkwargsr   Úspeaker_embeddings_pathr   Úspeaker_embeddings_jsonr   s	            r   r5   zBarkProcessor.from_pretrained7   sØ  € ð, —
’
˜7Ñ#Ô#ˆØ'Ñ3Ý&1Ø1Ø,Ø Ÿ*š* [°$Ñ7Ô7Ø Ÿ*š* [°$Ñ7Ô7Ø%ŸzšzÐ*:¸EÑBÔBØŸ
š
 9¨dÑ3Ô3Ø!'§¢Ð,>ÀÑ!FÔ!FØØŸš J°Ñ5Ô5Ø16Ø6;Ø8=ð'ñ 'ô 'Ð#ð 'Ð.Ý—’ð`�"œ'Ÿ,š,Ð'HÐJfÑgÔgð `ð `ð `ñô ð ð
 &*Ð"Ð"åÐ1Ñ2Ô2ð LÐ6MÝ)-¬Ð3JÑ)KÔ)KÐ&ðLð Lð Lñ Lô Lð Lð Lð Lð Lð Lð Løøøð Lð Lð Lð Løð "&ÐàÐ)ØÐ!3Ð3Ð3Ø5VÐ" >Ñ2Ý!Ô1Ð2SÐ^Ð^ÐW]Ð^Ð^ˆ	àˆs˜YÐ;MÐNÑNÔNÐNs   Ã<DÄD!Ä$D!r   FÚpush_to_hubc           	      ó,  •— | j         ��ot          j        t          j                             ||d¦  «        d¬¦  «         i }||d<   t          j                             ||¦  «        }| j        D ]°}|                      |¦  «        }	i }
| j         |         D ]„}t          j                             ||› d|› �¦  «        }|                      |||¦  «         t          j	        ||	|         d¬¦  «         t          j                             ||› d|› d	�¦  «        |
|<   Œ…|
||<   Œ±t          t          j                             ||¦  «        d
¦  «        5 }t          j        ||¦  «         ddd¦  «         n# 1 swxY w Y    t          ¦   «         j        ||fi |¤Ž dS )a|  
        Saves the attributes of this processor (tokenizer...) in the specified directory so that it can be reloaded
        using the [`~BarkProcessor.from_pretrained`] method.

        Args:
            save_directory (`str` or `os.PathLike`):
                Directory where the tokenizer files and the speaker embeddings will be saved (directory will be created
                if it does not exist).
            speaker_embeddings_dict_path (`str`, *optional*, defaults to `"speaker_embeddings_path.json"`):
                The name of the `.json` file that will contains the speaker_embeddings nested path dictionary, if it
                exists, and that will be located in `pretrained_model_name_or_path/speaker_embeddings_directory`.
            speaker_embeddings_directory (`str`, *optional*, defaults to `"speaker_embeddings/"`):
                The name of the folder in which the speaker_embeddings arrays will be saved.
            push_to_hub (`bool`, *optional*, defaults to `False`):
                Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the
                repository you want to push to with `repo_id` (will default to the name of `save_directory` in your
                namespace).
            kwargs:
                Additional key word arguments passed along to the [`~utils.PushToHubMixin.push_to_hub`] method.
        NÚv2T)Úexist_okr*   Ú_F)Úallow_picklez.npyÚw)r   r/   Úmakedirsr0   r1   Úavailable_voice_presetsÚ_load_voice_presetÚ_reject_path_traversalÚnpÚsaver2   r3   Údumpr   Úsave_pretrained)r   Úsave_directoryr8   Úspeaker_embeddings_directoryr<   r9   Úembeddings_dictÚembeddings_subdirÚ
prompt_keyÚvoice_presetÚtmp_dictÚkeyÚtarget_filepathÚfpr   s                 €r   rJ   zBarkProcessor.save_pretrainedq   sè  ø€ ð8 Ô"Ñ.ÝŒK�œŸš ^Ð5QÐSWÑXÔXÐcgÐhÑhÔhÐhà ˆOà.<ˆO˜NÑ+å "¤§¢¨^Ð=YÑ ZÔ ZÐØ"Ô:ð 
7ð 
7�
Ø#×6Ò6°zÑBÔB�à�ØÔ2°:Ô>ð jð j�CÝ&(¤g§l¢lÐ3DÈÐF[ÐF[ÐVYÐF[ÐF[Ñ&\Ô&\�OØ×/Ò/Ð0AÀ?ÐT^Ñ_Ô_Ð_Ý”G˜O¨\¸#Ô->ÈUÐSÑSÔSÐSÝ$&¤G§L¢LÐ1MÐR\ÐOhÐOhÐ_bÐOhÐOhÐOhÑ$iÔ$i�H˜S‘M�Mà.6� 
Ñ+Ð+å•b”g—l’l >Ð3OÑPÔPÐRUÑVÔVð /ÐZ\Ý”	˜/¨2Ñ.Ô.Ð.ð/ð /ð /ñ /ô /ð /ð /ð /ð /ð /ð /øøøð /ð /ð /ð /ð 	 �‰ŒÔ °ÐFÐF¸vÐFÐFÐFÐFÐFs   ÅE.Å.E2Å5E2Úbase_dirÚtarget_pathÚoffending_valuec                 ó  — t           j                             | ¦  «        }t           j                             |¦  «        }	 t           j                             ||g¦  «        |k    }n# t          $ r d}Y nw xY w|st	          d|›�¦  «        ‚d S )NFzInvalid voice preset path: )r/   r0   ÚabspathÚ
commonpathÚ
ValueError)rU   rV   rW   ÚbaseÚtargetÚ	containeds         r   rF   z$BarkProcessor._reject_path_traversal¦   s¡   € õ Œw�Š˜xÑ(Ô(ˆÝ”—’ Ñ-Ô-ˆð	Ýœ×*Ò*¨D°&¨>Ñ:Ô:¸dÒBˆIˆIøÝð 	ð 	ð 	àˆIˆIˆIð	øøøð ð 	PÝÐN¸?ÐNÐNÑOÔOÐOð	Pð 	Ps   Á %A& Á&A5Á4A5rP   c                 ó„  — | j         |         }i }|                     d¦  «        }| j                              dd¦  «        }dD �]|}||vrt          d|› d|› d�¦  «        ‚|                      |t          j                             |||         ¦  «        ||         ¦  «         t          | j                              dd¦  «        ||         |                     dd ¦  «        |                     d	d ¦  «        |                     d
d¦  «        |                     dd ¦  «        |                     dd¦  «        ||                     dd ¦  «        ddd¬¦  «        }|€St          dt          j                             | j                              dd¦  «        ||         ¦  «        › d|› d�¦  «        ‚t          j
        |¦  «        ||<   �Œ~|S )Nr   r*   ú/r   ú#Voice preset unrecognized, missing z% as a key in self.speaker_embeddings[z].r   r    r!   Fr"   r#   r$   r%   r)   z{` does not exists
                    , no preloaded voice preset will be used - Make sure to provide correct paths to the z 
                    embeddings.)r   r+   r[   rF   r/   r0   r1   r	   r,   rG   r4   )	r   rP   r9   Úvoice_preset_pathsÚvoice_preset_dictr   r*   rR   r0   s	            r   rE   z BarkProcessor._load_voice_preset¹   sî  € Ø!Ô4°\ÔBÐàÐØ—
’
˜7Ñ#Ô#ˆØÔ.×2Ò2°>À3ÑGÔGˆØFð 	3ñ 	3ˆCØÐ,Ð,Ð,Ý Øt¸#ÐtÐtÐdpÐtÐtÐtñô ð ð ×'Ò'Ø�bœgŸlšl¨<Ð9KÈCÔ9PÑQÔQÐSeÐfiÔSjñô ð õ ØÔ'×+Ò+¨N¸CÑ@Ô@Ø" 3Ô'Ø Ÿ*š* [°$Ñ7Ô7Ø Ÿ*š* [°$Ñ7Ô7Ø%ŸzšzÐ*:¸EÑBÔBØŸ
š
 9¨dÑ3Ô3Ø!'§¢Ð,>ÀÑ!FÔ!FØØŸš J°Ñ5Ô5Ø16Ø6;Ø8=ðñ ô ˆDð ˆ|Ý ð#�"œ'Ÿ,š, tÔ'>×'BÒ'BÀ>ÐSVÑ'WÔ'WÐYkÐloÔYpÑqÔqð #ð #Øjvð#ð #ð #ñô ð õ &(¤W¨T¡]¤]Ð˜cÑ"Ñ"à Ð r   c           	      ó„  — dD ]¼}||vrt          d|› d�¦  «        ‚t          ||         t          j        ¦  «        s-t	          |› dt          | j        |         ¦  «        › d�¦  «        ‚t          ||         j        ¦  «        | j        |         k    r-t          |› dt          | j        |         ¦  «        › d�¦  «        ‚Œ½d S )Nr   ra   z
 as a key.z voice preset must be a z
D ndarray.)	r[   Ú
isinstancerG   ÚndarrayÚ	TypeErrorÚstrÚpreset_shapeÚlenÚshape)r   rP   rR   s      r   Ú_validate_voice_preset_dictz)BarkProcessor._validate_voice_preset_dictá   sç   € ØFð 	jð 	jˆCØ˜,Ð&Ð&Ý Ð!VÀsÐ!VÐ!VÐ!VÑWÔWÐWå˜l¨3Ô/µ´Ñ<Ô<ð iÝ 3Ð gÐ gÅÀDÔDUÐVYÔDZÑ@[Ô@[Ð gÐ gÐ gÑhÔhÐhå�< Ô$Ô*Ñ+Ô+¨tÔ/@ÀÔ/EÒEÐEÝ  CÐ!hÐ!hÅÀTÔEVÐWZÔE[ÑA\ÔA\Ð!hÐ!hÐ!hÑiÔiÐið Fð	jð 	jr   Úreturnc                 ó–   — | j         €g S t          | j                              ¦   «         ¦  «        }d|v r|                     d¦  «         |S )z…
        Returns a list of available voice presets.

        Returns:
            `list[str]`: A list of voice preset names.
        Nr*   )r   ÚlistÚkeysÚremove)r   Úvoice_presetss     r   rD   z%BarkProcessor.available_voice_presetsì   sS   € ð Ô"Ð*ØˆIå˜TÔ4×9Ò9Ñ;Ô;Ñ<Ô<ˆØ˜]Ð*Ð*Ø× Ò  Ñ0Ô0Ð0ØÐr   TÚremove_unavailablec                 óT  — g }| j         �š| j        D ]S}	 |                      |¦  «        }n%# t          $ r |                     |¦  «         Y Œ:w xY w|                      |¦  «         ŒT|r.t                               dt          |¦  «        › d|› d�¦  «         |r|D ]}| j         |= Œd S d S d S )NzThe following z' speaker embeddings are not available: zU If you would like to use them, please check the paths or try downloading them again.)	r   rD   rE   r[   Úappendrl   r-   r.   rj   )r   rs   Úunavailable_keysrP   rc   s        r   Ú_verify_speaker_embeddingsz(BarkProcessor._verify_speaker_embeddingsü   s"  € àÐØÔ"Ð.Ø $Ô <ð Dð D�ðØ(,×(?Ò(?ÀÑ(MÔ(MÐ%Ð%øÝ!ð ð ð à$×+Ò+¨LÑ9Ô9Ð9Ø�Hðøøøð ×0Ò0Ð1BÑCÔCÐCÐCàð Ý—’ðk¥SÐ)9Ñ%:Ô%:ð kð kÐcsð kð kð kñô ð ð
 "ð >Ø$4ð >ð >�LØÔ/°Ð=Ð=ð% /Ð.ð >ð >ð>ð >s   ”*ªAÁAÚpté   c           
      óª  — |�“t          |t          ¦  «        s~t          |t          ¦  «        r&| j        �|| j        v r|                      |¦  «        }nCt          |t          ¦  «        r|                     d¦  «        s|dz   }t          j        |¦  «        }|� | j        |fi |¤Ž t          ||¬¦  «        } | j
        |f|d||||dœ|¤Ž}	|�||	d<   |	S )aù  
        voice_preset (`str`, `dict[np.ndarray]`):
            The voice preset, i.e the speaker embeddings. It can either be a valid voice_preset name, e.g
            `"en_speaker_1"`, or directly a dictionary of `np.ndarray` embeddings for each submodel of `Bark`. Or
            it can be a valid file name of a local `.npz` single voice preset containing the keys
            `"semantic_prompt"`, `"coarse_prompt"` and `"fine_prompt"`.

        Returns:
            [`BatchEncoding`]: A [`BatchEncoding`] object containing the output of the `tokenizer`.
            If a voice preset is provided, the returned object will include a `"history_prompt"` key
            containing a [`BatchFeature`], i.e the voice preset with the right tensors type.
        Nz.npz)ÚdataÚtensor_typeÚ
max_length)Úreturn_tensorsÚpaddingr}   Úreturn_attention_maskÚreturn_token_type_idsÚadd_special_tokensÚhistory_prompt)re   Údictrh   r   rE   ÚendswithrG   r4   rl   r   r   )
r   ÚtextrP   r~   r}   r‚   r€   r�   r9   Úencoded_texts
             r   Ú__call__zBarkProcessor.__call__  s!  € ð0 Ð#­J°|ÅTÑ,JÔ,JÐ#å˜<­Ñ-Ô-ð5àÔ+Ð7Ø  DÔ$;Ð;Ð;à#×6Ò6°|ÑDÔD��õ ˜l­CÑ0Ô0ð 9¸×9NÒ9NÈvÑ9VÔ9Vð 9Ø#/°&Ñ#8�Lå!œw |Ñ4Ô4�àÐ#Ø,ˆDÔ,¨\ÐDÐD¸VÐDÐDÐDÝ'¨\À~ÐVÑVÔVˆLà%�t”~Øð	
à)Ø Ø!Ø"7Ø"7Ø1ð	
ð 	
ð ð	
ð 	
ˆð Ð#Ø-9ˆLÐ)Ñ*àÐr   )N)r   )r   r   F)T)NNrx   ry   FTF)Ú__name__Ú
__module__Ú__qualname__ri   r   Úclassmethodr5   ÚboolrJ   Ústaticmethodrh   rF   rE   r„   rl   Úpropertyro   rD   rw   r   r   rˆ   Ú__classcell__)r   s   @r   r   r   "   s×  ø€ € € € € ð ØØðð €Lð5ð 5ð 5ð 5ð 5ð 5ð àMkð7Oð 7Oð 7Oñ „[ð7Oðx &DØ%9Ø!ð3Gð 3Gð
 ð3Gð 3Gð 3Gð 3Gð 3Gð 3Gðj ðP¨ð P¸3ð PÐQTð Pð Pð Pñ „\ðPð$&!ð &!¨s°T©zð &!ð &!ð &!ð &!ðP	jð 	j¸¸t¹ð 	jð 	jð 	jð 	jð ð¨ð ð ð ñ „Xðð>ð >¸Tð >ð >ð >ð >ð. ð ØØØØ Ø"Ø#ð7ð 7ð 
ð7ð 7ð 7ñ „^ð7ð 7ð 7ð 7ð 7r   r   )Ú__doc__r3   r/   ÚnumpyrG   Úfeature_extraction_utilsr   Úprocessing_utilsr   Útokenization_utils_baser   Úutilsr   r   Ú	utils.hubr	   Úautor   Ú
get_loggerr‰   r-   r   Ú__all__© r   r   ú<module>rœ      s  ððð ð €€€Ø 	€	€	€	à Ð Ð Ð à 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø .Ð .Ð .Ð .Ð .Ð .Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ø $Ð $Ð $Ð $Ð $Ð $Ø  Ð  Ð  Ð  Ð  Ð  ð 
ˆÔ	˜HÑ	%Ô	%€ð ðhð hð hð hð h�Nñ hô hñ „ðhðV	 Ð
€€€r   