o
    {qi                     @   s   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mZ dZd	d
 Zdd ZG dd de jdZG dd deZdS )    N)create_pipeline)load_pipeline_config)AttrDict)DependencyError   )CLISubcommandExecutor)add_common_cli_optsparse_common_argsprepare_common_init_argsc                 C   sZ   |   }| D ]"\}}||v r&t|| tr&t|tr&t|| |||< q|||< q|S N)copyitems
isinstancedict_merge_dicts)Zd1Zd2reskv r   `/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddleocr/_pipelines/base.pyr   !   s    
r   c                 C   sT   t | trdd |  D S t | trdd |  D S t | tr(dd | D S | S )Nc                 S      i | ]	\}}|t |qS r   _to_builtin.0r   r   r   r   r   
<dictcomp>-       z_to_builtin.<locals>.<dictcomp>c                 S   r   r   r   r   r   r   r   r   /   r   c                 S   s   g | ]}t |qS r   r   )r   itemr   r   r   
<listcomp>1   s    z_to_builtin.<locals>.<listcomp>)r   r   r   r   list)objr   r   r   r   +   s   


r   c                       sn   e Zd Zdd fdd
Zeejdd Zdd Zd	d
 Z	e
ejdd Zdd Zdd Zdd Z  ZS )PaddleXPipelineWrapperN)paddlex_configc                   s6   t    || _t|td| _|  | _|  | _	d S )N)default_enable_hpi)
super__init___paddlex_configr	   _DEFAULT_ENABLE_HPI_common_args_get_merged_paddlex_config_merged_paddlex_config_create_paddlex_pipelinepaddlex_pipeline)selfr"   Zcommon_args	__class__r   r   r%   7   s   

zPaddleXPipelineWrapper.__init__c                 C      t r   NotImplementedErrorr-   r   r   r   _paddlex_pipeline_nameE      z-PaddleXPipelineWrapper._paddlex_pipeline_namec                 C   sJ   t |ddd}t| j}t|| W d    d S 1 sw   Y  d S )Nwzutf-8)encoding)openr   r*   yamlZ	safe_dump)r-   Z	yaml_pathfconfigr   r   r   export_paddlex_config_to_yamlJ   s   
"z4PaddleXPipelineWrapper.export_paddlex_config_to_yamlc                 C   s   | j   d S r   )r,   closer3   r   r   r   r=   O   s   zPaddleXPipelineWrapper.closec                 C   r0   r   r1   )clsr   r   r   get_cli_subcommand_executorR   r5   z2PaddleXPipelineWrapper.get_cli_subcommand_executorc                 C   s   i S r   r   r3   r   r   r   _get_paddlex_config_overridesW   s   z4PaddleXPipelineWrapper._get_paddlex_config_overridesc                 C   sF   | j d u rt| j}nt| j trt| j }n| j }|  }t||S r   )r&   r   r4   r   strr@   r   )r-   r;   Z	overridesr   r   r   r)   Z   s   

z1PaddleXPipelineWrapper._get_merged_paddlex_configc              
   C   sF   t d | j}ztdd| ji|W S  ty" } ztd|d }~ww )Nr;   zA dependency error occurred during pipeline creation. Please refer to the installation documentation to ensure all required dependencies are installed.r   )r
   r(   r   r*   r   RuntimeError)r-   kwargser   r   r   r+   f   s   z/PaddleXPipelineWrapper._create_paddlex_pipeline)__name__
__module____qualname__r%   propertyabcabstractmethodr4   r<   r=   classmethodr?   r@   r)   r+   __classcell__r   r   r.   r   r!   6   s    r!   )	metaclassc                   @   s4   e Zd Zeejdd Zdd Zejdd ZdS )PipelineCLISubcommandExecutorc                 C   r0   r   r1   r3   r   r   r   subparser_nameq   r5   z,PipelineCLISubcommandExecutor.subparser_namec                 C   s:   |j | jd}| | t|tdd |jdtdd |S )N)nameT)r#   Zallow_multiple_devicesz--paddlex_configz,Path to PaddleX pipeline configuration file.)typehelp)
add_parserrO   _update_subparserr   r'   add_argumentrA   )r-   Z
subparsers	subparserr   r   r   add_subparserv   s   
z+PipelineCLISubcommandExecutor.add_subparserc                 C   r0   r   r1   )r-   rV   r   r   r   rT      s   z/PipelineCLISubcommandExecutor._update_subparserN)	rE   rF   rG   rH   rI   rJ   rO   rW   rT   r   r   r   r   rN   p   s    rN   )rI   r9   Zpaddlexr   Zpaddlex.inferencer   Zpaddlex.utils.configr   Zpaddlex.utils.depsr   Z	_abstractr   r(   r   r	   r
   r'   r   r   ABCMetar!   rN   r   r   r   r   <module>   s   
: