
    =pjA              
      ,   S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	J
r
JrJrJr  S SKJr  SSKJrJr  S	S
KJr  SSKJr  SSKJrJrJrJrJr  SSKJrJrJ r   SSK!J"r"  SSK#J$r$  SSK%J&r&  SSK'J(r(  SSK)J*r*  SSK+J,r,  SSK-J.r.  SSK/J0r0  SSK1J2r2  SSK3J4r4  SSK5J6r6  SSK7J8r8  SSK9J:r:  SSK;J<r<  SSK=J>r>  SSK?J@r@  SSKAJBrBJCrCJDrD  SS KEJFrF  SS!KGJHrH  SS"KIJJrJ  SS#KKJLrL  SS$KMJNrN  SS%KOJPrP  SS&KQJRrR  SS'KSJTrT  SS(KUJVrV  SS)KWJXrX  SS*KYJZrZ  SS+K[J\r\  SS,K]J^r^  SS-K_J`r`  SS.KaJbrb  SHS/ jrcSIS0 jrdSS1KeJfrf  S2S3.     SJS4 jjrgSKS5 jrh        SLS6 jriSMS7 jrj\" S25      SNS8 j5       rkS2S2S9.         SOS: jjrl\" SS;9SPS< j5       rmSQS= jrn      SRS> jro          SSS? jrp    STS@ jrq          SUSA jrr        SVSB jrs            SWSC jrt                  SXSD jruS2S2S2S2SS2SES2S2SF.	                     SYSG jjrvg2)Z    )annotations)Mapping)	lru_cache)Path)AnyDictOptionalTupleUnion)	BaseModel   )errorslogging   )official_models   )UadPredictor)BindingUnknownModelErrorUnsupportedEngineErrorget_bindingget_supported_engines)GenAIConfigneed_local_modeluses_server_backend)DocVLMPredictor)InferenceEngine)FaceFeaturePredictor)FormulaRecPredictor)ClasPredictor)ImageFeaturePredictor)MLClasPredictor)WarpPredictor)InstanceSegPredictor)KptPredictor)LayoutAnalysisPredictor)BEVDet3DPredictor)WhisperPredictor)DetPredictor)OVDetPredictor)OVSegPredictor)BasePredictorLocalModelPredictorRunnerPredictor)	HPIConfig)PaddlePredictorOption)SegPredictor)TablePredictor)TextDetPredictor)TextRecPredictor)TextToPinyinPredictor)Fastspeech2Predictor)PwganPredictor)TSAdPredictor)TSClsPredictor)TSFcPredictor)&resolve_paddle_engine_from_model_files)VideoClasPredictor)VideoDetPredictorc                `     [        U 5      $ ! [         a  n[        SU < S35      UeS nAff = fNz(No engine bindings registered for model .)r   r   
ValueError)
model_namees     ]/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/paddlex/inference/models/__init__.py_get_supported_enginesrE   E   s>    $Z00 6znAF
	s   
 
