§
    ‚Štj!,  ã                   ó   — d dl mZ d dlmZ d dl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 )é   )ÚBatchFeature)Ú
ImageInput)Ú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__© ó    úf/var/www/html/CA-Chatbot/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	 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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                 óº  ‡ ‡— t          |t          ¦  «        r|g}n?t          |t          ¦  «        s*t          |d         t          ¦  «        st          d¦  «        ‚ ‰ j        t
          fd‰ j        j        i|¤Ž}|d                              dd¦  «        }|d                              dd	¦  «        }|d                              d
d	¦  «        }|r|�t          d¦  «        ‚|s|€|€t          d¦  «        ‚i }‰ j
        › }	‰ j        › ‰ j        › �}
|sÕ|�Ó ‰ j        |fi |d         ¤Ž}t          |j        ¦  «        }g }|D ]—}‰ j        |v rwt#          |¦  «        }|\  }}|‰ j        z  }|‰ j        z  }||dz   z  }|	› |› d|› ‰ j        › d|z  › |
› �}|                     ‰ j        |d¦  «        }‰ j        › |› �}‰ j        |v °w|                     |¦  «         Œ˜ˆ fd„|D ¦   «         }nX|rV‰                      ||‰ j        ¦  «        \  }}|	› |› d|› ‰ j        › �Šˆˆ fd„|D ¦   «         }||ggt1          |¦  «        z  |d<   |d                              dd	¦  «        }|d                              dd¦  «        } ‰ j        |fi |d         ¤dd	i¤Ž}‰                      ||dg¬¦  «         |r‰                      |d         ¦  «        |d<   t7          i |¥|¥|¬¦  «        S )aA  
        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`.
        é    zAInvalid input text. Please provide a string, or a list of stringsÚtokenizer_init_kwargsr!   r   Fr"   r$   Nr%   zGYou should not provide `images` when `return_for_image_generation=True`zOYou must provide either text or images when `return_for_image_generation=False`r   Ú*ú<placeholder>c                 óF   •— g | ]}|                      d ‰j        ¦  «        ‘ŒS )rG   )Úreplacer.   )Ú.0Úsampler;   s     €r   ú
<listcomp>z*Emu3Processor.__call__.<locals>.<listcomp>‰   s*   ø€ ÐcÐcÐcÈ&�F—N’N ?°DÔ4DÑEÔEÐcÐcÐcr   c                 ó*   •— g | ]}‰j         › |› ‰› �‘ŒS r   )r7   )rJ   rK   Úimage_promptr;   s     €€r   rL   z*Emu3Processor.__call__.<locals>.<listcomp>�   s+   ø€ ÐQÐQÐQÀ&�t”~Ð= vÐ=¨|Ð=Ð=ÐQÐQÐQr   Úimage_sizesÚreturn_tensorsr#   Úimage)Ú
modalitiesÚ	input_idsÚmm_token_type_ids)ÚdataÚtensor_type)Ú
isinstanceÚstrÚlistÚ	TypeErrorÚ_merge_kwargsr    r=   Úinit_kwargsÚpopÚ
ValueErrorr1   r6   r3   r<   ÚiterrO   r.   Únextr8   r5   rI   r7   ÚappendÚcalculate_generate_sizeÚlenÚ_check_special_mm_tokensÚcreate_mm_token_type_idsr   )r;   r@   rA   r>   Úoutput_kwargsr   r$   r%   Úimage_featuresÚimage_start_tokensÚimage_end_tokensrO   Úprompt_stringsrK   Ú
image_sizeÚheightÚwidthÚimage_seq_lengthÚimage_placeholderrP   r#   Útext_inputsrN   s   `                     @r   Ú__call__zEmu3Processor.__call__L   sõ  øø€ õ& �d�CÑ Ô ð 	aØ�6ˆDˆDÝ˜D¥$Ñ'Ô'ð 	aµ
¸4À¼7ÅCÑ0HÔ0Hð 	aÝÐ_Ñ`Ô`Ð`à*˜Ô*Ýð
ð 
à"&¤.Ô"<ð
ð ð
ð 
ˆð
 '4°MÔ&B×&FÒ&FÐGdÐfkÑ&lÔ&lÐ#Ø˜oÔ.×2Ò2°7¸DÑAÔAˆØ" ?Ô3×7Ò7¸ÀdÑKÔKˆ
à&ð 	h¨6Ð+=ÝÐfÑgÔgÐgà*ð 	p¨t¨|ÀÀÝÐnÑoÔoÐoàˆØ $Ô 6Ð8ÐØ"œnÐD¨dÔ.BÐDÐDÐð +ð 	J¨vÐ/AØ1˜TÔ1°&Ð[Ð[¸MÈ/Ô<ZÐ[Ð[ˆNÝ˜~Ô9Ñ:Ô:ˆKàˆNØð .ð .�ØÔ&¨&Ð0Ð0Ý!% kÑ!2Ô!2�JØ$.‘M�F˜EØ# tÔ'<Ñ<�FØ! TÔ%:Ñ:�EØ'-°¸±Ñ';Ð$à+=ð  )d¸vð  )dð  )dÈð  )dÈtÔOkð  )dÐm|ð  @Pñ  nPð  )dð  Rbð  )dð  )dÐ%Ø#Ÿ^š^¨DÔ,<Ð>OÐQRÑSÔS�FØ $¤Ð8°Ð8Ð8�Fð Ô&¨&Ð0Ð0ð ×%Ò% fÑ-Ô-Ð-Ð-ØcÐcÐcÐcÐTbÐcÑcÔcˆDˆDð )ð 	JØ ×8Ò8¸À
ÈDÔLaÑbÔb‰MˆF�EØ0Ð`°&Ð`Ð`¸5Ð`À$ÔB^Ð`Ð`ˆLØQÐQÐQÐQÐQÈDÐQÑQÔQˆDØ.4°e¨_Ð,=ÅÀDÁ	Ä	Ñ,IˆN˜=Ñ)ð ' }Ô5×9Ò9Ð:JÈDÑQÔQˆØ#0°Ô#?×#CÒ#CÐD^Ð`eÑ#fÔ#fÐ Ø$�d”n TÐ_Ð_¨]¸=Ô-IÐ_Ð_ÐZ^Ð_Ð_Ð_ˆØ×%Ò% d¨KÀWÀIÐ%ÑNÔNÐNà#ð 	gØ/3×/LÒ/LÈ[ÐYdÔMeÑ/fÔ/fˆKÐ+Ñ,ÝÐ!B KÐ!B°>Ð!BÐP^Ð_Ñ_Ô_Ð_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   ra   rc   Úupdater   )	r;   rO   r>   Úvision_datars   rl   rm   rn   rt   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%   ru   rm   rl   Úcurrent_areaÚtarget_ratioÚtoken_heightÚtoken_widths
             r   rb   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_tokensrQ   zPIL.Image.Image)rP   Úpixel_valuesz# got an unexpected generation_mode=z.. Supported options are only `text` and `image)Úpost_process_image_text_to_textr‡   r^   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   r†   )NN)TN)r   r   r   r:   r   r   r   r
   rY   r	   r    r   rq   rz   rb   r‡   rŽ   Ú__classcell__)r?   s   @r   r*   r*   8   s8  ø€ € € € € ð ð	Rð Rð Rð Rð Rð Rð" ð %)ØaeðM`ð M`à˜TÑ!ðM`ð Ð+Ñ+¨d°9¬oÑ=ÀÐEVÔ@WÑWÐZ^Ñ^ðM`ð Ð,Ô-ð	M`ð
 
ðM`ð M`ð M`ñ „^ðM`ð^ -ð  -ð  -ð  -ðD)ð )ð )ðB *ð Bð Bð Bð Bð LPð!ð !ð !ð !ð !ð !ð !ð !r   r*   N)Úimage_processing_utilsr   Úimage_utilsr   Úprocessing_utilsr   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‹  ðð" 3Ð 2Ð 2Ð 2Ð 2Ð 2Ø %Ð %Ð %Ð %Ð %Ð %Ø dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dÐ dØ 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ð 	&ñ 	&ô 	&ð 	&ðð ð ð ð Ð*°%ð ñ ô ð ð Ø	€�;ÐÑÔðqð qð qð qð q�Nñ qô qñ  Ôñ „ðqðh Ð
€€€r   