
    Igr                     >    d Z ddlZddlmZ ddlmZ  G d de      Zy)z
Processor class for TrOCR.
    N)contextmanager   )ProcessorMixinc                   v     e Zd ZdZddgZdZdZd fd	Zd Zd Z	d	 Z
ed
        Zed        Zed        Z x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	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        g/var/www/html/answerous/venv/lib/python3.12/site-packages/transformers/models/trocr/processing_trocr.pyr   zTrOCRProcessor.__init__,   s     &(MM
 !'

+> ?-<-H/N_"HIIABB)4!%!5!5*/'    c                 R   | j                   r | j                  |i |S |j                  dd      }|j                  dd      }t        |      dkD  r
|d   }|dd }||t	        d      | | j
                  |g|i |}| | j                  |fi |}|S |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 doctsring of the above two methods for more information.
        imagesNtextr      zBYou need to specify either an `images` or `text` input to process.	input_idslabels)r   r   r   lenr   r   r	   )r   argsr   r   r   inputs	encodingss          r   __call__zTrOCRProcessor.__call__@   s     **)4))4:6::Hd+zz&$'t9q=!WF8D>dlabb)T))&B4B6BF&t6v6I<M^(5F8Mr   c                 :     | 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   r#   r   s      r   r(   zTrOCRProcessor.batch_decodea   s     
 +t~~**D;F;;r   c                 :     | j                   j                  |i |S )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)   s      r   r+   zTrOCRProcessor.decodeh   s     
 %t~~$$d5f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"TrOCRProcessor.as_target_processoro   sH     
 	9	

 +/'!%!%!5!5*/'s   AA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_classr-   s    r   feature_extractor_classz&TrOCRProcessor.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   r-   s    r   r   z TrOCRProcessor.feature_extractor   s"    i	
 ###r   )NN)__name__
__module____qualname____doc__
attributesr0   tokenizer_classr   r&   r(   r+   r   r.   propertyr1   r   __classcell__)r   s   @r   r   r      sp     $[1J0%O0(B<6 0 0 * * $ $r   r   )r6   r   
contextlibr   processing_utilsr   r    r   r   <module>r>      s#     % .t$^ t$r   