
    Li              
       8   d dl Z d dlZd dlZd dlZd dlZd dlmZmZ d dlZd dl	Z	d dl
Zd dl
mZ ddddddZdddddddddd	Z e j                  d       G d	 d
             Z edd       edd      dZdedefdZdededefdZdeddfdZdededee   fdZdedededdfdZdededefdZdededefd Zd!edefd"Z ej:                         d!edej<                  fd#       Zd$edefd%Z  ej:                         d!edejB                  fd&       Z"y)'    N)Optionalcast)sentencepiece_model_pb2gemma3)zgemini-2.5-prozgemini-2.5-flashzgemini-2.5-flash-litezgemini-2.0-flashzgemini-2.0-flash-lite)	zgemini-2.5-pro-preview-06-05zgemini-2.5-pro-preview-05-06zgemini-2.5-pro-exp-03-25zgemini-live-2.5-flashzgemini-2.5-flash-preview-05-20zgemini-2.5-flash-preview-04-17z#gemini-2.5-flash-lite-preview-06-17zgemini-2.0-flash-001zgemini-2.0-flash-lite-001T)frozenc                   "    e Zd ZU eed<   eed<   y)_TokenizerConfig	model_url
model_hashN)__name__
__module____qualname__str__annotations__     b/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/google/genai/_local_tokenizer_loader.pyr	   r	   2   s    ./r   r	   zyhttps://raw.githubusercontent.com/google/gemma_pytorch/33b652c465537c6158f9a472ea5700e5e770ad3f/tokenizer/tokenizer.model@61a7b147390c64585d6c3543dd6fc636906c9af3865a5548f27f31aee1d4c8e2)r
   r   zhttps://raw.githubusercontent.com/google/gemma_pytorch/014acb7ac4563a5f77c76d7ff98f31b568c16508/tokenizer/gemma3_cleaned_262144_v2.spiece.model@1299c11d7cf632ef3b4e11937501358ada021bbdf7c47638d13c0ee982f2e79c)gemma2r   file_url_pathreturnc                     t        j                  |       }|j                          t        t        |j
                        S )z.Loads file bytes from the given file url path.)requestsgetraise_for_statusr   bytescontent)r   resps     r   
_load_filer    I   s-    	m	$$	eT\\	""r   
model_dataexpected_hashc                 h    |st        d      t        j                  |       j                         |k(  S )z:Returns true if the content is valid by checking the hash.zexpected_hash is required)
ValueErrorhashlibsha256	hexdigestr!   r"   s     r   _is_valid_modelr)   P   s.    	
0
11	
	#	-	-	/=	@@r   	file_pathc                     t         j                  j                  |       sy	 t        j                  |        y# t        $ r Y yw xY w)zRemoves the file if exists.N)ospathexistsremoveOSError)r*   s    r   _maybe_remove_filer1   W   s8    			"
	IIi	 		s   8 	AAc                     t         j                  j                  |       syt        | d      5 }|j	                         }ddd       t        |      r|S t        |        y# 1 sw Y   $xY w)z&Loads the content from the cache path.Nrbr(   )r,   r-   r.   openreadr)   r1   )r*   r"   fr   s       r   _maybe_load_from_cacher7   b   s_     
		"It ffhG}EN Y	 s   A!!A*	cache_dir
cache_pathr   c                 .   	 t        j                  | d       | dz   t        t        j                               z   dz   }t        |d      5 }|j                  |       ddd       t        j                  ||       y# 1 sw Y    xY w# t        $ r Y yw xY w)z$Saves the content to the cache path.T)exist_ok.z.tmpwbN)	r,   makedirsr   uuiduuid4r4   writerenamer0   )r8   r9   r   tmp_pathr6   s        r   _maybe_save_to_cacherD   r   s    	KK	D)3TZZ\!22V;H	h	 gggIIh
#  
 		s*   A	B A<B <BB 	BBfile_urlc                     t        |       }t        ||      s5t        j                  |      j	                         }t        d| d| d      |S )z*Loads model bytes from the given file url.r(   z2Downloaded model file is corrupted. Expected hash z. Got file hash r<   )r    r)   r%   r&   r'   r$   )rE   r"   r   actual_hashs       r   _load_from_urlrH      sZ    x '	G=	I..)335K
	'(8Q	H  
.r   c                 `   t         j                  j                  t        j                         d      }t        j                  | j                               j                         }t         j                  j                  ||      }t        ||      }|st        | |      }t        |||       |S )aY  Loads model bytes from the given file url.

  1. If the find local cached file for the given url and the cached file hash
     matches the expected hash, the cached file is returned.
  2. If local cached file is not found or the hash does not match, the file is
     downloaded from the given url. And write to local cache and return the
     file bytes.
  3. If the file downloaded from the given url does not match the expected
     hash, raise ValueError.

  Args:
      file_url: The url of the file to load.
      expected_hash: The expected hash of the file.

  Returns:
      The file bytes.
  vertexai_tokenizer_model)r*   r"   rE   r"   )r8   r9   r   )r,   r-   jointempfile
gettempdirr%   sha1encoder'   r7   rH   rD   )rE   r"   	model_dirfilename
model_pathr!   s         r   _loadrT      s    $ ggll8..02LM)\\(//+,668(ww||Ix0*%-* 
OJj* 
r   tokenizer_namec           	          | t         vr,t        d|  dt        t         j                                      t	        t         |    j
                  t         |    j                        S )z6Loads model proto bytes from the given tokenizer name.z
Tokenizer z( is not supported.Supported tokenizers: rK   )_TOKENIZERSr$   listkeysrT   r
   r   )rU   s    r   _load_model_proto_bytesrZ      sh    ;&

^$ %!!%k&6&6&8!9 :	<  
>*44/::
 r   c                 b    t        j                         }|j                  t        |              |S )z0Loads model proto from the given tokenizer name.)r   
ModelProtoParseFromStringrZ   )rU   model_protos     r   load_model_protor_      s,    
 (224+5nEF	r   
model_namec                 ,   | t         j                         v r	t         |    S | t        j                         v r	t        |    S t        d|  ddj	                  t         j                                ddj	                  t        j                                d      )z1Gets the tokenizer name for the given model name.zModel z% is not supported. Supported models: z, z.
)!_GEMINI_MODELS_TO_TOKENIZER_NAMESrY   (_GEMINI_STABLE_MODELS_TO_TOKENIZER_NAMESr$   rL   )r`   s    r   get_tokenizer_namerd      s    499;;,Z88;@@BB3J??zl?		JkJpJpJr@s?ttvw{  xA  xA  Bj  Bo  Bo  Bq  xr  ws  sv  w	 r   c                 b    t        j                         }|j                  t        |              |S )z<Loads sentencepiece tokenizer from the given tokenizer name.)spmSentencePieceProcessorLoadFromSerializedProtorZ   )rU   	processors     r   get_sentencepiecerj      s,     ((*)##$;N$KL	r   )#dataclasses	functoolsr%   r,   rM   typingr   r   r?   r   sentencepiecerf   r   rb   rc   	dataclassr	   rW   r   r   r    boolr)   r1   r7   rD   rH   rT   rZ   	lru_cacher\   r_   rd   rg   rj   r   r   r   <module>rr      s       	  !    1
  % %% ! %-$, (%&.&.+3$!)
, ( d#  $  NN  dN #c #e #A5 A A A	# 	$ 	&)e_ 		#&	16				 	C 	E 	s 3 5 D
C 
E 
 '' 3 3  c c.H.H  r   