§
    ‚Štja7  ã                   ó¨   — d dl mZmZmZ d dlmZmZ ddlmZ ddl	m
Z
mZ ddlmZ  G d„ ded	¬
¦  «        ZdZe G d„ de¦  «        ¦   «         ZdgZdS )é    )ÚProcessingKwargsÚProcessorMixinÚUnpack)ÚPreTokenizedInputÚ	TextInputé   )ÚBatchFeature)Ú
ImageInputÚmake_flat_list_of_images)Úauto_docstringc                   ó   — e Zd ZdddiiZdS )ÚLlama4ProcessorKwargsÚtext_kwargsÚpadding_sideÚleftN)Ú__name__Ú
__module__Ú__qualname__Ú	_defaults© ó    új/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/llama4/processing_llama4.pyr   r      s"   € € € € € àØ˜Fð
ð€I€I€Ir   r   F)Útotala>  {{- bos_token }}
{%- if custom_tools is defined %}
    {%- set tools = custom_tools %}
{%- endif %}
{%- if not tools_in_user_message is defined %}
    {%- set tools_in_user_message = true %}
{%- endif %}
{%- if not date_string is defined %}
    {%- if strftime_now is defined %}
        {%- set date_string = strftime_now("%d %b %Y") %}
    {%- else %}
        {%- set date_string = "26 Jul 2024" %}
    {%- endif %}
{%- endif %}
{%- if not tools is defined %}
    {%- set tools = none %}
{%- endif %}

