o
    i!                     @   s   d Z ddlZddlZddlmZ ddl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 deddZeeZG dd deZdgZdS )z
Processor class for Donut.
    N)contextmanager)OptionalUnion   )
ImageInput)ProcessingKwargsProcessorMixinUnpack)PreTokenizedInput	TextInput)loggingc                   @   s   e Zd Zi ZdS )DonutProcessorKwargsN)__name__
__module____qualname__	_defaults r   r   ^/home/ubuntu/.local/lib/python3.10/site-packages/transformers/models/donut/processing_donut.pyr      s    r   F)totalc                
       s   e Zd ZdZddgZdZdZd fdd	Z				dd	ee	 d
ee
eee eef  dee fddZedd Zedd ZdddZedd Zedd Z  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AutoTokenizerNc                    sR   d }d|v rt dt |d}|d ur|n|}t || | j| _d| _d S )Nfeature_extractorzhThe `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor` instead.F)	warningswarnFutureWarningpopsuper__init__r   current_processor_in_target_context_manager)selfr   r   kwargsr   	__class__r   r   r    9   s   

zDonutProcessor.__init__imagestextr$   c           	      K   s   | j r| j||fi |S |du r|du rtd| jtfd| jji|}|dur5| j|fi |d }|durP|durE|d dd | j|fi |d }|du rV|S |du r\|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.
        NzBYou 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!   
ValueError_merge_kwargsr   r   init_kwargsr   
setdefault)	r#   r'   r(   audiovideosr$   output_kwargsinputs	encodingsr   r   r   __call__I   s0   zDonutProcessor.__call__c                 C   s   | j j}t|ddg S )Nr-   r.   )r   model_input_nameslist)r#   image_processor_input_namesr   r   r   r9   s   s   z DonutProcessor.model_input_namesc                 c   s2    t d d| _| j| _dV  | j| _d| _dS )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#   r   r   r   as_target_processory   s   
z"DonutProcessor.as_target_processorFc                 C   s@  |du r	| j  }i }|rtd|tj}|du rn|| d }d|vr(n|d|dd  }|tdtd  }t|}td| d|tj}	|	du rZ|	|d}n|	
 }	t|}
t|	}t|
 d| |tjtjB }|dur|
d }d|v rd|v r| j|d|d	}|rt|dkr|d
 }|||< n?g ||< |dD ]%}| }||v r|d
 dkr|dd dkr|dd }|| | qt|| dkr|| d
 ||< |||	t|	 d  }|dd dkr
|g| j|dd d|d	 S |s|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(.*?)T)is_inner_valueadded_vocabr   z<sep/><z/>   text_sequence)r   get_added_vocabresearch
IGNORECASEstartindexlenescapereplacegroupDOTALLstrip
token2jsonsplitappendfind)r#   tokensrA   rB   outputpotential_startstart_tokenkeykey_escaped	end_tokenstart_token_escapedend_token_escapedcontentvalueleafr   r   r   rS      s\   



$,zDonutProcessor.token2jsonc                 C      t 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_classr<   r   r   r   feature_extractor_class   
   z&DonutProcessor.feature_extractor_classc                 C   rc   )Nz[`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.)r   r   r   r   r<   r   r   r   r      rf   z DonutProcessor.feature_extractor)NN)NNNN)FN)r   r   r   __doc__
attributesrd   tokenizer_classr    r   r   r   strr:   r   r
   r	   r   r8   propertyr9   r   r=   rS   re   r   __classcell__r   r   r%   r   r   %   s4    
*


:
r   )rg   rH   r   
contextlibr   typingr   r   image_utilsr   processing_utilsr   r   r	   tokenization_utils_baser
   r   utilsr   r   
get_loggerr   loggerr   __all__r   r   r   r   <module>   s   
 
0