
    sgJ-                         d dl Z d dlmZ d dlmZmZmZ d dlmZ ddl	m
Z
mZ ddlmZ ddlmZmZ  e       rd	d
lmZ ndZ ej&                  e      ZdddZg dZ G d de      Zy)    N)copyfile)ListOptionalTuple)
processors   )
AddedTokenBatchEncoding)PreTrainedTokenizerFast)is_sentencepiece_availablelogging   )MBart50Tokenizerzsentencepiece.bpe.modelztokenizer.json)
vocab_filetokenizer_file)4ar_ARcs_CZde_DEen_XXes_XXet_EEfi_FIfr_XXgu_INhi_INit_ITja_XXkk_KZko_KRlt_LTlv_LVmy_MMne_NPnl_XXro_ROru_RUsi_LKtr_TRvi_VNzh_CNaf_ZAaz_AZbn_INfa_IRhe_ILhr_HRid_IDka_GEkm_KHmk_MKml_INmn_MNmr_INpl_PLps_AFpt_XXsv_SEsw_KEta_INte_INth_THtl_XXuk_UAur_PKxh_ZAgl_ESsl_SIc                       e Zd ZU dZeZddgZeZg Z	e
e   ed<   g Ze
e   ed<   	 	 	 	 	 	 	 	 	 	 d fd	Zedefd	       Zedefd
       Zej(                  deddfd       Z	 dde
e   dee
e      de
e   fdZ	 	 	 d de
e   dedee
e      dedef
 fdZd Zd ZdeddfdZdeddfdZdedee   dee   fdZddedee   dee   fdZ xZ S )!MBart50TokenizerFastu	  
    Construct a "fast" MBART tokenizer for mBART-50 (backed by HuggingFace's *tokenizers* library). Based on
    [BPE](https://huggingface.co/docs/tokenizers/python/latest/components.html?highlight=BPE#models).

    This tokenizer inherits from [`PreTrainedTokenizerFast`] 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.
        src_lang (`str`, *optional*):
            A string representing the source language.
        tgt_lang (`str`, *optional*):
            A string representing the target language.
        eos_token (`str`, *optional*, defaults to `"</s>"`):
            The end of sequence token.
        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.
        cls_token (`str`, *optional*, defaults to `"<s>"`):
            The classifier token which is used when doing sequence classification (classification of the whole sequence
            instead of per-token classification). It is the first token of the sequence when built with special tokens.
        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.
        pad_token (`str`, *optional*, defaults to `"<pad>"`):
            The token used for padding, for example when batching sequences of different lengths.
        mask_token (`str`, *optional*, defaults to `"<mask>"`):
            The token used for masking values. This is the token used when training this model with masked language
            modeling. This is the token which the model will try to predict.

    Examples:

    ```python
    >>> from transformers import MBart50TokenizerFast

    >>> tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50", src_lang="en_XX", tgt_lang="ro_RO")
    >>> src_text = " UN Chief Says There Is No Military Solution in Syria"
    >>> tgt_text = "Şeful ONU declară că nu există o soluţie militară în Siria"
    >>> model_inputs = tokenizer(src_text, text_target=tgt_text, return_tensors="pt")
    >>> # model(**model_inputs) should work
    ```	input_idsattention_maskprefix_tokenssuffix_tokensNc                    t        |
t              rt        |
dd      n|
}
|j                  dg       xs g |d<   |dxx   t        D cg c]  }||d   vs| c}z  cc<   t        |   |f||||||||	|
d	| || _        t        D ci c]  }|| j                  |       c}| _	        ||nd| _
        || _        | j                  | j                     | _        | j                  | j                         y c c}w c c}w )NTF)lstriprstripadditional_special_tokens)	src_langtgt_langr   	eos_token	sep_token	cls_token	unk_token	pad_token
mask_tokenr   )
isinstancestrr	   getFAIRSEQ_LANGUAGE_CODESsuper__init__r   convert_tokens_to_idslang_code_to_id	_src_langrQ   cur_lang_code_idset_src_lang_special_tokens)selfr   rP   rQ   r   rR   rS   rT   rU   rV   rW   kwargscode	lang_code	__class__s                 h/var/www/html/venv/lib/python3.12/site-packages/transformers/models/mbart50/tokenization_mbart50_fast.pyr]   zMBart50TokenizerFast.__init__]   s-    KUU_adJeZ
4Fku
.4jj9TVX.Y._]_*+*+30
t6JeCf7fD0
 	
+ 		
)!	
 	
 % Oe 
