o
    㥵i                     @   s@  d dl Z d dlmZ d dlZd dlZd dlmZmZmZm	Z	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 d dlmZ ejed	d
d d dlmZmZ d dlmZ d dlmZ d dl m!Z! G dd deZ"e#dkre" Z$e %de$j&j'Z(e(re() \Z*Z+n	e$j&j',d\Z*Z+ej-e$j.e*e/e+e$j&j0dd dS dS )    N)Lock)DependsFactoryClassHTTPException	HttpRouteKuiOpenAPIRoutes)
CORSConfig)Info)bearer_auth)logger)	Annotatedz.project-rootT)	indicator
pythonpath)MsgPackRequest
parse_args)ExceptionHandler)ModelManager)routesc                   @   s"   e Zd Zdd ZdefddZdS )APIc                    s   t   _ fdd}tt|gd _ttdddj _t j jdd   t j	t
 jittdt d	 _t  jj_ jj jj_ jj jj_ j j d S )
Nc                    s@   dt tttf f fdd} fdd}jjd ur|S |S )Ntokenc                    s&   | j jkrtdd d  I d H S )Ni  zInvalid token)argsapi_keyr   )r   )endpointself D/home/ubuntu/.local/lib/python3.10/site-packages/tools/api_server.pyverify"   s   z.API.__init__.<locals>.api_auth.<locals>.verifyc                      s     I d H S )Nr   r   r   r   r   passthrough'   s   z3API.__init__.<locals>.api_auth.<locals>.passthrough)r   strr   r   r   r   )r   r   r    r   r   r   api_auth!   s
    zAPI.__init__.<locals>.api_auth)http_middlewareszFish Speech APIz1.5.0)titleversion   )http)r   exception_handlersfactory_classcors_config)r   r   r	   r   r   r   openapir   r   http_exception_handler	Exceptionother_exception_handlerr   r   r
   appr   statelockdevicemax_text_length
on_startupinitialize_app)r   r#   r   r"   r   __init__   s6   
zAPI.__init__r0   c              	      sP   t | jj| jj| jj| jj| jj| jj| jjd|j	_
td| jj  d S )N)moder3   halfcompilellama_checkpoint_pathdecoder_checkpoint_pathdecoder_config_namez)Startup done, listening server at http://)r   r   r8   r3   r9   r:   r;   r<   r=   r1   model_managerr   infolisten)r   r0   r   r   r   r6   Q   s   

zAPI.initialize_appN)__name__
__module____qualname__r7   r   r6   r   r   r   r   r      s    3r   __main__z\[([^\]]+)\]:(\d+)$:r?   )hostportworkers	log_level)1re	threadingr   pyrootutilsuvicornkui.asgir   r   r   r   r   r   r	   kui.corsr
   kui.openapi.specificationr   kui.securityr   logurur   typing_extensionsr   
setup_root__file__tools.server.api_utilsr   r   tools.server.exception_handlerr   tools.server.model_managerr   tools.server.viewsr   r   rA   apisearchr   r@   matchgroupsrF   rG   splitrunr0   intrH   r   r   r   r   <module>   s<    $	L
