§
    9²¸jL$  ã                   ó  — d dl mZ d dlmZmZmZmZmZmZ d dl	m
Z
mZ d dlmZmZ d dlmZ  e¦   «         rddlmZmZ  G d„ d	ed
¬¦  «        Z G d„ ded
¬¦  «        Ze ed¬¦  «         G d„ de¦  «        ¦   «         ¦   «         ZdgZdS )é   )Ú
ImageInput)ÚBatchFeatureÚMultiModalDataÚProcessingKwargsÚProcessorMixinÚ
TextKwargsÚUnpack)ÚPreTokenizedInputÚ	TextInput)Úauto_docstringÚis_vision_available)Úrequiresé   )ÚEmu3ImageProcessorKwargsÚsmart_resizec                   ó   — e Zd ZU dZeed<   dS )ÚEmu3TextKwargsaÖ  
    return_for_image_generation (`bool`, *optional*, defaults to `False`):
        Whether the processed text is intended for image generation tasks. When `True`, the processor prepares
        inputs for image generation by appending image start tokens and size information to the prompt, and
        images should not be provided. When `False`, the processor prepares inputs for text generation from
        images and text, requiring both inputs to be provided.
    Úreturn_for_image_generationN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚboolÚ__annotations__© ó    úx/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/emu3/processing_emu3.pyr   r      s*   € € € € € € ðð ð "&Ð%Ð%Ñ%Ð%Ð%r   r   F)Útotalc                   ó:   — e Zd ZU eed<   eed<   dddœdddœdœZd	S )
ÚEmu3ProcessorKwargsÚtext_kwargsÚimages_kwargsF)r   Úreturn_mm_token_type_idsz1:1i é )ÚratioÚ
image_area)r!   r"   N)r   r   r   r   r   r   Ú	_defaultsr   r   r   r    r    (   sX   € € € € € € ØÐÐÑØ+Ð+Ð+Ñ+ð ,1Ø(-ð
ð 
ð
 Ø ð
ð 
ð	ð 	€I€I€Ir   r    )Úvision)Úbackendsc            
       óÞ   ‡ — e Zd ZeZ	 dˆ fd„	Ze	 	 ddedz  dee	z  e
e         z  e
e	         z  dz  dee         defˆ fd„¦   «         Zdˆ fd„	Zd	ed
edefd„Zdd„Zd„ Zdefd„Z	 dd„Zˆ xZS )ÚEmu3ProcessorNc                 ó  •— |j         | _         |j        | _        |j        | _        |j        | _        |j        | _        |j        | _        |j	        | _	        d| _
        t          ¦   «                              |||¬¦  «         d S )Né   )Úchat_template)Úimage_tokenÚimage_token_idÚ	boi_tokenÚimage_start_tokenÚ	eoi_tokenÚimage_end_tokenÚimage_wrapper_tokenÚfake_token_around_imageÚ	eof_tokenÚ	bos_tokenÚdownsample_ratioÚsuperÚ__init__)ÚselfÚimage_processorÚ	tokenizerr-   ÚkwargsÚ	__class__s        €r   r:   zEmu3Processor.__init__<   s}   ø€ ð %Ô0ˆÔØ'Ô6ˆÔØ!*Ô!4ˆÔØ(Ô2ˆÔØ'0Ô'DˆÔ$Ø"Ô,ˆŒØ"Ô,ˆŒØ !ˆÔÝ‰Œ×Ò˜¨)À=ÐÑQÔQÐQÐQÐQr   ÚimagesÚtextr>   Úreturnc                 ó6  •‡—  | j         t          fd| j        j        i|¤Ž}|d                              dd¦  «        }|d                              dd ¦  «        }|d                              dd ¦  «        }|rk|�t          d¦  «        ‚|                      ||| j        ¦  «        \  }}	| j        › |› d	|	› | j	        › �Št          |t          ¦  «        r|g}ˆfd
„|D ¦   «         } t          ¦   «         j        d||dœ|¤Ž}
|r||	ggt          |¦  «        z  |
d<   |
S )NÚtokenizer_init_kwargsr!   r   Fr"   r$   r%   zGYou should not provide `images` when `return_for_image_generation=True`Ú*c                 ó   •— g | ]}|› ‰› �‘Œ	S r   r   )Ú.0ÚsampleÚimage_prompts     €r   ú
<listcomp>z*Emu3Processor.__call__.<locals>.<listcomp>e   s$   ø€ ÐAÐAÐA°&�vÐ-˜|Ð-Ð-ÐAÐAÐAr   ©r@   rA   Úimage_sizesr   )Ú_merge_kwargsr    r=   Úinit_kwargsÚpopÚ
ValueErrorÚcalculate_generate_sizer8   r1   r5   Ú
isinstanceÚstrr9   Ú__call__Úlen)r;   r@   rA   r>   Úoutput_kwargsr   r$   r%   ÚheightÚwidthÚmodel_inputsrI   r?   s              @€r   rT   zEmu3Processor.__call__M   sm  øø€ ð +˜Ô*Ýð
ð 
à"&¤.Ô"<ð
ð ð
ð 
ˆð
 '4°MÔ&B×&FÒ&FÐGdÐfkÑ&lÔ&lÐ#Ø˜oÔ.×2Ò2°7¸DÑAÔAˆØ" ?Ô3×7Ò7¸ÀdÑKÔKˆ
