
     `i!                         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ed          Z ej        e          Z G 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                       e Zd Zi ZdS )DonutProcessorKwargsN)__name__
__module____qualname__	_defaults     ~/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/donut/processing_donut.pyr   r      s        IIIr   r   F)totalc            
            e Zd ZdZddgZdZdZd fd	Z	 	 	 	 ddee	         d	ee
eee         eef                  d
ee         fdZe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AutoTokenizerNc                     d }d|v r/t          j        dt                     |                    d          }||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__s        r   r%   zDonutProcessor.__init__9   s     &((M  
 !'

+> ? ?-<-H//N_)444!%!5*/'''r   imagestextr)   c                 f   | j         r | j        ||fi |S ||t          d           | j        t          fd| j        j        i|}| | j        |fi |d         }|2||d                             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.
        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	encodingss	            r   __call__zDonutProcessor.__call__I   s    * 	B)4)&$AA&AAA>dlabbb** 
 
"&."<
 
 
 )T)&SSM/4RSSF!m,778LeTTT&tLL}]/KLLI<M^(5F8"+K"8F;Mr   c                 B    | j         j        }t          |ddgz             S )Nr2   r3   )r   model_input_nameslist)r(   image_processor_input_namess     r   r?   z DonutProcessor.model_input_namess   s'    &*&:&L#/;2IIJJJr   c              #      K   t          j        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(   s    r   as_target_processorz"DonutProcessor.as_target_processory   sW      
 	9	
 	
 	

 +/'!%!%!5*/'''r   Fc                    || j                                         }i }|rt          j        d|t          j                  }|n||                                d         }d|vrn|d|                    d          dz            }|t          d          t          d                    }t          j        |          }t          j        d| d|t          j                  }	|	|	                    |d          }n|	
                                }	t          j        |          }
t          j        |	          }t          j        |
 d| |t          j        t          j        z            }||
                    d                                          }d|v r?d|v r;|                     |d|	          }|r t          |          dk    r|d
         }|||<   ng ||<   |                    d          D ]Y}|                                }||v r$|d
         dk    r|dd         dk    r
|dd         }||                             |           Zt          ||                   dk    r||         d
         ||<   ||                    |	          t          |	          z   d                                         }|dd         dk    r$|g|                     |dd         d|	          z   S ||r|r|gn|S |rg n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(   tokensrI   rJ   outputpotential_startstart_tokenkeykey_escaped	end_tokenstart_token_escapedend_token_escapedcontentvalueleafs                  r   r[   zDonutProcessor.token2json   s0    .88::K *	p iFFO& !6!6!8!8!:!:;K+%%%&B(9(9#(>(>(B&BCKc%jjCHH945C)C..K	"8+"8"8"8&"-PPI R88%OO--	&(i&<&<#$&Ii$8$8!)*DD1BDDfbm^`^gNg  &%mmA..4466G((W-?-? $Ze f f  0"5zzQ(-a*/F3K&(s$+MM)$<$< 5 5D#'::<<D#{22tAw#~~$rss)W[J[J['+AbDz"3K..t4444vc{++q00*0+a.F3KI 6 6Y G I IJPPRR"1":**"8doofQRRjQUcno&o&oooU  *	pX  	G-9F8869'F22ov-FFr   c                 D    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_classrC   s    r   feature_extractor_classz&DonutProcessor.feature_extractor_class   s'    u	
 	
 	
 ))r   c                 D    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   rC   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=   propertyr?   r   rD   r[   rm   r   __classcell__)r*   s   @r   r   r   %   sM         $[1J0%O0 0 0 0 0 0$ (,NR( ($( uS$s)Y8IIJK( -.( ( ( (T K K XK
 0 0 ^08G 8G 8G 8Gt * * X* $ $ X$ $ $ $ $r   r   )ro   rP   r    
contextlibr   typingr   r   image_utilsr   processing_utilsr   r	   r
   tokenization_utils_baser   r   utilsr   r   
get_loggerr   loggerr   __all__r   r   r   <module>r~      s.    
			  % % % % % % " " " " " " " " % % % % % % H H H H H H H H H H C C C C C C C C          +5     
	H	%	%l$ l$ l$ l$ l$^ l$ l$ l$^ 
r   