o
    Zh                     @   s   d 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
 ddlmZmZmZ dd	lmZmZ G d
d deddZG dd deZdgZdS )z
Processor class for TrOCR.
    N)contextmanager)ListUnion   )BatchFeature)
ImageInput)ProcessingKwargsProcessorMixinUnpack)PreTokenizedInput	TextInputc                   @   s   e Zd Zi ZdS )TrOCRProcessorKwargsN)__name__
__module____qualname__	_defaults r   r   Y/var/www/auris/lib/python3.10/site-packages/transformers/models/trocr/processing_trocr.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d
e	e
eee
 ee f dee defddZdd Zdd Zedd Zedd Zedd Z  ZS )TrOCRProcessora  
    Constructs a TrOCR processor which wraps a vision image processor and a TrOCR tokenizer into a single processor.

    [`TrOCRProcessor`] offers all the functionalities of [`ViTImageProcessor`/`DeiTImageProcessor`] and
    [`RobertaTokenizer`/`XLMRobertaTokenizer`]. See the [`~TrOCRProcessor.__call__`] and [`~TrOCRProcessor.decode`] for
    more information.

    Args:
        image_processor ([`ViTImageProcessor`/`DeiTImageProcessor`], *optional*):
            An instance of [`ViTImageProcessor`/`DeiTImageProcessor`]. The image processor is a required input.
        tokenizer ([`RobertaTokenizer`/`XLMRobertaTokenizer`], *optional*):
            An instance of [`RobertaTokenizer`/`XLMRobertaTokenizer`]. The tokenizer is a required input.
    image_processor	tokenizerZAutoImageProcessorZAutoTokenizerNc                    sr   d }d|v rt dt |d}|d ur|n|}|d u r!td|d u r)tdt || | j| _d| _	d S )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__r   r   r   4   s   

zTrOCRProcessor.__init__imagestextr#   returnc           	      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r4| j|fi |d }|durC| j|fi |d }|du rI|S |du rO|S |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
        [`~TrOCRProcessor.as_target_processor`] this method forwards all its arguments to TrOCRTokenizer's
        [`~TrOCRTokenizer.__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.Ztokenizer_init_kwargsZimages_kwargsZtext_kwargsZ	input_idslabels)r!   r    r   Z_merge_kwargsr   r   Zinit_kwargsr   )	r"   r&   r'   ZaudioZvideosr#   Zoutput_kwargsZinputs	encodingsr   r   r   __call__H   s*   zTrOCRProcessor.__call__c                 O      | j j|i |S )z
        This method forwards all its arguments to TrOCRTokenizer's [`~PreTrainedTokenizer.batch_decode`]. Please refer
        to the docstring of this method for more information.
        )r   batch_decoder"   argsr#   r   r   r   r-   p      zTrOCRProcessor.batch_decodec                 O   r,   )z
        This method forwards all its arguments to TrOCRTokenizer's [`~PreTrainedTokenizer.decode`]. Please refer to the
        docstring of this method for more information.
        )r   decoder.   r   r   r   r1   w   r0   zTrOCRProcessor.decodec                 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_processor~   s   
z"TrOCRProcessor.as_target_processorc                 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_classr2   r   r   r   feature_extractor_class   
   z&TrOCRProcessor.feature_extractor_classc                 C   r4   )Nz[`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.)r   r   r   r   r2   r   r   r   r      r7   z TrOCRProcessor.feature_extractor)NN)NNNN)r   r   r   __doc__
attributesr5   Ztokenizer_classr   r   r   r   r   r   r
   r   r   r+   r-   r1   r   r3   propertyr6   r   __classcell__r   r   r$   r   r   !   s6    
(

r   )r8   r   
contextlibr   typingr   r   Zimage_processing_utilsr   Zimage_utilsr   Zprocessing_utilsr   r	   r
   Ztokenization_utils_baser   r   r   r   __all__r   r   r   r   <module>   s   
~