ð 'ð 	BØÐ!Ý Ð!jÑkÔkÐkØ ×8Ò8¸À
ÈDÔLaÑbÔb‰MˆF�EØ"Ô4Ðd°fÐdÐd¸uÐdÀdÔFbÐdÐdˆLÝ˜$¥Ñ$Ô$ð Ø�v�ØAÐAÐAÐA¸DÐAÑAÔAˆDà'•u‘w”wÔ'ÐR¨v¸DÐRÐRÀMÐRÐRˆØ&ð 	HØ,2°E¨?Ð*;½cÀ$¹i¼iÑ*GˆL˜Ñ'ØÐr   c                 óœ   •‡ —  t          ¦   «         j        d||dœ|¤Ž^}}}|�$|                     dd¦  «        rˆ fd„|D ¦   «         }||d d fS )NrK   Úadd_special_tokensTc                 ó&   •— g | ]}‰j         › |› �‘ŒS r   )r7   )rG   rH   r;   s     €r   rJ   z7Emu3Processor.prepare_inputs_layout.<locals>.<listcomp>p   s&   ø€ ÐCÐCÐC°F�t”~Ð/ vÐ/Ð/ÐCÐCÐCr   r   )r9   Úprepare_inputs_layoutÚget)r;   r@   rA   r>   Ú_r?   s   `    €r   r]   z#Emu3Processor.prepare_inputs_layoutl   sq   øø€ Ø8�5™7œ7Ô8Ð\ÀÈTÐ\Ð\ÐU[Ð\Ð\Ðˆ��qØÐ §
¢
Ð+?ÀÑ FÔ FÐàCÐCÐCÐC¸dÐCÑCÔCˆDØ�t˜T 4Ð'Ð'r   Úimage_inputsÚ	image_idxc                 ó¶   — |d         |         \  }}|| j         z  }|| j         z  }||dz   z  }| j        › |› d|› | j        › | j        |z  › | j        › | j        › �S )NrL   r   rE   )r8   r1   r5   r.   r6   r3   )r;   r`   ra   r>   rW   rX   Úimage_seq_lengths          r   Úreplace_image_tokenz!Emu3Processor.replace_image_tokens   s›   € Ø$ ]Ô3°IÔ>‰ˆ�Ø˜4Ô0Ñ0ˆØ˜Ô.Ñ.ˆØ! U¨Q¡YÑ/ÐàÔ%ð 6 vð 6ð 6°ð 6ØÔ+ð6àÔÐ"2Ñ2ð6ð Œ~ð6ð  $Ô3ð6ð 6ð	
r   c                 óV  — i }|�šg }|D ]j\  }}t          ||| j        j        | j        j        | j        j        ¦  «        \  }}|| j        z  }|| j        z  }||dz   z  }|                     |¦  «         Œkdgt          |¦  «        z  }|                     ||dœ¦  «         t          di |¤ŽS )a»  
        Computes the number of placeholder tokens needed for multimodal inputs with the given sizes.

        Args:
            image_sizes (`list[list[int]]`, *optional*):
                The input sizes formatted as (height, width) per each image.

        Returns:
            `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided
            input modalities, along with other useful data.
        Nr   )Únum_image_tokensÚnum_image_patchesr   )
