
    ~j{[                        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mZ dd	lmZ dd
lmZmZmZmZmZmZmZ ddlm Z m!Z!m"Z" ddl#m$Z$m%Z% ddl&m'Z' ddl(m)Z) d Z*d Z+d Z,d Z-d Z.d Z/dS )    N)Path   )create_pipeline)MODEL_FILE_PREFIX)get_model_paths)load_pipeline_config)get_all_supported_repo_namessetup)logging)get_dep_versionget_genai_dep_specsget_genai_fastdeploy_specget_paddle2onnx_dep_specsget_serving_dep_specsis_dep_availableis_paddle2onnx_plugin_available)get_gpu_compute_capabilityget_paddle_cuda_versionis_cuda_available)install_packagesuninstall_packages)interactive_get_pipeline)PIPELINE_ARGUMENTSc                  D   d } t          j        d          }|                    d          }|                    d          }|                    d          }|                    d          }|                    ddd	d
           |                    ddd           |                    dt          ddgdd           |                    ddddd           |                    dddd           |                    dt          dd d!"           |                    d#t          d$%           |                    d&t          d d'(           |                    d)t          d d*(           |                    d+t          d d,(           |                    d-t          d d.(           |                    d/dd0           |                    d1t
          j        d2%           |                    d3t          d d4(           |                    d5dd6           |                    d7t          d8d9(           |                    d:t          d;d<(           |                    d=dd>           |                    d?t          d@%           |                    dAt          dB%           |                    dCt          dDdE(           |                                \  }}|j	        }g }	|j
        5|j        s-|j        s%|"t          j                            |          rt!          |          dF         }
n|}
|
t"          vrUdG                    t#          j                              }t)          j        dH|
 dI| dJ           t-          j        dK           t"          |
         }	|	g }	|                    |
                                 dL          }|	D ]O}|                    |dM         |dN         t2          u r| n|dN         |                    dOdP|
 dQ          %           P||	fS )Rzparse cli argumentsc                 *    | dv rdS | dv rdS | dv rdS | S )zuconvert str type value
        to None type if it is "None",
        to bool type if it means True or False.
        )NonenoneNONEN)TRUETruetrueTtT)FALSEFalsefalseFfF )ss    c/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddlex/paddlex_cli.py	parse_strzargs_cfg.<locals>.parse_str4   s<    
 (((444447775    zCommand-line interface for PaddleX. Use the options below to install plugins, run pipeline predictions, or start the serving application.zInstall PaddleX OptionszPipeline Predict OptionszServing OptionszPaddle2ONNX Optionsz	--install*PLUGINz"Install specified PaddleX plugins.)nargsmetavarhelpz	--no_deps
store_truez>Install custom development plugins without their dependencies.)actionr2   z
--platformz
github.comz	gitee.comz7Platform to use for installation (default: github.com).)typechoicesdefaultr2   z-yz--yesupdate_reposz6Automatically confirm prompts and update repositories.)destr4   r2   z--use_local_reposFz%Use local repositories if they exist.)r4   r7   r2   z--deps_to_replace+Nz=Replace dependency version when installing from repositories.)r5   r0   r7   r2   z
--pipelinez/Name of the pipeline to execute for prediction.)r5   r2   z--inputzJInput data or path for the pipeline, supports specific file and directory.)r5   r7   r2   z--save_pathz$Path to save the prediction results.z--enginezInference engine.z--devicez5Device to run the pipeline on (e.g., 'cpu', 'gpu:0').z
--use_hpipz&Use high-performance inference plugin.z--hpi_configz)High-performance inference configuration.z--get_pipeline_configz4Retrieve the configuration for a specified pipeline.z--servez1Start the serving application to handle requests.z--hostz0.0.0.0z,Host address to serve on (default: 0.0.0.0).z--porti  z(Port number to serve on (default: 8080).z--paddle2onnxz*Convert PaddlePaddle model to ONNX format.z--paddle_model_dirz,Directory containing the PaddlePaddle model.z--onnx_model_dirz$Output directory for the ONNX model.--opset_version   z!Version of the ONNX opset to use.pipeline_namez, zUnsupported pipeline: z-, CLI predict only supports these pipelines: 
r   z Pipeline Optionsnamer5   r2   zArgument for z
 pipeline.)argparseArgumentParseradd_argument_groupadd_argumentstrastliteral_evalintparse_known_argspipelineinstallservepaddle2onnxospathisfiler   r   joinkeysr   errorsysexit
