Ë
    S^(h#  ã                   óÎ   — d Z ddlZddl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mZmZ ddlmZmZ dd	lmZ  G d
„ ded¬«      Z ej*                  e«      Z G d„ de«      ZdgZy)z
Processor class for Donut.
é    N)Úcontextmanager)ÚListÚOptionalÚUnioné   )Ú
ImageInput)ÚProcessingKwargsÚProcessorMixinÚUnpack)ÚPreTokenizedInputÚ	TextInput)Úloggingc                   ó   — e Zd Zi Zy)ÚDonutProcessorKwargsN)Ú__name__Ú
__module__Ú__qualname__Ú	_defaults© ó    úh/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/donut/processing_donut.pyr   r      s   „ Ø�Ir   r   F)Útotalc            
       ó¶   ‡ — e Zd ZdZddgZdZdZdˆ fd„	Z	 	 	 	 ddede	e
eee   eef      d	ee   fd
„Zd„ Zd„ Zed„ «       Zdd„Zed„ «       Zed„ «       Zˆ xZS )ÚDonutProcessoraÙ  
    Constructs a Donut processor which wraps a Donut image processor and an XLMRoBERTa tokenizer into a single
    processor.

    [`DonutProcessor`] offers all the functionalities of [`DonutImageProcessor`] and
    [`XLMRobertaTokenizer`/`XLMRobertaTokenizerFast`]. See the [`~DonutProcessor.__call__`] and
    [`~DonutProcessor.decode`] for more information.

    Args:
        image_processor ([`DonutImageProcessor`], *optional*):
            An instance of [`DonutImageProcessor`]. The image processor is a required input.
        tokenizer ([`XLMRobertaTokenizer`/`XLMRobertaTokenizerFast`], *optional*):
            An instance of [`XLMRobertaTokenizer`/`XLMRobertaTokenizerFast`]. The tokenizer is a required input.
    Úimage_processorÚ	tokenizerÚAutoImageProcessorÚAutoTokenizerc                 óø   •— d }d|v r+t        j                  dt        «       |j                  d«      }|�|n|}|€t	        d«      ‚|€t	        d«      ‚t
        ‰| �  ||«       | j                  | _        d| _	        y )NÚfeature_extractorzhThe `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor` instead.z)You need to specify an `image_processor`.z"You need to specify a `tokenizer`.F)
ÚwarningsÚwarnÚFutureWarningÚpopÚ
ValueErrorÚsuperÚ__init__r   Úcurrent_processorÚ_in_target_context_manager)Úselfr   r   Úkwargsr    Ú	__class__s        €r   r'   zDonutProcessor.__init__9   s�   ø€ Ø ÐØ &Ñ(Ü�M‰Mðäôð
 !'§
