o
    &j+                     @   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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
ZG dd deZdd ZdS )    N)request)onnx2paddle   )archive)BosFileSystem)
gen_result)result)X2PADDLE_CACHE_PATH   c                   @   sT   e Zd ZdZdd Ze dd Zeddd Ze d	d
 Zeddd Z	dS )ModelConvertApia[  !
    Integrate multiple model convertion tools, and provide convertion service for users.
    When user uploads a model to this server, convert model and upload the results to VDL Bos.
    When user downloads the model, we get the data from Bos and send it to client.
    Maybe users can download from bos directy if frontend can achieve it.
    c                 C   sL   zt  | _td| _W dS  ty%   t dd| _| j  d| _Y dS w )zd
        Initialize a object to provide service. Need a BosFileSystem client to write data.
        ZBOS_BUCKET_NAMEF)Z
write_flagzvisualdl-serverN)r   
bos_clientosgetenvbucket_name	ExceptionZrenew_bos_client_from_server)self r   r/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/visualdl/component/inference/model_convert_server.py__init__-   s   
zModelConvertApi.__init__c                 C   s@  t jd }|j }i }|dv rd}nd}|dvrd}|dvr!d}t }|| | }||d	< tj	
td
|}	tj	|	sGtj|	dd t }
|
| |
  zddl}W n tyf   tdw zE|du rtjj  t|
j|	|d W d   n1 sw   Y  n tjj  t|
j|	|||d W d   n1 sw   Y  W n ty } ztd|d}~ww t }ttj	|	 ttj	 |	 t| t!tj	
td
d|d:}| }d| j"|}z| j#j||dd W n ty } zt$d|| W Y d}~nd}~ww W d   n	1 s&w   Y  W d   n	1 s6w   Y  t!tj	
|	ddd@}d| j"|}| }z	| j#|| W n tyv } zt$d|| W Y d}~nd}~ww t%&|'d}W d   n	1 sw   Y  t()|	 ||d< t$t*| |S )z5
        Convert onnx model to paddle model.
        model)trueTrueyesZYesyTF)armZopenclx86ZmetalZxpuZbmZmluZ
intel_fpgaZhuawei_ascend_npuZimagination_nnaZrockchip_npuZmediatek_apuZhuawei_kirin_npuZamlogic_npur   )protobufnaive_bufferr   
request_idr   )exist_okr   Nz=[ERROR] onnx is not installed, use "pip install onnx>=1.6.0".)convert_to_liter    lite_valid_placeslite_model_typezw[Convertion error] {}.
 Please open an issue at https://github.com/PaddlePaddle/X2Paddle/issues to report your problem.z{}.tarrbbos://{}/onnx2paddle/{}.tarappendz5Exception: Write file {}.tar to bos failed, due to {}Zinference_modelzmodel.pdmodelz%bos://{}/onnx2paddle/{}/model.pdmodelz?Exception: Write file {}/model.pdmodel to bos failed, due to {}utf-8)+r   filesstreamreadhashlibmd5update	hexdigestr   pathjoinr	   existsmakedirstempfileNamedTemporaryFilewriteflushonnxr   RuntimeErrorpaddleZfluidZdygraphguardr   nameformatgetcwdchdirdirnamer   basenameopenr   r   printbase64	b64encodedecodeshutilrmtreelen)r   r    r"   r#   model_handledatar   hlidentitytarget_pathfpr8   eZ
origin_dirffilenamemodel_fpmodel_encodedr   r   r   onnx2paddle_model_convert;   s   







/
z)ModelConvertApi.onnx2paddle_model_convertzapplication/octet-streamc                 C   F   d | j|}d}| j|r| j|}|stdtt| |S )z;
        Download converted paddle model from bos.
        r%   NSThe requested model can not be downloaded due to not existing or convertion failed.r=   r   r   r2   	read_filer9   rC   rI   r   r   rR   rK   r   r   r   onnx2paddle_model_download      z*ModelConvertApi.onnx2paddle_model_downloadc                 C   s  t jd }t jd }|j }|j }i }zt|}W n ty'   d}Y nw |dvr.d}t }|||  |	 }	|	|d< t
 }
t
 z}|
| || |
  |  ztj|
j|j||d}W n ty} } ztd|d	}~ww |std
d| j|	}d	}|rz| jj||dd W n ty } ztd|	| W Y d	}~nd	}~ww t|d}W d	   n1 sw   Y  W d	   n1 sw   Y  ||d< tt| |S )z5
        Convert paddle model to onnx model.
        r   param   )onnxruntimeZtensorrtZothersr_   r   )opset_versiondeploy_backendzz[Convertion error] {}.
 Please open an issue at https://github.com/PaddlePaddle/Paddle2ONNX/issues to report your problem.NzA[Convertion error] Please check your input model and param files."bos://{}/paddle2onnx/{}/model.onnxFr&   z<Exception: Write file {}/model.onnx to bos failed, due to {}r(   )r   r)   r*   r+   intr   r,   r-   r.   r/   r4   r5   r6   r7   paddle2onnxZexportr<   r9   r=   r   r   rC   rD   rE   rF   rI   )r   r`   ra   rJ   Zparams_handleZ
model_dataZ
param_datar   rL   rM   rS   Zparam_fpZ
onnx_modelrP   rR   rT   r   r   r   paddle2onnx_convert   s   










$z#ModelConvertApi.paddle2onnx_convertc                 C   rV   )z9
        Download converted onnx model from bos.
        rb   NrW   rX   rZ   r   r   r   paddle2onnx_download   r\   z$ModelConvertApi.paddle2onnx_downloadN)
__name__
__module____qualname____doc__r   r   rU   r[   re   rf   r   r   r   r   r   %   s    
f

@r   c                     sN   t  } | jddgf| jdgf| jg df| jdgfd dtf fdd}|S )	Nr`   ra   r   r!   )zpaddle2onnx/convertzpaddle2onnx/downloadzonnx2paddle/convertzonnx2paddle/downloadr0   c                    sH    | }|sttddddd fS |\}} fdd|D }|| S )Nr   zapi not found)statusmsgzapplication/jsonc                    s   g | ]}  |qS r   )get).0r<   argsr   r   
<listcomp>  s    z?create_model_convert_api_call.<locals>.call.<locals>.<listcomp>)rm   jsondumpsr   )r0   rp   ZroutemethodZcall_arg_namesZ	call_argsZroutesro   r   call  s   
z+create_model_convert_api_call.<locals>.call)r   re   rf   rU   r[   str)apirv   r   ru   r   create_model_convert_api_call  s   
	ry   )rD   r,   rr   r   rG   r4   r:   rd   Zflaskr   Zx2paddle.convertr   Zxarfiler   Zvisualdl.io.bfiler   Zvisualdl.server.apir   r   Zvisualdl.utils.dirr	   Z_max_cache_numbersobjectr   ry   r   r   r   r   <module>   s&    b