o
    پi                  	   @   s   d dl Z 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 d dlm	Z	 d dl
Z
d dlmZ eeZe Zddede	e fddZ		dded
edB dedefddZdedd	fddZdefddZdd ZedddefddZdS )    N)	lru_cache)Optional)envsmodel_name_or_path	cache_dirc                 C   sd   |pt }tjtj|dd | dd}t| 	 }|| d }t
jtj||dd}|S )NT)exist_ok/-z.locki  )mode)temp_dirosmakedirspathdirnamereplacehashlibsha256encode	hexdigestfilelockFileLockjoin)r   r   lock_dir
model_name	hash_namelock_file_namelock r   D/home/ubuntu/.local/lib/python3.10/site-packages/sglang/cli/utils.py	_get_lock   s   r   T	local_dirdownloadreturnc                 C   sF  ddl m} tj| rtd | S |s| S t| ~ tj	
 r#dnd}z"td||  || d|d}td	| tj|W W  d
   S  ty_ } ztd| W Y d
}~nd
}~ww z"td||  || d|d}td	| tj|W W  d
   S  ty } z
td| ||f |d
}~ww 1 sw   Y  d
S )a  
    Resolve a model path. If it's a local directory, return it.
    If it's a Hugging Face Hub ID, download only the config file
    (`model_index.json` or `config.json`) and return its directory.

    Args:
        model_name_or_path: Local path or Hugging Face Hub model ID
        local_dir: Local directory to save the downloaded file (if any)
        download: Whether to download from Hugging Face Hub when needed

    Returns:
        Local directory path that contains the downloaded config file, or the original local directory.
    r   )hf_hub_downloadzModel already exists locallyzMS HubzHF Hubz.Downloading model_index.json from %s for %s...model_index.json)repo_idfilenamer    zDownloaded to %sNz(model_index.json not found or failed: %sz)Downloading config.json from %s for %s...zconfig.jsonzbCould not find model locally at %s and failed to download model_index.json/config.json from %s: %s)6sglang.multimodal_gen.runtime.utils.hf_diffusers_utilsr#   r   r   existsloggerinfor   r   SGLANG_USE_MODELSCOPEgetr   	Exceptiondebug
ValueError)r   r    r!   r#   
source_hub	file_pathe_indexe_configr   r   r   _maybe_download_model   sf   

!r4   
model_pathc                 C   s`   t j| d}t j|sdS t|}t|}W d   n1 s#w   Y  d|vr.dS dS )a&  
    Verify if the model directory contains a valid diffusers configuration.

    Args:
        model_path: Path to the model directory

    Returns:
        The loaded model configuration as a dictionary if the model is a diffusers model
        None if the model is not a diffusers model
    r$   FN_diffusers_versionT)r   r   r   r(   openjsonload)r5   config_pathfconfigr   r   r   is_diffusers_model_pathd   s   
r=   c                 C   s"   t | } t| }|rtd |S )NzDiffusion model detected)r4   r=   r)   r*   )r5   is_diffusion_modelr   r   r   get_is_diffusion_model   s
   
r?   c                    s   d }t  D ](\}}|dkr|d t k r |d  } nq|dr.|ddd } nq|d u rFt fdddD rBtdtd	|S )
Nz--model-path   z--model-path==c                 3   s    | ]}| v V  qd S Nr   ).0h
extra_argvr   r   	<genexpr>   s    z!get_model_path.<locals>.<genexpr>)z-hz--helpa  Usage: sglang serve --model-path <model-name-or-path> [additional-arguments]

This command can launch either a standard language model server or a diffusion model server.
The server type is determined by the model path.
For specific arguments, please provide a model_path.zFError: --model-path is required. Please provide the path to the model.)	enumeratelen
startswithsplitanyr-   )rF   r5   iargr   rE   r   get_model_path   s(   
rO   r@   )maxsizec               	   C   sX   zt jd} | stjg dtjd d} | }| W S  tjt	fy+   d}Y dS w )NSGLANG_GIT_COMMIT)gitz	rev-parseHEAD)stderrzutf-8zN/A)
r   environr,   
subprocesscheck_outputDEVNULLstripdecodeCalledProcessErrorFileNotFoundError)commit_hash_CACHED_COMMIT_HASHr   r   r   get_git_commit_hash   s   
r_   rB   )NT)r   r8   loggingr   rV   tempfile	functoolsr   typingr   r   sglang.srt.environr   	getLogger__name__r)   
gettempdirr   strr   boolr4   r=   r?   rO   r_   r   r   r   r   <module>   s8    

F