capitalizeboolget)r,   parserinstall_grouppipeline_groupserving_grouppaddle2onnx_groupargsremaining_argsrI   pipeline_argsr=   support_pipelinespipeline_specific_groupargs                 r+   args_cfgrc   1   s      $ 	T F --.GHHM../IJJN--.?@@M112GHH 1	     M    
 {+F     E     4	     L     3%V      Y	      3	       	      D	      5     
 8     
 C	      @    
 ;	     7	     ""9 #   
 ""; #   
 ""3 #   
 ""Q5X #   
 "2244D.}HM \%%t7G% 7>>(## 	%0::?KMM$M 222 $		*<*A*C*C D DMzzzevzzz   HQKKK*=9 M"(";";''))<<<#
 #
 ! 	 	C#00F"%f+"5"5YY3v;WWV%N]%N%N%NOO 1     =  r-   c                    d }d }d }d }dt           j        d<   dt           j        d<   | j        d	d	         }d
|v rK|                    d
           |r(t	          j        d           t          j        d            |             d	S d|v rK|                    d           |r(t	          j        d           t          j        d            |             d	S t          t          d |                    }|r|D ]}|                    |           |r(t	          j        d           t          j        d           t          |          dk    s,t          |d                             d                    dk    r(t	          j        d           t          j        d           |d                             d          d         } ||           d	S t          t          d |                    }	|	r]|	D ]}|                    |           |r(t	          j        d           t          j        d           d |	D             }
 ||
           d	S t                      }g }|D ]}||vr|                    |           |r)t	          j        d|           t          j        d           |r|}nt          |          dk    r|}t          || j        | j        | j        | j        | j                   d	S )zinstall paddlexc                      	 t          t                                 n:# t          $ r- t          j        dd           t          j        d           Y nw xY wt          j        d           d S )NInstallation failedTexc_infor   z)Successfully installed the serving plugin)r   r   	Exceptionr   rR   rS   rT   infor)   r-   r+   _install_serving_depsz&install.<locals>._install_serving_deps   sz    	2445555 	 	 	M/$????HQKKKKK	 	@AAAAA    4AAc                      	 t          t                                 n:# t          $ r- t          j        dd           t          j        d           Y nw xY wt          j        d           d S )Nrf   Trg   r   z-Successfully installed the Paddle2ONNX plugin)r   r   ri   r   rR   rS   rT   rj   r)   r-   r+   _install_paddle2onnx_depsz*install.<locals>._install_paddle2onnx_deps   sz    	6889999 	 	 	M/$????HQKKKKK	 	DEEEEErl   c                 6   g d}| |vr*t          j        d||            t          j        d           d}| dk    rSt	                      }|st          j        d           |d         dk    rd	}n |d         d
k    rt          j        d           dddd}||          }t          |                                          |hz
  }|D ]}t          |          }|mt          j        d| d| d| d| d	           	 t          |g           F# t          $ r- t          j        dd           t          j        d           Y |w xY wt          j                            d|          5 }	t          |          }	 |"t          |gdt          |	          g           nvt!          dt#          |           d          }
|
                                dv r2t          |g           t          |gdt          |	          g           n	 d d d            d S n:# t          $ r- t          j        dd           t          j        d           Y nw xY wd d d            n# 1 swxY w Y   t          j        d           t'                      st          j        d            d S d S )!N)cpugpunpuzgFailed to install the high-performance plugin.
