Ë
    S^(hé3  ã                   ó”   — d Z ddlZddlZddlmZmZm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)ÚListÚOptionalÚTupleé   )Ú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       úl/var/www/skyplay_api_hub/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            
       óB  ‡ — 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e      dee   fd„Z	 ddee   deee      dedee   fˆ fd„Z	 ddee   deee      dee   fd„Zddedee   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__\   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)r:   r1   ©r?   s    r   Ú
vocab_sizezBioGptTokenizer.vocab_sizeˆ   s   € ô �4—<‘<Ó Ð r   c                 óB   — t        | j                  fi | j                  ¤ŽS ©N)r7   r1   Úadded_tokens_encoderrI   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 )NrP   )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)r;   Ú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>)r6   r<   r   Úminr;   r:   Ú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.ri   )r5   rX   r*   rl   Úlistrw   )r?   rV   Úbypass_tokenizerÚsplit_tokensrp   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.)r1   rc   r%   )r?   rp   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.)r3   rc   r%   )r?   rk   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.ri   Ú r`   )Úreplacero   r5   r^   r*   )r?   r\   ÚtrV   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-Ú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_maskr:   )r?   r†   r‡   rŽ   rG   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   c                 óŽ   — | j                   g}|€t        ||z   «      dgz  S t        ||z   «      dgz  t        ||z   «      dgz  z   S )aà  
        Create a mask from the two sequences passed to be used in a sequence-pair classification task. A FAIRSEQ
        Transformer sequence pair mask has the following format:

        ```
        0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
        | first sequence    | second sequence |
        ```

        If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s).

        Args:
            token_ids_0 (`List[int]`):
                List of IDs.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s).
        r   r   )rŠ   r:   r‹   s       r   Ú$create_token_type_ids_from_sequencesz4BioGptTokenizer.create_token_type_ids_from_sequences  s^   € ð. × Ñ Ð!ˆð ÐÜ�{ SÑ(Ó)¨Q¨CÑ/Ð/Ü�; Ñ$Ó%¨¨Ñ+¬c°+ÀÑ2CÓ.DÈÀsÑ.JÑJÐJr   Ú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   rf   z1BioGptTokenizer.save_vocabulary.<locals>.<lambda>J  s   € ÐY[Ð\]ÑY^€ r   rg   zSaving vocabulary to zZ: BPE merge indices are not consecutive. Please check that the tokenizer is not corrupted!ri   r   )ÚosÚpathÚisdirÚloggerÚerrorro   ÚVOCAB_FILES_NAMESr.   Úwriter/   Údumpsr1   Úsortedr;   r2   Úwarning)
r?   r“   r”   r	   Ú
merge_fileÚfrk   Ú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__V  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>)FrL   )NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r¢   Úvocab_files_namesÚmodel_input_namesr>   ÚpropertyrJ   rN   rX   r^   rw   r|   r~   r€   r…   r   Úintr   r�   Úboolr�   r’   Ústrr   r¬   r±   r´   Ú__classcell__)rG   s   @r   r   r   .   sM  ø„ ñ(ðT *ÐØ$Ð&6Ð7Ðð ØØØØõ*
ðX ñ!ó ð!ò?ò
òEò*óXòIò7òð JNñ5Ø ™9ð5Ø3;¸DÀ¹IÑ3Fð5à	ˆc‰ó5ð2 sxñ.Ø ™9ð.Ø3;¸DÀ¹IÑ3Fð.Økoð.à	ˆc‰õ.ð8 JNñKØ ™9ðKØ3;¸DÀ¹IÑ3FðKà	ˆc‰óKñ<&¨cð &ÀHÈSÁMð &Ð]bÐcfÑ]gó &ò8ö
r   r   )r¹   r/   r�   Útypingr   r   r   Útokenization_utilsr   Úutilsr   Ú
get_loggerr¶   r    r¢   r   r   Ú__all__r'   r   r   ú<module>rÆ      s_   ðñ 'ã Û 	ß (Ñ (å 5Ý ð 
ˆ×	Ñ	˜HÓ	%€ð ØñÐ ò
ôxÐ)ô xðv	 Ð
�r   