Ë
    ÜÓ^j™3  ã                   óº   — d dl mZmZm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  e	«       rd d	lZdd
lmZ ddlmZ dZ G d„ ded¬«      Z G d„ de«      Zy	)é    )ÚAnyÚ	TypedDictÚoverloadé   )Ú
AudioInput)ÚGenerationConfig)Úis_torch_available)ÚChatÚChatTypeé   )ÚPipelineN)Ú%MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING)ÚSpeechT5HifiGanzmicrosoft/speecht5_hifiganc                   ó&   — e Zd ZU dZeed<   eed<   y)ÚAudioOutputz›
    audio (`AudioInput`):
        The generated audio waveform.
    sampling_rate (`int`):
        The sampling rate of the generated audio waveform.
    ÚaudioÚsampling_rateN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__Úint© ó    úo/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/pipelines/text_to_audio.pyr   r   !   s   … ñð ÓØÔr   r   F)Útotalc                   ó
  ‡ — e Zd ZdZdZdZdZdZdZ e	d¬«      Z
dddœˆ fd„
Zd	„ Zd
„ Zedededefd„«       Zedee   dedee   fd„«       Zedededefd„«       Zedee   dedee   fd„«       Zˆ fd„Z	 	 	 dd„Zd„ Zˆ xZS )ÚTextToAudioPipelineaô  
    Text-to-audio generation pipeline using any `AutoModelForTextToWaveform` or `AutoModelForTextToSpectrogram`. This
    pipeline generates an audio file from an input text and optional other conditional inputs.

    Unless the model you're using explicitly sets these generation parameters in its configuration files
    (`generation_config.json`), the following default values will be used:
    - max_new_tokens: 256

    Example:

    ```python
    >>> from transformers import pipeline

    >>> pipe = pipeline(model="suno/bark-small")
    >>> output = pipe("Hey it's HuggingFace on the phone!")

    >>> audio = output["audio"]
    >>> sampling_rate = output["sampling_rate"]
    ```

    Learn more about the basics of using a pipeline in the [pipeline tutorial](../pipeline_tutorial)

    <Tip>

    You can specify parameters passed to the model by using [`TextToAudioPipeline.__call__.forward_params`] or
    [`TextToAudioPipeline.__call__.generate_kwargs`].

    Example:

    ```python
    >>> from transformers import pipeline

    >>> music_generator = pipeline(task="text-to-audio", model="facebook/musicgen-small")

    >>> # diversify the music generation by adding randomness with a high temperature and set a maximum music length
    >>> generate_kwargs = {
    ...     "do_sample": True,
    ...     "temperature": 0.7,
    ...     "max_new_tokens": 35,
    ... }

    >>> outputs = music_generator("Techno music with high melodic riffs", generate_kwargs=generate_kwargs)
    ```

    </Tip>

    This pipeline can currently be loaded from [`pipeline`] using the following task identifiers: `"text-to-speech"` or
    `"text-to-audio"`.

    See the list of available models on [huggingface.co/models](https://huggingface.co/models?filter=text-to-speech).
    TNFé   )Úmax_new_tokens)Úvocoderr   c                ó@  •— t        ‰