AJIt11)<< 
 &.%9w  $ 4 4T^^ D((870
( 
s   	C8
C8
C=returnc                 p    | j                   r)t        j                  j                  | j                         S dS )NF)r   ospathisfilerc   s    rh   can_save_slow_tokenizerz,MBart50TokenizerFast.can_save_slow_tokenizer   s$    26//rww~~doo.LuL    c                     | j                   S N)r`   rn   s    rh   rP   zMBart50TokenizerFast.src_lang   s    ~~rp   new_src_langc                 H    || _         | j                  | j                          y rr   )r`   rb   )rc   rs   s     rh   rP   zMBart50TokenizerFast.src_lang   s    %((8rp   token_ids_0token_ids_1c                 |    || j                   |z   | j                  z   S | j                   |z   |z   | j                  z   S )a  
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
        adding special tokens. The special tokens depend on calling set_lang.

        An MBART-50 sequence has the following format, where `X` represents the sequence:

        - `input_ids` (for encoder) `[src_lang_code] X [eos]`
        - `labels`: (for decoder) `[tgt_lang_code] X [eos]`

        BOS is never used. Pairs of sequences are not the expected use case, but they will be handled without a
        separator.

        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.
        )rJ   rK   )rc   ru   rv   s      rh    build_inputs_with_special_tokensz5MBart50TokenizerFast.build_inputs_with_special_tokens   sG    0 %%3d6H6HHH!!K/+=@R@RRRrp   	src_textsrP   	tgt_textsrQ   c                 B    || _         || _        t        |   ||fi |S rr   )rP   rQ   r\   prepare_seq2seq_batch)rc   ry   rP   rz   rQ   rd   rg   s         rh   r|   z*MBart50TokenizerFast.prepare_seq2seq_batch   s*     ! w,Y	LVLLrp   c                 8    | j                  | j                        S rr   )rb   rP   rn   s    rh   _switch_to_input_modez*MBart50TokenizerFast._switch_to_input_mode       //>>rp   c                 8    | j                  | j                        S rr   )set_tgt_lang_special_tokensrQ   rn   s    rh   _switch_to_target_modez+MBart50TokenizerFast._switch_to_target_mode   r   rp   c                    | j                  |      | _        | j                  g| _        | j                  g| _        | j                  | j                        }| j                  | j                        }t        j                  |dgz   |z   |ddgz   |z   t        t        ||z   | j                  | j                  z                     | j                  _        y)z]Reset the special tokens to the source lang setting. prefix=[src_lang_code] and suffix=[eos].$A$Bsinglepairspecial_tokensNr^   ra   rJ   eos_token_idrK   convert_ids_to_tokensr   TemplateProcessinglistzip
_tokenizerpost_processor)rc   rP   prefix_tokens_strsuffix_tokens_strs       rh   rb   z0MBart50TokenizerFast.set_src_lang_special_tokens        $ : :8 D"334"//0 66t7I7IJ 66t7I7IJ)3)F)F$v-0AA"dD\14EE$58I$I4K]K]`d`r`rKr st*
&rp   c                    | j                  |      | _        | j                  g| _        | j                  g| _        | j                  | j                        }| j                  | j                        }t        j                  |dgz   |z   |ddgz   |z   t        t        ||z   | j                  | j                  z                     | j                  _        y)zaReset the special tokens to the target language setting. prefix=[src_lang_code] and suffix=[eos].r   r   r   Nr   )rc   rQ   r   r   s       rh   r   z0MBart50TokenizerFast.set_tgt_lang_special_tokens   r   rp   return_tensorsc                 v    ||t        d      || _         | |fd|d|}| j                  |      }||d<   |S )zIUsed by translation pipeline, to prepare inputs for the generate functionzATranslation requires a `src_lang` and a `tgt_lang` for this modelT)add_special_tokensr   forced_bos_token_id)
ValueErrorrP   r^   )rc   
raw_inputsr   rP   rQ   extra_kwargsinputstgt_lang_ids           rh   _build_translation_inputsz.MBart50TokenizerFast._build_translation_inputs   sY     x/`aa jiT.i\hi00:(3$%rp   save_directoryfilename_prefixc                    | j                   st        d      t        j                  j	                  |      st
        j                  d| d       y t        j                  j                  ||r|dz   ndt        d   z         }t        j                  j                  | j                        t        j                  j                  |      k7  rt        | j                  |       |fS )NzhYour fast tokenizer does not have the necessary information to save the vocabulary for a slow tokenizer.zVocabulary path (z) should be a directory- r   )ro   r   rk   rl   isdirloggererrorjoinVOCAB_FILES_NAMESabspathr   r   )rc   r   r   out_vocab_files       rh   save_vocabularyz$MBart50TokenizerFast.save_vocabulary   s    ++ 
 ww}}^,LL,^,<<STUo_s22QbcoQpp
 77??4??+rww~/NNT__n5  rp   )
NNNN</s>r   z<s>z<unk>z<pad>z<mask>rr   )r   Nr%   )!__name__
__module____qualname____doc__r   vocab_files_namesmodel_input_namesr   slow_tokenizer_classrJ   r   int__annotations__rK   r]   propertyboolro   rY   rP   setterr   rx   r
   r|   r~   r   rb   r   r   r   r   __classcell__)rg   s   @rh   rG   rG   )   s   *X *$&67+!M49!!M49! -9^ M M M #   __9S 9T 9 9
 JNS9S3;DI3FS	cS@  )-
M9
M 
M DI&	
M
 
M 

M??
C 
D 

C 
D 

*-
9A#
RZ[^R_
!c !HSM !]bcf]g !rp   rG   )rk   shutilr   typingr   r   r   
tokenizersr   tokenization_utilsr	   r
   tokenization_utils_fastr   utilsr   r   tokenization_mbart50r   
get_loggerr   r   r   r[   rG    rp   rh   <module>r      sh     
  ( ( ! ; > 8 6 
		H	%#<P`a  n Z!2 Z!rp   