o
    yqi)                     @   s  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m	Z	 d dl
mZ d dlmZ ddlmZ edZh d	Zg d
ZG dd deZdd Zdd Ze ZdFddZe ZeddZdd Ze dFddZddddZdd Zdd  Z e d!d" Z!ddd#d$d%Z"dd&d'd(Z#d)d* Z$d+d, Z%d-d. Z&d/d0 Z'd1d2 Z(d3d4 Z)d5d6 Z*d7d8 Z+dGd:d;Z,dGd<d=Z-d>d? Z.d@dA Z/dBdC Z0dDdE Z1dS )H    N)defaultdict)	lru_cachewraps)Requirement)Version   )loggingzG(?:;|and)*[ \t]*extra[ \t]*==[ \t]*['\"]([a-z0-9]+(?:-[a-z0-9]+)*)['\"]>   pluginsbaseall)zfastdeploy-serverzvllm-serverzsglang-serverc                   @   s   e Zd ZdS )DependencyErrorN)__name__
__module____qualname__ r   r   Y/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddlex/utils/deps.pyr   #   s    r   c                 C   s@   t | }|r|d| d |  | | d   fS d | fS )Nr   )_EXTRA_PATTERNsearchgroupstartend)dep_specmr   r   r   '_get_extra_name_and_remove_extra_marker'   s   
*r   c                  C   s   t jd} i }| dg D ]}|tvrtt||< qt jdD ]*}t|\}}|d urK|tvrK| }t	|}||v sAJ ||| |j
 | q!|S )NpaddlexzProvides-Extra)	importlibmetadataget_all_COLLECTIVE_EXTRA_NAMESr   listrequiresr   rstripr   nameappend)r   extrasr"   r   
extra_namereqr   r   r   _get_extras0   s   r'   Fc                 C   sj   t t}tjdD ](}t|\}}| r|d u s"| s2|d u s"|dkr2| }t|}||j 	| q
|S )Nr   r
   )
r   r   r   r   r    r   r!   r   r"   r#   )required_only	dep_specsr   r%   r&   r   r   r   _get_base_dep_specsD   s   r*   T)r(   c                 C   s(   zt j| W S  t jjy   Y d S w N)r   r   versionZPackageNotFoundError)depr   r   r   get_dep_versionU   s
   r.   c                C   s   | dv r
|r
t d| dkrtjdd uS | dkr"tjdd uS | dkr.tjdd uS | d	kr:tjd	d uS t| }|d u rDd
S |rc| tvrNt dt|  D ]}t|t|jv r` dS qRd S dS )N)paddlepaddlepaddle-custom-deviceultra-infer
fastdeployzx`check_version` is not allowed to be `True` for `paddlepaddle`, `paddle-custom-device`, `ultra-infer`, and `fastdeploy`.r/   Zpaddler0   Zpaddle_custom_devicer1   Zultra_inferr2   FzHCurrently, `check_version=True` is supported only for base dependencies.T)	
ValueErrorr   util	find_specr.   BASE_DEP_SPECSr   r   	specifier)r-   Zcheck_versionr,   r   r   r   r   is_dep_available\   s8   r8   )obj_namec                 G   sR   dd |D }t |dkr'| d urd|  d}nd}|dd| 7 }t|d S )	Nc                 S   s   g | ]}t |s|qS r   r8   .0r-   r   r   r   
<listcomp>~       z require_deps.<locals>.<listcomp>r   `z#` is not ready for use, because theZThez+ following dependencies are not available:

)lenjoinr   )r9   depsZunavailable_depsmsgr   r   r   require_deps}   s   rE   c                         fdd}|S )Nc                    s"   t   fdd}t _|S )Nc                     s    t  jd ji  | i |S Nr9   rE   _deps_r   )argskwargsfuncr   r   _wrapper   s   z7function_requires_deps.<locals>._deco.<locals>._wrapper)r   setrI   )rM   rN   rC   rL   r   _deco   s   
z%function_requires_deps.<locals>._decor   rC   rQ   r   rP   r   function_requires_deps   s   	rS   c                     rF   )Nc                    s~   t  j fdd}t _t dd D ]}t|dr( j|j qd jv r2 jn fdd}|| _ S )	Nc                    s(   t  jd ji | g|R i |S rG   rH   selfrJ   rK   clsold_init_funcr   r   rN      s   z4class_requires_deps.<locals>._deco.<locals>._wrapperr   rI   __init__c                    s   t  | j|i |S r+   )superrZ   rT   )rW   r   r   _forward   s   z4class_requires_deps.<locals>._deco.<locals>._forward)	r   rZ   rO   rI   inspectgetmrohasattrupdate__dict__)rW   rN   Zbase_clsr\   rP   rV   r   rQ      s   


