ó
    qyüi1  ã                   óø   • S SK r S SKJs  Js  Jr  SSKJr  SSKJ	r	  SSK
JrJr  SSKJrJrJr  SSKJrJr  SSKJrJrJr  SS	KJr  SS
KJr  \R8                  " \5      r\\" SS9 " S S\5      5       5       rS/r g)é    Né   )ÚTorchvisionBackend)ÚBatchFeature)Úgroup_images_by_shapeÚreorder_images)ÚIMAGENET_DEFAULT_MEANÚIMAGENET_DEFAULT_STDÚSizeDict)ÚImagesKwargsÚUnpack)Úauto_docstringÚis_torchdynamo_compilingÚlogging)Ú
TensorType)Úrequires)Útorch)Úbackendsc                    ó  ^ • \ rS rSrSr\r\rSSS.r	SSS.r
SrSrSrSrSrSSS\S	S4S
 jrS\S   S\S\SSS\S\S\S\S\S\\\   -  S-  S\\\   -  S-  S\S-  S\S-  S\S-  S\\-  S-  S	\4 S jrS\\   4U 4S jjrS rS rS rU =r$ )!ÚSLANeXtImageProcessoré&   é   i   )ÚheightÚwidthTÚimageztorch.TensorÚsizeÚreturnc                 óf
  • UR                   u  p4pVUR                  X4-  XV5      nUR                  n[        UR                  UR
                  5      [        XV5      -  n[        XX-  5      n	[        Xh-  5      n
[        R                  " U
[        R                  US9nUS-   [        U5      [        U
5      -  -  S-
  nUR                  5       R                  [        R                  5      nXÍR                  5       -
  n[        R                  " US:  [        R                  " U5      U5      n[        R                  " US:  [        R                  " U5      U5      n[        R                  " XÖS-
  :¬  [        R                   " U5      U5      n[        R                  " XÖS-
  :¬  [        R"                  " XÖS-
  5      U5      nUS-  S-   R                  5       R                  [        R                  5      nSU-
  n[        R                  " U	[        R                  US9nUS-   [        U5      [        U	5      -  -  S-
  nUR                  5       R                  [        R                  5      nUUR                  5       -
  n[        R                  " US:  [        R                  " U5      U5      n[        R                  " US:  [        R                  " U5      U5      n[        R                  " UUS-
  :¬  [        R                   " U5      U5      n[        R                  " UUS-
  :¬  [        R"                  " UUS-
  5      U5      nUS-  S-   R                  5       R                  [        R                  5      nSU-
  nUR%                  SS5      R                  [        R&                  5      nUR                  [        R                  5      nUR)                  5       nUS-   R)                  5       nUR)                  5       nUS-   R)                  5       nUS S 2US S 2S 4   US S S 24   4   nUS S 2US S 2S 4   US S S 24   4   nUS S 2US S 2S 4   US S S 24   4   nUS S 2US S 2S 4   US S S 24   4   n UR                  SU	S5      n!UR                  SU	S5      n"UR                  SSU
5      n#UR                  SSU
5      n$U"U$U-  U#U-  -   -  U!U$U-  U#U -  -   -  -   n%U%S-   S	-	  n%U%R%                  SS5      R                  [        R&                  5      n&U&R                  X4Xš5      R                  UR*                  S
9$ )N)ÚdtypeÚdeviceg      à?r   é   r   i   éÿ   i    é   )r   )ÚshapeÚviewr   Úmaxr   r   Úroundr   ÚarangeÚfloat32ÚfloatÚfloorÚtoÚint32ÚwhereÚ
zeros_likeÚ	ones_likeÚ	full_likeÚclampÚuint8Úlongr   )'Úselfr   r   Ú
batch_sizeÚchannelsr   r   r   ÚscaleÚtarget_heightÚtarget_widthÚ
target_colÚsrc_colÚsrc_col_floorÚsrc_col_fracÚweight_rightÚweight_leftÚ
target_rowÚsrc_rowÚsrc_row_floorÚsrc_row_fracÚweight_bottomÚ
weight_topÚimage_uint8Úimage_int32Úcol_leftÚ	col_rightÚrow_topÚ
row_bottomÚpixel_top_leftÚpixel_top_rightÚpixel_bottom_leftÚpixel_bottom_rightÚweight_bottom_3dÚweight_top_3dÚweight_right_3dÚweight_left_3dÚinterpÚresults'                                          Úq/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/models/slanext/image_processing_slanext.pyÚ_resizeÚSLANeXtImageProcessor._resize4   s£  € ð
 /4¯k©kÑ+ˆ
˜fØ—
‘
˜:Ñ0°&Ó@ˆà—‘ˆä�D—K‘K §¡Ó,¬s°6Ó/AÑAˆÜ˜f™nÓ-ˆÜ˜U™]Ó+ˆä—\’\ ,´e·m±mÈFÑSˆ
Ø Ñ#¬¨e«´u¸\Ó7JÑ(JÑKÈcÑQˆØŸ™›×*Ñ*¬5¯;©;Ó7ˆØ×!4Ñ!4Ó!6Ñ6ˆä—{’{ =°1Ñ#4´e×6FÒ6FÀ|Ó6TÐVbÓcˆÜŸš M°AÑ$5´u×7GÒ7GÈÓ7VÐXeÓfˆÜ—{’{ =¸A±IÑ#=¼u¿ºÈ|Ó?\Ð^jÓkˆÜŸšØ Q™YÑ&¬¯ª¸ÈqÁyÓ(QÐS`ó
ˆð % tÑ+¨cÑ1×8Ñ8Ó:×=Ñ=¼e¿k¹kÓJˆØ˜\Ñ)ˆä—\’\ -´u·}±}ÈVÑTˆ
Ø Ñ#¬¨f«¼¸mÓ8LÑ(LÑMÐPSÑSˆØŸ™›×*Ñ*¬5¯;©;Ó7ˆØ ×!4Ñ!4Ó!6Ñ6ˆÜ—{’{ =°1Ñ#4´e×6FÒ6FÀ|Ó6TÐVbÓcˆÜŸš M°AÑ$5´u×7GÒ7GÈÓ7VÐXeÓfˆÜ—{’{ =°F¸Q±JÑ#>ÄÇÂÐP\Ó@]Ð_kÓlˆÜŸšØ˜V a™ZÑ'¬¯ª¸ÈÐQRÉ
Ó)SÐUbó
ˆð &¨Ñ,¨sÑ2×9Ñ9Ó;×>Ñ>¼u¿{¹{ÓKˆØ˜MÑ)ˆ
à—k‘k ! SÓ)×,Ñ,¬U¯[©[Ó9ˆØ!—n‘n¤U§[¡[Ó1ˆØ ×%Ñ%Ó'ˆØ" QÑ&×,Ñ,Ó.ˆ	Ø×$Ñ$Ó&ˆØ# aÑ'×-Ñ-Ó/ˆ
à$¢Q¨²°4°Ñ(8¸(À4ÊÀ7Ñ:KÐ%KÑLˆØ%¢a¨²°D°Ñ)9¸9ÀTÊ1ÀWÑ;MÐ&MÑNˆØ'ª¨:²a¸°gÑ+>ÀÈÊqÈÑ@QÐ(QÑRÐØ(ª¨J²q¸$°wÑ,?ÀÈ4ÒQRÈ7ÑASÐ)SÑTÐà(×-Ñ-¨a°ÀÓBÐØ"Ÿ™¨¨=¸!Ó<ˆØ&×+Ñ+¨A¨q°,Ó?ˆØ$×)Ñ)¨!¨Q°Ó=ˆØØ˜^Ñ+¨oÀÑ.OÑOñ
à Ð1BÑ BÀ_ÐWiÑEiÑ iÑjñkˆð ˜GÑ$¨Ñ+ˆØ—‘˜a Ó%×(Ñ(¬¯©Ó5ˆà�{‰{˜:°ÓM×PÑPÐW\×WbÑWbÐPÐcÐcó    ÚimagesÚ	do_resizeÚresamplez"tvF.InterpolationMode | int | NoneÚdo_center_cropÚ	crop_sizeÚ
do_rescaleÚrescale_factorÚdo_normalizeÚ
image_meanNÚ	image_stdÚdo_padÚpad_sizeÚdisable_groupingÚreturn_tensorsc           	      ó  • Ub$  [        5       (       d  [        R                  S5        [        XS9u  nn0 nUR	                  5        H"  u  nnU(       a  U R                  UUS9nUUU'   M$     [        UU5      n[        UUS9u  nn0 nUR	                  5        H8  u  nnU(       a  U R                  UU5      nU R                  UXxXšU5      nUUU'   M:     [        UU5      nU(       a  U R                  UXÞS9n[        SU0US9$ )Nz&Resampling is not supported in SLANeXt)rf   )r   r   )re   rf   Úpixel_values)ÚdataÚtensor_type)r   ÚloggerÚwarning_oncer   ÚitemsrW   r   Úcenter_cropÚrescale_and_normalizeÚpadr   )r4   rZ   r[   r   r\   r]   r^   r_   r`   ra   rb   rc   rd   re   rf   rg   ÚkwargsÚgrouped_imagesÚgrouped_images_indexÚresized_images_groupedr#   Ústacked_imagesÚresized_imagesÚprocessed_images_groupedÚprocessed_imagess                            rV   Ú_preprocessÚ!SLANeXtImageProcessor._preprocessv   s/  € ð& ÑÔ(@×(BÑ(BÜ×ÑÐ HÔIô 0EÀVÑ/oÑ,ˆÐ,Ø!#ÐØ%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%§¡°NÈ Ð!N�Ø,:Ð" 5Ó)ñ &<ô (Ð(>Ð@TÓUˆô 0EÀ^ÐfvÑ/wÑ,ˆÐ,Ø#%Ð Ø%3×%9Ñ%9Ö%;Ñ!ˆE�>ÞØ!%×!1Ñ!1°.À)Ó!L�à!×7Ñ7Ø 
¸LÐV_óˆNð /=Ð$ UÓ+ñ &<ô *Ð*BÐDXÓYÐæØ#Ÿx™xÐ(8À8˜xÐoÐä .Ð2BÐ!CÐQ_Ñ`Ð`rY   rr   c                 óF   >• [         TU ]  " S0 UD6  U R                  5         g )N© )ÚsuperÚ__init__Úinit_decoder)r4   rr   Ú	__class__s     €rV   r   ÚSLANeXtImageProcessor.__init__¨   s   ø€ Ü‰ÒÑ"˜6Ò"Ø×ÑÕrY   c                 óì  • / SQnU[        S5       Vs/ s H  nSUS-    S3PM     sn-  nU[        S5       Vs/ s H  nSUS-    S3PM     sn-  nSU;  a  UR                  S5        SU;   a  UR                  S5        S	/U-   S
/-   n[        U5       VVs0 s H  u  p#X2_M	     snnU l        Xl        / SQU l        U R                  S	   U l        U R                  S
   U l        gs  snf s  snf s  snnf )a•  
Initialize the decoder vocabulary for table structure recognition.

Builds a character dictionary mapping HTML table structure tokens (e.g., `<thead>`, `<tr>`, `<td>`, colspan/
rowspan attributes) to integer indices. The dictionary includes special `"sos"` (start-of-sequence) and
`"eos"` (end-of-sequence) tokens. Merged `<td></td>` tokens are used in place of standalone `<td>` tokens
when applicable.
)
z<thead>z</thead>z<tbody>z</tbody>z<tr>z</tr>ú<td>ú<tdÚ>z</td>é   z
 colspan="r   Ú"z
 rowspan="ú	<td></td>r„   ÚsosÚeos)r„   r…   r‰   N)	ÚrangeÚappendÚremoveÚ	enumerateÚdictÚ	characterÚtd_tokenÚbos_idÚeos_id)r4   Údict_characterÚiÚchars       rV   r€   Ú"SLANeXtImageProcessor.init_decoder¬   sú   € ò
ˆð 	¼%À¼)ÓDº)°Q˜Z¨¨A© w¨aÓ0¹)ÑDÑDˆØ¼%À¼)ÓDº)°Q˜Z¨¨A© w¨aÓ0¹)ÑDÑDˆà˜nÓ,Ø×!Ñ! +Ô.Ø�^Ó#Ø×!Ñ! &Ô)à˜ >Ñ1°U°GÑ;ˆÜ,5°nÔ,EÔFÒ,E¡ �T’WÑ,EÒFˆŒ	Ø'ŒÚ4ˆŒØ—i‘i Ñ&ˆŒØ—i‘i Ñ&ˆ�ùò EùÚDùó Gs   “C&¹C+ÂC0c                 óú  • UR                   U l        U R                  SS n[        U R                  5      [        U R                  5      /n[        U R                  5      nUR                  SS9nUR                  SS9R                  n/ nUR                  S   n[        U5       HÁ  n/ n	/ n
[        UR                  S   5       H[  n[        XXU4   5      nUS:”  a  XÄ:X  a    O@XÃ;   a  M&  U R                  U   nU	R                  U5        U
R                  X(U4   5        M]     UR                  U	5        [        R                  " U
5      R                  5       R                  5       nMÃ     / SQUS   -   / SQ-   nUWS.$ )a×  
Post-process the raw model outputs to decode the predicted table structure into an HTML token sequence.

Converts the model's predicted probability distributions over the structure vocabulary into a sequence of
HTML tokens representing the table structure. The decoded tokens are wrapped with `<html>`, `<body>`, and
`<table>` tags to form a complete HTML table structure.

Args:
    outputs ([`SLANeXtForTableRecognitionOutput`]):
        Raw outputs from the SLANeXt model. The `last_hidden_state` field contains the predicted probability
        distributions over the structure vocabulary at each decoding step, with shape
        `(batch_size, max_text_length, num_classes)`.

Returns:
    `dict`: A dictionary containing:
        - **structure** (`list[str]`): The predicted HTML table structure as a list of tokens, wrapped with
          `<html>`, `<body>`, and `<table>` tags.
        - **structure_score** (`float`): The mean confidence score across all predicted tokens.
r   r    r   )Údim)z<html>z<body>z<table>)z</table>z</body>z</html>)Ú	structureÚstructure_score)Úlast_hidden_stateÚpredÚintr“   r”   Úargmaxr%   Úvaluesr#   rŒ   r‘   r�   r   ÚstackÚmeanÚitem)r4   ÚoutputsÚstructure_probsÚignored_tokensÚend_idxÚstructure_idxÚstructure_str_listr5   Úbatch_indexÚstructure_listÚ
score_listÚpositionÚchar_idxÚtextrœ   r›   s                   rV   Úpost_process_table_recognitionÚ4SLANeXtImageProcessor.post_process_table_recognitionÐ   sn  € ð( ×-Ñ-ˆŒ	ØŸ)™) A a˜.ˆÜ˜dŸk™kÓ*¬C°·±Ó,<Ð=ˆÜ�d—k‘kÓ"ˆà'×.Ñ.°1Ð.Ð5ˆØ)×-Ñ-°!Ð-Ð4×;Ñ;ˆàÐØ"×(Ñ(¨Ñ+ˆ
Ü  Ö,ˆKØˆNØˆJÜ! -×"5Ñ"5°aÑ"8Ö9�Ü˜}¸(Ð-BÑCÓD�Ø˜a“< HÓ$7ÙØÓ-ÙØ—~‘~ hÑ/�Ø×%Ñ% dÔ+Ø×!Ñ! /¸xÐ2GÑ"HÖIñ :ð ×%Ñ% nÔ5Ü#Ÿkšk¨*Ó5×:Ñ:Ó<×AÑAÓCŠOñ -ò 4Ð6HÈÑ6KÑKÒNpÑpˆ	Ø&¸?ÑKÐKrY   )r“   r‘   r�   r”   rž   r’   ) Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r\   r   rb   r	   rc   r   re   Údo_convert_rgbr[   r_   ra   rd   r
   rW   ÚlistÚboolr)   Ústrr   r   rz   r   r   r   r€   r±   Ú__static_attributes__Ú__classcell__)r�   s   @rV   r   r   &   sz  ø† ð €HØ&€JØ$€IØ CÑ(€DØ¨Ñ,€HØ€NØ€IØ€JØ€LØ€Fð@dàð@dð ð@dð 
ô	@dðD0aà�^Ñ$ð0að ð0að ð	0að
 7ð0að ð0að ð0að ð0að ð0að ð0að ˜D ™KÑ'¨$Ñ.ð0að ˜4 ™;Ñ&¨Ñ-ð0að �t‘ð0að ˜T‘/ð0að  ™+ð0að  ˜jÑ(¨4Ñ/ð!0að$ 
ô%0aðd ¨Ñ!5÷ ò"'÷H.Lð .LrY   r   )!r   Ú$torchvision.transforms.v2.functionalÚ
transformsÚv2Ú
functionalÚtvFÚimage_processing_backendsr   Úimage_processing_utilsr   Úimage_transformsr   r   Úimage_utilsr   r	   r
   Úprocessing_utilsr   r   Úutilsr   r   r   Úutils.genericr   Úutils.import_utilsr   Ú
get_loggerr³   rl   r   Ú__all__r}   rY   rV   Ú<module>rÌ      sz   ðó, ß 2Ó 2å ;Ý 2ß Eß PÑ Pß 4ß FÑ FÝ 'Ý *ð 
×	Ò	˜HÓ	%€ð Ù	�:ÑôVLÐ.ó VLó ó ðVLðr #Ð
#�rY   