¡
Ð+>Ó ?Ðà-<Ð-H™/ÐN_ˆØÐ"ÜÐHÓIÐIØÐÜÐAÓBÐBä‰Ñ˜¨)Ô4Ø!%×!5Ñ!5ˆÔØ*/ˆÕ'r   ÚimagesÚtextr+   c                 ó|  — | j                   r | j                  ||fi |¤ŽS |€|€t        d«      ‚ | j                  t        fd| j
                  j                  i|¤Ž}|� | j                  |fi |d   ¤Ž}|�-|�|d   j                  dd«        | j
                  |fi |d   ¤Ž}|€S |€S d   d<   |d   |d<   |S )	a   
        When used in normal mode, this method forwards all its arguments to AutoImageProcessor's
        [`~AutoImageProcessor.__call__`] and returns its output. If used in the context
        [`~DonutProcessor.as_target_processor`] this method forwards all its arguments to DonutTokenizer's
        [`~DonutTokenizer.__call__`]. Please refer to the docstring of the above two methods for more information.
        zBYou need to specify either an `images` or `text` input to process.Útokenizer_init_kwargsÚimages_kwargsÚtext_kwargsÚadd_special_tokensFÚ	input_idsÚlabels)	r)   r(   r%   Ú_merge_kwargsr   r   Úinit_kwargsr   Ú
setdefault)	r*   r-   r.   ÚaudioÚvideosr+   Úoutput_kwargsÚinputsÚ	encodingss	            r   Ú__call__zDonutProcessor.__call__M   s  € ð ×*Ò*Ø)�4×)Ñ)¨&°$ÑA¸&ÑAÐAàˆ>˜d˜lÜÐaÓbÐbà*˜×*Ñ*Ü ñ
à"&§.¡.×"<Ñ"<ð
ð ñ
ˆð ÐØ)�T×)Ñ)¨&ÑS°MÀ/Ñ4RÑSˆFØÐØÐ!Ø˜mÑ,×7Ñ7Ð8LÈeÔTØ&˜Ÿ™ tÑL¨}¸]Ñ/KÑLˆIàˆ<ØˆMØˆ^ØÐà(¨Ñ5ˆF�8ÑØ"+¨KÑ"8ˆF�;ÑØˆMr   c                 ó:   —  | j                   j                  |i |¤ŽS )z¾
        This method forwards all its arguments to DonutTokenizer's [`~PreTrainedTokenizer.batch_decode`]. Please refer
        to the docstring of this method for more information.
        )r   Úbatch_decode©r*   Úargsr+   s      r   r@   zDonutProcessor.batch_decodew   s    € ð
 +ˆt�~‰~×*Ñ*¨DÐ;°FÑ;Ð;r   c                 ó:   —  | j                   j                  |i |¤ŽS )z¸
        This method forwards all its arguments to DonutTokenizer's [`~PreTrainedTokenizer.decode`]. Please refer to the
        docstring of this method for more information.
        )r   ÚdecoderA   s      r   rD   zDonutProcessor.decode~   s    € ð
 %ˆt�~‰~×$Ñ$ dÐ5¨fÑ5Ð5r   c              #   óž   K  — t        j                  d«       d| _        | j                  | _        d–— | j
                  | _        d| _        y­w)z�
        Temporarily sets the tokenizer for processing the input. Useful for encoding the labels when fine-tuning TrOCR.
        zï`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your labels by using the argument `text` of the regular `__call__` method (either in the same call as your images inputs, or in a separate call.TNF)r!   r"   r)   r   r(   r   ©r*   s    r   Úas_target_processorz"DonutProcessor.as_target_processor…   sH   è ø€ ô
 	�‰ð9ô	
ð
 +/ˆÔ'Ø!%§¡ˆÔÛØ!%×!5Ñ!5ˆÔØ*/ˆÕ'ùs   ‚AAc                 óà  — |€| j                   j                  «       }i }|�r4t        j                  d|t        j                  «      }|€�n|j                  d«      }t        j                  |«      }t        j                  d|› d�|t        j                  «      }|j                  «       }|€|j                  |d«      }�n’|j                  «       }t        j                  |«      }	t        j                  |«      }
t        j                  |	› d|
› �|t        j                  t        j                  z  «      }|�Ê|j                  d«      j                  «       }d|v r3d|v r/| j                  |d	|¬
«      }|r�t        |«      dk(  r|d   }|||<   ntg ||<   |j                  d«      D ]?  }|j                  «       }||v r|d   dk(  r|dd dk(  r|dd }||   j                  |«       ŒA t        ||   «      dk(  r||   d   ||<   ||j                  |«      t        |«      z   d j                  «       }|dd dk(  r|g| j                  |dd d	|¬
«      z   S |r�Œ4t        |«      r|r|gS |S |rg S d|iS )zS
        Convert a (generated) token sequence into an ordered JSON format.
        Nz	<s_(.*?)>é   z</s_ú>Ú z(.*?)z<s_T)Úis_inner_valueÚadded_vocabr   z<sep/>ú<éþÿÿÿz/>é   Útext_sequence)r   Úget_added_vocabÚreÚsearchÚ
IGNORECASEÚgroupÚescapeÚreplaceÚDOTALLÚstripÚ
token2jsonÚlenÚsplitÚappendÚfind)r*   ÚtokensrL   rM   ÚoutputÚstart_tokenÚkeyÚkey_escapedÚ	end_tokenÚstart_token_escapedÚend_token_escapedÚcontentÚvalueÚleafs                 r   r[   zDonutProcessor.token2json•   s{  € ð ÐØŸ.™.×8Ñ8Ó:ˆKàˆâÜŸ)™) L°&¼"¿-¹-ÓHˆKØÐ"ÙØ×#Ñ# AÓ&ˆCÜŸ)™) C›.ˆKäŸ	™	 T¨+¨°aÐ"8¸&Ä"Ç-Á-ÓPˆIØ%×+Ñ+Ó-ˆKØÐ ØŸ™¨°RÓ8’à%ŸO™OÓ-�	Ü&(§i¡i°Ó&<Ð#Ü$&§I¡I¨iÓ$8Ð!ÜŸ)™)Ø*Ð+¨5Ð1BÐ0CÐDÀfÌbÏmÉmÔ^`×^gÑ^gÑNgó�ð Ð&Ø%Ÿm™m¨AÓ.×4Ñ4Ó6�GØ Ñ(¨W¸Ñ-?Ø $§¡°ÈÐZe Ó f˜Ù Ü" 5›z¨QšØ(-¨a© Ø*/˜F 3šKà&(˜˜s™Ø$+§M¡M°)Ó$<ò 5˜DØ#'§:¡:£<˜DØ# {Ñ2°t¸A±wÀ#²~È$ÈrÈsÈ)ÐW[ÒJ[Ø'+¨A¨b z Ø" 3™K×.Ñ.¨tÕ4ð	5ô
 ˜v c™{Ó+¨qÒ0Ø*0°©+°a©.˜F 3™Kà §¡¨IÓ 6¼¸Y»Ñ GÐ IÐJ×PÑPÓR�Ø˜"˜1�: Ò*Ø"˜8 d§o¡o°f¸Q¸R°jÐQUÐcn oÓ&oÑoÐoóM ôP ˆvŒ;Ù-�F�8Ð9°6Ð9á'�2ÐF¨o¸vÐ-FÐFr   c                 óN   — t        j                  dt        «       | j                  S )Nzg`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.)r!   r"   r#   Úimage_processor_classrF   s    r   Úfeature_extractor_classz&DonutProcessor.feature_extractor_classË   s"   € ä�‰ØuÜô	
ð ×)Ñ)Ð)r   c                 óN   — t        j                  dt        «       | j                  S )Nz[`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.)r!   r"   r#   r   rF   s    r   r    z DonutProcessor.feature_extractorÓ   s"   € ä�‰ØiÜô	
ð ×#Ñ#Ð#r   )NN)NNNN)FN)r   r   r   Ú__doc__Ú
attributesrl   Útokenizer_classr'   r   r   r   Ústrr   r   r   r   r   r>   r@   rD   r   rG   r[   Úpropertyrm   r    Ú__classcell__)r,   s   @r   r   r   %   s¿   ø„ ñð $ [Ð1€JØ0ÐØ%€Oõ0ð, "ØNRØØñ(àð(ð �u˜S $ s¡)¨YÐ8IÐIÑJÑKð(ð Ð-Ñ.ó(òT<ò6ð ñ0ó ð0ó4Gðl ñ*ó ð*ð ñ$ó ô$r   r   )ro   rS   r!   Ú
contextlibr   Útypingr   r   r   Úimage_utilsr   Úprocessing_utilsr	   r
   r   Útokenization_utils_baser   r   Úutilsr   r   Ú
get_loggerr   Úloggerr   Ú__all__r   r   r   ú<module>r~      sf   ðñó 
Û Ý %ß (Ñ (å %ß HÑ Hß CÝ ôÐ+°5õ ð 
ˆ×	Ñ	˜HÓ	%€ôt$�^ô t$ðn Ð
�r   