r   r<   Úspatial_factorÚ
min_pixelsÚ
max_pixelsr8   ÚappendrU   Úupdater   )	r;   rL   r>   Úvision_datarf   rW   rX   rc   rg   s	            r   Ú_get_num_multimodal_tokensz(Emu3Processor._get_num_multimodal_tokens   sæ   € ð ˆØÐ"Ø!ÐØ!,ð :ð :‘�˜Ý ,ØØØÔ(Ô7ØÔ(Ô3ØÔ(Ô3ñ!ô !‘�˜ð   4Ô#8Ñ8�Ø Ô!6Ñ6�Ø#)¨U°Q©YÑ#7Ð Ø ×'Ò'Ð(8Ñ9Ô9Ð9Ð9à!" ¥c¨+Ñ&6Ô&6Ñ 6ÐØ×ÒÐ4DÐ[lÐmÐmÑnÔnÐnåÐ,Ð, Ð,Ð,Ð,r   c                 ó  — t          t          |                     d¦  «        ¦  «        \  }}||z  }||z  dz  }t          t          ||z  |z  ¦  «        ¦  «        }t          t          ||z  |z  ¦  «        ¦  «        }	||	fS )Nú:g      à?)ÚmapÚintÚsplitÚround)
r;   r$   r%   rh   rX   rW   Úcurrent_areaÚtarget_ratioÚtoken_heightÚtoken_widths
             r   rQ   z%Emu3Processor.calculate_generate_size¡   s‚   € Ý�C §¢¨SÑ!1Ô!1Ñ2Ô2‰ˆˆvØ˜v‘~ˆØ" \Ñ1°cÑ9ˆå�5 ¨,Ñ!6¸Ñ!GÑHÔHÑIÔIˆÝ�% ¨Ñ 4°~Ñ EÑFÔFÑGÔGˆØ˜[Ð(Ð(r   c                 ó(   —  | j         j        |fi |¤ŽS ©N)r<   Úpostprocess)r;   r@   r>   s      r   r{   zEmu3Processor.postprocessª   s    € Ø/ˆtÔ#Ô/°ÐAÐA¸&ÐAÐAÐAr   Tc                 óº   — |�|dk    r | j         |fd|i|¤ŽS |dk    r|                      |d¬¦  «        }|d         S t          | j        j        › d|› d	�¦  «        ‚)
a  
        Post-process the output of a multimodal model to return the requested modality output.
        If the model cannot generated the requested modality, an error will be raised.

        Args:
            generated_outputs (`torch.Tensor` or `np.ndarray`):
                The output of the model `generate` function. The output is expected to be a tensor of shape `(batch_size, sequence_length)`
                or `(sequence_length,)`.
            skip_special_tokens (`bool`, *optional*, defaults to `True`):
                Whether or not to remove special tokens in the output. Argument passed to the tokenizer's `batch_decode` method.
            generation_mode (`str`, *optional*):
                Generation mode indicated which modality to output and can be one of `["text", "image", "audio"]`.
            **kwargs:
                Additional arguments to be passed to the tokenizer's `batch_decode method`.

        Returns:
            `list[Union[str, PIL.Image.Image]]`: The decoded text or generated image.
        NrA   Úskip_special_tokensÚimagezPIL.Image.Image)Úreturn_tensorsÚpixel_valuesz# got an unexpected generation_mode=z.. Supported options are only `text` and `image)Úpost_process_image_text_to_textr{   rP   r?   r   )r;   Úgenerated_outputsr}   Úgeneration_moder>   r@   s         r   Úpost_process_multimodal_outputz,Emu3Processor.post_process_multimodal_output­   s±   € ð* Ð" o¸Ò&?Ð&?Ø7�4Ô7Ø!ðð Ø7JðØNTðð ð ð  Ò'Ð'Ø×%Ò%Ð&7ÐHYÐ%ÑZÔZˆFØ˜.Ô)Ð)õ Ø”>Ô*ð  Oð  OÈð  Oð  Oð  Oñô ð r   rz   )NN)TN)r   r   r   r    Úvalid_processor_kwargsr:   r   r   r   r
   Úlistr	   r   rT   r]   Údictrr   rS   rd   rn   rQ   r{   r„   Ú__classcell__)r?   s   @r   r*   r*   7   s  ø€ € € € € ð 1Ðð ð	Rð Rð Rð Rð Rð Rð" ð %)Øaeðð à˜TÑ!ðð Ð+Ñ+¨d°9¬oÑ=ÀÐEVÔ@WÑWÐZ^Ñ^ðð Ð,Ô-ð	ð
 
ðð ð ð ð ñ „^ðð<(ð (ð (ð (ð (ð (ð

°ð 

Àð 

ÐSVð 

ð 

ð 

ð 

ð -ð  -ð  -ð  -ðD)ð )ð )ðB *ð Bð Bð Bð Bð LPð!ð !ð !ð !ð !ð !ð !ð !r   r*   N)Úimage_utilsr   Úprocessing_utilsr   r   r   r   r   r	   Útokenization_utils_baser
   r   Úutilsr   r   Úutils.import_utilsr   Úimage_processing_emu3r   r   r   r    r*   Ú__all__r   r   r   ú<module>r�      s  ðð" &Ð %Ð %Ð %Ð %Ð %Ø rÐ rÐ rÐ rÐ rÐ rÐ rÐ rÐ rÐ rÐ rÐ rÐ rÐ rÐ rÐ rØ CÐ CÐ CÐ CÐ CÐ CÐ CÐ CØ 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø *Ð *Ð *Ð *Ð *Ð *ð ÐÑÔð NØMÐMÐMÐMÐMÐMÐMÐMð	&ð 	&ð 	&ð 	&ð 	&�Z uð 	&ñ 	&ô 	&ð 	&ðð ð ð ð Ð*°%ð ñ ô ð ð Ø	€�;ÐÑÔðUð Uð Uð Uð U�Nñ Uô Uñ  Ôñ „ðUðp Ð
€€€r   