
    uj                         d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ  ee j                  Zd Z G d	 d
e          ZdS )    N)ArgumentParser)model_file_download)
CLICommand)HubApi)
get_logger)	log_levelc                      t          |           S )z> Function which will be called for a specific sub parser.
    )LlamafileCMD)argss    h/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/modelscope/cli/llamafile.pysubparser_funcr      s         c                   F    e Zd ZdZd Zedefd            Zd Zd Z	d Z
dS )	r
   	llamafilec                    || _         | j         j        | _        | j        | j                            d          dk    rt	          d| j         d          | j         j        Ld | j         _        | j         j                                                            d          st	          d          t                      | _
        d S )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__zLlamafileCMD.__init__   s    		= DM$7$7$<$<$A$AC$-CCCDDD9>%!%DI9>''))22<@@ O !MNNN88r   parsersc                    |                      t          j                  }|                    dt          dd           |                                }|                    dt          ddd	           |                    d
t          dd           |                    dt          dd           |                    dt          ddd	           |                    t                     dS )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_argszLlamafileCMD.define_args$   s%    ##L$566Q 	 	
 	
 	
 3355` 	 	
 	
 	
 	i 	 	
 	
 	
 	Q 	 	
 	
 	
 	U 	 	
 	
 	
 	00000r   c                    | j         j        rd | j         _        | j                            | j        d          }g }|D ]^}|d         }|rR|                                                    d          r+d|                                vr|                    |           _|st          d| j         d          t                              d| j         d	| d           |d
         }d}|D ]h}| j         j        r|| j         j        k    r|}d} nE| j         j        r8| j         j                                        |                                v r|}d} ni|rt          d| d           nt          d| d           t          j                            t!          | j         j        || j         j                            }t&          j                            d          r|                     |          }| j         j                                        dk    r*t          d| d           |                     |           d S t          d| d           d S )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   r8   sysplatform
startswith_rename_extensionlaunch_execute_llamafile)	r   	all_files
llamafilesr>   	file_pathselected_filefoundfdownloaded_files	            r   executezLlamafileCMD.executeU   s   9> 	&!%DIH,,T]d,KK	
 	- 	-DVI -Y__..77 " " -&,IOO4E4E&E&E!!),,, 	MK4=KKKM M MI$-IIJIII	K 	K 	K #1 	 	Ay~ !ty~"5"5 !y! di&8&>&>&@&@AGGII&M&M ! 	JJJJKKKKn^knnn   '//	$):MO O OP P <""5)) 	F"44_EEO9!!##v--H_HHHIII##O44444rrrr    r   c                 &   t          j        |          j        }|dz  }t          j        ||           |}d}	 dd l}|j                                        }n# t          $ r Y nw xY w|rt          d           | d}t          j	        |           d S )NI   Fr   z;GPU detected, launching model with llamafile GPU option >>>z	 -ngl 999)
r@   statst_modechmodtorchcudais_availableModuleNotFoundErrorr?   system)r   rK   current_modenew_modeexecute_cmdhas_gpurV   s          r   rH   zLlamafileCMD._execute_llamafile   s    wy))1%'
H%%%	LLLj--//GG" 	 	 	 D	  	4MO O O(333K
	+s   A 
A$#A$c                     t           j                            |          \  }}t           j                            |          \  }}t           j                            || d          }t          j        ||           |S )Nz.exe)r@   rA   splitsplitextjoinrename)r   original_file_name	directoryfilename	base_name_new_filenames          r   rF   zLlamafileCMD._rename_extension   sk     gmm,>??	8w''11	1w||I)/A/A/ABB
	$l333r   N)__name__
__module____qualname__r+   r    staticmethodr   r2   rP   rH   rF    r   r   r
   r
      s~        D
 
 
 .1^ .1 .1 .1 \.1`/ / /b  &    r   r
   )loggingr@   rC   argparser   
modelscoper   modelscope.cli.baser   modelscope.hub.apir   modelscope.utils.loggerr   WARNINGr=   r   r
   rn   r   r   <module>rv      s     				 



 # # # # # # * * * * * * * * * * * * % % % % % % . . . . . .	go	.	.	.  I I I I I: I I I I Ir   