o
    c۷i                     @   s^   d dl 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G dd de	ZdS )    N)Any)Audio)assert_soundfile_installedis_soundfile_installed)BaseCompletionRequestc                   @   s   e Zd ZU dZdZedB ed< dZedB ed< eed< dZedB ed< dZ	ee
B dB ed< ded	eeef fd
dZeddeeef ded	d fddZdS )SpeechRequestaa  Request for text-to-speech synthesis.

    Supports both preset voices and voice cloning via reference audio.

    Attributes:
        id: Optional unique identifier for the speech request.
        model: Optional model identifier for the speech synthesis.
        input: Text input to be converted to speech.
        voice: Optional preset voice identifier (e.g., 'Neutral Male', 'Neutral Female') to use for speech synthesis.
        ref_audio: Optional reference audio for voice cloning, provided as a base64-encoded string or raw bytes.
            Takes precedence over voice when both are provided.
    Nidmodelinputvoice	ref_audiokwargsreturnc                 K   s   | j dhd}t  | jdur;t| jtrt| j}nt| j}t }t	j
||j|j|jd |d ||d< || |S )zConvert this SpeechRequest to an OpenAI-compatible request dictionary.

        Args:
            **kwargs: Additional key-value pairs to include in the request dictionary.

        Returns:
            An OpenAI-compatible request dictionary.
        r   )excludeN)formatr   )
model_dumpr   r   
isinstancebytesioBytesIOr   from_base64sfwriteaudio_arraysampling_rater   seekupdate)selfr   openai_requestbufferaudio r!   \/home/ubuntu/vllm_env/lib/python3.10/site-packages/mistral_common/protocol/speech/request.py	to_openai    s   	


zSpeechRequest.to_openaiFr   strictc                    s    fdd|  D }|d }durRt|tjr| }nt|dr*|j }n|}t|t	rNt
j||d}|jdusEJ d|j||j|d< n||d< |d}t|trb|d	 |d<  d
i |S )a:  Create a SpeechRequest instance from an OpenAI-compatible request dictionary.

        Args:
            openai_request: The OpenAI request dictionary.
            strict: A flag indicating whether to perform strict validation of the audio data.

        Returns:
            An instance of SpeechRequest.
        c                    s    i | ]\}}| j v r||qS r!   )model_fields).0kvclsr!   r"   
<dictcomp>H   s     z-SpeechRequest.from_openai.<locals>.<dictcomp>r   Nfile)r$   z+Audio format must be set, got audio.format=r   r   r!   )itemsgetr   r   r   getvaluehasattrr,   readr   r   
from_bytesr   	to_base64dict)r*   r   r$   converted_dictr   audio_bytesr    r   r!   r)   r"   from_openai=   s    




zSpeechRequest.from_openai)F)__name__
__module____qualname____doc__r   str__annotations__r	   r   r   r   r   r4   r#   classmethodboolr7   r!   r!   r!   r"   r      s   
 &r   )r   typingr   mistral_common.audior   mistral_common.importsr   r   mistral_common.protocol.baser   	soundfiler   r   r!   r!   r!   r"   <module>   s    