o
    ei                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	m
Z
mZ ddlmZmZ ddlmZ eeZG d	d
 d
e
ddZG dd deddZeG dd de	ZdgZdS )z
Processor class for UDOP.
    )logging   )BatchFeature)
ImageInput)ProcessingKwargsProcessorMixin
TextKwargsUnpack)PreTokenizedInput	TextInput)auto_docstringc                   @   sN   e Zd ZU ee eee  B dB ed< eee  eeee   B dB ed< dS )UdopTextKwargsNword_labelsboxes)__name__
__module____qualname__listint__annotations__ r   r   f/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/transformers/models/udop/processing_udop.pyr      s   
 (r   F)totalc                   @   s2   e Zd ZU eed< ddddddddddd	iZdS )UdopProcessorKwargstext_kwargsTFr   )	add_special_tokenspadding
truncationstridereturn_overflowing_tokensreturn_special_tokens_maskreturn_offsets_mappingreturn_lengthverboseN)r   r   r   r   r   	_defaultsr   r   r   r   r   #   s   
 r   c                
       st   e Zd ZdZ fddZe		ddedB deeB e	e B e	e B de
e defd	d
Zdd Zedd Z  ZS )UdopProcessoras  
    Constructs a UDOP processor which combines a LayoutLMv3 image processor and a UDOP tokenizer into a single processor.

    [`UdopProcessor`] offers all the functionalities you need to prepare data for the model.

    It first uses [`LayoutLMv3ImageProcessor`] to resize, rescale and normalize document images, and optionally applies OCR
    to get words and normalized bounding boxes. These are then provided to [`UdopTokenizer`],
    which turns the words and bounding boxes into token-level `input_ids`, `attention_mask`, `token_type_ids`, `bbox`.
    Optionally, one can provide integer `word_labels`, which are turned into token-level `labels` for token
    classification tasks (such as FUNSD, CORD).

    Additionally, it also supports passing `text_target` and `text_pair_target` to the tokenizer, which can be used to
    prepare labels for language modeling tasks.
    c                    s   t  || d S )N)super__init__)selfimage_processor	tokenizer	__class__r   r   r'   E   s   zUdopProcessor.__init__Nimagestextkwargsreturnc                 K   s  | j tfd| jji|}|d dd }|d dd }|d dd }|d dd}|d dd}	|d d	d }
| jjrI|d urItd
| jjrU|d urUtd|r]|	s]td|
d urk| jdi |d S | jdd|i|d }|dd }|dd }|d d	d  |d dd  ||d d< |d ur|n||d d< ||d d< |d ur| jjr|d u rt	|t
r|g}||d d< | jdd|d ur|n|i|d }|du r| |d |d |d< || |S )Ntokenizer_init_kwargsr   r   r   	text_pairr   Fr!   text_targetzdYou cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True.zaYou cannot provide word labels if you initialized the image processor with apply_ocr set to True.zKYou cannot return overflowing tokens without returning the offsets mapping.r-   images_kwargswordstext_pair_targetr.   Tpixel_valuesoverflow_to_sample_mappingr   )_merge_kwargsr   r*   init_kwargspopgetr)   	apply_ocr
ValueError
isinstancestrget_overflowing_imagesupdate)r(   r-   r.   r/   output_kwargsr   r   r2   r   r!   r3   featuresfeatures_wordsfeatures_boxesencoded_inputsr   r   r   __call__H   sd   

zUdopProcessor.__call__c                 C   sL   g }|D ]	}| ||  qt|t|kr$tdt| dt| |S )Nz`Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got z and )appendlenr>   )r(   r-   r8   images_with_overflow
sample_idxr   r   r   rA      s   z$UdopProcessor.get_overflowing_imagesc                 C   s"   | j j}| jj}t|| dg S )Nbbox)r*   model_input_namesr)   r   )r(   tokenizer_input_namesimage_processor_input_namesr   r   r   rN      s   zUdopProcessor.model_input_names)NN)r   r   r   __doc__r'   r   r   r   r
   r   r	   r   r   rH   rA   propertyrN   __classcell__r   r   r+   r   r%   4   s$    Ir%   N)rQ   transformersr   image_processing_utilsr   image_utilsr   processing_utilsr   r   r   r	   tokenization_utils_baser
   r   utilsr   
get_loggerr   loggerr   r   r%   __all__r   r   r   r   <module>   s   

s