ó
    À‘´j&  ã                   ó>  • % S r SSKrSSKJr  SSKJr  SSKJrJr  SSK	r	SSK
Jr  SSKJrJr  SSKJr  SS	K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  Sr  " S S\5      r! " S S\!5      r" " S S\!5      r#\#\"S.r$\%\&\'\!   4   \(S'   S\&S\S\!4S jr)g)uk  
DoclingParser â€” docling pipeline machinery behind the `Parser` ABC.

`DoclingParser` is abstract: it owns the converter construction, the run
fingerprint, and the `parse` template, but defers the pipeline-option bundle
to a concrete subclass. The configuration is a *type*, not a runtime flag:

- `TunedFinancialParser` â€” the canonical configuration (TableFormerMode.ACCURATE,
  DoclingParse backend, OCR, picture classification, page_batch_size=32). The
  accelerator device defaults to AUTO and is resolved at runtime; RapidOCR is
  switched to CUDA only when the resolved device is CUDA. The default everywhere.
- `LegacyDoclingParser` â€” docling's default backend and PdfPipelineOptions,
  with do_ocr / do_table_structure / do_cell_matching set from the
  constructor. OCR is off unless a caller passes `do_ocr=True`, and no caller
  does, while docling's own default is on. Kept
  for regression comparison and as a fallback; select it with `--preset legacy`.

Use `parser_for_preset(preset)` to map a preset string to the right class.

The header-consolidation post-processor that builds `TableMetadata` lives in
`core.consolidation` and operates on the DoclingDocument. That keeps these
classes focused on a single job: source -> `ParseResult`, which carries the
DoclingDocument on `.document` beside the page scores, parsed-page cells and
per-table OCR/native provenance.
é    N)Úabstractmethod)ÚPath)ÚAnyÚClassVar)ÚDoclingParseDocumentBackend)ÚAcceleratorDeviceÚAcceleratorOptions)ÚInputFormat)ÚPdfPipelineOptionsÚRapidOcrOptionsÚTableFormerMode)Úsettings)ÚDocumentConverterÚPdfFormatOption)Údecide_device)ÚParser)ÚParseResulté    c                   óì   • \ rS rSr% Sr\\   \S'   \\   \S'   SSS\\	R                  S4S\S	\S
\S\S\	S\S-  SS4S jjr\S\4S j5       rS\S\4S jrSS jrS\S\\\4   4S jrS\S\4S jrSrg)ÚDoclingParseré3   a
  Abstract docling parser. Subclasses define the pipeline-option bundle.

Subclasses set the `preset` and `backend_label` class attributes and
implement `build_pipeline_options`. Everything else (converter, perf
settings, fingerprint, the `parse` template) is shared.
ÚpresetÚbackend_labelFTNÚdo_ocrÚdo_table_structureÚdo_cell_matchingÚpage_batch_sizeÚaccelerator_deviceÚnum_threadsÚreturnc                 óš   • Xl         X l        X0l        X@l        XPl        Ub  X`l        g [
        R                  " 5       =(       d    SU l        g )Né   )r   r   r   r   r   ÚosÚ	cpu_countr   )Úselfr   r   r   r   r   r   s          Ú?/home/mande/repo/quber/src/quber/core/parsers/docling_parser.pyÚ__init__ÚDoclingParser.__init__@   s?   € ð ŒØ"4ÔØ 0ÔØ.ÔØ"4ÔØ*5Ñ*A˜;ÕÌÏÊË×H[ÐZ[ˆÕó    c                 ó   • g)z;Return the docling pipeline options for this configuration.N© ©r%   s    r&   Úbuild_pipeline_optionsÚ$DoclingParser.build_pipeline_optionsP   s   € ð 	r)   Úpipeline_optionsc                 óB   • [        [        R                  [        US90S9$ )zLDefault converter (no explicit backend). Overridden by presets that pin one.)r/   ©Úformat_options)r   r
   ÚPDFr   ©r%   r/   s     r&   Úbuild_converterÚDoclingParser.build_converterU   s!   € ä Ü'ŸO™O¬_ÐN^Ñ-_Ð`ñ
