§
    9²¸j~!  ã                   óh   — d Z ddlmZ ddlmZ ddlmZ g d¢Ze G d„ de¦  «        ¦   «         ZdgZ	dS )	z
Processor class for EVOLLA.
é   )ÚBatchFeature)ÚProcessorMixin)Úauto_docstring)Úaa_seqÚfoldseekÚmsac                   ó†  ‡ — e Zd Zdˆ fd„	Zdd„Z	 ddefd„Ze	 	 	 	 ddee	         e	z  dz  d	eee	                  ee	         z  dz  d
edz  dedz  fd„¦   «         Z
	 	 ddee	         e	z  dz  d	eee	                  ee	         z  dz  fd„Z	 	 ddee	         e	z  dz  d	eee	                  ee	         z  dz  fd„Zd„ Zd„ Zˆ xZS )ÚEvollaProcessorNé   é   c                 óÄ   •— |€t          d¦  «        ‚|€t          d¦  «        ‚t          ¦   «                              ||¦  «         d| j        _        || _        || _        dS )a‰  
        protein_tokenizer (`EsmTokenizer`):
            An instance of [`EsmTokenizer`]. The protein tokenizer is a required input.
        protein_max_length (`int`, *optional*, defaults to 1024):
            The maximum length of the sequence to be generated.
        text_max_length (`int`, *optional*, defaults to 512):
            The maximum length of the text to be generated.
        Nz+You need to specify an `protein_tokenizer`.z"You need to specify a `tokenizer`.z<|reserved_special_token_0|>)Ú
ValueErrorÚsuperÚ__init__Ú	tokenizerÚ	pad_tokenÚprotein_max_lengthÚtext_max_length)ÚselfÚprotein_tokenizerr   r   r   ÚkwargsÚ	__class__s         €ú|/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/evolla/processing_evolla.pyr   zEvollaProcessor.__init__   sj   ø€ ð Ð$ÝÐJÑKÔKÐKØÐÝÐAÑBÔBÐBå‰Œ×ÒÐ*¨IÑ6Ô6Ð6à#AˆŒÔ Ø"4ˆÔØ.ˆÔÐÐó    c                 ó   — g }|D ]n}|                      d¦  «        }|                      d¦  «        }d                     d„ t          ||¦  «        D ¦   «         ¦  «        }|                     |¦  «         Œo|                      |dd|d¬¦  «        }|S )Nr   r   Ú c                 óf   — g | ].\  }}|                      ¦   «         |                     ¦   «         z   ‘Œ/S © )ÚupperÚlower)Ú.0ÚsÚfs      r   ú
<listcomp>z4EvollaProcessor.process_proteins.<locals>.<listcomp>5   s1   € Ð"[Ð"[Ð"[¹T¸QÀ 1§7¢7¡9¤9¨q¯wªw©y¬yÑ#8Ð"[Ð"[Ð"[r   ÚptT)Úreturn_tensorsÚ
truncationÚ
max_lengthÚpadding)ÚgetÚjoinÚzipÚappendr   )	r   Úproteinsr   Úsa_sequencesÚproteinr   r   Úsa_sequenceÚ	sa_tokenss	            r   Úprocess_proteinsz EvollaProcessor.process_proteins0   s©   € ØˆØð 	-ð 	-ˆGØ—[’[ Ñ*Ô*ˆFØ—{’{ :Ñ.Ô.ˆHØŸ'š'Ð"[Ð"[ÅSÈÐQYÑEZÔEZÐ"[Ñ"[Ô"[Ñ\Ô\ˆKØ×Ò Ñ,Ô,Ð,Ð,à×*Ò*Ø¨¸$ÐK]Ðgkð +ñ 
ô 
ˆ	ð Ðr   r   c                 ó®   — g }|D ]4}| j                              |dd¬¦  «        }|                     |¦  «         Œ5|                       |dddd|¬¦  «        }|S )NFT)ÚtokenizeÚadd_generation_promptr%   Úlongest)Úadd_special_tokensr&   r)   r'   r(   )r   Úapply_chat_templater-   )r   Útextsr   ÚpromptsÚmessagesÚpromptÚprompt_inputss          r   Úprocess_textzEvollaProcessor.process_text=   s†   € ð
 ˆØð 	#ð 	#ˆHØ”^×7Ò7ØØØ&*ð 8ñ ô ˆFð
 �NŠN˜6Ñ"Ô"Ð"Ð"àŸšØØ$ØØØØ&ð 'ñ 
