Ë
    ÜÓ^jÎ7  ã                   óô   — d Z ddlZddlZddlZddlZddlmZ ddlmZm	Z	 ddl
ZddlmZ ddlmZ erddlmZ dd	lmZmZ dd
lmZ  ej,                  e«      ZddiZdZ ed¬«       G d„ de«      «       ZdgZy)z$Tokenization class for SigLIP model.é    N)Úcopyfile)ÚTYPE_CHECKINGÚAnyé   )Ú
AddedToken)ÚSentencePieceBackend)Ú	TextInput)ÚloggingÚrequires_backends)ÚrequiresÚ
vocab_filezspiece.modelu   â–�)Úsentencepiece)Úbackendsc            
       ó°  ‡ — e Zd ZdZeZddgZ	 	 	 	 	 	 	 d"deee	f   dz  ddfˆ fd„Z
ed„ «       Zd	„ Z	 d#d
ee   dee   dz  dedee   fˆ fd„Zdee   dee   f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e   fd„Zd„ Zd„ Zdedefd„Zddœd„Zd%dddee   fˆ fd„Zed„ «       Zd„ Zd„ Zd„ Zd„ Zd$ded edz  dee   fd!„Z ˆ xZ!S )&ÚSiglipTokenizeraè  
    Construct a Siglip tokenizer. Based on [SentencePiece](https://github.com/google/sentencepiece).

    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`):
            [SentencePiece](https://github.com/google/sentencepiece) file (generally has a *.spm* extension) that
            contains the vocabulary necessary to instantiate a tokenizer.
        eos_token (`str`, *optional*, defaults to `"</s>"`):
            The end of sequence token.
        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 `"</s>"`):
            The token used for padding, for example when batching sequences of different lengths.
        additional_special_tokens (`list[str]`, *optional*):
            Additional special tokens used by the tokenizer.
        sp_model_kwargs (`dict`, *optional*):
            Will be passed to the `SentencePieceProcessor.__init__()` method. The [Python wrapper for
            SentencePiece](https://github.com/google/sentencepiece/tree/master/python) can be used, among other things,
            to set:

            - `enable_sampling`: Enable subword regularization.
            - `nbest_size`: Sampling parameters for unigram. Invalid for BPE-Dropout.

              - `nbest_size = {0,1}`: No sampling is performed.
              - `nbest_size > 1`: samples from the nbest_size results.
              - `nbest_size < 0`: assuming that nbest_size is infinite and samples from the all hypothesis (lattice)
                using forward-filtering-and-backward-sampling algorithm.

            - `alpha`: Smoothing parameter for unigram sampling, and dropout probability of merge operations for
              BPE-dropout.
        model_max_length (`int`, *optional*, defaults to 64):
            The maximum length (in number of tokens) for model inputs.
        do_lower_case (`bool`, *optional*, defaults to `True`):
            Whether or not to lowercase the input when tokenizing.
    Ú	input_idsÚattention_maskNÚsp_model_kwargsÚreturnc	                 óR  •— t        | d«       t        |t        «      rt        |dddd¬«      n|}t        |t        «      rt        |dddd¬«      n|}t        |t        «      rt        |dddd¬«      n|}|€i n|| _        || _        t        ‰
| �  d|||||| j                  ||dœ|	¤Ž y )NÚprotobufTF)ÚrstripÚlstripÚ
normalizedÚspecial)r   Ú	eos_tokenÚ	unk_tokenÚ	pad_tokenÚadditional_special_tokensr   Úmodel_max_lengthÚdo_lower_case© )r   Ú
isinstanceÚstrr   r   r!   ÚsuperÚ__init__)Úselfr   r   r   r   r   r   r    r!   ÚkwargsÚ	__class__s             €úy/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/siglip/tokenization_siglip.pyr&   zSiglipTokenizer.__init__X   s×   ø€ ô 	˜$ 
Ô+ô ˜)¤SÔ)ô �y¨°dÀuÐVZÕ[àð 	ô ˜)¤SÔ)ô �y¨°dÀuÐVZÕ[àð 	ô ˜)¤SÔ)ô �y¨°dÀuÐVZÕ[àð 	ð &5Ð%<™rÀ/ˆÔØ*ˆÔä‰Ñð 
	
Ø!ØØØØ&?Ø ×0Ñ0Ø-Ø'ñ
	
ð ó
	
ó    c                 ó6   — | j                   j                  «       S ©N)Úsp_modelÚget_piece_size©r'   s    r*   Ú
vocab_sizezSiglipTokenizer.vocab_size…   s   € à�}‰}×+Ñ+Ó-Ð-r+   c                 óª   — t        | j                  «      D �ci c]  }| j                  |«      |“Œ }}|j                  | j                  «       |S c c}w r-   )Úranger1   Úconvert_ids_to_tokensÚupdateÚadded_tokens_encoder)r'   ÚiÚvocabs      r*   Ú	get_vocabzSiglipTokenizer.get_vocab‰   sK   € Ü;@ÀÇÁÓ;QÖR°a�×+Ñ+¨AÓ.°Ñ1ÐRˆÐRØ�‰�T×.Ñ.Ô/Øˆùò Ss   ˜AÚtoken_ids_0Útoken_ids_1Úalready_has_special_tokensc                 ó¤   •— |rt         ‰| �  ||d¬«      S |€dgt        |«      z  dgz   S dgt        |«      z  dgz   dgt        |«      z  z   dg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%   Úget_special_tokens_maskÚlen)r'   r:   r;   r<   r)   s       €r*   r?   z'SiglipTokenizer.get_special_tokens_maskŽ   sy   ø€ ñ$ &Ü‘7Ñ2Ø'°[Ð]að 3ó ð ð
 ÐØ�Cœ#˜kÓ*Ñ*¨q¨cÑ1Ð1Ø�”c˜+Ó&Ñ&¨1¨#Ñ-°!°´s¸;Ó7GÑ1GÑHÈAÈ3ÑNÐNr+   Ú	token_idsc                 ó¬   — t        |«      dkD  r7|d   | j                  k(  r%t        j                  d| j                  › d�«       |S || j                  gz   S )z.Do not add eos again if user already added it.r   éÿÿÿÿzThis sequence already has zQ. In future versions this behavior may lead to duplicated eos tokens being added.)r@   Úeos_token_idÚwarningsÚwarnr   )r'   rA   s     r*   Ú_add_eos_if_not_presentz'SiglipTokenizer._add_eos_if_not_presentª   s]   € äˆy‹>˜AÒ )¨B¡-°4×3DÑ3DÒ"DÜ�M‰MØ,¨T¯^©^Ð,<ð =+ð +ôð Ðà × 1Ñ 1Ð2Ñ2Ð2r+   c                 ót   — | j                   g}|€t        ||z   «      dgz  S t        ||z   |z   |z   «      dgz  S )aÇ  
        Create a mask from the two sequences passed to be used in a sequence-pair classification task. T5 does not make
        use of token type ids, therefore a list of zeros is returned.

        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 zeros.
        r   )rD   r@   )r'   r:   r;   Úeoss       r*   Ú$create_token_type_ids_from_sequencesz4SiglipTokenizer.create_token_type_ids_from_sequencesµ   sP   € ð  × Ñ Ð!ˆàÐÜ�{ SÑ(Ó)¨Q¨CÑ/Ð/Ü�; Ñ$ {Ñ2°SÑ8Ó9¸Q¸CÑ?Ð?r+   c                 óX   — | j                  |«      }|€|S | 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. A sequence has the following format:

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

        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.
        )rG   )r'   r:   r;   s      r*   Ú build_inputs_with_special_tokensz0SiglipTokenizer.build_inputs_with_special_tokensË   s;   € ð& ×2Ñ2°;Ó?ˆØÐØÐà×6Ñ6°{ÓCˆKØ Ñ,Ð,r+   c                 óD   — | j                   j                  «       }d |d<   |S )Nr.   )Ú__dict__Úcopy)r'   Ústates     r*   Ú__getstate__zSiglipTokenizer.__getstate__å   s#   € Ø—‘×"Ñ"Ó$ˆØ ˆˆjÑØˆr+   c                 óÊ   — || _         t        | d«      si | _        t        j                  di | j                  ¤Ž| _        | j
                  j                  | j                  «       y )Nr   r"   )rN   Úhasattrr   ÚspmÚSentencePieceProcessorr.   ÚLoadr   )r'   Úds     r*   Ú__setstate__zSiglipTokenizer.__setstate__ê   sO   € ØˆŒô �tÐ.Ô/Ø#%ˆDÔ ä×2Ñ2ÑJ°T×5IÑ5IÑJˆŒØ�‰×Ñ˜4Ÿ?™?Õ+r+   Útextc                 ój   — |j                  t        j                  ddt        j                  «      «      S )NÚ )Ú	translater$   Ú	maketransÚstringÚpunctuation)r'   rY   s     r*   Úremove_punctuationz"SiglipTokenizer.remove_punctuationô   s$   € Ø�~‰~œcŸm™m¨B°´F×4FÑ4FÓGÓHÐHr+   ©Úkeep_punctuation_exact_stringc                ó  ‡ — ‰ j                   r|j                  «       }|r*|j                  ˆ fd„|j                  |«      D «       «      }n‰ j	                  |«      }t        j                  dd|«      }|j                  «       }|S )a•  Returns canonicalized `text` (puncuation removed).

        Args:
            text (`str`):
                String to be canonicalized.
            keep_punctuation_exact_string (`str`, *optional*):
                If provided, then this exact string is kept. For example providing '{}' will keep any occurrences of '{}'
                (but will still remove '{' and '}' that appear separately).
        c              3   ó@   •K  — | ]  }‰j                  |«      –— Œ y ­wr-   )r`   )Ú.0Úpartr'   s     €r*   ú	<genexpr>z4SiglipTokenizer.canonicalize_text.<locals>.<genexpr>  s!   øè ø€ ò 6Ø26�×'Ñ'¨×-ñ6ùs   ƒz\s+ú )r!   ÚlowerÚjoinÚsplitr`   ÚreÚsubÚstrip)r'   rY   rb   s   `  r*   Úcanonicalize_textz!SiglipTokenizer.canonicalize_textø   sw   ø€ ð ×ÒØ—:‘:“<ˆDá(Ø0×5Ñ5ó 6Ø:>¿*¹*ÐEbÓ:cô6ó ‰Dð ×*Ñ*¨4Ó0ˆDÜ�v‰v�f˜c 4Ó(ˆØ�z‰z‹|ˆàˆr+   r	   c                 ó¾   •— t        ‰| �  t        |j                  t        d«      z   fi |¤Ž}t	        |«      dkD  r"|d   t        k(  r|d   | j
                  v r|dd }|S )z8
        Converts a string to a list of tokens.
        rh   r>   r   N)r%   ÚtokenizeÚSPIECE_UNDERLINEÚreplacer@   Úall_special_tokens)r'   rY   Úadd_special_tokensr(   Útokensr)   s        €r*   rq   zSiglipTokenizer.tokenize  se   ø€ ô ‘Ñ!Ô"2°T·\±\ÔBRÐTWÓ5XÑ"XÑcÐ\bÑcˆäˆv‹;˜Š?˜v a™yÔ,<Ò<ÀÈÁÈd×NeÑNeÑAeØ˜A˜B�ZˆFØˆr+   c                 óp   — t        | j                  j                  t        | j                  «      «      «      S r-   )r@   r.   Úencoder$   r   r0   s    r*   Úunk_token_lengthz SiglipTokenizer.unk_token_length  s%   € ä�4—=‘=×'Ñ'¬¨D¯N©NÓ(;Ó<Ó=Ð=r+   c                 ó  — | j                  |d¬«      }| j                  j                  |t        ¬«      }| j                  j                  | j                  |z   t        ¬«      }t        |«      | j                  k\  r|| j                  d S |S )u*  
        Returns a tokenized string.

        We de-activated the `add_dummy_prefix` option, thus the sentencepiece internals will always strip any
        SPIECE_UNDERLINE.

        For example: `self.sp_model.encode(f"{SPIECE_UNDERLINE}Hey", out_type = str)` will give `['H', 'e', 'y']` instead of `['â–�He', 'y']`.

        Thus we always encode `f"{unk_token}text"` and strip the `unk_token`. Here is an example with `unk_token = "<unk>"` and `unk_token_length = 4`.
        `self.tokenizer.sp_model.encode("<unk> Hey", out_type = str)[4:]`.
        Nra   )Úout_type)ro   r.   rx   r$   r   r@   ry   )r'   rY   r(   rv   s       r*   Ú	_tokenizezSiglipTokenizer._tokenize  s�   € ð ×%Ñ% dÈ$Ð%ÓOˆØ—‘×%Ñ% d´SÐ%Ó9ˆð —‘×%Ñ% d§n¡n°tÑ&;ÄcÐ%ÓJˆä25°f³+À×AVÑAVÒ2Vˆv�d×+Ñ+Ð-Ð.ÐbÐ\bÐbr+   c                 ó8   — | j                   j                  |«      S )z0Converts a token (str) in an id using the vocab.)r.   Úpiece_to_id)r'   Útokens     r*   Ú_convert_token_to_idz$SiglipTokenizer._convert_token_to_id2  s   € à�}‰}×(Ñ(¨Ó/Ð/r+   c                 ó<   — | j                   j                  |«      }|S )z=Converts an index (integer) in a token (str) using the vocab.)r.   Ú	IdToPiece)r'   Úindexr   s      r*   Ú_convert_id_to_tokenz$SiglipTokenizer._convert_id_to_token6  s   € à—‘×'Ñ'¨Ó.ˆØˆr+   c                 ó  — g }d}d}|D ]P  }|| j                   v r-|s|dz  }|| j                  j                  |«      |z   z  }d}g }Œ>|j                  |«       d}ŒR || j                  j                  |«      z  }|j	                  «       S )z:Converts a sequence of tokens (string) in a single string.r[   Frh   T)rt   r.   ÚdecodeÚappendrn   )r'   rv   Úcurrent_sub_tokensÚ
out_stringÚprev_is_specialr   s         r*   Úconvert_tokens_to_stringz(SiglipTokenizer.convert_tokens_to_string;  s¤   € àÐØˆ
ØˆØò 
	(ˆEà˜×/Ñ/Ñ/Ù&Ø #Ñ%�JØ˜dŸm™m×2Ñ2Ð3EÓFÈÑNÑN�
Ø"&�Ø%'Ñ"à"×)Ñ)¨%Ô0Ø"'‘ð
	(ð 	�d—m‘m×*Ñ*Ð+=Ó>Ñ>ˆ
Ø×ÑÓ!Ð!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                  | j                  «      t         j                  j                  |«      k7  rBt         j                  j                  | j                  «      rt        | j                  |«       |fS t         j                  j                  | j                  «      sCt        |d«      5 }| j                  j                  «       }|j                  |«       d d d «       |fS |fS # 1 sw Y   |fS xY w)NzVocabulary path (z) should be a directoryú-r[   r   Úwb)ÚosÚpathÚisdirÚloggerÚerrorrj   ÚVOCAB_FILES_NAMESÚabspathr   Úisfiler   Úopenr.   Úserialized_model_protoÚwrite)r'   rŒ   r�   Úout_vocab_fileÚfiÚcontent_spiece_models         r*   Úsave_vocabularyzSiglipTokenizer.save_vocabularyN  s%  € Ü�w‰w�}‰}˜^Ô,Ü�L‰LÐ,¨^Ð,<Ð<SÐTÔUØÜŸ™Ÿ™Ø±o˜_¨sÒ2È2ÔQbÐcoÑQpÑpó
ˆô �7‰7�?‰?˜4Ÿ?™?Ó+¬r¯w©w¯©¸~Ó/NÒNÔSU×SZÑSZ×SaÑSaÐbf×bqÑbqÔSrÜ�T—_‘_ nÔ5ð Ð Ð ô —‘—‘ §¡Ô0Ü�n dÓ+ð /¨rØ'+§}¡}×'KÑ'KÓ'MÐ$Ø—‘Ð-Ô.÷/ð Ð Ð �Ð Ð ÷	/ð Ð Ð ús   Ä+,E%Å%E0)ú</s>z<unk>r    NNé@   T)NFr-   )F)"Ú__name__Ú
__module__Ú__qualname__Ú__doc__r–   Úvocab_files_namesÚmodel_input_namesÚdictr$   r   r&   Úpropertyr1   r9   ÚlistÚintÚboolr?   rG   rJ   rL   rQ   rX   r`   ro   rq   ry   r|   r€   r„   r‹   ÚtuplerŸ   Ú__classcell__)r)   s   @r*   r   r   +   sÓ  ø„ ñ&ðP *ÐØ$Ð&6Ð7Ðð
 ØØØ"&Ø15ØØñ+
