ó
    �®žj—   ã                   óT  • S r SSKrSSKJrJr  SSKJr  SSKJrJ	r	J
r
JrJrJrJrJrJr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  \(       a  SS	KJr  \R<                  " \5      r  " S
 S\!\5      r" " S S\5      r# " S S\5      r$ " S S\5      r% " S S\5      r& " S S\5      r'g)z'Base classes for VLM inference engines.é    N)ÚABCÚabstractmethod)ÚEnum)
ÚTYPE_CHECKINGÚAnyÚClassVarÚDictÚListÚLiteralÚOptionalÚTypeÚget_argsÚ
get_origin)ÚImage)Ú	BaseModelÚ
ConfigDictÚFieldÚSerializeAsAnyÚfield_validator)ÚPydanticUndefined)ÚEngineModelConfigc                   óp   • \ rS rSrSrSrSrSrSrSr	Sr
S	rS
r\SS S\4S j5       r\SS S\4S j5       rSrg)ÚVlmEngineTypeé    z)Types of VLM inference engines available.ÚtransformersÚmlxÚvllmÚapiÚ
api_ollamaÚapi_lmstudioÚ
api_openaiÚauto_inlineÚengine_typeÚreturnc                 ód   • UU R                   U R                  U R                  U R                  1;   $ )z*Check if an engine type is an API variant.)ÚAPIÚ
API_OLLAMAÚAPI_LMSTUDIOÚ
API_OPENAI©Úclsr#   s     Úf/home/mande/repo/quber/.venv/lib/python3.13/site-packages/docling/models/inference_engines/vlm/base.pyÚis_api_variantÚVlmEngineType.is_api_variant1   s4   € ð Ø�G‰GØ�N‰NØ×ÑØ�N‰Nð	
ñ 
ð 	
ó    c                 óN   • UU R                   U R                  U R                  1;   $ )z3Check if an engine type is an inline/local variant.)ÚTRANSFORMERSÚMLXÚVLLMr*   s     r,   Úis_inline_variantÚVlmEngineType.is_inline_variant;   s-   € ð Ø×ÑØ�G‰GØ�H‰Hð
ñ 
ð 	
r/   © N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r1   r2   r3   r&   r'   r(   r)   ÚAUTO_INLINEÚclassmethodÚboolr-   r4   Ú__static_attributes__r6   r/   r,   r   r       su   † Ù3ð "€LØ
€CØ€Dð €CØ€JØ!€LØ€Jð  €Kàð
¨ð 
¸Tó 
ó ð
ð ð
¨Oð 
Àó 
ó ó
r/   r   c                   ó~   ^ • \ rS rSr% Sr\" SS9r\" SS9r\	\
S'   0 r\\\	\S    4      \
S'   \U 4S	 j5       rS
rU =r$ )ÚBaseVlmEngineOptionséE   z¤Base configuration for VLM inference engines.