{#- This block extracts the system message, so we can slot it into the right place. #}
{%- if messages[0]['role'] == 'system' %}    
    {%- if messages[0]['content'] is string %}
        {%- set system_message = messages[0]['content']|trim %}
    {%- else %}
        {#- FIXME: The processor requires an array, always. #}
        {%- set system_message = messages[0]['content'][0]['text']|trim %}
    {%- endif %}
    {%- set messages = messages[1:] %}
    {%- set user_supplied_system_message = true %}
{%- else %}
    {%- set system_message = "" %}
    {%- set user_supplied_system_message = false %}
{%- endif %}

{#- System message if the user supplied one #}
{%- if user_supplied_system_message %}
    {{- "<|header_start|>system<|header_end|>

" }}
    {%- if tools is not none %}
        {{- "Environment: ipython
" }}
    {%- endif %}
    {%- if tools is not none and not tools_in_user_message %}
        {{- "You have access to the following functions. To call a function, please respond with JSON for a function call." }}
        {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
        {{- "Do not use variables.

" }}
        {%- for t in tools %}
            {{- t | tojson(indent=4) }}
            {{- "

" }}
        {%- endfor %}
    {%- endif %}
    {{- system_message }}
    {{- "<|eot|>" }}
{%- endif %}

{#- Custom tools are passed in a user message with some extra guidance #}
{%- if tools_in_user_message and not tools is none %}
    {#- Extract the first user message so we can plug it in here #}
    {%- if messages | length != 0 %}
        {%- set first_user_message = messages[0]['content']|trim %}
        {%- set messages = messages[1:] %}
    {%- else %}
        {{- raise_exception("Cannot put tools in the first user message when there's no first user message!") }}
{%- endif %}
    {{- '<|header_start|>user<|header_end|>

' -}}
    {{- "Given the following functions, please respond with a JSON for a function call " }}
    {{- "with its proper arguments that best answers the given prompt.

" }}
    {{- 'Respond in the format {"name": function name, "parameters": dictionary of argument name and its value}.' }}
    {{- "Do not use variables.

" }}
    {%- for t in tools %}
        {{- t | tojson(indent=4) }}
        {{- "

" }}
    {%- endfor %}
    {{- first_user_message + "<|eot|>"}}
{%- endif %}

{%- for message in messages %}
    {%- if not (message.role == 'ipython' or message.role == 'tool' or 'tool_calls' in message) %}
    {{- '<|header_start|>' + message['role'] + '<|header_end|>

' }}
        {%- if message['content'] is string %}
            {{- message['content'] }}
        {%- else %}
            {%- for content in message['content'] %}
                {%- if content['type'] == 'image' %}
                    {{- '<|image|>' }}
                {%- elif content['type'] == 'text' %}
                    {{- content['text'] }}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
        {{- "<|eot|>" }}
    {%- elif 'tool_calls' in message and message.tool_calls|length > 0 %}
       {{- '<|header_start|>assistant<|header_end|>

' -}}
       {{- '<|python_start|>' }}
        {%- if message['content'] is string %}
            {{- message['content'] }}
        {%- else %}
            {%- for content in message['content'] %}
                {%- if content['type'] == 'image' %}
                    {{- '<|image|>' }}
                {%- elif content['type'] == 'text' %}
                    {{- content['text'] }}
                {%- endif %}
            {%- endfor %}
        {%- endif %}
       {{- '<|python_end|>' }}
        {%- for tool_call in message.tool_calls %}
           {{- '{"name": "' + tool_call.function.name + '", ' }}
           {{- '"parameters": ' }}
           {{- tool_call.function.arguments | tojson }}
           {{- "}" }}
        {%- endfor %}
       {{- "<|eot|>" }}
    {%- elif message.role == "tool" or message.role == "ipython" %}
        {{- "<|header_start|>ipython<|header_end|>

" }}
        {%- if message.content is mapping or message.content is iterable %}
            {{- message.content | tojson }}
        {%- else %}
            {{- message.content }}
        {%- endif %}
        {{- "<|eot|>" }}
    {%- endif %}
{%- endfor %}
{%- if add_generation_prompt %}
    {{- '<|header_start|>assistant<|header_end|>

' }}
{%- endif %}
c                   ó¸   ‡ — e Zd Zddddddddddd	efd
edefˆ fd„Z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ˆ xZS )ÚLlama4ProcessorNé   g      à?ú	<|image|>ú<|image_start|>ú<|image_end|>ú	<|patch|>ú<|tile_x_separator|>ú<|tile_y_separator|>Ú
patch_sizeÚpixel_shuffle_ratioc                 óL  •— t          ¦   «                              |||¬¦  «         t          t          d|dz  z  ¦  «        ¦  «        | _        || _        || _        || _        |                     | j        ¦  «        | _	        || _
        || _        |	| _        |
| _        || _        dS )a~  
        patch_size (`int`, *optional*, defaults to 28):
            The size of image patches for tokenization.
        pixel_shuffle_ratio (`float`, *optional*, defaults to `0.5`):
            The ratio used for pixel shuffling when processing images. This controls the downsampling factor
            applied to image patches. The actual downsampling ratio is calculated as `1 / (pixel_shuffle_ratio^2)`.
        fake_image_token (`str`, *optional*, defaults to `"<|image|>"`):
            The placeholder token in the text that will be replaced with actual image tokens. This token serves
            as a marker indicating where images should be inserted in the text sequence.
        image_token (`str`, *optional*, defaults to `"<|image|>"`):
            The token to be used to represent an image in the text.
        start_of_image_token (`str`, *optional*, defaults to `"<|image_start|>"`):
            The special token that marks the beginning of an image sequence in the text. This token is prepended
            to image token sequences to delimit image boundaries.
        end_of_image_token (`str`, *optional*, defaults to `"<|image_end|>"`):
            The special token that marks the end of an image sequence in the text. This token is appended to
            image token sequences to delimit image boundaries.
        patch_token (`str`, *optional*, defaults to `"<|patch|>"`):
            The token used to represent individual image patches. Multiple patch tokens are used to represent
            the full image, with the number depending on the image size and patch configuration.
        tile_x_separator_token (`str`, *optional*, defaults to `"<|tile_x_separator|>"`):
            The token used to separate tiles (patches) horizontally within an image. This token is inserted
            between patches in the same row when images are split into multiple tiles.
        tile_y_separator_token (`str`, *optional*, defaults to `"<|tile_y_separator|>"`):
            The token used to separate tiles (patches) vertically within an image. This token is inserted
            between rows of patches when images are split into multiple tiles.
        )Úchat_templateg      ð?é   N)ÚsuperÚ__init__ÚintÚroundÚdownsample_ratior#   Úfake_image_tokenÚimage_tokenÚconvert_tokens_to_idsÚimage_token_idÚstart_of_img_tokenÚend_of_img_tokenÚimg_patch_tokenÚ
tile_tokenÚtile_global_token)ÚselfÚimage_processorÚ	tokenizerr#   r$   r-   r.   Ústart_of_image_tokenÚend_of_image_tokenÚpatch_tokenÚtile_x_separator_tokenÚtile_y_separator_tokenr&   ÚkwargsÚ	__class__s                 €r   r)   zLlama4Processor.__init__%   s¥   ø€ õV 	‰Œ×Ò˜¨)À=ÐÑQÔQÐQå #¥E¨#Ð1DÀaÑ1GÑ*HÑ$IÔ$IÑ JÔ JˆÔØ$ˆŒà 0ˆÔØ&ˆÔØ'×=Ò=¸dÔ>NÑOÔOˆÔØ"6ˆÔØ 2ˆÔØ*ˆÔØ0ˆŒØ!7ˆÔÐÐr   c                 óÈ   — d}|\  }}||z  dk    r?t          |¦  «        D ]/}t          |¦  «        D ]}|d|z  z  }||dz
  k     r|dz  }Œ|dz  }Œ0|dz  }|d|z  z  }|dz  }|S )zÔ
        Create a structured string representation of image tokens

        Args:
           num_patches: Number of patches in the image

        Returns:
            String with appropriate image tokens
        r   é   r    r!   r"   r   r   )Úrange)r6   Úaspect_ratioÚnum_patches_per_chunkÚ
img_stringÚratio_hÚratio_wÚyyÚxxs           r   Ú_prompt_split_imagez#Llama4Processor._prompt_split_image^   s¯   € ð 'ˆ
Ø'Ñˆ�Ø�WÑ˜qÒ Ð Ý˜G‘n”nð 5ð 5�Ý ™.œ.ð =ð =�BØ +Ð0EÑ"EÑE�JØ˜G a™KÒ'Ð'Ø"Ð&<Ñ<˜
øàÐ4Ñ4�
�
à�kÑ!ˆ
Ø�kÐ$9Ñ9Ñ9ˆ
Ø�oÑ%ˆ
àÐr   ÚimagesÚtextr>   Úreturnc                 óZ  ‡ — |€t          d¦  «        ‚ ‰ j        t          fd‰ j        j        i|¤Ž}t          |t          t          f¦  «        s|g}i }|��ù‰ j         	                    |¦  «        }t          |¦  «        } ‰ j        dd|i|d         ¤Ž}|d         d         j        dd…         \  }}t          |‰ j        z  |‰ j        z  z  ‰ j        z  ¦  «        }|                     d	¦  «        }	t!          ˆ fd
„|D ¦   «         ¦  «        }
|
t#          |¦  «        k    r#t          d|
› dt#          |¦  «        › d�¦  «        ‚d}g }|D ]â}|                     ‰ j        ¦  «        }|dk    r|                     |¦  «         Œ8|                     ‰ j        ¦  «        }g }t-          |¦  «        D ]V\  }}|                     |¦  «         ||k     r6‰                      |	|         |¦  «        }|dz  }|                     |¦  «         ŒW|                     d                     |¦  «        ¦  «         Œã|t#          |¦  «        k    rt          d¦  «        ‚|}|d                              dd¦  «        } ‰ j        |fi |d         ¤Ž}‰                      ||dg¬¦  «         t5          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`.
        NzYou have to specify text.Útokenizer_init_kwargsrK   Úimages_kwargsÚpixel_valuesr   éþÿÿÿÚaspect_ratiosc              3   óL   •K  — | ]}|                      ‰j        ¦  «        V — Œd S )N)Úcountr-   )Ú.0Úpromptr6   s     €r   ú	<genexpr>z+Llama4Processor.__call__.<locals>.<genexpr>¢   s2   øè è € Ð$\Ð$\ÈV V§\¢\°$Ô2GÑ%HÔ%HÐ$\Ð$\Ð$\Ð$\Ð$\Ð$\r   zFound z) placeholders across the batch, but have z flattened images.rA   Ú zONumber of image placeholders in the prompt does not match the number of images.r   Úreturn_tensorsÚimage)Ú
modalities)ÚdataÚtensor_typer   )Ú
ValueErrorÚ_merge_kwargsr   r8   Úinit_kwargsÚ
isinstanceÚlistÚtupler7   Úfetch_imagesr   Úshaper*   r#   r,   ÚpopÚsumÚlenrU   r-   ÚappendÚsplitÚ	enumeraterJ   ÚjoinÚ_check_special_mm_tokensr	   )r6   rK   rL   r>   Úoutput_kwargsÚimage_inputsÚimage_heightÚimage_widthrD   rS   Útotal_placeholdersÚimage_indexÚprocessed_textrW   Úplaceholder_countÚprompt_splitsÚ
new_promptÚlocal_image_indexÚ
split_partÚtokens_for_this_imagerZ   Útext_inputss   `                     r   Ú__call__zLlama4Processor.__call__y   sA  ø€ ð" ˆ<ÝÐ8Ñ9Ô9Ð9à*˜Ô*Ý!ð
ð 
à"&¤.Ô"<ð
ð ð
ð 
ˆõ ˜$¥¥u Ñ.Ô.ð 	Ø�6ˆDð ˆØÑØÔ)×6Ò6°vÑ>Ô>ˆFÝ-¨fÑ5Ô5ˆFØ/˜4Ô/Ð`Ð`°vÐ`ÀÈÔA_Ð`Ð`ˆLØ(4°^Ô(DÀQÔ(GÔ(MÈbÈcÈcÔ(RÑ%ˆL˜+Ý$'Ø ¤Ñ0°[ÀDÄOÑ5SÑTÐX\ÔXmÑmñ%ô %Ð!ð )×,Ò,¨_Ñ=Ô=ˆMå!$Ð$\Ð$\Ð$\Ð$\ÐW[Ð$\Ñ$\Ô$\Ñ!\Ô!\ÐØ!¥S¨¡[¤[Ò0Ð0Ý ð@Ð/ð @ð @Ý # F¡¤ð@ð @ð @ñô ð ð
 ˆKØˆNØð ;ð ;�Ø$*§L¢L°Ô1FÑ$GÔ$GÐ!Ø$¨Ò)Ð)à"×)Ò)¨&Ñ1Ô1Ð1ØØ &§¢¨TÔ-BÑ CÔ C�Ø�
Ý5>¸}Ñ5MÔ5Mð Að AÑ1Ð% zØ×%Ò% jÑ1Ô1Ð1Ø(Ð+<Ò<Ð<Ø04×0HÒ0HØ)¨+Ô6Ð8Mñ1ô 1Ð-ð $ qÑ(˜Ø"×)Ò)Ð*?Ñ@Ô@Ð@øØ×%Ò% b§g¢g¨jÑ&9Ô&9Ñ:Ô:Ð:Ð:à�c &™kœkÒ)Ð)Ý Ð!rÑsÔsÐsà!ˆDà& }Ô5×9Ò9Ð:JÈDÑQÔQˆØ$�d”n TÐJÐJ¨]¸=Ô-IÐJÐJˆØ×%Ò% d¨KÀWÀIÐ%ÑNÔNÐNåÐ!@ KÐ!@°<Ð!@ÈnÐ]Ñ]Ô]Ð]r   )NN)r   r   r   r&   r*   Úfloatr)   rJ   r   r
   r   r   rc   r   r   r	   r}   Ú__classcell__)r?   s   @r   r   r   #   s$  ø€ € € € € ð ØØØ%(Ø$ØØ.Ø*ØØ5Ø5Ø#ð78ð 78ð ð	78ð
 #ð78ð 78ð 78ð 78ð 78ð 78ðrð ð ð6 ð %)ØaeðL^ð L^à˜TÑ!ðL^ð Ð+Ñ+¨d°9¬oÑ=ÀÐEVÔ@WÑWÐZ^Ñ^ðL^ð Ð.Ô/ð	L^ð
 
ðL^ð L^ð L^ñ „^ðL^ð L^ð L^ð L^ð L^r   r   N)Útransformers.processing_utilsr   r   r   Ú$transformers.tokenization_utils_baser   r   Úimage_processing_utilsr	   Úimage_utilsr
   r   Úutilsr   r   r&   r   Ú__all__r   r   r   ú<module>r†      s
  ðð  SÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RØ MÐ MÐ MÐ MÐ MÐ MÐ MÐ Mà 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø #Ð #Ð #Ð #Ð #Ð #ðð ð ð ð Ð,°Eð ñ ô ð ð ]P€ð ðb^ð b^ð b^ð b^ð b^�nñ b^ô b^ñ „ðb^ðJ Ð
€€€r   