Ë
    ÛÓ^j'   ã                   óZ   — 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	y)	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
„ Zd„ Zd„ Zd„ Zˆ xZS )ÚEvollaProcessorNc                 ó˜   •— |€t        d«      ‚|€t        d«      ‚t        ‰| �	  ||«       d| j                  _        || _        || _        y)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         €úw/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/evolla/processing_evolla.pyr   zEvollaProcessor.__init__   sU   ø€ ð Ð$ÜÐJÓKÐKØÐÜÐAÓBÐBä‰ÑÐ*¨IÔ6à#Aˆ�‰Ô Ø"4ˆÔØ.ˆÕó    c           
      óP  — g }|D ]‚  }|j                  d«      }|j                  d«      }dj                  t        ||«      D ��cg c]&  \  }}|j                  «       |j	                  «       z   ‘Œ( c}}«      }	|j                  |	«       Œ„ | j                  |dd|d¬«      }
|
S c c}}w )Nr   r   Ú ÚptT)Úreturn_tensorsÚ
truncationÚ
max_lengthÚpadding)ÚgetÚjoinÚzipÚupperÚlowerÚappendr   )r   Úproteinsr   Úsa_sequencesÚproteinr   r   ÚsÚfÚsa_sequenceÚ	sa_tokenss              r   Úprocess_proteinsz EvollaProcessor.process_proteins2   s¨   € ØˆØò 	-ˆGØ—[‘[ Ó*ˆFØ—{‘{ :Ó.ˆHØŸ'™'ÄSÈÐQYÓEZ×"[¹T¸QÀ 1§7¡7£9¨q¯w©w«yÓ#8Ó"[Ó\ˆKØ×Ñ Õ,ð		-ð ×*Ñ*Ø¨¸$ÐK]Ðgkð +ó 
ˆ	ð Ðùó #\s   Á+B"r   c                 ó¤   — g }|D ]1  }| j                   j                  |dd¬«      }|j                  |«       Œ3 | j                  |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?   sw   € ð
 ˆØò 	#ˆHØ—^‘^×7Ñ7ØØØ&*ð 8ó ˆFð
 �N‰N˜6Õ"ð	#ð Ÿ™ØØ$ØØØØ&ð 'ó 
ˆð Ðr   r&   Úmessages_listr   c                 óÐ  — |�|€t        d«      ‚|�|n| j                  }|�|n| j                  }t        |t        «      r|g}t        |t
        t        f«      rt        |d   t
        t        f«      s|g}t        |t
        t        f«      rt        d„ |D «       «      st        d«      ‚t        |t
        t        f«      r6t        d„ |D «       «      s$t        ddj                  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 «       «      sŒst        d|› �«      ‚ nt        dt        |«      › d
�«      ‚| j                  ||«      }| 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.
        z3You need to specify `messages_list` and `proteins`.é    c              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­w©N©Ú
