o
    eM                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
 d dl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 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 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 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! d dl"m#Z# d dl"m$Z$ d dl%m&Z& G dd de'Z(dd Z)dS )     N)Process)Path   )create_gradio_client_app)create_gradio_client_app_en)analyse_config)check_process_zombie)"copy_config_file_to_default_config)delete_files_for_process)"exchange_format_to_original_format)generate_metric_tableget_alive_fastdeploy_servers"get_config_filenames_for_one_modelget_config_for_one_modelget_process_model_configuration)get_process_output)get_start_arguments)
json2pbtxt)kill_process)launch_process)mark_pid_for_dead_process)"original_format_to_exchange_format)validate_data)
gen_result)result)FASTDEPLOYSERVER_PATHc                   @   s$  e Zd Zdd Ze dd Ze dd Ze dd Ze d	d
 Ze dd Z	eddd Z
e dd Ze dd Ze dd Ze dd Ze dd Ze dd Ze dd Ze dd Ze d d! Ze d"d# Ze d$d% Ze d&d' Zd.d)d*Zd+d, Zd-S )/FastDeployServerApic                 C   s$   t t | _i | _d | _d | _d S N)r   osgetcwdroot_diropened_serversclient_portclient_en_portself r*   ^D:\Projects\ConvertPro\env\Lib\site-packages\visualdl/component/inference/fastdeploy_server.py__init__5   s   
zFastDeployServerApi.__init__c                 C   s   | j ttj|jvrd}t|d \}}}t| j ttj|kr+|d t	|}tj
ttj|| j |d}|S )N.z..)
parent_dirZsub_dir)r$   r   r"   pathabspathparentswalksendappendsortedrelpath)r)   cur_dirZsub_dirs	filenamesZ
directorysr*   r*   r+   get_directory<   s   
z!FastDeployServerApi.get_directoryc                 C   s   t |\}}t||S r!   )r   r   )r)   r7   Zall_model_configsZall_model_versionsr*   r*   r+   
get_configL   s   zFastDeployServerApi.get_configc                 C   s   t |}t|}tjtj||}t|| }tt 	|}d|v r)t
dtj|d }	ttj||tj|d|	tj   ttj||d}
|
| W d    d S 1 sew   Y  d S )NZ	vdlbackupz.Backup config file is not permitted to update.r   z{}_vdlbackup_{}.pbtxtw)jsonloadsr   r"   r/   joinr0   r   r   dumpsRuntimeErrorsplitextshutilcopyformatdatetimenow	isoformatopenwrite)r)   r7   
model_nameconfigconfig_filenameZ
all_models	model_dirZfiltered_configZ
text_protobasenamefr*   r*   r+   config_updateR   s2   

z!FastDeployServerApi.config_updatec                 C   sN   t |}t|}| d urtd|d r|d nt|j}|| j|< |S )Nu^   Failed to launch fastdeployserver，please check fastdeployserver is installed in environment.zserver-name)r<   r=   r   pollr@   strpidr%   )r)   ZconfigsprocessZserver_namer*   r*   r+   start_serverj   s   

z FastDeployServerApi.start_serverc                 C   sP   || j v rt| j |  | j |= n|tttv rt| t| |   d S r!   )r%   r   setr"   listdirr   r
   _poll_zombie_processr)   	server_idr*   r*   r+   stop_serverw   s   

zFastDeployServerApi.stop_serverz
text/plainc                 C   s@   t |}|| jv rt||S t|tttv rt||S d S r!   )intr%   r   rR   rV   r"   rW   r   )r)   rZ   lengthr*   r*   r+   get_server_output   s   


