
    6pj                         S r 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	 S
\5      rg)uL   ``modelscope llamafile`` — download and run a llamafile from a model repo.    N)ArgumentParser)
CLICommand)model_file_download)HubApi)
get_logger)	log_levelc                   Z   ^  \ rS rSrSrU 4S jr\S\SS4S j5       rS r	S	 r
S
 rSrU =r$ )LlamafileCMD   	llamafilec                   > [         TU ]  U5        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".)super__init__argsmodelmodel_idcount
ValueErrorfileaccuracylowerendswithr   api)selfr   	__class__s     T/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/modelscope/cli/llamafile.pyr   LlamafileCMD.__init__   s    		== DMM$7$7$<$A1$--CDD99>>%!%DII99>>'')22<@@ !MNN8    
subparsersreturnNc                 f   U R                  [        R                  SS9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 )Nz/Download and run a llamafile from a model repo.)helpz--modelTzCThe id of the model, whose repo must contain at least one llamafile)typerequiredr&   z
--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_dirzCDirectory 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.)_command)
add_parserr
   nameadd_argumentstradd_mutually_exclusive_groupset_defaults)r#   parsergroups      r    registerLlamafileCMD.register!   s    &&B ' D 	Q 	 	
 335` 	 	
 	i 	 	
 	Q 	 	
 	U 	 	
 	\2r"   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
llamafilesrB   	file_pathselected_filefoundfdownloaded_files	            r    executeLlamafileCMD.executeR   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)
rD   statst_modechmodtorchcudais_availableModuleNotFoundErrorrC   system)r   rO   current_modenew_modeexecute_cmdhas_gpur[   s          r    rL   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)rD   rE   splitsplitextjoinrename)r   original_file_name	directoryfilename	base_name_new_filenames          r    rJ   LlamafileCMD._rename_extension   sZ     ggmm,>?	ww''1	ww||ID/AB
		$3r"   )r   r   )__name__
__module____qualname____firstlineno__r.   r   staticmethodr   r5   rT   rL   rJ   __static_attributes____classcell__)r   s   @r    r
   r
      sE    D
 .3^ .3 .3 .3`/b& r"   r
   )__doc__loggingrD   rG   argparser   modelscope_hub.cli.baser   
modelscoper   modelscope.hub.apir   modelscope.utils.loggerr   WARNINGrA   r
    r"   r    <module>r      s<    R  	 
 # . * % .	goo	.I: Ir"   