Ë
    ÛÓ^jJ/  ã                   ó€   — d Z ddlZddlZddlmZ ddlmZ  ej                  e«      Z	dddœZ
d	„ Z G d
„ de«      ZdgZy)z Tokenization classes for BioGPT.é    Né   )ÚPreTrainedTokenizer)Úloggingz
vocab.jsonz
merges.txt)Ú
vocab_fileÚmerges_filec                 ób   — t        «       }| d   }| dd D ]  }|j                  ||f«       |}Œ |S )zƒ
    Return set of symbol pairs in a word. word is represented as tuple of symbols (symbols being variable-length
    strings)
    r   é   N)ÚsetÚadd)ÚwordÚpairsÚ	prev_charÚchars       úy/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/biogpt/tokenization_biogpt.pyÚ	get_pairsr      sF   € ô
 ‹E€EØ�Q‘€IØ�Q�R�ò ˆØ�	‰	�9˜dÐ#Ô$Ø‰	ðð €Ló    c            
       ó  ‡ — e Zd ZdZeZddgZ	 	 	 	 	 dˆ fd„	Zed„ «       Z	d„ Z
d„ Zd„ Zd	„ Zdd
„Zd„ Zd„ Zd„ Z	 ddee   dee   dz  dee   fd„Z	 ddee   dee   dz  dedee   fˆ fd„Zddededz  dee   fd„Zd„ Zd„ Zˆ xZS )ÚBioGptTokenizera:  
    Construct an FAIRSEQ Transformer tokenizer. Moses tokenization followed by Byte-Pair Encoding.

    This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
    this superclass for more information regarding those methods.

    Args:
        vocab_file (`str`):
            Path to the vocabulary file.
        merges_file (`str`):
            Merges file.
        unk_token (`str`, *optional*, defaults to `"<unk>"`):
            The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
            token instead.
        bos_token (`str`, *optional*, defaults to `"<s>"`):
            The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token.

            <Tip>

            When building a sequence using special tokens, this is not the token that is used for the beginning of
            sequence. The token used is the `cls_token`.

            </Tip>

        eos_token (`str`, *optional*, defaults to `"</s>"`):
            The end of sequence token.

            <Tip>

            When building a sequence using special tokens, this is not the token that is used for the end of sequence.
            The token used is the `sep_token`.

            </Tip>

        sep_token (`str`, *optional*, defaults to `"</s>"`):
            The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
            sequence classification or for a text and a question for question answering. It is also used as the last
            token of a sequence built with special tokens.
        pad_token (`str`, *optional*, defaults to `"<pad>"`):
            The token used for padding, for example when batching sequences of different lengths.
    Ú	input_idsÚattention_maskc           
      óØ  •— 	 dd l }	d| _        |	| _        i | _        i | _        	 t        |d¬«      5 }
t        j                  |
«      | _	        d d d «       | j                  j                  «       D ��ci c]  \  }}||“Œ
 c}}| _        t        |d¬«      5 }|j                  «       j                  d«      d d }d d d «       D �cg c]  }t        |j                  «       d d «      ‘Œ  }}t        t!        |t#        t%        |«      «      «      «      | _        i | _        t+        ‰| �X  d