z%FastDeployServerApi.get_server_outputc                 C   s"   t |}d}|dd}t||S )N	localhostzmetrics-portiB  )r   getr   )r)   rZ   argshostportr*   r*   r+   get_server_metric   s   
z%FastDeployServerApi.get_server_metricc                 C   s   t  S r!   r   r(   r*   r*   r+   get_server_list   s   z#FastDeployServerApi.get_server_listc                 C   s&   |    t|du rtd|d S )NTut   Server {} is down due to exception or killed，please check the reason according to the log, then close this server.)rX   r   r@   rD   rY   r*   r*   r+   check_server_alive   s   z&FastDeployServerApi.check_server_alivec                 C   s   t |S r!   r   rY   r*   r*   r+   get_server_config   s   z%FastDeployServerApi.get_server_configc                 C   s   t d}| }|d dkrtd|d }i }| D ]\}}||vr*t ||< |D ]}|| |d  q,qg }| D ] \}}	|||g d t|	D ]}
|d d	 |
|
d
 qPq?|S )zF
        Get all available fastdeploy models from hub server.
        z:http://paddlepaddle.org.cn/paddlehub/fastdeploy_listmodelsstatusr   z5Failed to get pre-trained model list from hub server.dataname)valuelabelchildrenrm   )rk   rl   )	requestsr`   r<   r@   itemsrV   addr4   r5   )r)   resr   ri   Z
model_listcategorymodelsmodelZmodels_infoZmodel_namesrJ   r*   r*   r+   get_pretrain_model_list   s<   

z+FastDeployServerApi.get_pretrain_model_listc                 C   s  t jt j|||}zdd l}W n ty   tdw |j||d}|rd|v r;t	|t jt j
|d nGt |D ]<}d|v sJd|v rht	t j||t jt j
|dt j|d	  q@t	t j||t jt j
|| q@t| g }	t t j||D ]3}
td
|
ri }|
|d< |
|d< g |d< t t j|||
D ]}|d ||d q|	| q|	S td|)Nr   u   fastdeploy is required for visualizing results，please refer to https://github.com/PaddlePaddle/FastDeploy to install fastdeploy)rj   r/   z.onnxz
model.onnxz.pdmodelz
.pdiparamszmodel{}r   \d+titlekeyrm   rx   ry   z(Failed to download pre-trained model {}.)r"   r/   r>   r0   Z
fastdeploy	Exceptionr@   Zdownload_modelrB   movedirnamerW   rD   rA   rmtreerematchr4   )r)   r7   rJ   versionpretrain_model_nameZversion_resource_dirfdZ
model_pathfilenameversion_info_for_frontendversion_name#version_filenames_dict_for_frontendr*   r*   r+   download_pretrain_model   s   
z+FastDeployServerApi.download_pretrain_modelc                 C   s   t |||S r!   r   r)   r7   rj   rL   r*   r*   r+   get_config_for_model  s   z(FastDeployServerApi.get_config_for_modelc                 C   s
   t ||S r!   r   )r)   r7   rj   r*   r*   r+   get_config_filenames_for_model  s   
z2FastDeployServerApi.get_config_filenames_for_modelc                 C   sX   | j ttj|jvrtdtjtj|||r't	tj||| t
||S )Nz4Failed to delete config file, please check filepath.)r$   r   r"   r/   r0   r1   r@   existsr>   remover   r   r*   r*   r+   delete_config_for_model
  s   

z+FastDeployServerApi.delete_config_for_modelc              
   C   sr   t jt j||}t jt j|dr.tt j|dt j|dtj	 
  |dkr7t|| d S )Nzconfig.pbtxtzconfig_vdlbackup_{}.pbtxt)r"   r/   r>   r0   r   rB   rC   rD   rE   rF   rG   r	   )r)   r7   rj   rL   rM   r*   r*   r+   set_default_config_for_model  s   
z0FastDeployServerApi.set_default_config_for_modelc           
      C   s   | j ttj|jvrtdtjtj||||}tj|r(t	| g }t
tj||D ]3}td|rgi }||d< ||d< g |d< t
tj|||D ]}	|d |	|	d qU|| q4|S )Nz6Failed to delete resource file, please check filepath.rw   rx   ry   rm   rz   )r$   r   r"   r/   r0   r1   r@   r>   r   r   rW   r   r   r4   )
r)   r7   rJ   r   resource_filenameresource_pathr   r   r   r   r*   r*   r+   delete_resource_for_model#  sD   