| �  |i |¤Ž d | _        | j                  j                  t        j                  «       v rE|€<t        j                  t        «      j                  | j                  j                  «      n|| _        | j                  j                  j                  dv rd | _        || _        | j                  �%| j                  j                  j                  | _        | j                  €Ï| j                  j                  }| j                  j                   j#                  dd «      }|�D|j%                  |j'                  «       j)                  «       D ��ci c]  \  }}|€Œ	||“Œ c}}«       dD ]H  }	t+        ||	d «      }|�|| _        Œt+        |dd «      €Œ(t+        |j,                  |	d «      }|€ŒB|| _        ŒJ | j                  €J| j                  �=t/        | j                  d«      r&| j                  j0                  j                  | _        y y y y c c}}w )N)ÚmusicgenÚspeecht5Úgeneration_config)Úsample_rater   Úcodec_configÚfeature_extractor)ÚsuperÚ__init__r"   ÚmodelÚ	__class__r   Úvaluesr   Úfrom_pretrainedÚDEFAULT_VOCODER_IDÚtoÚdeviceÚconfigÚ
model_typeÚ	processorr   Ú__dict__ÚgetÚupdateÚto_dictÚitemsÚgetattrr(   Úhasattrr)   )Úselfr"   r   ÚargsÚkwargsr3   Ú
gen_configÚkÚvÚsampling_rate_namer-   s             €r   r+   zTextToAudioPipeline.__init__m   så  ø€ Ü‰Ñ˜$Ð) &Ò)àˆŒØ�:‰:×ÑÔ#H×#OÑ#OÓ#QÑQð �?ô  ×/Ñ/Ô0BÓC×FÑFÀtÇzÁz×GXÑGXÔYàð ŒLð �:‰:×Ñ×'Ñ'Ð+CÑCà!ˆDŒNà*ˆÔØ�<‰<Ð#Ø!%§¡×!4Ñ!4×!BÑ!BˆDÔà×ÑÐ%ð —Z‘Z×&Ñ&ˆFØŸ™×,Ñ,×0Ñ0Ð1DÀdÓKˆJØÐ%Ø—‘°
×0BÑ0BÓ0D×0JÑ0JÓ0L×^©¨¨1ÐPQÑP]˜q !™tÓ^Ô_à&Fò ;Ð"Ü '¨Ð0BÀDÓ I�Ø Ð,Ø)6�DÕ&Ü˜V ^°TÓ:ÑFÜ$+¨F×,?Ñ,?ÐASÐUYÓ$Z�MØ$Ñ0Ø-:˜Õ*ð;ð ×ÑÐ%¨$¯.©.Ð*DÌÐQU×Q_ÑQ_ÐatÔIuØ!%§¡×!AÑ!A×!OÑ!OˆDÕð JvÐ*DÐ%ùó _s   Å
H
Å)H
c                 ó6  — t        |t        «      r|g}| j                  j                  j                  dk(  rSd}t        | j                  d«      r!t        | j                  j                  dd«      }|ddddœ}|j                  |«       |}| j                  �| j                  n| j                  }t        |t        «      r" |j                  |j                  fdddœ|¤Ž}|S | j                  j                  j                  d	k(  r7|D �cg c]  }|j                  d
«      sd|› �n|‘Œ }}|j!                  dd«       | j                  j                  j                  dk(  r%|D �cg c]  }|j                  d
«      sd|› �n|‘Œ }} ||fi |¤ddi¤Ž}|S c c}w c c}w )NÚbarkr    Úsemantic_configÚmax_input_semantic_lengthFT)Ú
max_lengthÚadd_special_tokensÚreturn_attention_maskÚreturn_token_type_ids)ÚtokenizeÚreturn_dictÚcsmú[z[0]rI   Údiaz[S1] Úreturn_tensorsÚpt)Ú
isinstanceÚstrr,   r3   r4   r<   r&   r;   rF   r8   r5   Ú	tokenizerr
   Úapply_chat_templateÚmessagesÚ