isinstanceÚdict©Ú.0Úps     r   ú	<genexpr>z+EvollaProcessor.__call__.<locals>.<genexpr>ƒ   s   è ø€ Ò:aÐST¼:ÀaÌ×;NÑ:aùó   ‚zUThe proteins should be a list of dictionaries, but not all elements are dictionaries.c              3   ó\   K  — | ]$  }t        d „ |j                  «       D «       «      –— Œ& y­w)c              3   ó,   K  — | ]  }|t         v –— Œ y ­wr>   )ÚPROTEIN_VALID_KEYS)rC   Úks     r   rE   z5EvollaProcessor.__call__.<locals>.<genexpr>.<genexpr>†   s   è ø€ Ò:¨A�Ô'Ô'Ñ:ùs   ‚N)ÚallÚkeysrB   s     r   rE   z+EvollaProcessor.__call__.<locals>.<genexpr>…   s&   è ø€ ò ;
Ø?@ŒCÑ:°·±³Ô:×:ñ;
ùs   ‚*,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        «      –— Œ y ­wr>   r?   ©rC   Úms     r   rE   z+EvollaProcessor.__call__.<locals>.<genexpr>’   s   è ø€ ÒA°1œ: a¬×.ÑAùrF   zfEach message in messages_list should be a list of dictionaries, but not all elements are dictionaries.c              3   óT   K  — | ]   }t        |j                  «       «      d k7  –— Œ" y­w)é   N)ÚlenrL   rO   s     r   rE   z+EvollaProcessor.__call__.<locals>.<genexpr>–   s   è ø€ Ò<¨a”s˜1Ÿ6™6›8“}¨Õ)Ñ<ùs   ‚&(c              3   óX   K  — | ]"  }t        |j                  «       «      d dhk7  –— Œ$ y­w)ÚroleÚcontentN)ÚsetrL   rO   s     r   rE   z+EvollaProcessor.__call__.<locals>.<genexpr>–   s+   è ø€ ò DØ=>”C˜Ÿ™›“M f¨iÐ%8Õ8ñDùs   ‚(*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 Ú	input_idsÚattention_mask)Úprotein_input_idsÚprotein_attention_maskrX   rY   )Údata)r   r   r   r@   rA   ÚlistÚtuplerK   r!   rI   Ú	TypeErrorÚtypeÚanyr-   r9   r   )	r   r&   r:   r   r   r   r6   r,   Útext_tokenss	            r   Ú__call__zEvollaProcessor.__call__W   s+  € ð> Ð˜}Ð4ÜÐRÓSÐSà3EÐ3QÑ/ÐW[×WnÑWnÐØ-<Ð-H™/Èd×NbÑNbˆô �h¤Ô%Ø �zˆHä�m¤d¬E ]Ô3¼JÀ}ÐUVÑGWÔZ^Ô`eÐYfÔ<gØ*˜OˆMä�h¤¤u Ô.´sÑ:aÐX`Ô:aÔ7aÜÐtÓuÐuÜ�h¤¤u Ô.´sñ ;
ØDLô;
ô 8
ô ØDØ—9‘9Ô/Ó0Ð1ð 2Ø$˜:ð'óð ô �m¤d¬E ]Ô3Ø)ò �Ü! (¬T´5¨MÔ:Ü#Ð&aÔbfÐgoÓbpÐaqÐqrÐ$sÓtÐtÜÑA¸ÔAÔAÜ$ð Aóð ô Ñ<°8Ô<Ô<Äñ DØBJôDõ Aô %ð$Ø$, :ð/óð ñô ØXÔY]Ð^kÓYlÐXmÐmnÐoóð ð ×)Ñ)¨(Ð4FÓGˆ	à×'Ñ'¨°ÓGˆäà%.¨{Ñ%;Ø*3Ð4DÑ*EØ(¨Ñ5Ø"-Ð.>Ñ"?ñ	ô
ð 	
r   c                 ó:   —  | j                   j                  |i |¤ŽS r>   )r   Úbatch_decode©r   Úargsr   s      r   re   zEvollaProcessor.batch_decode®   s   € Ø*ˆt�~‰~×*Ñ*¨DÐ;°FÑ;Ð;r   c                 ó:   —  | j                   j                  |i |¤ŽS r>   )r   Údecoderf   s      r   ri   zEvollaProcessor.decode±   s   € Ø$ˆt�~‰~×$Ñ$ dÐ5¨fÑ5Ð5r   c                 ó:   —  | j                   j                  |i |¤ŽS r>   )r   re   rf   s      r   Úprotein_batch_decodez$EvollaProcessor.protein_batch_decode´   s    € Ø2ˆt×%Ñ%×2Ñ2°DÐC¸FÑCÐCr   c                 ó:   —  | j                   j                  |i |¤ŽS r>   )r   ri   rf   s      r   Úprotein_decodezEvollaProcessor.protein_decode·   s    € Ø,ˆt×%Ñ%×,Ñ,¨dÐ=°fÑ=Ð=r   )Né   é   )rn   )ro   )NNNN)Ú__name__Ú
__module__Ú__qualname__r   r-   Úintr9   r   r]   rA   rc   re   ri   rk   rm   Ú__classcell__)r   s   @r   r
   r
      s²   ø„ õ/ó(ð   #ñð óð0 ð .2Ø>BØ)-Ø&*ñT
à�t‘*˜tÑ# dÑ*ðT
ð ˜D ™JÑ'¨$¨t©*Ñ4°tÑ;ðT
ð   $™Jð	T
ð
 ˜t™òT
ó ðT
òl<ò6òDö>r   r
   N)
Ú__doc__Úfeature_extraction_utilsr   Úprocessing_utilsr   Úutilsr   rI   r
   Ú__all__© r   r   ú<module>r{      sJ   ðñõ 5õõ $ò 3Ð ð ô[>�nó [>ó ð[>ð| Ð
�r   