o
    1jJ*                     @  s  d dl m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
mZmZmZmZmZmZmZmZmZ d dlZd dlmZmZ ddlmZ dd	l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'm(Z( edrzd dl)Z)edrd dl*Z*d dl+m,Z, d dl-m.Z. d dl/m0Z0 edrd dl1m2Z2 ede!dZ3edZ4edZ5G dd deZ6d<d"d#Z7edG d$d% d%ee3 Z8edG d&d' d'ee3 Z9edddd(d)d=d0d1Z:edd>d:d;Z;dS )?    )annotationsN)Queue)Thread)
AnyAsyncGeneratorCallableDictGenericListOptionalTuple	TypedDictTypeVar)	ParamSpec	TypeGuard   )logging)class_requires_depsfunction_requires_depsis_dep_available)SERVING_SERIAL_PIPELINE_CALLS   )BasePipeline   )	AppConfig)AIStudioNoResultResponse)
call_asyncgenerate_log_idaiohttpfastapi)jsonable_encoder)RequestValidationError)JSONResponseZ	starlette)HTTPException	PipelineT)boundPRc                   @  s   e Zd ZU ded< dS )_ErrorstrerrorN)__name__
__module____qualname____annotations__ r/   r/   m/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/serving/basic_serving/_app.pyr(   =   s   
 r(   objobjectreturnTypeGuard[_Error]c                 C  s&   t | to|  dhkot | d tS )Nr*   )
isinstancedictkeysr)   )r1   r/   r/   r0   	_is_errorA   s
   
r8   c                      sP   e Zd Zd fddZedddZdddZdddZdd Zdd Z	  Z
S )PipelineWrapperpipeliner$   r3   Nonec                   sn   t    || _t| _| jrt nd | _d| _t	 | _
| jr&t| jddnd | _| jd ur5| j  d S d S )NF)targetdaemon)super__init__	_pipeliner   _serialize_callsr   _queue_closedasyncioZget_running_loop_loopr   _worker_threadstart)selfr:   	__class__r/   r0   r?   P   s   


zPipelineWrapper.__init__c                 C     | j S Nr@   rI   r/   r/   r0   r:   ^      zPipelineWrapper.pipelineargsr   kwargs	List[Any]c                   s.   d fdd} j |g|R i |I d H S )Nr3   rS   c                    sp   g }t  jj| i | }|D ]}t|r tjd|d d|| qW d    |S 1 s1w   Y  |S )N  r*   )status_codedetail)
contextlibclosingr@   Zpredictr8   r   r#   append)rQ   rR   outputititemrO   r/   r0   _inferc   s   
z%PipelineWrapper.infer.<locals>._infer)r3   rS   )call)rI   rQ   rR   r]   r/   rO   r0   inferb   s   zPipelineWrapper.inferfuncCallable[P, R]P.argsP.kwargsr'   c                   sh   | j rtd| jst|g|R i |I d H S | j }| jd us%J | j||||f |I d H S )Nz)`PipelineWrapper` has already been closed)rC   RuntimeErrorrA   r   rE   Zcreate_futurerB   put)rI   r`   rQ   rR   futr/   r/   r0   r^   q   s   

zPipelineWrapper.callc                   sL   | j s$| jd ur| jd usJ | jd  t| jjI d H  d| _ d S d S )NT)rC   rG   rB   re   r   joinrO   r/   r/   r0   close{   s   

zPipelineWrapper.closec              
   C  s   | j d usJ | jsZ| j  }|d u rd S |\}}}}z3z||i |}| j|j| W n tyG } z| j|j| W Y d }~nd }~ww W | j   n| j   w | jr
d S d S rM   )	rB   rC   getrE   Zcall_soon_threadsafeZ
set_result	ExceptionZset_exception	task_done)rI   r\   r`   rQ   rR   rf   resulter/   r/   r0   rF      s    
zPipelineWrapper._worker)r:   r$   r3   r;   )r3   r$   )rQ   r   rR   r   r3   rS   )r`   ra   rQ   rb   rR   rc   r3   r'   )r+   r,   r-   r?   propertyr:   r_   r^   rh   rF   __classcell__r/   r/   rJ   r0   r9   N   s    


r9   c                      sh   e Zd Zd fddZedddZedd
dZejdddZedddZejdddZ  Z	S )
AppContextconfigr   r3   r;   c                  s&   t    || _i | _d | _d | _d S rM   )r>   r?   _configextrar@   _aiohttp_session)rI   rq   rJ   r/   r0   r?      s
   

zAppContext.__init__c                 C  rL   rM   )rr   rO   r/   r/   r0   rq      rP   zAppContext.configPipelineWrapper[PipelineT]c                 C     | j std| j S )Nz`pipeline` has not been set.)r@   AttributeErrorrO   r/   r/   r0   r:         zAppContext.pipelinevalc                 C  
   || _ d S rM   rN   rI   ry   r/   r/   r0   r:         
