o
    0j9%                     @   s  d dl mZ d dlmZmZmZ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 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" 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/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z= ddl>m?Z? dd l@mAZA dd!lBmCZC dd"lDmEZE dd#lFmGZG dd$lHmIZImJZJ dd%lKmLZL dd&lMmNZN dd'lOmPZP dd(lQmRZR dd)lSmTZT dd*lUmVZV d+eWd,eWfd-d.ZXd/eWd,eeWef fd0d1ZY	2dDd2d2d2d2d2d2d2d3d/eeW d4eeeWef  d5eeW d6eeW d7eeeWef  d8ee d9eeZ d:eeeeWef ef  d;ed,efd<d=Z[d4ed,efd>d?Z\d4ed,efd@dAZ]d4ed,efdBdCZ^d2S )E    )Path)AnyDictOptionalUnion   )logging)parse_config   )	HPIConfigPaddlePredictorOption   )AnomalyDetectionPipeline)PedestrianAttributeRecPipelineVehicleAttributeRecPipeline)BasePipeline)BaseChatBaseGeneratePromptBaseRetriever)DocPreprocessorPipeline)DocUnderstandingPipeline)FaceRecPipeline)FormulaRecognitionPipeline)ImageClassificationPipeline)%ImageMultiLabelClassificationPipeline)InstanceSegmentationPipeline)KeypointDetectionPipeline)LayoutParsingPipeline)BEVDet3DPipeline)%MultilingualSpeechRecognitionPipeline)ObjectDetectionPipeline)OCRPipeline)OpenVocabularyDetectionPipeline)"OpenVocabularySegmentationPipeline)PaddleOCRVLPipeline)PP_ChatOCRv3_PipelinePP_ChatOCRv4_Pipeline)PP_DocTranslation_Pipeline)ShiTuV2Pipeline)RotatedObjectDetectionPipeline)SealRecognitionPipeline)SemanticSegmentationPipeline)SmallObjectDetectionPipeline)TableRecognitionPipelineTableRecognitionPipelineV2)TextToSpeechPipeline)TSAnomalyDetPipeline)TSClsPipeline)TSFcPipeline)VideoClassificationPipeline)VideoDetectionPipelinepipeline_namereturnc                 C   s4   t tjjjd |  d  }t | sdS |S )a  
    Get the full path of the pipeline configuration file based on the provided pipeline name.

    Args:
        pipeline_name (str): The name of the pipeline.

    Returns:
        str: The full path to the pipeline configuration file or None if not found.
    zconfigs/pipelines.yamlN)r   __file__parentresolveexists)r5   pipeline_path r=   e/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/__init__.pyget_pipeline_path<   s   r?   pipelinec                 C   sF   |  ds|  dst| }|du rtd|  dn| }t|}|S )a  
    Load the pipeline configuration.

    Args:
        pipeline (str): The name of the pipeline or the path to the config file.

    Returns:
        Dict[str, Any]: The parsed pipeline configuration.

    Raises:
        Exception: If the config file of pipeline does not exist.
    z.ymlr7   NzThe pipeline (zC) does not exist! Please use a pipeline name or a config file path!)endswithr?   	Exceptionr	   )r@   r<   configr=   r=   r>   load_pipeline_configP   s   