Supported device types: %s. Your input device type: %s.
   zhpip_links.htmlrq   zZNo CUDA version found. Please make sure you have installed PaddlePaddle with CUDA enabled.r      zhpip_links_cu12.html   zcCurrently, only CUDA versions 11.x and 12.x are supported by the high-performance inference plugin.zultra-infer-pythonzultra-infer-gpu-pythonzultra-infer-npu-pythonz'The high-performance inference plugin 'z' is mutually exclusive with 'z' (version z installed). Uninstalling 'z'...zFailed to uninstall packagesTrg   r   paddlexz--find-linkspip_install_optszDThe high-performance inference plugin is already installed (version z&). Do you want to reinstall it? (y/n):)yyesrf   z<Successfully installed the high-performance inference pluginzThe Paddle2ONNX plugin is not available. It is recommended to run `paddlex --install paddle2onnx` to install the Paddle2ONNX plugin to use the full functionality of high-performance inference.)r   rR   rS   rT   r   setvaluesr   rj   r   ri   	importlib	resourcesrN   r   rD   inputreprlowerr   )device_typeSUPPORTED_DEVICE_TYPEShpip_links_filecuda_versionpackage_mappingpackageother_packagesother_packageversionr(   responses              r+   _install_hpi_depsz"install.<locals>._install_hpi_deps   s   !6!6!6444ML&	   HQKKK+%244L p   A"$$"8aB&&y  
 (++
 

 "+._335566'B+ 
	  
	 M%m44G" wg  w  wer  w  w  @G  w  w  dq  w  w  w   &7777       M"@4PPPPHQKKKKK  #  %%iAA 	Q%g..G?$ 	^SVV4L      % U_cdk_l_l  U  U  U   H  ~~''<77*G9555($I . #A.     +	 	 	 	 	 	 	 	,    3dCCCC-	 	 	 	 	 	 	 	 	 	 	 	 	 	 	4 	STTT.00 	L S    	 	sI   8D		4E ?E $I5BHI4IIIIIIc                    g }g }| D ]1}d|v r|                     |           |                     |           2|rt          d          st          j        d           t	                      }|dv rd}n.|dv rd}n't          j        d|d	          d
|d          d           	 t          t          d          gd|g           n:# t          $ r- t          j	        dd           t          j        d           Y nw xY wg }|D ]Q}	 t          |          }n9# t          $ r, t          j	        d|           t          j        d           Y nw xY w||z  }R	 t          |d           n:# t          $ r- t          j	        dd           t          j        d           Y nw xY w| D ]}d|v sd|v rt          dgd           t                      r	 t          dgd           t	                      }|J |dk    rt          dgd           nt          dgd           n:# t          $ r- t          j	        dd           t          j        d           Y nw xY w nt          j        dt          |           dk    rdnd z              d S )!N
fastdeploypaddlepaddlez"Please install PaddlePaddle first.))   r   )	   r   zEhttps://www.paddlepaddle.org.cn/packages/stable/fastdeploy-gpu-80_90/))r      )r   r   zEhttps://www.paddlepaddle.org.cn/packages/stable/fastdeploy-gpu-86_89/z%The compute capability of the GPU is r   .r   zX, which is not supported. The supported compute capabilities are 8.0, 8.6, 8.9, and 9.0.rq   z--extra-index-urlrw   rf   Trg   z%Invalid generative AI plugin type: %srs   required)constraintsvllmsglangxformerswheel)rt   r   zflash-attn == 2.8.3zflash-attn == 2.8.2z/Successfully installed the generative AI pluginr*    )appendr   rS   rT   r   r   r   ri   r   rR   r   
ValueErrorr   rj   len)plugin_typesfd_plugin_typesnot_fd_plugin_typesplugin_typecap	index_urlreqsrs           r+   _install_genai_depsz$install.<locals>._install_genai_depsL  s    ' 	8 	8K{**&&{3333#**;7777 	#N33 ?=>>>,..C&&&c		(((c		 fCF  f  fSV  f  f  f   .u556&99%E        3dCCCC . 	 	K'44   E{SSS AIDD	Tz::::: 	 	 	M/$????HQKKKKK	 ( 	 	K$$K(?(? *:FFFF$&& $('
KKKK8::"'>>,!6 7Z     -!6 7Z    % $ $ $&;dKKKK$ E=,''!++ss5	
 	
 	
 	
 	
sI   "!C 4C;:C;D3E
EE' '4FFAH4IIr    PADDLE_PDX_DEBUGr%   PADDLE_PDX_EAGER_INITNservingz5`serving` cannot be used together with other plugins.rs   rL   z9`paddle2onnx` cannot be used together with other plugins.c                 ,    |                      d          S )Nzhpi-
startswithr?   s    r+   <lambda>zinstall.<locals>.<lambda>  s    4??6+B+B r-   z5`hpi-xxx` cannot be used together with other plugins.r   r   -zInvalid HPI plugin installation format detected.
Correct format: paddlex --install hpi-<device_type>
Example: paddlex --install hpi-gpu)r   c                 ,    |                      d          S )Ngenai-r   r   s    r+   r   zinstall.<locals>.<lambda>  s    T__X-F-F r-   z7`genai-xxx` cannot be used together with other plugins.c                 <    g | ]}|t          d           d         S )r   N)r   ).0ps     r+   
<listcomp>zinstall.<locals>.<listcomp>  s'    HHHQaH0HHHr-   zUnknown plugins: %s)
repo_namesno_depsplatformr8   use_local_reposdeps_to_replace)rM   environrJ   remover   rR   rS   rT   listfilterr   splitr	   r   r
   r   r   r8   r   r   )r]   rk   rn   r   r   pluginshpi_pluginsr   r   genai_pluginsgenai_plugin_typesall_repo_namesunknown_pluginsr   s                 r+   rJ   rJ      s   B B BF F FK K KZD
 D
 D