ð 	
r)   c                 ó   • g)z4Hook for process-wide perf tweaks. No-op by default.Nr+   r,   s    r&   Úapply_perf_settingsÚ!DoclingParser.apply_perf_settings[   s   � r)   c                 ó  • UR                   R                  n[        US5      (       a  UR                  O
[	        U5      nUR
                  R                  n[        US5      (       a  UR                  O
[	        U5      nUR                  R                  R                  n[        UR                  [        5      =(       a8    [        UR                  R                  =(       d    0 R                  SS5      5      n[        U R                   R"                  5      nU R$                  UUR
                  R&                  UR(                  UR*                  UR,                  U R.                  UUUR                   R0                  U R2                  UUS.$ )NÚnameú!EngineConfig.onnxruntime.use_cudaF)r   Ú
table_moder   r   Údo_picture_classificationÚgenerate_picture_imagesÚbackendÚaccelerator_device_requestedÚaccelerator_device_resolvedr   r   Ú
ocr_engineÚocr_cuda)Úaccelerator_optionsÚdeviceÚhasattrr;   ÚstrÚtable_structure_optionsÚmodeÚocr_optionsÚ	__class__Ú__name__Ú
isinstancer   ÚboolÚrapidocr_paramsÚgetr   r   Úvaluer   r   r   r>   r?   r   r   r   )	r%   r/   rF   Údevice_namerJ   Ú	mode_namerC   rD   Úresolved_devices	            r&   ÚfingerprintÚDoclingParser.fingerprint^   s9  € Ø&×:Ñ:×AÑAˆÜ%,¨V°V×%<Ñ%<�f—k’kÄ#ÀfÃ+ˆØ$×<Ñ<×AÑAˆÜ!(¨¨v×!6Ñ!6�D—I’I¼CÀ»Iˆ	Ø%×1Ñ1×;Ñ;×DÑDˆ
ÜÐ.×:Ñ:¼OÓL÷ 
ÔQUØ×)Ñ)×9Ñ9×?¸R×DÑDØ3°UóóR
ˆô
 (¨×(?Ñ(?×(EÑ(EÓFˆà—k‘kØ#Ø 0× HÑ H× YÑ YØ&×-Ñ-Ø)9×)SÑ)SØ'7×'OÑ'OØ×)Ñ)Ø,7Ø+:Ø+×?Ñ?×KÑKØ#×3Ñ3Ø$Ø ñ
ð 	
r)   Úsourcec                 ól  • U R                  5       nSUl        U R                  U5      nU R                  5         U R	                  U5      n[
        R                  " SS[        U5      0UD6   UR                  [        U5      5      n[        R                  " U5      sS S S 5        $ ! , (       d  f       g = f)NTrX   )zdocling_parser.parse)r-   Úgenerate_parsed_pagesr5   r8   rV   ÚlogfireÚspanrH   Úconvertr   Úfrom_conversion)r%   rX   r/   Ú	converterrV   Úresults         r&   ÚparseÚDoclingParser.parsez   s‘   € Ø×6Ñ6Ó8Ðð
 26ÐÔ.Ø×(Ñ(Ð)9Ó:ˆ	Ø× Ñ Ô"à×&Ñ&Ð'7Ó8ˆÜ�\Š\ÑT¼¸V»ÐTÈÓTØ×&Ñ&¤s¨6£{Ó3ˆFÜ×.Ò.¨vÓ6÷ U×T×Tús   Á+0B%Â%
B3)r   r   r   r   r   r   ©r    N)rM   Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   rH   Ú__annotations__ÚCANONICAL_PAGE_BATCH_SIZEr   ÚAUTOrO   Úintr'   r   r   r-   r   r5   r8   Údictr   rV   r   r   ra   Ú__static_attributes__r+   r)   r&   r   r   3   sò   ‡ ñð �S‰MÓà˜C‘=Ó ð Ø#'Ø!%Ø8Ø0A×0FÑ0FØ"&ñ]àð]ð !ð]ð ð	]ð
 ð]ð .ð]ð ˜4‘Zð]ð 
õ]ð  ðÐ(:ó ó ðð
Ð0Bð 
ÐGXô 
ôCð
Ð,>ð 
À4ÈÈSÈÁ>ô 
ð87˜Dð 7 [÷ 7r)   r   c                   ó.   • \ rS rSrSrSrSrS\4S jrSr	g)	ÚLegacyDoclingParseréŠ   zPDocling's default backend and PdfPipelineOptions, with OCR off unless asked for.ÚlegacyÚdefaultr    c                 ó”   • [        5       nU R                  Ul        U R                  UR                  l        U R                  Ul        U$ ©N)r   r   r   rI   r   r4   s     r&   r-   Ú*LegacyDoclingParser.build_pipeline_options�   sB   € Ü-Ó/ÐØ.2×.EÑ.EÐÔ+ØDH×DYÑDYÐ×0Ñ0ÔAØ"&§+¡+ÐÔØÐr)   r+   N)
rM   rd   re   rf   rg   r   r   r   r-   rm   r+   r)   r&   ro   ro   Š   s   † ÙZà€FØ€Mð Ð(:÷  r)   ro   c                   ó^   • \ rS rSrSrSr\R                   rS\4S jr	S\S\
4S jrSS	 jrS
rg)ÚTunedFinancialParseré˜   zLCanonical (tuned-financial) configuration for dense SEC financial documents.útuned-financialr    c                 ó¬  • [        5       nU R                  Ul        U R                  UR                  l        SUl        [
        R                  UR                  l        SUl        SUl	        [        U R                  U R                  S9Ul        [        U R                  R                  5      nUR!                  S5      (       a  [#        SSS.S9Ul        U$ )NT)rF   r   Úcudar   )r<   z.EngineConfig.onnxruntime.cuda_ep_cfg.device_id)rP   )r   r   r   rI   r   r   ÚACCURATErJ   r>   r?   r	   r   r   rE   r   rR   Ú
startswithr   rK   )r%   r/   rU   s      r&   r-   Ú+TunedFinancialParser.build_pipeline_optionsž   sÉ   € Ü-Ó/ÐØ.2×.EÑ.EÐÔ+ØDH×DYÑDYÐ×0Ñ0ÔAØ"&ÐÔÜ8G×8PÑ8PÐ×0Ñ0Ô5Ø59ÐÔ2Ø37ÐÔ0Ü/AØ×*Ñ*Ø×(Ñ(ñ0
ÐÔ,ô (¨×(?Ñ(?×(EÑ(EÓFˆØ×%Ñ% f×-Ñ-Ü+:à9=ØFGñ!ñ,ÐÔ(ð  Ðr)   r/   c                 óL   • [        [        R                  [        U[        S90S9$ )N)r/   r@   r1   )r   r
   r3   r   r   r4   s     r&   r5   Ú$TunedFinancialParser.build_converterº   s(   € Ü ä—‘¤Ø%5Ü7ñ"ðñ
ð 	
r)   Nc                 óB   • U R                   [        R                  l         g rt   )r   r   Úperfr,   s    r&   r8   Ú(TunedFinancialParser.apply_perf_settingsÄ   s   € Ø(,×(<Ñ(<Œ�‰Õ%r)   r+   rc   )rM   rd   re   rf   rg   r   r   r   r   r-   r   r5   r8   rm   r+   r)   r&   rw   rw   ˜   s<   † ÙVà€FØ/×8Ñ8€Mð Ð(:ô  ð8
Ð0Bð 
ÐGXô 
÷=r)   rw   )ry   rq   ÚPARSERS_BY_PRESETr   Úkwargsr    c                 ó¤   •  [         U    nU" S0 UD6$ ! [         a2    SR                  [        [         5      5      n[	        SU < SU 35      Sef = f)zHInstantiate the parser for a preset name (forwards kwargs to the class).z, zUnknown parser preset z; valid presets: Nr+   )r„   ÚKeyErrorÚjoinÚsortedÚ
ValueError)r   r…   ÚclsÚvalids       r&   Úparser_for_presetr�   Ï   sd   € ð`Ü Ñ'ˆñ ‰=�‰=Ðøô ó `Ø—	‘	œ&Ô!2Ó3Ó4ˆÜÐ1°&±Ð;LÈUÈGÐTÓUÐ[_Ð_ð`ús	   ‚	 “<A)*rg   r#   Úabcr   Úpathlibr   Útypingr   r   r[   Ú%docling.backend.docling_parse_backendr   Ú%docling.datamodel.accelerator_optionsr   r	   Údocling.datamodel.base_modelsr
   Ú"docling.datamodel.pipeline_optionsr   r   r   Údocling.datamodel.settingsr   Údocling.document_converterr   r   Údocling.utils.accelerator_utilsr   Úquber.core.parsers.baser   Úquber.core.parsers.resultr   ri   r   ro   rw   r„   rl   rH   Útyperh   r�   r+   r)   r&   Ú<module>r›      s­   ðòó4 
Ý Ý ß  ã Ý Mß WÝ 5÷ñ õ
 0ß IÝ 9å *Ý 1àÐ ôT7�Fô T7ôn ˜-ô  ô-=˜=ô -=ðd ,Ø!ñ5Ð �4˜˜T -Ñ0Ð0Ñ1ó ð˜cð ¨Sð °]õ r)   