startswithÚ
setdefault)r=   Útextr?   rH   Ú
new_kwargsÚpreprocessorÚoutputÚts           r   Ú
preprocesszTextToAudioPipeline.preprocess•   s˜  € Ü�dœCÔ Ø�6ˆDà�:‰:×Ñ×'Ñ'¨6Ò1ð ˆJÜ�t×-Ñ-Ð/@ÔAÜ$ T×%;Ñ%;×%KÑ%KÐMhÐjmÓn�
à(Ø&+Ø)-Ø).ñ	ˆJð ×Ñ˜fÔ%ØˆFà)-¯©Ð)C�t—~’~ÈÏÉˆÜ�dœDÔ!Ø5�\×5Ñ5Ø—‘ðàØ ñð ñ	ˆFð ˆð �z‰z× Ñ ×+Ñ+¨uÒ4ØKOÖPÀa¨¯©°cÔ):˜#˜a˜S™	ÀÑAÐP�ÐPØ×!Ñ!Ð"6¸Ô=Ø�z‰z× Ñ ×+Ñ+¨uÒ4ØMQÖRÈ¨1¯<©<¸Ô+<˜% ˜s™À!ÑCÐR�ÐRÙ! $ÑF¨&ÑFÀÒFˆFàˆùò Qùò Ss   ÄFÅ!Fc                 óz  — | j                  || j                  ¬«      }|d   }|d   }| j                  j                  «       rž| j                  || j                  ¬«      }d|vr| j                  |d<   |j                  |«       |j                  ddi«       | j                  j                  j                  dv r	d|vrd|d<    | j                  j                  di |¤|¤Ž}n>t        |«      rt        d	|j                  «       › �«      ‚ | j                  di |¤|¤Žd
   }| j                  �| j                  |«      }|S )N)r2   Úforward_paramsÚgenerate_kwargsr&   Úreturn_dict_in_generateT)rN   Úoutput_audiozñYou're using the `TextToAudioPipeline` with a forward-only model, but `generate_kwargs` is non empty. For forward-only TTA models, please use `forward_params` instead of `generate_kwargs`. For reference, the `generate_kwargs` used here are: r   r   )Ú_ensure_tensor_on_devicer2   r,   Úcan_generater&   r8   r3   r4   ÚgenerateÚlenÚ
ValueErrorÚkeysr"   )r=   Úmodel_inputsr?   ra   rb   r]   s         r   Ú_forwardzTextToAudioPipeline._forward½   sT  € à×.Ñ.¨v¸d¿k¹kÐ.ÓJˆØÐ 0Ñ1ˆØ Ð!2Ñ3ˆà�:‰:×"Ñ"Ô$à"×;Ñ;¸OÐTX×T_ÑT_Ð;Ó`ˆOð #¨/Ñ9Ø7;×7MÑ7M�Ð 3Ñ4ð ×!Ñ! /Ô2ð ×!Ñ!Ð#<¸dÐ"CÔDà�z‰z× Ñ ×+Ñ+¨wÑ6ð "¨Ñ7Ø59�N >Ñ2à(�T—Z‘Z×(Ñ(ÑJ¨<ÐJ¸>ÑJ‰Fä�?Ô#Ü ðKàKZ×K_ÑK_ÓKaÐJbðdóð ð
  �T—Z‘ZÑA ,ÐA°.ÑAÀ!ÑDˆFà�<‰<Ð#à—\‘\ &Ó)ˆFàˆr   Útext_inputsra   Úreturnc                  ó   — y ©Nr   ©r=   rm   ra   s      r   Ú__call__zTextToAudioPipeline.__call__ç   s   € ØPSr   c                  ó   — y rp   r   rq   s      r   rr   zTextToAudioPipeline.__call__ê   s   € Ø\_r   c                  ó   — y rp   r   rq   s      r   rr   zTextToAudioPipeline.__call__í   s   € ØUXr   c                  ó   — y rp   r   rq   s      r   rr   zTextToAudioPipeline.__call__ð   s   € Øadr   c                 ó$   •— t        ‰| �  |fi |¤ŽS )aL  
        Generates speech/audio from the inputs. See the [`TextToAudioPipeline`] documentation for more information.

        Args:
            text_inputs (`str`, `list[str]`, `ChatType`, or `list[ChatType]`):
                One or several texts to generate. If strings or a list of string are passed, this pipeline will
                generate the corresponding text. Alternatively, a "chat", in the form of a list of dicts with "role"
                and "content" keys, can be passed, or a list of such chats. When chats are passed, the model's chat
                template will be used to format them before passing them to the model.
            forward_params (`dict`, *optional*):
                Parameters passed to the model generation/forward method. `forward_params` are always passed to the
                underlying model.
            generate_kwargs (`dict`, *optional*):
                The dictionary of ad-hoc parametrization of `generate_config` to be used for the generation call. For a
                complete overview of generate, check the [following
                guide](https://huggingface.co/docs/transformers/en/main_classes/text_generation). `generate_kwargs` are
                only passed to the underlying model if the latter is a generative model.

        Return:
            `AudioOutput` or a list of `AudioOutput`, which is a `TypedDict` with two keys:

            - **audio** (`np.ndarray` of shape `(nb_channels, audio_length)`) -- The generated audio waveform.
            - **sampling_rate** (`int`) -- The sampling rate of the generated audio waveform.
        )r*   rr   )r=   rm   ra   r-   s      €r   rr   zTextToAudioPipeline.__call__ó   s   ø€ ô2 ‰wÑ Ñ>¨~Ñ>Ð>r   c                 óÀ   — t        | dd «      �| j                  |d<   t        | dd «      �| j                  |d<   | j                  |d<   |r|ni |r|ni dœ}|€i }i }|||fS )NÚassistant_modelÚassistant_tokenizerrU   )ra   rb   )r;   rx   rU   ry   )r=   Úpreprocess_paramsra   rb   ÚparamsÚpostprocess_paramss         r   Ú_sanitize_parametersz(TextToAudioPipeline._sanitize_parameters  s�   € ô �4Ð*¨DÓ1Ð=Ø15×1EÑ1EˆOÐ-Ñ.Ü�4Ð.°Ó5ÐAØ+/¯>©>ˆO˜KÑ(Ø59×5MÑ5MˆOÐ1Ñ2ñ 1?™nÀBÙ2A™Àrñ
ˆð
 Ð$Ø "ÐØÐà  &Ð*<Ð<Ð<r   c                 óX  — d}t        |t        «      rd|v r|d   }nd}|d   }nt        |t        «      r|d   }|r'| j                  �| j                  j	                  |«      }t        |t
        «      r`|D �cg c]?  }|j                  dt        j                  ¬«      j                  «       j                  «       ‘ŒA }}t        |«      dkD  r|n|d   }n=|j                  dt        j                  ¬«      j                  «       j                  «       }t        || j                  ¬	«      S c c}w )
