
    cCi$                         S SK JrJr  SSKJr  SSKJr  SSKJrJ	r	J
r
  SSKJrJr  SSKJr  \R                   " \5      r " S S	\S
S9r " S S\	5      rS/rg)    )OptionalUnion   )BatchFeature)
ImageInput)ProcessingKwargsProcessorMixinUnpack)PreTokenizedInput	TextInput)loggingc                   "    \ rS rSrSS00 S.rSrg)Ovis2ProcessorKwargs   paddingF)text_kwargsimage_kwargs N)__name__
__module____qualname____firstlineno__	_defaults__static_attributes__r       d/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/ovis2/processing_ovis2.pyr   r      s     u
 	Ir   r   F)totalc            
          ^  \ rS rSrSrSS/rSrSr     SU 4S jjr  SS\	\
   S	\\\\\   \\   4   S
\\   S\4S jjrS	\\   S\\\      4S jrS rS r\S 5       rSrU =r$ )Ovis2Processor%   a  
Constructs a Ovis2 processor which wraps Ovis2 image processor and a Qwen2 tokenizer into a single processor.

[`Ovis2Processor`] offers all the functionalities of [`Ovis2VideoProcessor`], [`Ovis2ImageProcessor`] and [`Qwen2TokenizerFast`]. See the
[`~Ovis2Processor.__call__`] and [`~Ovis2Processor.decode`] for more information.

Args:
    image_processor ([`Ovis2ImageProcessor`], *optional*):
        The image processor is a required input.
    tokenizer ([`Qwen2TokenizerFast`], *optional*):
        The tokenizer is a required input.
    chat_template (`str`, *optional*): A Jinja template which will be used to convert lists of messages
        in a chat into a tokenizable string.
    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_processor	tokenizerAutoImageProcessorAutoTokenizerc                    > XPl         [        US5      (       a  UR                  OUU l        [        USS 5      (       a  UR                  OUR                  U R                  5      U l        [        TU ]  " X4SU0UD6  g )Nimage_tokenimage_token_idchat_template)image_seq_lengthhasattrr&   getattrr'   convert_tokens_to_idssuper__init__)selfr!   r"   r(   r&   r)   kwargs	__class__s          r   r.   Ovis2Processor.__init__=   s{     !14;I}4U4U900[f y"2D99 $$001A1AB 	
 	[=[TZ[r   imagestextr0   returnc                    U R                   " [        4SU R                  R                  0UD6n[	        U[
        5      (       a  U/nO8[	        U[        5      (       d#  [	        US   [
        5      (       d  [        S5      e0 nUbF  U R                  " U40 US   D6nUR                  S5      R                  5       nU R                  X&5      nU R                  " U40 US   D6n[        0 UEUES9$ )a  
Main method to prepare for the model one or several sequences(s) and image(s). This method forwards the `text`
and `kwargs` arguments to Qwen2TokenizerFast's [`~Qwen2TokenizerFast.__call__`] if `text` is not `None` to encode
the text. To prepare the image(s), this method forwards the `images` and `kwargs` arguments to
Ovis2ImageProcessor's [`~Ovis2ImageProcessor.__call__`] if `images` is not `None`. Please refer to the docstring
of the above two methods for more information.

Args:
    images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`):
        The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
        tensor. Both channels-first and channels-last formats are supported.
    text (`str`, `List[str]`, `List[List[str]]`):
        The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
        (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
        `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).

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_kwargsr   zAInvalid input text. Please provide a string, or a list of stringsimages_kwargsgridsr   )data)_merge_kwargsr   r"   init_kwargs
isinstancestrlist
ValueErrorr!   poptolist_expand_image_tokensr   )r/   r3   r4   r0   output_kwargsimage_inputsimage_gridstext_inputss           r   __call__Ovis2Processor.__call__O   s    B ** 
"&.."<"<
 
 dC  6DD$''
47C0H0H`aa//Y-:XYL&**73::<K,,T?DnnTJ]=-IJ!@K!@<!@AAr   r9   c                    / nSnU H  nSU;   a  X$   nUS   US   pSSU R                   -   S3n	Xx-  S:  aU  [        U5       HF  n
[        U5       H%  nU	SU R                   -   -  n	XS-
  :  d  M   U	S-  n	M'     XS-
  :  d  MA  U	S-  n	MH     U	S	-  n	UR                  SU	S5      nUS-  nSU;   a  M  UR                  U5        M     U$ )
Nr   <image>   z<IMG_START>z
<IMG_ATOM>z
<IMG_GRID>z	<IMG_COL>z	<IMG_ROW>z	<IMG_END>)r)   rangereplaceappend)r/   r4   r9   processed_text
grid_indexsamplegridrowcolplaceholderrcs               r   rC   #Ovis2Processor._expand_image_tokens   s    
 
Fv%(7DGS +L4;P;P,P+QQ[\9q="3Z!&sA'lT=R=R.R-STK 7{ +{ : ", Qw;';6K ( {*	;Ba
 v%  !!&)# $ r   c                 :    U R                   R                  " U0 UD6$ )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/   argsr0   s      r   r[   Ovis2Processor.batch_decode   s    
 ~~**D;F;;r   c                 :    U R                   R                  " U0 UD6$ )z
This method forwards all its arguments to Qwen2TokenizerFast's [`~PreTrainedTokenizer.decode`]. Please refer to
the docstring of this method for more information.
)r"   decoder\   s      r   r`   Ovis2Processor.decode   s    
 ~~$$d5f55r   c                     U R                   R                  nU R                  R                  n[        U5      [        U5      -   $ )N)r"   model_input_namesr!   r?   )r/   tokenizer_input_namesimage_processor_input_namess      r   rc    Ovis2Processor.model_input_names   s;     $ @ @&*&:&:&L&L#)*T2M-NNNr   )r)   r&   r'   )NNNrK      )NN)r   r   r   r   __doc__
attributesimage_processor_classtokenizer_classr.   r   r   r   r   r   r?   r
   r   r   rH   intrC   r[   r`   propertyrc   r   __classcell__)r1   s   @r   r   r   %   s    & $[1J0%O \( (,^b4B$4B I0$y/4HYCZZ[4B -.	4B
 
4Bl9o DI6<6 O Or   r   N)typingr   r   feature_extraction_utilsr   image_utilsr   processing_utilsr   r	   r
   tokenization_utils_baser   r   utilsr   
get_loggerr   loggerr   r   __all__r   r   r   <module>rx      sX     # 4 % H H C  
		H	%+5 MO^ MO` 
r   