N &,BJ!"*1BJ&'l111oGGy!!! 	MQRRRHQKKK}%%% 	MUVVVHQKKK!!###vBBGLLMMK  	 	ANN1 	MQRRRHQKKK{a3{1~';';C'@'@#A#AQ#F#FM5  
 HQKKK!!n**3//2k2222 F FPPQQM  	 	ANN1 	MSTTTHQKKKHH-HHH.///133NO & &N""""1%%% +_=== $

	W		#
	&,,    Fr-   c                     t          | ||||          }  | j        |fi |}|D ].}	|	                                 |r|	                    |           /dS )zpipeline predict)rI   enginedeviceuse_hpip
hpi_config)	save_pathN)r   predictprintsave_all)
rI   r   r   r   r   r   r   r_   resultress
             r+   pipeline_predictr     s       H Xe55}55F . .		 	.LL9L---. .r-   c                   	 ddl m}m} n:# t          $ r- t	          j        dd           t          j        d           Y nw xY wt          |           }		 t          |	||||          } n:# t          $ r- t	          j        dd           t          j        d           Y nw xY w || |	          }
 ||
||           d S )	Nr   )create_pipeline_app
run_serverz!Failed to load the serving moduleTrg   )configr   r   r   r   zFailed to create the pipeline)hostport)inference.serving.basic_servingr   r   RuntimeErrorr   rR   rS   rT   r   r   ri   )rI   r   r   r   r   r   r   r   r   pipeline_configapps              r+   rK   rK     s   TTTTTTTTT   9DIIII +844O
""!
 
 
    5EEEE 
h
8
8CJsD))))))s!    4AAA* *4B! B!c                f  	
 t                      st          j        d           t           d
t           d	dg	fd}d }
fd}	fd}fd	}| st          j        d
           t	          |           } |s| }t	          |          }t          j        d|             t          j        d|             ||             |              || ||           |                                r|                    |           s || |            || |           t          j        d           d S )Nz,Please install the Paddle2ONNX plugin first.z.onnxz.ymlz
scaler.pklc                    | t          j        d           |                                 st          j        |  d           |                                 st          j        |  d           t	          |           }d|vrt          j        d           | z  }|                                st          j        | d           d S d S )Nz!Input directory must be specifiedz does not existz is not a directorypaddlez!PaddlePaddle model does not exist)rS   rT   existsis_dirr   )	input_dirmodel_pathsconfig_pathCONFIG_FILENAMEs      r+   _check_input_dirz(paddle_to_onnx.<locals>._check_input_dir  s    H8999!! 	4H	222333!! 	8H	666777%i00;&&H8999/1!!## 	6H44455555	6 	6r-   c                  Z    t          j        d          t          j        d           d S d S )NrL   z>Paddle2ONNX is not available. Please install the plugin first.)shutilwhichrS   rT   r)   r-   r+   _check_paddle2onnxz*paddle_to_onnx.<locals>._check_paddle2onnx$  s1    <&&.HUVVVVV /.r-   c                    t          |           }t          j        d           ddt          |d         d         j                  dt          |d         d         j                  dt          |d         d         j                  d	t          |z            d
t          |          g}	 t          j        |           n8# t          j        $ r&}t          j
        d|j                    Y d }~nd }~ww xY wt          j        d           d S )Nz"Paddle2ONNX conversion starting...rL   z--model_dirr   r   z--model_filenamez--params_filenamer   z--save_filer;   z-Paddle2ONNX conversion failed with exit code z Paddle2ONNX conversion succeeded)r   r   rj   rD   parentr?   