-(-c                     [        X5      $ ! [         a  n[        SU < S35      UeS nAf[         a  n[        [	        U5      5      UeS nAff = fr?   )r   r   rA   r   str)rB   enginerC   s      rD   _get_predictor_bindingrI   N   sZ    (:.. 6znAF
	 " (Q a'(s   
 
A(AA

A)resolve_model_nameNmodel_formatsc                   Uc  [        [        R                  " U US95      $ [        U5      nUR                  5       (       d  [	        U S35      eU$ )NrK   z does not exist!)r   r   get_model_pathexistsFileNotFoundError)rB   	model_dirrL   resolveds       rD   _resolve_model_dirrS   \   s[     **+
 	
 IH??9+-= >??O    c                v    [        U 5      nS H  nX!;   d  M
  Us  $    [        SU < S[        U5      < S35      e)N)paddle_staticpaddle_dynamiczModel z6 does not support engine 'paddle'. Supported engines: r@   )rE   rA   list)rB   	supportedrH   s      rD   _resolve_default_paddle_enginerZ   o   sM    &z2I5M 6 
 "9o0	3 rT   c                    US:w  a  US 4$ Uc  [        U 5      S 4$ [        X5      n[        U5      nUc  [        SU< S35      eXC4$ )Npaddlez$No Paddle model files were found in r@   )rZ   rS   r;   rA   )rB   rH   rQ   model_dir_resolvedresolved_engines        rD   _resolve_requested_enginer_   z   sf    
 t|-j94??+JB<=OPO?	}ANOO..rT   c                @     [        U 5      S:H  $ ! [         a     gf = f)N)flexibleF)rE   rA   )rB   s    rD   _is_flexible_only_modelrb      s)    %j1]BB s    
c                     [         R                  " U 5      " 5       $ ! [        R                   a  n[	        SU < S35      UeS nAff = f)NzUnsupported engine: r@   )r   getr   ClassNotFoundExceptionrA   )rH   rC   s     rD   _get_inference_enginerf      sK    D""6*,,(( D/z;<!CDs    AAArB   devicec               6    [        U 5      R                  UUUS9$ )zIParse, validate and normalize engine-specific config to a canonical dict.rg   )rf   normalize_config)rH   cfgrB   rh   s       rD   normalize_engine_configrl      s*     !(99 :  rT   )maxsizec                 Z    [        [        R                  " 5       R                  5       5      $ )z.Registered inference engine ids (bucket keys).)	frozensetr   allkeys rT   rD   #_engine_config_bucket_key_allowlistrs      s!     _((*//122rT   c           	     R   [        5       n[        UR                  5       5      nU Vs1 s H  oDU;   d  M
  UiM     nnX5-
  nU(       a-  U(       a&  [        S[	        U5      < S[	        U5      < S35      eU(       d  U$ UR                  5        HA  u  pG[        U[        5      (       a  M  [        SU< S[        U5      R                  < S35      e   UR                  U 5      n[        U[        5      (       a  [        U5      O0 n	X;  a  [        R                  " SU 5        U	$ s  snf )zResolve bucketed engine_config to a single dict for the resolved engine.

Bucketed form: every top-level key must be a registered engine name, and every
value must be a mapping. Mixing those keys with any other top-level key is not
allowed (strict).
zengine_config cannot mix per-engine bucket keys with flat config keys. Use either a flat dict for the resolved engine only, or a dict whose top-level keys are exclusively inference engine names. Flat-like keys: z; bucket-like keys: r@   zJBucketed engine_config requires each top-level value to be a mapping; key z
 has type zbBucketed engine_config has no entry for resolved engine %r; using an empty config for that engine.)rs   setrq   rA   sorteditems
isinstancer   type__name__rd   dictr   warning)
rH   rk   	allowlistrq   kallowlistedothervbucketflat_for_engines
             rD   _flatten_bucketed_engine_configr      s    45Isxxz?D"5d9n1dK5Eu  &e}//CF;DWCZZ[]
 	
 
		!W%%e:d1g&6&6%9<   WWV_F&0&A&Ad6lrO,	
 ? 6s
   	D$D$c                L    Ub  [        U[        5      (       d  U$ [        X5      $ )N)rx   r{   r   )rH   rk   s     rD   %_maybe_flatten_bucketed_engine_configr      s%     {*S$//
*677rT   c                b    Ub  U$ [        U5      (       a  gU(       a  g[        U 5      (       a  gg)Ngenai_clienthpira   r\   )r   rb   )rB   rH   use_hpipgenai_configs       rD   _resolve_effective_enginer      s4     <((z**rT   c                >    Ub  U$ U S:X  a  U$ U S:X  a  U$ U S:X  a  U$ g )NrV   r   r   rr   rH   engine_config	pp_option
hpi_configr   s        rD   _select_engine_config_sourcer      s<       rT   c                `    Uc  U R                   (       d  U$ [        UUU R                  5       S9$ )NrK   )needs_local_modelrS   get_supported_model_formatsinference_enginerB   rQ   r]   s       rD   _resolve_local_model_dirr     s8     %-=-O-O!!&BBD rT   c                n    U (       d  g [         R                  " U5      nXS   S   :w  a  [        S5      eU$ )NGlobalrB   u9   Model name mismatch，please input the correct model dir.)r,   load_configrA   )r   rB   rQ   configs       rD   _load_local_model_configr     s;     &&y1FH%l33TVVMrT   c                    [        U [        5      (       d  g SSKJn  [	        X5      (       d#  [        S[        U5      R                  < S35      eUR                  UUUUUS9$ )Nr   )RunnerEnginezInferenceEngine z& does not support runner construction.)rB   rQ   model_configr   binding)	
issubclassr.   enginesr   rx   RuntimeErrorry   rz   build_runner)predictor_clsr   r   rB   rQ   r   r   r   s           rD   _build_predictor_runnerr   &  ss     m_55%&55t$45>>AAgh
 	
 ((!# )  rT   c           
         U R                   nUUS.n	U(       a  XYS'   [        UUU UUUUS9n
U
b  XS'   [        U[        5      (       a  XiS'   XyS'   U	$ )N)rB   
batch_sizer   )r   r   r   rB   rQ   r   r   runnerrQ   r   )	predictorr   r   r-   )r   r   rB   r   normalized_engine_configpredictor_engine_configrQ   r   r   create_kwargsr   s              rD   _build_predictor_kwargsr   A  s|     %%M  M )@o&$#)!.F "(h-!455%.k"(4n%rT   F)	rQ   rh   rH   r   r   r   r   r   r   c       	        *   U(       a  Ub  [         R                  " SU5        [        U US9u  pn[        XXy5      n[	        XU5      u  p;[        U5      n[        X5      nUR                  n[        UU UUS9n[        UUUUU	S9n[        UU5      n[        UUU US9nU(       a  UR                  U5        UR                  5         U(       a
  U(       a  UnO[        UU US9nUR                  U5      n[!        UU UUUUUUS9nUR"                  " S0 UDU
D6$ )	a  Create a predictor for the given model and engine.

Args:
    model_name: Model name.
    model_dir (Optional[str]): Path to model directory. Required for local engines
        when the model is not official. Ignored when a local model is not needed.
    device (Optional[str]): Device to run on (e.g. `'gpu'`, `'cpu'`). Used by local
        engines.
    engine (Optional[str]): Inference engine. One of `'paddle'` (resolved to
        `paddle_static` or `paddle_dynamic` from local model files when
        `model_dir` is provided; otherwise resolved from predictor support,
        preferring `paddle_static`), `'paddle_static'`,
        `'paddle_dynamic'`, `'hpi'`, `'flexible'`, `'transformers'`,
        `'onnxruntime'`, `'genai_client'`, or `None`.
        When `None`: if `genai_config.backend` is a server backend, engine
        becomes `'genai_client'`; else if `use_hpip=True` and model supports
        hpi, engine becomes `'hpi'`; else if model is flexible-only, engine
        becomes `'flexible'`; otherwise defaults to `'paddle'`.
    engine_config (Optional[Dict[str, Any]]): Engine-specific config for the
        resolved engine (flat dict), **or** a bucketed dict whose top-level keys
        are only registered engine names (e.g. ``hpi``, ``paddle_static``), each
        mapping to a nested dict. Bucketed and flat keys must not be mixed at the
        same level; use either form exclusively.
    batch_size (int): Batch size for inference. Defaults to 1.
    pp_option (Optional[PaddlePredictorOption]): Paddle predictor options. Used when
        `engine='paddle_static'` and `engine_config` is not specified. Prefer
        `engine_config` for new code.
    use_hpip: When `engine` is `None`, if `True`, resolves to `engine='hpi'`.
        Ignored when `engine` is set.
    hpi_config (Optional[Union[Dict[str, Any], HPIConfig]]): HPI configuration.
        Used when `engine='hpi'` and `engine_config` is not specified. Prefer
        `engine_config` for new code.
    genai_config (Optional[Union[Dict[str, Any], GenAIConfig]]): GenAI configuration.
        Mainly used when `engine='genai_client'` and `engine_config` is not specified.
        Prefer `engine_config` for new code.

Returns:
    A predictor instance.
z`use_hpip` only takes effect when `engine` is None. Since engine=%r is explicitly set, use_hpip will be ignored. To use HPI, pass engine='hpi' instead.)rB   rQ   r   r   rg   )r   rB   rQ   )r   rB   r   r   r   rQ   r   r   rr   )r   r|   rJ   r   r_   rf   rI   r   r   r   r   rl   ensure_model_filesensure_environmentr   to_predictor_configr   r   )rB   rQ   rh   rH   r   r   r   r   r   r   kwargsr]   resolved_configrequested_enginepredictor_binding
need_localconfig_to_validater   r   r   r   s                        rD   create_predictorr   d  so   j F&$ 		
 7I73JO
 'z8RF!:I"F -V4.zB!33J1)-	 6#! ?"  7	  ++,>?''): )(!(
 /BB  ,!!9 7$)	M &&AA&AArT   )rB   rG   returnzTuple[str, ...])rB   rG   rH   rG   r   r   )rB   rG   rQ   Optional[str]r   r   )rB   rG   r   rG   )rB   rG   rH   rG   rQ   r   r   ztuple[str, Optional[Path]])rB   rG   r   bool)rH   rG   r   r   )
rH   rG   rk   zAOptional[Union[Dict[str, Any], PaddlePredictorOption, BaseModel]]rB   r   rh   r   r   Dict[str, Any])r   ro   )rH   rG   rk   r   r   r   )rH   rG   rk   NOptional[Union[Dict[str, Any], PaddlePredictorOption, HPIConfig, GenAIConfig]]r   r   )
rB   rG   rH   r   r   r   r   ,Optional[Union[Dict[str, Any], GenAIConfig]]r   rG   )rH   rG   r   r   )
r   r   rB   rG   rQ   r   r]   Optional[Path]r   r   )r   r   rB   rG   rQ   r   r   Optional[Dict[str, Any]])r   r   r   r   rB   rG   rQ   r   r   r   r   r   )r   r   r   r   rB   rG   r   intr   r   r   r   rQ   r   r   r   r   r   )rB   rG   rQ   r   rh   r   rH   r   r   r   r   r   r   zOptional[PaddlePredictorOption]r   r   r   z*Optional[Union[Dict[str, Any], HPIConfig]]r   r   r   r,   )w
__future__r   collections.abcr   	functoolsr   pathlibr   typingr   r   r	   r
   r   pydanticr   utilsr   r   utils.official_modelsr   anomaly_detectionr   bindingsr   r   r   r   r   common.genair   r   r   doc_vlmr   r   r   face_featurer   formula_recognitionr   image_classificationr    image_featurer!   image_multilabel_classificationr"   image_unwarpingr#   instance_segmentationr$   keypoint_detectionr%   layout_analysisr&   m_3d_bev_detectionr'   multilingual_speech_recognitionr(   object_detectionr)   open_vocabulary_detectionr*   open_vocabulary_segmentationr+   
predictorsr,   r-   r.   runners.hpir/   runners.paddle_static.configr0   semantic_segmentationr1   table_structure_recognitionr2   text_detectionr3   text_recognitionr4   text_to_pinyinr5   text_to_speech_acousticr6   text_to_speech_vocoderr7   ts_anomaly_detectionr8   ts_classificationr9   ts_forecastingr:   utils.model_pathsr;   video_classificationr<   video_detectionr=   rE   rI   utils.model_resolverrJ   rS   rZ   r_   rb   rf   rl   rs   r   r   r   r   r   r   r   r   r   rr   rT   rD   <module>r      s   # #   4 4  $ 3 +  M L $ $ . 4 / 0 < * 7 , 4 1 = * 5 8 K K " ? / 7 , . 1 9 2 / - ) E 4 .( 5 	
 
&/// /  	/$ 4D D !% 	J 	
   13 3
(V88	W8 T8  ?	
 	" T&%  	
 '    	
  & 	
   + "6   &  	 
   -  ,    +   L  $  .215=AAE}B}B }B 	}B
 }B ,}B }B /}B }B ;}B ?}B }BrT   