Ë
    ÛÓ^j!,  ã                   óô   — 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y)é   )Ú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<   y)Ú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__© ó    ús/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/emu3/processing_emu3.pyr   r      s   … ñð "&Ô%r   r   F)Útotalc                   ó8   — e Zd ZU eed<   eed<   dddœdddœdœZy	)
Ú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    )   s/   … ØÓØ+Ó+ð ,1Ø(-ñ
ð
 Ø ñ
ñ	�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        ‰| �1  |||¬«       y )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Ñ0ˆÔØ'×6Ñ6ˆÔØ!*×!4Ñ!4ˆÔØ(×2Ñ2ˆÔØ'0×'DÑ'DˆÔ$Ø"×,Ñ,ˆŒØ"×,Ñ,ˆŒØ !ˆÔÜ‰Ñ˜¨)À=ÐÕ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   j                  dd«      }|d   j                  dd	«      }|d   j                  d
d	«      }|r|�t        d«      ‚|s|€|€t        d«      ‚i }| j                  › }	| j                  › | j                  › �}
|�s|�� | j                  |fi |d   ¤Ž}t        |j                  «      }g }|D ]¯  }| j                   |v rŽt#        |«      }|\  }}|| j$                  z  }|| j$                  z  }||dz   z  }|	› |› d|› | j&                  › d|z  › |
› �}|j)                  | j                   |d«      }| j*                  › |› �}| j                   |v rŒŽ|j-                  |«       Œ± |D �cg c]  }|j)                  d| j                   «      ‘Œ  }}nj|rh| j/                  ||| j$                  «      \  }}|	› |› d|› | j&                  › �}|D �cg c]  }| j*                  › |› |› �‘Œ }}||ggt1        |«      z  |d<   |d   j                  dd	«      }|d   j                  dd«      } | j                  |fi |d   ¤dd	i¤Ž}| j3                  ||dg¬«       |r| j5                  |d   «      |d<   t7        i |¥|¥|¬«      S c c}w c c}w )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   Ú*z<placeholder>Ú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<   ÚiterrG   r.   Únextr8   r5   Úreplacer7   Ú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_tokensrG   Úprompt_stringsÚsampleÚ
image_sizeÚheightÚwidthÚimage_seq_lengthÚimage_placeholderÚimage_promptrH   r#   Útext_inputss                          r   Ú__call__zEmu3Processor.__call__L   s—  € ô& �dœCÔ Ø�6‰DÜ˜D¤$Ô'´
¸4À¹7ÄCÔ0HÜÐ_Ó`Ð`à*˜×*Ñ*Üñ
à"&§.¡.×"<Ñ"<ð
ð ñ
ˆð
 '4°MÑ&B×&FÑ&FÐGdÐfkÓ&lÐ#Ø˜oÑ.×2Ñ2°7¸DÓAˆØ" ?Ñ3×7Ñ7¸ÀdÓKˆ
á&¨6Ð+=ÜÐfÓgÐgá*¨t¨|ÀÀÜÐnÓoÐoàˆØ $× 6Ñ 6Ð7ÐØ"Ÿn™nÐ-¨d×.BÑ.BÐ-CÐDÐò +¨vÑ/AØ1˜T×1Ñ1°&Ñ[¸MÈ/Ñ<ZÑ[ˆNÜ˜~×9Ñ9Ó:ˆKàˆNØò .�Ø×&Ñ&¨&Ñ0Ü!% kÓ!2�JØ$.‘M�F˜EØ# t×'<Ñ'<Ñ<�FØ! T×%:Ñ%:Ñ:�EØ'-°¸±Ñ';Ð$à+=Ð*>¸v¸hÀaÈÀwÈt×OkÑOkÐNlÐm|ð  @Pñ  nPð  mQð  Rbð  Qcð  )dÐ%Ø#Ÿ^™^¨D×,<Ñ,<Ð>OÐQRÓS�FØ $§¡Ð/°¨xÐ8�Fð ×&Ñ&¨&Ò0ð ×%Ñ% fÕ-ð.ð UcÖcÈ&�F—N‘N ?°D×4DÑ4DÕEÐcˆDÑcñ )Ø ×8Ñ8¸À
ÈD×LaÑLaÓb‰MˆF�EØ0Ð1°&°¸¸5¸'À$×B^ÑB^ÐA_Ð`ˆLØLPÖQÀ&�t—~‘~Ð& v h¨|¨nÒ=ÐQˆDÐQØ.4°e¨_Ð,=ÄÀDÃ	Ñ,IˆN˜=Ñ)ð ' }Ñ5×9Ñ9Ð:JÈDÓQˆØ#0°Ñ#?×#CÑ#CÐD^Ð`eÓ#fÐ Ø$�d—n‘n TÑ_¨]¸=Ñ-IÑ_ÐZ^Ò_ˆØ×%Ñ% d¨KÀWÀIÐ%ÔNá#Ø/3×/LÑ/LÈ[ÐYdÑMeÓ/fˆKÐ+Ñ,ÜÐ!B KÐ!B°>Ð!BÐP^Ô_Ð_ùò# dùò Rs   Ç##K6ÉK;c                 óˆ  — i }|�´g }|D ]Š  \  }}t        ||| j                  j                  | j                  j                  | j                  j                  «      \  }}|| j
                  z  }|| j
                  z  }||dz   z  }|j                  |«       ŒŒ dgt        |«      z  }|j                  ||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.
        r   )Únum_image_tokensÚnum_image_patchesr   )