|||||d	œ|¤Ž y # t        $ r t        d«      ‚w xY w# 1 sw Y   ŒþxY wc c}}w # 1 sw Y   Œ¥xY wc c}w )Nr   zqYou need to install sacremoses to use BioGptTokenizer. See https://pypi.org/project/sacremoses/ for installation.Úenúutf-8©Úencodingú
éÿÿÿÿé   )Ú	bos_tokenÚ	eos_tokenÚ	sep_tokenÚ	unk_tokenÚ	pad_token© )Ú
sacremosesÚImportErrorÚlangÚsmÚcache_moses_tokenizerÚcache_moses_detokenizerÚopenÚjsonÚloadÚencoderÚitemsÚdecoderÚreadÚsplitÚtupleÚdictÚzipÚrangeÚlenÚ	bpe_ranksÚcacheÚsuperÚ__init__)Úselfr   r   r"   r   r    r!   r#   Úkwargsr%   Úvocab_handleÚkÚvÚmerges_handleÚmergesÚmergeÚ	__class__s                   €r   r;   zBioGptTokenizer.__init__Z   sb  ø€ ð	Ûð ˆŒ	ØˆŒà%'ˆÔ"Ø')ˆÔ$àÜ�* wÔ/ð 	3°<ÜŸ9™9 \Ó2ˆDŒL÷	3à)-¯©×);Ñ);Ó)=×>¡  A˜˜1™Ó>ˆŒÜ�+¨Ô0ð 	;°MØ"×'Ñ'Ó)×/Ñ/°Ó5°c°rÐ:ˆF÷	;à8>Ö?¨u”%˜Ÿ™› b qÐ)Õ*Ð?ˆÐ?Üœc &¬%´°F³Ó*<Ó=Ó>ˆŒØˆŒ
ä‰Ñð 	
ØØØØØñ	
ð ó	
øô- ò 	ÜðMóð ð	ú÷	3ð 	3üã>÷	;ð 	;üâ?s.   ƒD1 ±E	Á1EÂ#EÃ#E'Ä1EÅ	EÅE$c                 ó,   — t        | j                  «      S )zReturns vocab size)r7   r.   ©r<   s    r   Ú
vocab_sizezBioGptTokenizer.vocab_size†   s   € ô �4—<‘<Ó Ð r   c                 óB   — t        | j                  fi | j                  ¤ŽS ©N)r4   r.   Úadded_tokens_encoderrF   s    r   Ú	get_vocabzBioGptTokenizer.get_vocab‹   s   € Ü�D—L‘LÑ> D×$=Ñ$=Ñ>Ð>r   c                 ó¸   — || j                   vr+| j                  j                  |¬«      }|| j                   |<   | j                   |   j                  |ddd¬«      S )N©r'   TF)Úaggressive_dash_splitsÚ
return_strÚescape)r)   r(   ÚMosesTokenizerÚtokenize)r<   Útextr'   Úmoses_tokenizers       r   Úmoses_tokenizezBioGptTokenizer.moses_tokenizeŽ   sc   € Ø�t×1Ñ1Ñ1Ø"Ÿg™g×4Ñ4¸$Ð4Ó?ˆOØ/>ˆD×&Ñ& tÑ,Ø×)Ñ)¨$Ñ/×8Ñ8Ø¨¸%Èð 9ó 
ð 	
r   c                 ó°   — || j                   vr+| j                  j                  |¬«      }|| j                   |<   | j                   |   j                  |«      S )NrM   )r*   r(   ÚMosesDetokenizerÚ
detokenize)r<   Útokensr'   Úmoses_detokenizers       r   Úmoses_detokenizez BioGptTokenizer.moses_detokenize–   sT   € Ø�t×3Ñ3Ñ3Ø $§¡× 8Ñ 8¸dÐ 8Ó CÐØ1BˆD×(Ñ(¨Ñ.Ø×+Ñ+¨DÑ1×<Ñ<¸VÓDÐDr   c                 ó  ‡ — t        |d d «      |d   dz   fz   }|‰ j                  v r‰ j                  |   S t        |«      }|s|dz   S 	 t        |ˆ fd„¬«      }|‰ j                  vrnÎ|\  }}g }d}|t        |«      k  r�	 |j                  ||«      }	|j                  |||	 «       |	}||   |k(  r6|t        |«      dz
  k  r%||dz      |k(  r|j                  ||z   «       |dz  }n|j                  ||   «       |dz  }|t        |«      k  rŒ�t        |«      }|}t        |«      dk(  rnt        |«      }Œídj                  |«      }|d	k(  rd
}|‰ j                  |<   |S # t        $ r |j                  ||d  «       Y Œpw xY w)Nr   ú</w>c                 óN   •— ‰j                   j                  | t        d«      «      S )NÚinf)r8   ÚgetÚfloat)Úpairr<   s    €r   ú<lambda>z%BioGptTokenizer.bpe.<locals>.<lambda>¦   s   ø€ °·±×1CÑ1CÀDÌ%ÐPUË,Ó1W€ r   ©Úkeyr   r	   r   ú z
  </w>z