ô 
ˆð Ðr   r.   Úmessages_listr   c                 ód  — |�|€t          d¦  «        ‚|�|n| j        }|�|n| j        }|                      ||¦  «        \  }}|                      ||¦  «         |                      ||¦  «        }|                      ||¦  «        }t          |d         |d         |d         |d         dœ¬¦  «        S )a¯  
        proteins (`Union[List[dict], dict]`):
            A list of dictionaries or a single dictionary containing the following keys:
                - `"aa_seq"` (`str`) -- The amino acid sequence of the protein.
                - `"foldseek"` (`str`) -- The foldseek string of the protein.
        messages_list (`Union[List[List[dict]], List[dict]]`):
            A list of lists of dictionaries or a list of dictionaries containing the following keys:
                - `"role"` (`str`) -- The role of the message.
                - `"content"` (`str`) -- The content of the message.
        protein_max_length (`int`, *optional*, defaults to 1024):
            The maximum length of the sequence to be generated.
        text_max_length (`int`, *optional*, defaults to 512):
            The maximum length of the text.

        Return:
            a dict with following keys:
                - `protein_input_ids` (`torch.Tensor` of shape `(batch_size, sequence_length)`) -- The input IDs for the protein sequence.
                - `protein_attention_mask` (`torch.Tensor` of shape `(batch_size, sequence_length)`) -- The attention mask for the protein sequence.
                - `text_input_ids` (`torch.Tensor` of shape `(batch_size, sequence_length)`) -- The input IDs for the text sequence.
                - `text_attention_mask` (`torch.Tensor` of shape `(batch_size, sequence_length)`) -- The attention mask for the text sequence.
        Nz3You need to specify `messages_list` and `proteins`.Ú	input_idsÚattention_mask)Úprotein_input_idsÚprotein_attention_maskrB   rC   )Údata)r   r   r   Úprepare_inputs_layoutÚvalidate_inputsr3   r?   r   )r   r.   r@   r   r   r   r2   Útext_tokenss           r   Ú__call__zEvollaProcessor.__call__U   sâ   € ð> Ð˜}Ð4ÝÐRÑSÔSÐSà3EÐ3QÐ/Ð/ÐW[ÔWnÐØ-<Ð-H˜/˜/ÈdÔNbˆØ"&×"<Ò"<¸XÀ}Ñ"UÔ"UÑˆ�-Ø×Ò˜X }Ñ5Ô5Ð5à×)Ò)¨(Ð4FÑGÔGˆ	Ø×'Ò'¨°ÑGÔGˆåà%.¨{Ô%;Ø*3Ð4DÔ*EØ(¨Ô5Ø"-Ð.>Ô"?ð	ð ð
ñ 
ô 
ð 	
r   c                 ó¼   — t          |t          ¦  «        r|g}t          |t          t          f¦  «        r%t          |d         t          t          f¦  «        s|g}||fS )Né    )Ú
isinstanceÚdictÚlistÚtuple)r   r.   r@   r   s       r   rG   z%EvollaProcessor.prepare_inputs_layoutˆ   sc   € õ �h¥Ñ%Ô%ð 	"Ø �zˆHå�m¥d­E ]Ñ3Ô3ð 	,½JÀ}ÐUVÔGWÕZ^Õ`eÐYfÑ<gÔ<gð 	,Ø*˜OˆMØ˜Ð&Ð&r   c                 ó$  — t          |t          t          f¦  «        r(t          d„ |D ¦   «         ¦  «        st	          d¦  «        ‚t          |t          t          f¦  «        rFt          d„ |D ¦   «         ¦  «        s-t	          dd                     t          ¦  «        › d|› �¦  «        ‚t          |t          t          f¦  «        r¯|D ]ª}t          |t          t          f¦  «        s t          dt          |¦  «        › d�¦  «        ‚t          d	„ |D ¦   «         ¦  «        st	          d
¦  «        ‚t          d„ |D ¦   «         ¦  «        st          d„ |D ¦   «         ¦  «        rt	          d|› �¦  «        ‚Œ«d S t	          dt          |¦  «        › d�¦  «        ‚)Nc              3   ó@   K  — | ]}t          |t          ¦  «        V — Œd S ©N©rM   rN   ©r!   Úps     r   ú	<genexpr>z2EvollaProcessor.validate_inputs.<locals>.<genexpr>�   s-   è è € Ð:aÐ:aÐST½:ÀaÍÑ;NÔ;NÐ:aÐ:aÐ:aÐ:aÐ:aÐ:ar   zUThe proteins should be a list of dictionaries, but not all elements are dictionaries.c              3   ól   K  — | ]/}t          d „ |                     ¦   «         D ¦   «         ¦  «        V — Œ0dS )c              3   ó(   K  — | ]}|t           v V — Œd S rS   )ÚPROTEIN_VALID_KEYS)r!   Úks     r   rW   z<EvollaProcessor.validate_inputs.<locals>.<genexpr>.<genexpr>    s(   è è € Ð:Ð:¨A�Õ'Ð'Ð:Ð:Ð:Ð:Ð:Ð:r   N)ÚallÚkeysrU   s     r   rW   z2EvollaProcessor.validate_inputs.<locals>.<genexpr>Ÿ   sR   è è € ð ;
ð ;
Ø?@�CÐ:Ð:°·²±´Ð:Ñ:Ô:Ñ:Ô:ð;
ð ;
ð ;
ð ;
ð ;
ð ;
r   z2There should be a list of dictionaries with keys: z, z for each protein.But got: z;Each messages in messages_list should be a list instead of ú.c              3   ó@   K  — | ]}t          |t          ¦  «        V — Œd S rS   rT   ©r!   Úms     r   rW   z2EvollaProcessor.validate_inputs.<locals>.<genexpr>¬   s,   è è € ÐAÐA°1�: a­Ñ.Ô.ÐAÐAÐAÐAÐAÐAr   zfEach message in messages_list should be a list of dictionaries, but not all elements are dictionaries.c              3   ó`   K  — | ])}t          |                     ¦   «         ¦  «        d k    V — Œ*dS )é   N)Úlenr]   r`   s     r   rW   z2EvollaProcessor.validate_inputs.<locals>.<genexpr>°   s5   è è € Ð<Ð<¨a•s˜1Ÿ6š6™8œ8‘}”}¨Ò)Ð<Ð<Ð<Ð<Ð<Ð<r   c              3   ód   K  — | ]+}t          |                     ¦   «         ¦  «        d dhk    V — Œ,dS )ÚroleÚcontentN)Úsetr]   r`   s     r   rW   z2EvollaProcessor.validate_inputs.<locals>.<genexpr>°   sS   è è € ð Dð DØ=>•C˜Ÿš™œ‘M”M f¨iÐ%8Ò8ðDð Dð Dð Dð Dð Dr   zlEach message in messages_list should be a list of dictionaries with two keys: 'role' and 'content'.But got: zFThe messages_list should be a list of lists of dictionaries, but it's )
rM   rO   rP   r\   r   r+   rZ   Ú	TypeErrorÚtypeÚany)r   r.   r@   r   r<   s        r   rH   zEvollaProcessor.validate_inputs–   s  € õ �h¥¥u Ñ.Ô.ð 	vµsÐ:aÐ:aÐX`Ð:aÑ:aÔ:aÑ7aÔ7að 	vÝÐtÑuÔuÐuÝ�h¥¥u Ñ.Ô.ð 	µsð ;
ð ;
ØDLð;
ñ ;
ô ;
ñ 8
ô 8
ð 	õ ð'Ø—9’9Õ/Ñ0Ô0ð'ð 'à$ð'ð 'ñô ð õ �m¥d­E ]Ñ3Ô3ð 	Ø)ð ð �Ý! (­Tµ5¨MÑ:Ô:ð uÝ#Ð$sÕbfÐgoÑbpÔbpÐ$sÐ$sÐ$sÑtÔtÐtÝÐAÐA¸ÐAÑAÔAÑAÔAð Ý$ð Añô ð õ Ð<Ð<°8Ð<Ñ<Ô<Ñ<Ô<ð Åð Dð DØBJðDñ Dô Dñ Aô Að õ %ð/Ø$,ð/ð /ñô ð ððð õ ØoÕY]Ð^kÑYlÔYlÐoÐoÐoñô ð r   c                 ó&   —  | j         j        |i |¤ŽS rS   )r   Úbatch_decode©r   Úargsr   s      r   Úprotein_batch_decodez$EvollaProcessor.protein_batch_decode¼   s   € Ø2ˆtÔ%Ô2°DÐC¸FÐCÐCÐCr   c                 ó&   —  | j         j        |i |¤ŽS rS   )r   Údecodern   s      r   Úprotein_decodezEvollaProcessor.protein_decode¿   s   € Ø,ˆtÔ%Ô,¨dÐ=°fÐ=Ð=Ð=r   )Nr   r   )r   )r   )NNNN)NN)Ú__name__Ú
__module__Ú__qualname__r   r3   Úintr?   r   rO   rN   rJ   rG   rH   rp   rs   Ú__classcell__)r   s   @r   r
   r
      sÒ  ø€ € € € € ð/ð /ð /ð /ð /ð /ð(ð ð ð ð   #ðð ð ðð ð ð ð0 ð .2Ø>BØ)-Ø&*ð0
ð 0
à�t”*˜tÑ# dÑ*ð0
ð ˜D œJÔ'¨$¨t¬*Ñ4°tÑ;ð0
ð   $™Jð	0
ð
 ˜t™ð0
ð 0
ð 0
ñ „^ð0
ðh .2Ø>Bð'ð 'à�t”*˜tÑ# dÑ*ð'ð ˜D œJÔ'¨$¨t¬*Ñ4°tÑ;ð'ð 'ð 'ð 'ð  .2Ø>Bð$ð $à�t”*˜tÑ# dÑ*ð$ð ˜D œJÔ'¨$¨t¬*Ñ4°tÑ;ð$ð $ð $ð $ðLDð Dð Dð>ð >ð >ð >ð >ð >ð >r   r
   N)
Ú__doc__Úfeature_extraction_utilsr   Úprocessing_utilsr   Úutilsr   rZ   r
   Ú__all__r   r   r   ú<module>r~      s¢   ððð ð 5Ð 4Ð 4Ð 4Ð 4Ð 4Ø .Ð .Ð .Ð .Ð .Ð .Ø #Ð #Ð #Ð #Ð #Ð #ð 3Ð2Ð2Ð ð ðe>ð e>ð e>ð e>ð e>�nñ e>ô e>ñ „ðe>ðP Ð
€€€r   