o
    )j                     @   s|   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dZdd	 ZG d
d deZdS )    N)ArgumentParser)model_file_download)
CLICommand)HubApi)
get_logger)Z	log_levelc                 C   s   t | S )z> Function which will be called for a specific sub parser.
    )LlamafileCMD)args r	   Y/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/cli/llamafile.pysubparser_func   s   r   c                   @   sB   e Zd ZdZdd ZedefddZdd Zd	d
 Z	dd Z
dS )r   Z	llamafilec                 C   sv   || _ | j j| _| jd u s| jddkrtd| j d| j jd ur5d | j _| j j ds5t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   r	   r	   r
   __init__   s   
zLlamafileCMD.__init__parsersc                 C   s   |  tj}|jdtddd | }|jdtdddd	 |jd
tddd |jdtddd |jdtdddd	 |jtd 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
--accuracyFZ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groupr	   r	   r
   define_args$   sF   		zLlamafileCMD.define_argsc           	      C   s  | j jrd | j _| jj| jdd}g }|D ]}|d }|r/| dr/d| vr/|| q|s;t	d| j dt
d| j d	| d |d
 }d}|D ]&}| j jrb|| j jkrb|}d} n| j jrv| j j | v rv|}d} nqP|rtd| d ntd| d tjt| j j|| j jd}tjdr| |}| j j dkrt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   Zget_model_filesr   r   r   appendr   loggerinfoprintospathabspathr   r   r/   sysplatform
startswith_rename_extensionZlaunch_execute_llamafile)	r   	all_filesZ
llamafilesr4   	file_pathZselected_filefoundfZdownloaded_filer	   r	   r
   executeU   sb   



zLlamafileCMD.executec                 C   sv   t |j}|dB }t || |}d}zdd l}|j }W n	 ty(   Y nw |r4td | d}t 	| d S )NI   Fr   z;GPU detected, launching model with llamafile GPU option >>>z	 -ngl 999)
r6   statst_modechmodtorchcudaZis_availableModuleNotFoundErrorr5   system)r   r?   Zcurrent_modeZnew_modeZexecute_cmdZhas_gpurG   r	   r	   r
   r=      s"   
zLlamafileCMD._execute_llamafilec                 C   sD   t j|\}}t j|\}}t j|| d}t || |S )Nz.exe)r6   r7   splitsplitextjoinrename)r   Zoriginal_file_name	directoryfilename	base_name_Znew_filenamer	   r	   r
   r<      s
   zLlamafileCMD._rename_extensionN)__name__
__module____qualname__r#   r   staticmethodr   r*   rB   r=   r<   r	   r	   r	   r
   r      s    01r   )loggingr6   r9   argparser   Z
modelscoper   Zmodelscope.cli.baser   Zmodelscope.hub.apir   Zmodelscope.utils.loggerr   WARNINGr3   r   r   r	   r	   r	   r
   <module>   s   