'aiohttp.ClientSession'c                 C  rv   )Nz#`aiohttp_session` has not been set.)rt   rw   rO   r/   r/   r0   aiohttp_session   rx   zAppContext.aiohttp_sessionc                 C  rz   rM   )rt   r{   r/   r/   r0   r~      r|   )rq   r   r3   r;   )r3   r   )r3   ru   )ry   ru   r3   r;   )r3   r}   )ry   r}   r3   r;   )
r+   r,   r-   r?   rn   rq   r:   setterr~   ro   r/   r/   rJ   r0   rp      s    rp   T)app_aiohttp_sessionr:   
app_configr   r   bool/Tuple['fastapi.FastAPI', AppContext[PipelineT]]c           	        s   t jd)fdd}tj|d}tt |d|j_|jd	d
dd*dd}d+ddd,ddd-fdd |	t
d. fd!d"}|	td/fd$d%}|	td0fd'd(}|fS )1Napp'fastapi.FastAPI'r3   AsyncGenerator[None, None]c              	    s   t t _zD r4tjt d4 I d H }|_d V  W d   I d H  n1 I d H s.w   Y  nd V  W j I d H  d S W j I d H  d S j I d H  w )N)Z
cookie_jar)r9   r$   r:   r   ZClientSessionZDummyCookieJarr~   rh   )r   r~   )r   ctxr:   r/   r0   _app_lifespan   s    (& z!create_app.<locals>._app_lifespan)Zlifespan)rq   z/healthZcheckHealth)operation_idr   c                     s   t t dddS )Nr   ZHealthylogIdZ	errorCodeZerrorMsg)r   r   r/   r/   r/   r0   _check_health   s   z!create_app.<locals>._check_healthrequestfastapi.RequestOptional[str]c                   sH   z	|   I d H }W n
 ty   Y d S w t|tr"d|v r"|d S d S )Nr   )jsonrj   r5   r6   )r   bodyr/   r/   r0   _try_get_log_id   s   z#create_app.<locals>._try_get_log_idlocTuple[str | int, ...]r)   c                 S  s`   d}t | D ]'\}}t|tr|dkr|d7 }||7 }qt|tr*|d| d7 }qtd|S )N r   .[]zUnexpected type)	enumerater5   r)   int	TypeError)r   pathixr/   r/   r0   _loc_to_dot_sep   s   


z#create_app.<locals>._loc_to_dot_sepvalidation_error1pydantic.ValidationError | RequestValidationErrorList[Dict[str, Any]]c                   s<   g }|   D ]}|d  |d |d d}|| q|S )Ntyper   msg)r   r   r   )errorsrY   )r   Zconverted_errorsr*   Zconverted_error)r   r/   r0   _convert_validation_errors   s   
z.create_app.<locals>._convert_validation_errorsexcr!   r"   c                   sB   | I d H p
t  } |}tt|dt|d}t|ddS )N  r   contentrU   )r   r    r   r   dumpsr"   )r   r   log_idr   json_compatible_data)r   r   r/   r0   _validation_exception_handler   s   z1create_app.<locals>._validation_exception_handlerr#   c                   s:    | I d H p
t  }tt||j|jd}t||jdS )Nr   r   )r   r    r   rU   rV   r"   r   r   r   r   r   r/   r0   _http_exception_handler  s   
z+create_app.<locals>._http_exception_handlerrj   c                   s>    | I d H p
t  }td tt|ddd}t|ddS )NzUnhandled exceptionrT   zInternal server errorr   r   )r   r   	exceptionr    r   r"   r   r   r/   r0   _unexpected_exception_handler  s   
z1create_app.<locals>._unexpected_exception_handler)r   r   r3   r   )r3   r   )r   r   r3   r   )r   r   r3   r)   )r   r   r3   r   )r   r   r   r!   r3   r"   )r   r   r   r#   r3   r"   )r   r   r   rj   r3   r"   )rW   asynccontextmanagerr   ZFastAPIrp   r$   statecontextri   Zexception_handlerr!   r#   rj   )	r:   r   r   r   r   r   r   r   r   r/   )r   r   r   r   r   r:   r0   
create_app   s"   

r   r   r   r   r)   r   rR   r   r   c                 K  s(   | j |f|dtidtiddd|S )Nmodel)r   rT   T)r   	responsesZresponse_model_exclude_none)postr   )r   r   r   rR   r/   r/   r0   primary_operation*  s   r   )r1   r2   r3   r4   )r:   r$   r   r   r   r   r3   r   )
r   r   r   r)   r   r)   rR   r   r3   r   )<
__future__r   rD   rW   r   queuer   	threadingr   typingr   r   r   r   r	   r
   r   r   r   r   ZpydanticZtyping_extensionsr   r   utilsr   Z
utils.depsr   r   r   Zutils.flagsr   Z	pipelinesr   Zinfra.configr   Zinfra.modelsr   Zinfra.utilsr   r   r   r   Zfastapi.encodersr    Zfastapi.exceptionsr!   Zfastapi.responsesr"   Zstarlette.exceptionsr#   r$   r&   r'   r(   r8   r9   rp   r   r   r/   r/   r/   r0   <module>   sN   0
D
!t