o
    㥵iK                     @   s   d dl Z d dlmZ d dlmZmZ d dlmZ d dlm	Z	m
Z
 dd Zded	ed
efddZded
efddZd
efddZdS )    N)partial)AnyCallable)i18n)ServeReferenceAudioServeTTSRequestc                 C   s   |rt ||}ng }t| |r|nd|||||||	rt|	nd|
d
}||D ] }|j dkr7 |jdf  S dkrEdtt|jf  S 	 	 q'dtdfS )zO
    Wrapper for the inference function.
    Used in the Gradio interface.
    N)
textreference_id
referencesmax_new_tokenschunk_lengthtop_prepetition_penaltytemperatureseeduse_memory_cachefinalerrorzNo audio generated)	get_reference_audior   int	inferencecodeaudiobuild_html_error_messager   r   )r   r	   reference_audioreference_textr   r   r   r   r   r   r   enginer
   reqresult r   I/home/ubuntu/.local/lib/python3.10/site-packages/tools/webui/inference.pyinference_wrapper	   s0   

r!   r   r   returnc                 C   s@   t | d}| }W d   n1 sw   Y  t||dgS )z(
    Get the reference audio bytes.
    rbN)r   r   )openreadr   )r   r   
audio_fileaudio_bytesr   r   r    r   :   s   
r   r   c                 C   s,   t | tr| ntd} dtt|  dS )NzUnknown errorz?
    <div style="color: red; 
    font-weight: bold;">
        z
    </div>
    )
isinstance	Exceptionhtmlescapestr)r   r   r   r    r   E   s   r   c                 C   s   t t| dS )zB
    Get the inference function with the immutable arguments.
    r   )r   r!   r-   r   r   r    get_inference_wrapperQ   s   r.   )r*   	functoolsr   typingr   r   fish_speech.i18nr   fish_speech.utils.schemar   r   r!   r,   listr   r   r.   r   r   r   r    <module>   s    1