rD   NrC   deviceengineengine_config	pp_optionuse_hpip
hpi_configrC   rF   rG   rH   rI   rJ   rK   kwargsc          
   
   K   s  | du r|du rt d|du rt| }n| dur+|d | kr+td|d | |d  | }|d }	|du r>|dd}n|dd |du rO|dd}n|dd |du r`|dd}n||d< |du ro|d	d}n||d	< t|	d|||||||d
|} | S )a,  Create a pipeline instance from a pipeline name or config.


    Args:
        pipeline (Optional[str], optional): Pipeline name (e.g. "OCR", "object_detection") or
            path to a YAML config file. Required if `config` is None.
        config (Optional[Dict[str, Any]], optional): Pipeline configuration dict. If provided,
            `pipeline` name is optional and used only for validation. If both are provided
            and pipeline names differ, `config` takes precedence.
        device (Optional[str], optional): Device to run on (e.g. "gpu", "cpu", "npu:0").
        engine (Optional[str], optional): Inference engine for models in the pipeline.
        engine_config (Optional[Dict[str, Any]], optional): Engine-specific config.
        pp_option (Optional[PaddlePredictorOption], optional): Paddle predictor options.
        use_hpip (Optional[bool], optional): Whether to use HPIP.
        hpi_config (Optional[Union[Dict[str, Any], HPIConfig]], optional): HPIP configuration.

    Returns:
        BasePipeline: The created pipeline instance.
    Nz=Both `pipeline` and `config` cannot be None at the same time.r5   ziThe pipeline name in the config (%r) is different from the specified pipeline name (%r). %r will be used.rJ   FrK   rG   rH   rE   r=   )
ValueErrorrD   r   warningcopypopgetr   )
r@   rC   rF   rG   rH   rI   rJ   rK   rL   r5   r=   r=   r>   create_pipelinei   sN   


rR   c                 O   .   d| v r
t | d | d }t|| }|S )a  Creates an instance of a chat bot based on the provided configuration.

    Args:
        config (Dict): Configuration settings, expected to be a dictionary with at least a 'model_name' key.
        *args: Additional positional arguments. Not used in this function but allowed for future compatibility.
        **kwargs: Additional keyword arguments. Not used in this function but allowed for future compatibility.

    Returns:
        BaseChat: An instance of the chat bot class corresponding to the 'model_name' in the config.
    Zchat_bot_config_errorapi_type)rM   r   rQ   )rC   argsrL   rT   Zchat_botr=   r=   r>   create_chat_bot   s
   rV   c                 O   rS   )a  
    Creates a retriever instance based on the provided configuration.

    Args:
        config (Dict): Configuration settings, expected to be a dictionary with at least a 'model_name' key.
        *args: Additional positional arguments. Not used in this function but allowed for future compatibility.
        **kwargs: Additional keyword arguments. Not used in this function but allowed for future compatibility.

    Returns:
        BaseRetriever: An instance of a retriever class corresponding to the 'model_name' in the config.
    Zretriever_config_errorrT   )rM   r   rQ   )rC   rU   rL   rT   Z	retrieverr=   r=   r>   create_retriever   
   rW   c                 O   rS   )a  
    Creates a prompt engineering instance based on the provided configuration.

    Args:
        config (Dict): Configuration settings, expected to be a dictionary with at least a 'task_type' key.
        *args: Variable length argument list for additional positional arguments.
        **kwargs: Arbitrary keyword arguments.

    Returns:
        BaseGeneratePrompt: An instance of a prompt engineering class corresponding to the 'task_type' in the config.
    Zpe_config_error	task_type)rM   r   rQ   )rC   rU   rL   rY   Zper=   r=   r>   create_prompt_engineering   rX   rZ   )N)_pathlibr   typingr   r   r   r   utilsr   Zutils.configr	   modelsr   r   Zanomaly_detectionr   Zattribute_recognitionr   r   baser   
componentsr   r   r   Zdoc_preprocessorr   Zdoc_understandingr   Zface_recognitionr   Zformula_recognitionr   Zimage_classificationr   Zimage_multilabel_classificationr   Zinstance_segmentationr   Zkeypoint_detectionr   Zlayout_parsingr   Zm_3d_bev_detectionr   Zmultilingual_speech_recognitionr   Zobject_detectionr    Zocrr!   Zopen_vocabulary_detectionr"   Zopen_vocabulary_segmentationr#   Zpaddleocr_vlr$   Z
pp_chatocrr%   r&   Zpp_doctranslationr'   Zpp_shitu_v2r(   Zrotated_object_detectionr)   Zseal_recognitionr*   Zsemantic_segmentationr+   Zsmall_object_detectionr,   Ztable_recognitionr-   r.   Ztext_to_speechr/   Zts_anomaly_detectionr0   Zts_classificationr1   Zts_forecastingr2   Zvideo_classificationr3   Zvideo_detectionr4   strr?   rD   boolrR   rV   rW   rZ   r=   r=   r=   r>   <module>   s   	

M
