Ë
    T^(hŒ"  ã                   ót   — d dl mZmZ ddl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«      Zy)é    )ÚListÚUnioné   )Úis_torch_availableé   )ÚPipelineN)Ú%MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING)ÚSpeechT5HifiGanzmicrosoft/speecht5_hifiganc                   ój   ‡ — e Zd ZdZdddœˆ fd„
Zd„ Zd„ Zdeee	e   f   fˆ fd„Z
	 	 	 dd	„Zd
„ Zˆ xZS )ÚTextToAudioPipelinea5  
    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.

    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", framework="pt")

    >>> # 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).
    N)ÚvocoderÚsampling_ratec                óÄ  •— t        ‰| �  |i |¤Ž | j                  dk(  rt        d«      ‚d | _        | j
                  j                  t        j                  «       v rE|€<t        j                  t        «      j                  | j
                  j                  «      n|| _        || _        | j                  �%| j                  j                  j                  | _        | j                  €|| j
                  j                  }| j
                  j                   j#                  dd «      }|�|j%                  |j'                  «       «       dD ]  }t)        ||d «      }|€Œ|| _        Œ y y )NÚtfz5The TextToAudioPipeline is only available in PyTorch.Úgeneration_config)Úsample_rater   )ÚsuperÚ__init__Ú	frameworkÚ
ValueErrorr   ÚmodelÚ	__class__r	   Úvaluesr
   Úfrom_pretrainedÚDEFAULT_VOCODER_IDÚtoÚdevicer   ÚconfigÚ__dict__ÚgetÚupdateÚto_dictÚgetattr)	Úselfr   r   ÚargsÚkwargsr   Ú
gen_configÚsampling_rate_namer   s	           €úb/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/pipelines/text_to_audio.pyr   zTextToAudioPipeline.__init__N   s:  ø€ Ü‰Ñ˜$Ð) &Ò)à�>‰>˜TÒ!ÜÐTÓUÐUàˆŒØ�:‰:×ÑÔ#H×#OÑ#OÓ#QÑQð �?ô  ×/Ñ/Ô0BÓC×FÑFÀtÇzÁz×GXÑGXÔYàð ŒLð +ˆÔØ�<‰<Ð#Ø!%§¡×!4Ñ!4×!BÑ!BˆDÔà×ÑÐ%ð —Z‘Z×&Ñ&ˆFØŸ™×,Ñ,×0Ñ0Ð1DÀdÓKˆJØÐ%Ø—‘˜j×0Ñ0Ó2Ô3à&Fò 7Ð"Ü '¨Ð0BÀDÓ I�Ø Ñ,Ø)6�DÕ&ñ7ð &ó    c                 ó  — t        |t        «      r|g}| j                  j                  j                  dk(  r?| j
                  j                  j                  dd«      dddddœ}|j                  |«       |} | j                  |fi |¤dd	i¤Ž}|S )
NÚbarkÚmax_input_semantic_lengthé   FTÚ
max_length)r/   Úadd_special_tokensÚreturn_attention_maskÚreturn_token_type_idsÚpaddingÚreturn_tensorsÚpt)
Ú
isinstanceÚstrr   r   Ú
model_typer   Úsemantic_configr    r!   Ú	tokenizer)r$   Útextr&   Ú
new_kwargsÚoutputs        r)   Ú
preprocesszTextToAudioPipeline.preprocessm   s’   € Ü�dœCÔ Ø�6ˆDà�:‰:×Ñ×'Ñ'¨6Ò1ð #×4Ñ4×DÑD×HÑHÐIdÐfiÓjØ&+Ø)-Ø).Ø'ñˆJð ×Ñ˜fÔ%àˆFà�—‘ ÑD¨ÑD¸tÒDˆàˆr*   c                 óþ  — | j                  || j                  ¬«      }|d   }|d   }| j                  j                  «       r`| j                  || j                  ¬«      }d|vr| j                  |d<   |j                  |«        | j                  j                  di |¤|¤Ž}n>t        |«      rt        d|j                  «       › �«      ‚ | j                  di |¤|¤Žd   }| j                  �| j                  |«      }|S )N)r   Úforward_paramsÚgenerate_kwargsr   zñ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   © )Ú_ensure_tensor_on_devicer   r   Úcan_generater   r!   ÚgenerateÚlenr   Úkeysr   )r$   Úmodel_inputsr&   r@   rA   r=   s         r)   Ú_forwardzTextToAudioPipeline._forward„   s  € à×.Ñ.¨v¸d¿k¹kÐ.ÓJˆØÐ 0Ñ1ˆØ Ð!2Ñ3ˆà�:‰:×"Ñ"Ô$à"×;Ñ;¸OÐTX×T_ÑT_Ð;Ó`ˆOð #¨/Ñ9Ø7;×7MÑ7M�Ð 3Ñ4ð ×!Ñ! /Ô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_inputsc                 ó$   •— t        ‰| �  |fi |¤ŽS )a”  
        Generates speech/audio from the inputs. See the [`TextToAudioPipeline`] documentation for more information.

        Args:
            text_inputs (`str` or `List[str]`):
                The text(s) to generate.
            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:
            A `dict` or a list of `dict`: The dictionaries have 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   Ú__call__)r$   rJ   r@   r   s      €r)   rL   zTextToAudioPipeline.__call__¥   s   ø€ ô, ‰wÑ Ñ>¨~Ñ>Ð>r*   c                 ó¼   — | j                   �| j                   |d<   | j                  �| j                  |d<   | j                  |d<   |r|ni |r|ni dœ}|€i }i }|||fS )NÚassistant_modelr:   Úassistant_tokenizer)r@   rA   )rN   rO   r:   )r$   Úpreprocess_paramsr@   rA   ÚparamsÚpostprocess_paramss         r)   Ú_sanitize_parametersz(TextToAudioPipeline._sanitize_parameters½   s…   € ð ×ÑÐ+Ø15×1EÑ1EˆOÐ-Ñ.Ø×#Ñ#Ð/Ø+/¯>©>ˆO˜KÑ(Ø59×5MÑ5MˆOÐ1Ñ2ñ 1?™nÀBÙ2A™Àrñ
ˆð
 Ð$Ø "ÐØÐà  &Ð*<Ð<Ð<r*   c                 óâ   — i }t        |t        «      r|d   }nt        |t        «      r|d   }|j                  dt        j
                  ¬«      j                  «       |d<   | j                  |d<   |S )NÚwaveformr   Úcpu)r   ÚdtypeÚaudior   )r6   ÚdictÚtupler   ÚtorchÚfloatÚnumpyr   )r$   rU   Úoutput_dicts      r)   ÚpostprocesszTextToAudioPipeline.postprocessÔ   sh   € ØˆÜ�h¤Ô%Ø 
Ñ+‰HÜ˜¤%Ô(Ø ‘{ˆHØ'Ÿ{™{°%¼u¿{¹{˜{ÓK×QÑQÓSˆ�GÑØ'+×'9Ñ'9ˆ�OÑ$àÐr*   )NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r>   rI   r   r7   r   rL   rS   r_   Ú__classcell__)r   s   @r)   r   r      sN   ø„ ñ.ð` '+¸$ö 7ò>ò.ðB? E¨#¨t°C©y¨.Ñ$9õ ?ð4 ØØó	=ö.	r*   r   )Útypingr   r   Úutilsr   Úbaser   r[   Úmodels.auto.modeling_autor	   Ú!models.speecht5.modeling_speecht5r
   r   r   rB   r*   r)   ú<module>rj      s5   ð÷ å &Ý ñ ÔÛåQÝCà1Ð ô@˜(õ @r*   