r   r<   Úspatial_factorÚ
min_pixelsÚ
max_pixelsr8   rZ   r\   Úupdater   )	r;   rG   r>   Úvision_datarn   rf   rg   rh   ro   s	            r   Ú_get_num_multimodal_tokensz(Emu3Processor._get_num_multimodal_tokensœ   sà   € ð ˆØÐ"Ø!ÐØ!,ò :‘�˜Ü ,ØØØ×(Ñ(×7Ñ7Ø×(Ñ(×3Ñ3Ø×(Ñ(×3Ñ3ó!‘�˜ð   4×#8Ñ#8Ñ8�Ø ×!6Ñ!6Ñ6�Ø#)¨U°Q©YÑ#7Ð Ø ×'Ñ'Ð(8Õ9ð:ð "# ¤c¨+Ó&6Ñ 6ÐØ×ÑÐ4DÐ[lÑmÔnäÑ, Ñ,Ð,r   c                 óÐ   — t        t        |j                  d«      «      \  }}||z  }||z  dz  }t        t        ||z  |z  «      «      }t        t        ||z  |z  «      «      }	||	fS )Nú:g      à?)ÚmapÚintÚsplitÚround)
r;   r$   r%   rp   rg   rf   Úcurrent_areaÚtarget_ratioÚtoken_heightÚtoken_widths
             r   r[   z%Emu3Processor.calculate_generate_size¾   sp   € ÜœC §¡¨SÓ!1Ó2‰ˆˆvØ˜v‘~ˆØ" \Ñ1°cÑ9ˆäœ5 ¨,Ñ!6¸Ñ!GÓHÓIˆÜœ% ¨Ñ 4°~Ñ EÓFÓGˆØ˜[Ð(Ð(r   c                 ó<   —  | j                   j                  |fi |¤ŽS ©N)r<   Úpostprocess)r;   r@   r>   s      r   r‚   zEmu3Processor.postprocessÇ   s    € Ø/ˆt×#Ñ#×/Ñ/°ÑA¸&ÑAÐAr   c                 ó¾   — |�|dk(  r | j                   |fd|i|¤ŽS |dk(  r| j                  |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.
        rA   Úskip_special_tokensrI   zPIL.Image.Image)rH   Úpixel_valuesz# got an unexpected generation_mode=z.. Supported options are only `text` and `image)Úpost_process_image_text_to_textr‚   rV   r?   r   )r;   Úgenerated_outputsr„   Úgeneration_moder>   r@   s         r   Úpost_process_multimodal_outputz,Emu3Processor.post_process_multimodal_outputÊ   s™   € ð* Ð" o¸Ò&?Ø7�4×7Ñ7Ø!ñØ7JðØNTñð ð  Ò'Ø×%Ñ%Ð&7ÐHYÐ%ÓZˆFØ˜.Ñ)Ð)ô Ø—>‘>×*Ñ*Ð+Ð+NÈÐN_ð  `Nð  Oóð r   r�   )NN)TN)r   r   r   r:   r   r   r   r
   rQ   r	   r    r   rl   ru   r[   r‚   r‰   Ú__classcell__)r?   s   @r   r*   r*   8   s­   ø„ ð õ	Rð" ð %)ØaeñM`à˜TÑ!ðM`ð Ð+Ñ+¨d°9©oÑ=ÀÐEVÑ@WÑWÐZ^Ñ^ðM`ð Ð,Ñ-ð	M`ð
 
òM`ó ðM`ó^ -òD)ð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“      sz   ðõ" 3Ý %ß dÕ dß Cß 8Ý *ñ ÔßMô	&�Z uõ 	&ôÐ*°%õ ð Ù	�;Ôôq�Nó qó  ó ðqðh Ð
�r   