z"class_requires_deps.<locals>._decor   rR   r   rP   r   class_requires_deps   s   rb   c                 C   sB   dd t |  D }t|rdS tddd tt |  |D  dS )Nc                 S   s   g | ]}t |qS r   r:   r;   r   r   r   r=      s    z&is_extra_available.<locals>.<listcomp>Tz(These dependencies are not available: %sc                 S   s   g | ]\}}|s|qS r   r   )r<   dfr   r   r   r=      r>   F)EXTRASr   r   debugzip)extraflagsr   r   r   is_extra_available   s   rj   r9   altc                C   sl   t | s|d urt |rd S |d urd| d}nd}|d|  d|  d7 }|d ur2|d| d7 }t|)	Nr?   z#` requires additional dependencies.z%Additional dependencies are required.z, To install them, run `pip install "paddlex[ul   ]==<PADDLEX_VERSION>"` if you’re installing `paddlex` from an index, or `pip install -e "/path/to/PaddleX[u-   ]"` if you’re installing `paddlex` locally.z) Alternatively, you can install `paddlex[z]` instead.)rj   r   )rh   r9   rl   rD   r   r   r   require_extra   s   rm   )rl   c                   s    fdd}|S )Nc                    sX   t | j fdd}| j | jttr'tdks#J d || _| S )Nc                    s$   t  d | g|R i |S )Nrk   )rm   rT   )rl   rh   rX   pipeline_namer   r   rN      s   z8pipeline_requires_extra.<locals>._deco.<locals>._wrapperr   r   )r   rZ   entities
isinstancer   rA   )Zpipeline_clsrN   rl   rh   )rX   rn   r   rQ      s   
z&pipeline_requires_extra.<locals>._decor   )rh   rl   rQ   r   rq   r   pipeline_requires_extra   s   rr   c                   C      t dS )Nr1   r:   r   r   r   r   is_hpip_available      rt   c                   C      t  stdd S )NzSThe high-performance inference plugin is not available. Please install it properly.)rt   r   r   r   r   r   require_hpip   
   rw   c                   C   rs   NZservingrj   r   r   r   r   is_serving_plugin_available   ru   r{   c                   C   rv   )Nz@The serving plugin is not available. Please install it properly.)r{   r   r   r   r   r   require_serving_plugin   rx   r|   c                  C   "   g } t d  D ]}| |7 } q| S ry   re   valuesr)   itemr   r   r   get_serving_dep_specs      
r   c                   C   rs   NZpaddle2onnxr:   r   r   r   r   is_paddle2onnx_plugin_available   ru   r   c                   C   rv   )NzDThe Paddle2ONNX plugin is not available. Please install it properly.)r   r   r   r   r   r   require_paddle2onnx_plugin   rx   r   c                  C   r}   r   r~   r   r   r   r   get_paddle2onnx_dep_specs  r   r   anyc                 C   s   | dkr| t vrtd|  | dkr!t D ]	}t|r dS qdS d| v r)tdS td|  rCddlm} | rAtd	o@td
S dS dS )Nr   zUnknown backend type: TFr2   genai-r   )is_cuda_availableZxformersz
flash-attn) _SUPPORTED_GENAI_ENGINE_BACKENDSr3    is_genai_engine_plugin_availabler8   rj   envr   )backendber   r   r   r   r   
  s    r   c                 C   s8   t | s| dkrd}ndt|  d}t| dd S )Nr   z$The generative AI engine plugins arezThe generative AI z engine plugin isz+ not available. Please install it properly.)r   reprRuntimeError)r   prefixr   r   r   require_genai_engine_plugin  s   r   c                   C   rs   )Nzgenai-clientrz   r   r   r   r    is_genai_client_plugin_available'  ru   r   c                   C   rv   )NzMThe generative AI client plugin is not available. Please install it properly.)r   r   r   r   r   r   require_genai_client_plugin+  rx   r   c                 C   s*   d}| |vrt d|  | dkrdS t)N)gpuzUnsupported device type: r   zfastdeploy-gpu == 2.3.0rc0)r3   AssertionError)Zdevice_typeZSUPPORTED_DEVICE_TYPESr   r   r   get_genai_fastdeploy_spec2  s   r   c                 C   s`   | dkr| t vrtd|  d| v rtt|  dg }td|    D ]}||7 }q'|S )NclientzInvalid type: r2   z is not supportedr   )r   r3   r   re   r   )typer)   r   r   r   r   get_genai_dep_specs<  s   
r   )F)r   )2importlib.metadatar   importlib.utilr]   recollectionsr   	functoolsr   r   Zpackaging.requirementsr   Zpackaging.versionr    r   compiler   r   r   	Exceptionr   r   r'   re   r*   r6   ZREQUIRED_DEP_SPECSr.   r8   rE   rS   rb   rj   rm   rr   rt   rw   r{   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sZ   	

 


	