</w>)r3   r9   r   Úminr8   r7   ÚindexÚextendÚ
ValueErrorÚappendÚjoin)
r<   Útokenr   r   ÚbigramÚfirstÚsecondÚnew_wordÚiÚjs
   `         r   ÚbpezBioGptTokenizer.bpeœ   sª  ø€ Ü�U˜3˜B�ZÓ  E¨"¡I°Ñ$6Ð#8Ñ8ˆØ�D—J‘JÑØ—:‘:˜eÑ$Ð$Ü˜$“ˆáØ˜6‘>Ð!àÜ˜Ó$WÔXˆFØ˜TŸ^™^Ñ+ØØ"‰MˆE�6ØˆHØˆAØ”c˜$“i’-ðØŸ
™
 5¨!Ó,�Að
 —O‘O D¨¨1 IÔ.Ø�Aà˜‘7˜eÒ#¨¬C°«I¸©MÒ(9¸dÀ1ÀqÁ5¹kÈVÒ>SØ—O‘O E¨F¡NÔ3Ø˜‘F‘Aà—O‘O D¨¡GÔ,Ø˜‘F�Að ”c˜$“i“-ô  ˜X“ˆHØˆDÜ�4‹y˜AŠ~Øä! $›�ð9 ð: �x‰x˜‹~ˆØ�:ÒØˆDØ ˆ�
‰
�5ÑØˆøô/ "ò Ø—O‘O D¨¨ HÔ-Ùðús   ÂE  Å F Å?F c                 óì   — |r|j                  «       }n| j                  || j                  «      }g }|D ]=  }|sŒ|j                  t	        | j                  |«      j                  d«      «      «       Œ? |S )zReturns a tokenized string.rf   )r2   rU   r'   ri   Úlistrt   )r<   rS   Úbypass_tokenizerÚsplit_tokensrm   s        r   Ú	_tokenizezBioGptTokenizer._tokenizeÈ   sm   € áØ—:‘:“<‰Dà×&Ñ& t¨T¯Y©YÓ7ˆDàˆØò 	FˆEÚØ×#Ñ#¤D¨¯©°%«×)>Ñ)>¸sÓ)CÓ$DÕEð	Fð Ðr   c                 ó€   — | j                   j                  || j                   j                  | j                  «      «      S )z0Converts a token (str) in an id using the vocab.)r.   r`   r"   )r<   rm   s     r   Ú_convert_token_to_idz$BioGptTokenizer._convert_token_to_idÖ   s,   € à�|‰|×Ñ  t§|¡|×'7Ñ'7¸¿¹Ó'GÓHÐHr   c                 óN   — | j                   j                  || j                  «      S )z=Converts an index (integer) in a token (str) using the vocab.)r0   r`   r"   )r<   rh   s     r   Ú_convert_id_to_tokenz$BioGptTokenizer._convert_id_to_tokenÚ   s   € à�|‰|×Ñ  t§~¡~Ó6Ð6r   c                 óä   — |D �cg c]$  }|j                  dd«      j                  dd«      ‘Œ& }}dj                  |«      j                  «       }| j                  || j                  «      }|S c c}w )z:Converts a sequence of tokens (string) in a single string.rf   Ú r]   )Úreplacerl   r2   r[   r'   )r<   rY   ÚtrS   s       r   Úconvert_tokens_to_stringz(BioGptTokenizer.convert_tokens_to_stringÞ   sh   € ð DJÖJ¸a�!—)‘)˜C Ó$×,Ñ,¨V°SÕ9ÐJˆÐJØ—‘˜“×&Ñ&Ó(ˆà×$Ñ$ V¨T¯Y©YÓ7ˆØˆùò	 Ks   …)A-NÚtoken_ids_0Útoken_ids_1Úreturnc                 óV   — |€| j                   g|z   S | j                   g}||z   |z   |z   S )a‹  
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
        adding special tokens. A BioGPT sequence has the following format:

        - single sequence: `</s> X `
        - pair of sequences: `</s> A </s> B `

        Args:
            token_ids_0 (`List[int]`):
                List of IDs to which the special tokens will be added.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
        )Úsep_token_id)r<   rƒ   r„   Úseps       r   Ú build_inputs_with_special_tokensz0BioGptTokenizer.build_inputs_with_special_tokensç   sA   € ð& ÐØ×%Ñ%Ð&¨Ñ4Ð4Ø× Ñ Ð!ˆØ�[Ñ  3Ñ&¨Ñ4Ð4r   Úalready_has_special_tokensc                 ó¤   •— |rt         ‰| �  ||d¬«      S |�'dgdgt        |«      z  z   dgz   dgt        |«      z  z   S dgdgt        |«      z  z   S )aÄ  
        Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
        special tokens using the tokenizer `prepare_for_model` method.

        Args:
            token_ids_0 (`List[int]`):
                List of IDs.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.
            already_has_special_tokens (`bool`, *optional*, defaults to `False`):
                Whether or not the token list is already formatted with special tokens for the model.

        Returns:
            `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
        T)rƒ   r„   rŠ   r	   r   )r:   Úget_special_tokens_maskr7   )r<   rƒ   r„   rŠ   rD   s       €r   rŒ   z'BioGptTokenizer.get_special_tokens_maskÿ   sy   ø€ ñ$ &Ü‘7Ñ2Ø'°[Ð]að 3ó ð ð Ð"Ø�3˜1˜#¤ KÓ 0Ñ0Ñ1°Q°CÑ7¸A¸3ÄÀ[ÓAQÑ;QÑRÐRØˆs�q�cœC Ó,Ñ,Ñ-Ð-r   Úsave_directoryÚfilename_prefixc           	      ó.  — t         j                  j                  |«      st        j	                  d|› d�«       y t         j                  j                  ||r|dz   ndt        d   z   «      }t         j                  j                  ||r|dz   ndt        d   z   «      }t        |dd¬	«      5 }|j                  t        j                  | j                  d
dd¬«      dz   «       d d d «       d}t        |dd¬	«      5 }t        | j                  j                  «       d„ ¬«      D ]M  \  }}	||	k7  rt        j                  d|› d�«       |	}|j                  dj                  |«      dz   «       |dz  }ŒO 	 d d d «       ||fS # 1 sw Y   Œ�xY w# 1 sw Y   ||fS xY w)NzVocabulary path (z) should be a directoryú-r   r   r   Úwr   r   r   TF)ÚindentÚ	sort_keysÚensure_asciir   r   c                 ó   — | d   S )Nr	   r$   )Úkvs    r   rc   z1BioGptTokenizer.save_vocabulary.<locals>.<lambda>*  s   € ÐY[Ð\]ÑY^€ r   rd   zSaving vocabulary to zZ: BPE merge indices are not consecutive. Please check that the tokenizer is not corrupted!rf   r	   )ÚosÚpathÚisdirÚloggerÚerrorrl   ÚVOCAB_FILES_NAMESr+   Úwriter,   Údumpsr.   Úsortedr8   r/   Úwarning)
r<   r�   rŽ   r   Ú
merge_fileÚfrh   ÚwriterÚ
bpe_tokensÚtoken_indexs
             r   Úsave_vocabularyzBioGptTokenizer.save_vocabulary  s�  € Ü�w‰w�}‰}˜^Ô,Ü�L‰LÐ,¨^Ð,<Ð<SÐTÔUØÜ—W‘W—\‘\Ø±o˜_¨sÒ2È2ÔQbÐcoÑQpÑpó
ˆ
ô —W‘W—\‘\Ø±o˜_¨sÒ2È2ÔQbÐcpÑQqÑqó
ˆ
ô �*˜c¨GÔ4ð 	c¸Ø�G‰G”D—J‘J˜tŸ|™|°AÀÐTYÔZÐ]aÑaÔb÷	cð ˆÜ�*˜c¨GÔ4ð 		¸Ü+1°$·.±.×2FÑ2FÓ2HÑN^Ô+_ò Ñ'�
˜KØ˜KÒ'Ü—N‘NØ/°
¨|ð <Mð Môð (�EØ—‘˜SŸX™X jÓ1°DÑ8Ô9Ø˜‘
‘ñ÷		ð ˜:Ð%Ð%÷	cð 	cú÷		ð ˜:Ð%Ð%ús   Â*6E<Ã8A7FÅ<FÆFc                 óD   — | j                   j                  «       }d |d<   |S )Nr(   )Ú__dict__Úcopy)r<   Ústates     r   Ú__getstate__zBioGptTokenizer.__getstate__6  s"   € Ø—‘×"Ñ"Ó$ˆØˆˆd‰Øˆr   c                 óZ   — || _         	 dd l}|| _        y # t        $ r t        d«      ‚w xY w)Nr   znYou need to install sacremoses to use XLMTokenizer. See https://pypi.org/project/sacremoses/ for installation.)r¨   r%   r&   r(   )r<   Údr%   s      r   Ú__setstate__zBioGptTokenizer.__setstate__;  s>   € ØˆŒð	Ûð ˆ�øô ò 	ÜðMóð ð	ús   ‰ •*)z<unk>z<s>ú</s>r¯   z<pad>)FrI   )NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rœ   Úvocab_files_namesÚmodel_input_namesr;   ÚpropertyrG   rK   rU   r[   rt   ry   r{   r}   r‚   rv   Úintr‰   ÚboolrŒ   Ústrr3   r¦   r«   r®   Ú__classcell__)rD   s   @r   r   r   ,   s  ø„ ñ(ðT *ÐØ$Ð&6Ð7Ðð ØØØØõ*
ðX ñ!ó ð!ò?ò
òEò*óXòIò7òð GKñ5Ø ™9ð5Ø37¸±9¸tÑ3Cð5à	ˆc‰ó5ð2 puñ.Ø ™9ð.Ø37¸±9¸tÑ3Cð.Øhlð.à	ˆc‰õ.ñ6&¨cð &ÀCÈ$ÁJð &ÐZ_Ð`cÑZdó &ò8ö
r   r   )r³   r,   r—   Útokenization_pythonr   Úutilsr   Ú
get_loggerr°   rš   rœ   r   r   Ú__all__r$   r   r   ú<module>r¿      sY   ðñ 'ã Û 	å 6Ý ð 
ˆ×	Ñ	˜HÓ	%€ð ØñÐ ò
ôZÐ)ô Zðz Ð
�r   