
    9i                         S SK r S SKrS SKrS SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  \" \ R                  S9rS r " S	 S
\5      rg)    N)ArgumentParser)model_file_download)
CLICommand)HubApi)
get_logger)	log_levelc                     [        U 5      $ )z=Function which will be called for a specific sub parser.
    )LlamafileCMD)argss    X/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/cli/llamafile.pysubparser_funcr      s         c                   H    \ rS rSrSrS r\S\4S j5       rS r	S r
S rS	rg
)r
      	llamafilec                    Xl         U R                   R                  U l        U R                  b  U R                  R                  S5      S:w  a  [	        SU R                   S35      eU R                   R
                  bT  S U R                   l        U R                   R
                  R                  5       R                  S5      (       d  [	        S5      e[        5       U l
        g )N/   zInvalid model id [].
.llamafilez*file argument must ends with ".llamafile".)r   modelmodel_idcount
ValueErrorfileaccuracylowerendswithr   api)selfr   s     r   __init__LlamafileCMD.__init__   s    			== DMM$7$7$<$A1$--CDD99>>%!%DII99>>'')22<@@ !MNN8r   parsersc                 h   U R                  [        R                  5      nUR                  S[        SSS9  UR                  5       nUR                  S[        SSSS	9  UR                  S
[        SSS9  UR                  S[        SSS9  UR                  S[        SSSS	9  UR                  [        S9  g)z-define args for clear-cache command.
        z--modelTzCThe id of the model, whose repo must contain at least one llamafile)typerequiredhelpz
--accuracyFq4_k_mzRSelected accuracy of GGUF files in the repo. Ignored when "file" is also provided.)r%   r&   defaultr'   z--filez[The name of a specified llamafile in the model repo. This takes precedence over "accuracy".z--local_dirNzCDirectory where the selected llamafile would will be downloaded to.)r%   r)   r'   z--launchTruezGWhether to launch model with the downloaded llamafile, default to True.)func)
add_parserr
   nameadd_argumentstradd_mutually_exclusive_groupset_defaultsr   )r#   parsergroups      r   define_argsLlamafileCMD.define_args$   s     ##L$5$56Q 	 	
 335` 	 	
 	i 	 	
 	Q 	 	
 	U 	 	
 	0r   c                 F   U R                   R                  (       a  S U R                   l        U R                  R	                  U R
                  SS9n/ nU H^  nUS   nU(       d  M  UR                  5       R                  S5      (       d  M7  SUR                  5       ;  d  MM  UR                  U5        M`     U(       d  [        SU R
                   S35      e[        R                  SU R
                   S	U S35        US
   nSnU H  nU R                   R                  (       a  XpR                   R                  :X  a  UnSn  O]U R                   R                  (       d  MZ  U R                   R                  R                  5       UR                  5       ;   d  M  UnSn  O   U(       a  [        SU S35        O[        SU S35        [        R                  R                  [!        U R                   R"                  XPR                   R$                  S95      n[&        R(                  R+                  S5      (       a  U R-                  U5      nU R                   R.                  R                  5       S:X  a!  [        SU S35        U R1                  U5        g [        SU S35        g )NT)	recursivePathr   z-of-z(Cannot locate a valid llamafile in repo .zlist of llamafiles in repo z:
r   Fz$llamafile matching criteria found: [r   zKNo matched llamafile found in repo, choosing the first llamafile in repo: [])	local_dirwintruez Launching model with llamafile [z]:z-No Launching. Llamafile model downloaded to [z!], you may execute it separately.)r   r   r   r   get_model_filesr   r   r   appendr   loggerinfoprintospathabspathr   r   r;   sysplatform
startswith_rename_extensionlaunch_execute_llamafile)	r    	all_files
llamafilesrA   	file_pathselected_filefoundfdownloaded_files	            r   executeLlamafileCMD.executeU   s+   99>>!%DIIHH,,T]]d,K	
DVIyY__.77 " "&,IOO4E&E!!),	 
 :4==/KM M)$--J<qI	K #1Ayy~~!yy~~"5 !yy!!!dii&8&8&>&>&@AGGI&M !  8rJK]^k]llmn ''//		)):M:MOP <<""5))"44_EO99!!#v-4_4ERHI##O4??PPqrr   c                 :   [         R                  " U5      R                  nUS-  n[         R                  " X5        UnSn SS KnUR
                  R                  5       nU(       a  [        S5        U S3n[         R                  " U5        g ! [         a     N:f = f)NI   Fr   z;GPU detected, launching model with llamafile GPU option >>>z	 -ngl 999)
rC   statst_modechmodtorchcudais_availableModuleNotFoundErrorrB   system)r    rN   current_modenew_modeexecute_cmdhas_gpurZ   s          r   rK   LlamafileCMD._execute_llamafile   s    wwy)11%'
%	jj--/G
 MO(M3K
		+ # 	 	s   B 
BBc                     [         R                  R                  U5      u  p#[         R                  R                  U5      u  pE[         R                  R	                  X$ S35      n[         R
                  " X5        U$ )Nz.exe)rC   rD   splitsplitextjoinrename)r    original_file_name	directoryfilename	base_name_new_filenames          r   rI   LlamafileCMD._rename_extension   sZ     ggmm,>?	ww''1	ww||ID/AB
		$3r   )r   r   r   N)__name__
__module____qualname____firstlineno__r-   r!   staticmethodr   r4   rS   rK   rI   __static_attributes__ r   r   r
   r
      s8    D
 .1^ .1 .1`/b&r   r
   )loggingrC   rF   argparser   
modelscoper   modelscope.cli.baser   modelscope.hub.apir   modelscope.utils.loggerr   WARNINGr@   r   r
   rv   r   r   <module>r~      s>     	 
 # * * % .	goo	.I: Ir   