NFr   TÚ	sequencesr   Úcpu)r2   Údtyper   )r   r   )rS   ÚdictÚtupler5   ÚdecodeÚlistr1   ÚtorchÚfloatÚnumpyÚsqueezerh   r   r   )r=   r   Úneeds_decodingÚels       r   ÚpostprocesszTextToAudioPipeline.postprocess%  s  € ØˆÜ�eœTÔ"Ø˜%ÑØ˜g™‘à!%�Ø˜kÑ*‘Ü˜œuÔ%Ø˜!‘HˆEá˜dŸn™nÐ8Ø—N‘N×)Ñ)¨%Ó0ˆEä�eœTÔ"ØX]Ö^ÐRT�R—U‘U %¬u¯{©{�UÓ;×AÑAÓC×KÑKÕMÐ^ˆEÐ^Ü  ›Z¨!š^‘E°°q±‰Eà—H‘H E´·±�HÓ=×CÑCÓE×MÑMÓOˆEäØØ×,Ñ,ô
ð 	
ùò _s   Á7AD')NNN)r   r   r   r   Ú_pipeline_calls_generateÚ_load_processorÚ_load_image_processorÚ_load_feature_extractorÚ_load_tokenizerr   Ú_default_generation_configr+   r_   rl   r   rT   r   r   rr   r…   r   r}   rŒ   Ú__classcell__)r-   s   @r   r   r   -   sô   ø„ ñ2ðh  $ÐØ€OØ!ÐØ#ÐØ€Oñ "2Øô"Ðð '+¸$ö &PòP&òP(ðT ØS CÐS¸3ÐSÀ;ÒSó ØSàØ_ D¨¡IÐ_ÀÐ_ÈÈkÑIZÒ_ó Ø_àØX HÐXÀÐXÈÒXó ØXàØd D¨¡NÐdÀcÐdÈdÐS^ÑN_Òdó Ødô?ð: ØØó	=ö.
r   r   )Útypingr   r   r   Úaudio_utilsr   Ú
generationr   Úutilsr	   Úutils.chat_template_utilsr
   r   Úbaser   r†   Úmodels.auto.modeling_autor   Ú!models.speecht5.modeling_speecht5r   r0   r   r   r   r   r   ú<module>rœ      sO   ð÷ ,Ñ +å $Ý )Ý &ß 6Ý ñ ÔÛåQÝCà1Ð ô	�) 5õ 	ôO
˜(õ O
r   