o
    㥵iX	                     @   s   d dl mZ d dlmZ d dlmZmZ 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 d d	lmZ d
d ZG dd deZdedefddZdd Zdd ZdS )    )ArgumentParser)
HTTPStatus)	AnnotatedAnyN)ContentType)HTTPExceptionHttpRequest)TTSInferenceEngine)ServeTTSRequest)inference_wrapperc                  C   s   t  } | jdtdgdd | jdtdd | jdtdd | jd	td
d | jdtdd | jddd | jddd | jdtdd | jdtdd | jdtdd | jdtd d |  S )Nz--modetts)typechoicesdefaultz--llama-checkpoint-pathzcheckpoints/openaudio-s1-mini)r   r   z--decoder-checkpoint-pathz'checkpoints/openaudio-s1-mini/codec.pthz--decoder-config-namemodded_dac_vqz--devicecudaz--half
store_true)actionz	--compilez--max-text-lengthr   z--listenz127.0.0.1:8080z	--workers   z	--api-key)r   add_argumentstrint
parse_args)parser r   J/home/ubuntu/.local/lib/python3.10/site-packages/tools/server/api_utils.pyr      s*   r   c                   @   s,   e Zd Zdeeededf fddZdS )MsgPackRequestreturnapplication/msgpackapplication/jsonc                    sF   | j dkrt| jI d H S | j dkr| jI d H S ttjddid)Nr   r   Acceptz%application/msgpack, application/json)headers)content_type	ormsgpackunpackbbodyjsonr   r   UNSUPPORTED_MEDIA_TYPE)selfr   r   r   data(   s   

zMsgPackRequest.dataN)__name__
__module____qualname__r   r   r   r)   r   r   r   r   r   '   s    r   reqenginec                 C  s&   t | |D ]
}t|tr|V  qd S N)	inference
isinstancebytes)r-   r.   chunkr   r   r   inference_async9   s   
r4   c                 C  s   | V  d S r/   r   )bufferr   r   r   buffer_to_async_generator?   s   
r6   c                 C   s(   | dkrdS | dkrdS | dkrdS dS )Nwavz	audio/wavflacz
audio/flacmp3z
audio/mpegzapplication/octet-streamr   )audio_formatr   r   r   get_content_typeC   s   r;   )argparser   httpr   typingr   r   r#   baize.datastructuresr   kui.asgir   r   fish_speech.inference_enginer	   fish_speech.utils.schemar
   tools.server.inferencer   r0   r   r   r4   r6   r;   r   r   r   r   <module>   s    