z-FastDeployServerApi.delete_resource_for_modelc                 C   s   | j ttj|jvrtdtjtj||||}tjtj||||}tj|r6t	
|| g }ttj||D ]3}	td|	rui }
|	|
d< |	|
d< g |
d< ttj|||	D ]}|
d ||d qc||
 qB|S )Nz6Failed to rename resource file, please check filepath.rw   rx   ry   rm   rz   )r$   r   r"   r/   r0   r1   r@   r>   r   rB   r|   rW   r   r   r4   )r)   r7   rJ   r   r   new_filenamer   Znew_file_pathr   r   r   r   r*   r*   r+   rename_resource_for_modelC  sJ   

z-FastDeployServerApi.rename_resource_for_modelzhc                 C   s   dd }dd }|dkr.| j d u r+| | _ t }t|jd| j id}|  || j  | j S | jd u rM| | _t }t|jd| jid}|  || j | jS )Nc                  S   s2   t  t jt j} | d |  \}}|   |S )N)r_   r   )socketAF_INETSOCK_STREAMbindgetsocknameclose)Ztcpaddrrc   r*   r*   r+   get_free_tcp_portf  s
   
zGFastDeployServerApi.create_fastdeploy_client.<locals>.get_free_tcp_portc                 S   s8   	 zt d|  W d S  ty   td Y nw q)NTzhttp://localhost:{}/r   )ro   r`   rD   r{   timesleep)r&   r*   r*   r+   check_aliven  s   zAFastDeployServerApi.create_fastdeploy_client.<locals>.check_aliveenZserver_port)targetkwargs)r'   r   r   Zlaunchstartr&   r   )r)   langr   r   Zappthreadr*   r*   r+   create_fastdeploy_cliente  s.   



z,FastDeployServerApi.create_fastdeploy_clientc                 C   sL   g }| j  D ]\}}| d urt| || q|D ]}| j |= qd S r!   )r%   rp   rQ   r   r4   )r)   Zshould_deleterZ   rT   r*   r*   r+   rX     s   

z(FastDeployServerApi._poll_zombie_processN)r   )__name__
__module____qualname__r,   r   r9   r:   rP   rU   r[   r^   rd   re   rf   rg   rv   r   r   r   r   r   r   r   r   rX   r*   r*   r*   r+   r    4   sP    










!
8







!&r    c                     s6  t  } i d| jdgfd| jg dfd| jdgfd| jddgfd| jg d	fd
| jg d	fd| jg d	fd| jdgfd| j	dgfd| j
ddgfd| jdgfd| jg fd| jdgfd| jdgfd| jg fd| jdgfd| jg df| jg df| jg dfd dtf fdd }|S )!Nr9   dirrP   )r   rj   rK   rL   r:   r   rj   r   )r   rj   rL   r   r   rU   rK   r[   rZ   r^   r]   r   r   re   rd   rg   rv   rf   r   )r   rj   r   r   )r   rj   r   r   )r   rj   r   r   r   )r   r   r/   c                    sH    | }|sttddddd fS |\}} fdd|D }|| S )Nr   zapi not found)rh   msgzapplication/jsonc                    s   g | ]}  |qS r*   )r`   ).0rj   ra   r*   r+   
<listcomp>  s    z<create_fastdeploy_api_call.<locals>.call.<locals>.<listcomp>)r`   r<   r?   r   )r/   ra   ZroutemethodZcall_arg_namesZ	call_argsZroutesr   r+   call  s   
z(create_fastdeploy_api_call.<locals>.call)r    r9   rP   r:   r   r   r   r   rU   r[   r^   r   re   rd   rg   rv   rf   r   r   r   rR   )apir   r*   r   r+   create_fastdeploy_api_call  sp   	

!	r   )*rE   r<   r"   r   rB   r   r   multiprocessingr   pathlibr   ro   Zfastdeploy_client.client_appr   r   Zfastdeploy_libr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zvisualdl.server.apir   r   Zvisualdl.utils.dirr   objectr    r   r*   r*   r*   r+   <module>   sJ     e