o
    ei                     @   s   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
 d dlmZmZ eeZG dd dedd	ZeG d
d deZdgZdS )   )BatchFeature)
ImageInput)ProcessingKwargsProcessorMixinUnpack)PreTokenizedInput	TextInput)auto_docstringloggingc                   @   s   e Zd Zddii dZdS )Ovis2ProcessorKwargspaddingF)text_kwargsimage_kwargsN)__name__
__module____qualname__	_defaults r   r   h/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/transformers/models/ovis2/processing_ovis2.pyr      s
    
r   F)totalc                
       s   e Zd Z					d 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ee deee  fddZdd Zdd Zedd Z  ZS )Ovis2ProcessorN<image>   c                    sZ   || _ t|dr|jn|| _t|ddr|jn|| j| _t j||fd|i| dS )a  
        image_token (`str`, *optional*, defaults to `"<image>"`):
            Special token used to denote image location.
        image_seq_length (`int`, *optional*, defaults to 256):
            The number of image tokens to be used for each image in the input.
        image_tokenimage_token_idNchat_template)image_seq_lengthhasattrr   getattrr   convert_tokens_to_idssuper__init__)selfimage_processor	tokenizerr   r   r   kwargs	__class__r   r   r!   %   s   

zOvis2Processor.__init__imagestextr%   returnc                 K   s   | j tfd| jji|}t|tr|g}nt|ts&t|d ts&tdi }|durD| j|fi |d }|	d
 }| ||}| j|fi |d }ti ||dS )	a  
        Returns:
            [`BatchFeature`]: A [`BatchFeature`] with the following fields:

            - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`.
            - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
              `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
              `None`).
            - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
            - **image_sizes** -- Size of each image that will be used to unpad an image. Returned when `images` is not `None`.
        tokenizer_init_kwargs    zAInvalid input text. Please provide a string, or a list of stringsNimages_kwargsgridsr   )data)_merge_kwargsr   r$   init_kwargs
isinstancestrlist	TypeErrorr#   poptolist_expand_image_tokensr   )r"   r(   r)   r%   output_kwargsimage_inputsimage_gridstext_inputsr   r   r   __call__=   s$   
zOvis2Processor.__call__r.   c                 C   s   g }d}|D ]d}d|v re|| }|d |d }}dd| j   d}	|| dkrRt|D ]%}
t|D ]}|	d| j   7 }	||d k rF|	d7 }	q2|
|d k rQ|	d7 }	q,|	d	7 }	|d|	d}|d7 }d|v s|| q|S )
Nr,   r      z<IMG_START>z
<IMG_ATOM>z
<IMG_GRID>z	<IMG_COL>z	<IMG_ROW>z	<IMG_END>)r   rangereplaceappend)r"   r)   r.   processed_text
grid_indexsamplegridrowcolplaceholderrcr   r   r   r8   e   s.   z#Ovis2Processor._expand_image_tokensc                 O      | j j|i |S )z
        This method forwards all its arguments to Qwen2TokenizerFast's [`~PreTrainedTokenizer.batch_decode`]. Please
        refer to the docstring of this method for more information.
        )r$   batch_decoder"   argsr%   r   r   r   rL         zOvis2Processor.batch_decodec                 O   rK   )z
        This method forwards all its arguments to Qwen2TokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
        the docstring of this method for more information.
        )r$   decoderM   r   r   r   rP      rO   zOvis2Processor.decodec                 C   s    | j j}| jj}t|t| S )N)r$   model_input_namesr#   r4   )r"   tokenizer_input_namesimage_processor_input_namesr   r   r   rQ      s   z Ovis2Processor.model_input_names)NNNr   r   )NN)r   r   r   r!   r	   r   r   r   r4   r   r   r   r=   intr8   rL   rP   propertyrQ   __classcell__r   r   r&   r   r   #   s8    '

r   N)feature_extraction_utilsr   image_utilsr   processing_utilsr   r   r   tokenization_utils_baser   r   utilsr	   r
   
get_loggerr   loggerr   r   __all__r   r   r   r   <module>   s   
	
q