subprocess
check_callCalledProcessErrorrS   rT   
returncode)r   
output_diropset_versionr   cmdeONNX_MODEL_FILENAMEs         r+   _run_paddle2onnxz(paddle_to_onnx.<locals>._run_paddle2onnx(  s!   %i009::: H%a(/00H%a(-..H%a(-..
0011
	U!#&&&&, 	U 	U 	UHSQ\SSTTTTTTTT	U788888s   *B? ?C4C//C4c                 z    | z  }|z  }t          j        ||           t          j        d| d|            d S NzCopied z to )r   copyr   rj   )r   r   src_pathdst_pathr   s       r+   _copy_config_filez)paddle_to_onnx.<locals>._copy_config_file?  sP    ./Hh'''7x77X7788888r-   c                     D ]P}| |z  }|                                 s||z  }t          j        ||           t          j        d| d|            Qd S r   )r   r   r   r   rj   )r   r   filenamer   r   ADDITIONAL_FILENAMESs        r+   _copy_additional_filesz.paddle_to_onnx.<locals>._copy_additional_filesE  s{    , 	= 	=H 8+H??$$ !H,HK(+++L;8;;;;<<<<	= 	=r-   z.PaddlePaddle model directory must be specifiedzInput dir: zOutput dir: Done)	r   rS   rT   r   r   r   rj   r   samefile)paddle_model_dironnx_model_dirr   r   r   r   r   r   r   r   r   s           @@@r+   paddle_to_onnxr    s   *,, A?@@@.555*000O(>6 6 6 6 6W W W9 9 9 9 9.9 9 9 9 9= = = = =  CABBB,-- *).))NL1/11222L000111%&&&%~}EEE!!## A(?(?@P(Q(Q A*N;;;/@@@Lr-   c            	         t                      \  } }|                                 }t          t          j                  dk    r<t          j        d           |                                  t          j        d           |j	        t          |           dS |j
        r=t          |j        |j        |j        |j        pd|j        |j        |j                   dS |j        r#t'          |j        |j        |j                   dS |j        t1          |j        |j                   dS i }|D ]Y}|d                             d          }t7          ||          rt9          ||          ||<   At          j        d	| d
           Z	 t;          |j        |j        |j        |j        |j        f|j        pd|j        d| dS # t>          $ r. t          j         dd           t          j        d           Y dS w xY w)zAPI for command liner   z3No arguments provided. Displaying help information:rs   N)r   r   r   r   r   r   )r   r?   r   z	Argument z is missing in args)r   r   zPipeline prediction failedTrg   )!rc   
parse_argsr   rS   argvr   warning
print_helprT   rJ   rK   rI   r   r   r   r   r   r   rL   r  r  r  r   get_pipeline_configr   r   lstriphasattrgetattrr   r   ri   rR   )rX   r_   r]   pipeline_args_dictrb   arg_names         r+   mainr  a  sQ   $JJFMD
38}}MNNN|	 )M;;]*d	
 	
 	
 	
 	
 	
 
	 !,	
 	
 	
 	
 	
 	
 #/$T%=t~NNNNN!#$ O Ov;--c224** O3:43J3J&x00O$M$M$M$MNNNN MJKKN	 "]2d#	 	 )	 	 	 	 	    :TJJJJs   9G 4G:9G:)0r@   rE   importlib.resourcesr}   rM   r   r   rS   pathlibr   r   r   	constantsr   "inference.models.utils.model_pathsr   inference.pipelinesr   repo_managerr	   r
   utilsr   
utils.depsr   r   r   r   r   r   r   	utils.envr   r   r   utils.installr   r   utils.interactive_get_pipeliner   utils.pipeline_argumentsr   rc   rJ   r   rK   r  r  r)   r-   r+   <module>r     s    



     				      



             ( ( ( ( ( ( ? ? ? ? ? ? 5 5 5 5 5 5 = = = = = = = =                                
 @ ? ? ? ? ? ? ? D D D D D D 8 8 8 8 8 8x! x! x!vn n nb. . .2* * *0O O Of5 5 5 5 5r-   