ð ˜c 3˜h™¨$Ñ.ð+
ð 
õ+
ðZ ñ.ó ð.òð puñOØ ™9ðOØ37¸±9¸tÑ3CðOØhlðOà	ˆc‰õOð8	3°°c±ð 	3¸tÀC¹yó 	3ð GKñ@Ø ™9ð@Ø37¸±9¸tÑ3Cð@à	ˆc‰ó@ð. GKñ-Ø ™9ð-Ø37¸±9¸tÑ3Cð-à	ˆc‰ó-ò4ò
,ðI sð I¨só Ið HLô ñ0˜[ð ÐQUÐVYÑQZõ ð ñ>ó ð>òcò(0òò
"ñ&!¨cð !ÀCÈ$ÁJð !ÐZ_Ð`cÑZd÷ !r+   r   )r¥   r‘   rl   r^   rE   Úshutilr   Útypingr   r   r   rT   Útokenization_utils_baser   Ú tokenization_utils_sentencepiecer   r	   Úutilsr
   r   Úutils.import_utilsr   Ú
get_loggerr¢   r”   r–   rr   r   Ú__all__r"   r+   r*   ú<module>r·      sŠ   ðñ +ã 	Û 	Û Û Ý ß %ã å 1Ý Dñ Ý4ß /Ý *ð 
ˆ×	Ñ	˜HÓ	%€à! >Ð2Ð ð Ð ñ 
Ð%Ô&ôq!Ð*ó q!ó 'ðq!ðh	 Ð
�r+   