Engine options are independent of model specifications and prompts.
They only control how the inference is executed.
T©Úarbitrary_types_allowedzType of inference engine to use©Údescriptionr#   Ú	_registryc                 óp  >• [         TU ]  " S0 UD6  U [        L a  g U R                  R	                  S5      nU(       d  g S nUR
                  n[        U5      [        L a  [        U5      n[        U5      S:X  a  US   nUc  UR                  [        La  UR                  nUb  U [        R                  U'   g g )Nr#   é   r   r6   )ÚsuperÚ__pydantic_init_subclass__rA   Úmodel_fieldsÚgetÚ
annotationr   r   r   ÚlenÚdefaultr   rG   )r+   ÚkwargsÚfieldr#   ÚannÚvaluesÚ	__class__s         €r,   rK   Ú/BaseVlmEngineOptions.__pydantic_init_subclass__S   s´   ø€ ä‰Ò*Ñ4¨VÒ4ð Ô&Ò&Øð × Ñ ×$Ñ$ ]Ó3ˆÞØàˆð ×ÑˆÜ�c‹?œgÒ%Ü˜c“]ˆFÜ�6‹{˜aÓØ$ Q™i�ð Ñ 5§=¡=Ô8IÒ#IØŸ-™-ˆKàÑ"Ø:=Ô ×*Ñ*¨;Ò7ð #r/   r6   )r7   r8   r9   r:   r;   r   Úmodel_configr   r#   r   Ú__annotations__rG   r   r	   r   r=   rK   r?   Ú__classcell__)rU   s   @r,   rA   rA   E   sY   ø‡ ññ °dÑ;€Lá!&Ð3TÑ!U€K�ÓUð NP€Iˆx˜˜]¨DÐ1GÑ,HÐHÑIÑJÓOàô>ó ö>r/   rA   c                   óV   • \ rS rSr% \" SS9r\\   \S'   \	" SSS9\
S 5       5       rSrg	)
ÚVlmEngineOptionsMixinéq   z4Runtime configuration (transformers, mlx, api, etc.)rE   Úengine_optionsÚbefore)Úmodec                 óP  • [        U[        5      (       a  U$ [        U[        5      (       ay  UR                  S5      n[        R                  R                  U5      nU(       a  UR                  U5      $ [        R                  U5      (       a  SSKJ	n  UR                  U5      $ U$ )Nr#   r   )ÚApiVlmEngineOptions)
Ú
isinstancerA   ÚdictrM   rG   Úmodel_validater   r-   Ú$docling.datamodel.vlm_engine_optionsra   )r+   Úvaluer#   Ú	model_clsra   s        r,   Úresolve_engine_optionsÚ,VlmEngineOptionsMixin.resolve_engine_optionsv   sŠ   € ô �eÔ1×2Ñ2ØˆLô �eœT×"Ñ"ØŸ)™) MÓ2ˆKÜ,×6Ñ6×:Ñ:¸;ÓGˆIÞØ ×/Ñ/°Ó6Ð6ô ×+Ñ+¨K×8Ñ8ÝTà*×9Ñ9¸%Ó@Ð@àˆr/   r6   N)r7   r8   r9   r:   r   r]   r   rA   rX   r   r=   rh   r?   r6   r/   r,   r[   r[   q   s@   ‡ Ù;@ØJñ<€N�NÐ#7Ñ8ó ñ Ð%¨HÑ5Øñó ó 6ór/   r[   c                   óÂ   • \ rS rSr% Sr\" SS9r\" SS9r\	\
S'   \" SS9r\\
S	'   \" S
SS9r\\
S'   \" SSS9r\\
S'   \" \SS9r\\   \
S'   \" \SS9r\\\4   \
S'   Srg)ÚVlmEngineInputé�   zYInput to a VLM inference engine.

This is the generic interface that all engines accept.
TrC   zPIL Image to processrE   ÚimagezText prompt for the modelÚpromptg        z#Sampling temperature for generation©rP   rF   Útemperaturei   z$Maximum number of tokens to generateÚmax_new_tokensz(Strings that trigger generation stopping©Údefault_factoryrF   Ústop_stringsz#Additional generation configurationÚextra_generation_configr6   N)r7   r8   r9   r:   r;   r   rW   r   rm   r   rX   rn   Ústrrp   Úfloatrq   ÚintÚlistrt   r
   rc   ru   r	   r   r?   r6   r/   r,   rk   rk   �   s¢   ‡ ññ
 °dÑ;€LáÐ%;Ñ<€Eˆ5Ó<ÙÐ$?Ñ@€FˆCÓ@ÙØÐ!Fñ€K�ó ñ  ØÐ"Hñ€N�Có ñ $ØÐ*Tñ€L�$�s‘)ó ñ /4ØÐ*Oñ/Ð˜T # s (™^ö r/   rk   c                   óp   • \ rS rSr% Sr\" SS9r\\S'   \" SSS9r	\S-  \S	'   \" \
S
S9r\\\4   \S'   Srg)ÚVlmEngineOutputé¥   z\Output from a VLM inference engine.

This is the generic interface that all engines return.
zGenerated text from the modelrE   ÚtextNzReason why generation stoppedro   Ústop_reasonz#Additional metadata from the enginerr   Úmetadatar6   )r7   r8   r9   r:   r;   r   r}   rv   rX   r~   rc   r   r	   r   r?   r6   r/   r,   r{   r{   ¥   sU   ‡ ññ
 Ð"AÑB€Dˆ#ÓBÙ#ØÐ"Añ€K��t‘ó ñ  %ØÐ*Oñ €Hˆd�3˜�8‰nö r/   r{   c                   ó¸   • \ rS rSrSr SS\S\S   4S jjr\SS	 j5       r	\S
\
\   S\
\   4S j5       rS\S\4S jrS\\
\   -  S\\
\   -  4S jrSS jrSrg)ÚBaseVlmEngineé´   aÞ  Abstract base class for VLM inference engines.

An engine handles the low-level model inference with generic inputs
(PIL images + text prompts) and returns text predictions.

Engines are independent of:
- Pipeline stages (DoclingDocument, Page objects)
- Response formats (doctags, markdown, etc.)

But they ARE aware of:
- Model specifications (repo_id, revision, model_type via EngineModelConfig)

These model specs are provided at construction time for eager initialization.
NÚoptionsrW   r   c                 ó*   • Xl         X l        SU l        g)zÞInitialize the engine.

Args:
    options: Engine-specific configuration options
    model_config: Model configuration (repo_id, revision, extra_config)
                 If None, model must be specified in predict() calls
FN)rƒ   rW   Ú_initialized)Úselfrƒ   rW   s      r,   Ú__init__ÚBaseVlmEngine.__init__Ä   s   € ð ŒØ(ÔØ"'ˆÕr/   r$   c                 ó   • g)z®Initialize the engine (load models, setup connections, etc.).

This is called once before the first inference.
Implementations should set self._initialized = True when done.
Nr6   ©r†   s    r,   Ú
initializeÚBaseVlmEngine.initializeÔ   ó   � r/   Úinput_batchc                 ó   • g)zóRun inference on a batch of inputs.

This is the primary method that all engines must implement.
Single predictions are routed through this method.

Args:
    input_batch: List of inputs to process

Returns:
    List of outputs, one per input
Nr6   )r†   rŽ   s     r,   Úpredict_batchÚBaseVlmEngine.predict_batchÜ   r�   r/   Ú
input_datac                 ór   • U R                   (       d  U R                  5         U R                  U/5      nUS   $ )ac  Run inference on a single input.

This is a convenience method that wraps the input in a list and calls
predict_batch(). Engines should NOT override this method - all
inference logic should be in predict_batch().

Args:
    input_data: Generic input containing image, prompt, and config

Returns:
    Generic output containing generated text and metadata
r   )r…   r‹   r�   )r†   r’   Úresultss      r,   ÚpredictÚBaseVlmEngine.predictê   s3   € ð × × Ø�O‰OÔà×$Ñ$ j \Ó2ˆØ�q‰zÐr/   c                 ó²   • U R                   (       d  U R                  5         [        U[        5      (       a  U R	                  U5      $ U R                  U5      $ )zŠConvenience method to run inference.

Args:
    input_data: Single input or list of inputs

Returns:
    Single output or list of outputs
)r…   r‹   rb   ry   r�   r•   )r†   r’   s     r,   Ú__call__ÚBaseVlmEngine.__call__ý   sD   € ð × × Ø�O‰OÔä�j¤$×'Ñ'Ø×%Ñ% jÓ1Ð1à—<‘< 
Ó+Ð+r/   c                 ó   • g)zClean up resources (optional).

Called when the engine is no longer needed.
Implementations can override to release resources.
Nr6   rŠ   s    r,   ÚcleanupÚBaseVlmEngine.cleanup  r�   r/   )r…   rW   rƒ   )N)r$   N)r7   r8   r9   r:   r;   rA   r   r‡   r   r‹   r
   rk   r{   r�   r•   r˜   r›   r?   r6   r/   r,   r�   r�   ´   sª   † ñð$ 7;ñ(à%ð(ð Ð2Ñ3õ(ð  óó ðð ð¨¨nÑ)=ð À$ÀÑBWó ó ðð .ð °_ô ð&,Ø(¨4°Ñ+?Ñ?ð,à	˜4 Ñ0Ñ	0ô,÷&r/   r�   )(r;   ÚloggingÚabcr   r   Úenumr   Útypingr   r   r   r	   r
   r   r   r   r   r   Ú	PIL.Imager   Úpydanticr   r   r   r   r   Úpydantic_corer   Ú#docling.datamodel.stage_model_specsr   Ú	getLoggerr7   Ú_logrv   r   rA   r[   rk   r{   r�   r6   r/   r,   Ú<module>r§      s”   ðñ .ã ß #Ý ÷÷ ÷ õ ß RÕ RÝ +æÝEà×Ò˜Ó"€ô"
�C˜ô "
ôJ)>˜9ô )>ôX˜Iô ô8�Yô ô0�iô ôa�Cõ ar/   