o
    )i
                  
   @   s  d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZ eeZded	efd
dZde	eef d	efddZ		ddedee de	eedf d	ee fddZde	eef d	eeef fddZde	eef d	eeef fddZeded	efddZdS )    N)cache)PathLike)Path)OptionalUnion)VLLM_MODEL_REDIRECT_PATH)init_loggermodel_or_pathreturnc                 C   s   |   dS )Nzs3://)lower
startswith)r	    r   i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/transformers_utils/utils.pyis_s3   s   r   modelc              
   C   s   t | } |  s
dS | jdkrdS z| d}|d}W d   n1 s'w   Y  |dkW S  tyJ } ztd| | W Y d}~dS d}~ww )	z"Check if the file is a GGUF model.Fz.ggufTrb   Ns   GGUFzError reading file %s: %s)r   is_filesuffixopenread	Exceptionloggerdebug)r   fheaderer   r   r   check_gguf_file   s   

r   repo_idrevisiontokenc                 C   s:   ddl m} | }|| dd |j| |ddD }|S )z List files in a modelscope repo.r   )HubApic                 S   s    g | ]}|d  dkr|d qS )TypeZblobr   r   ).0filer   r   r   
<listcomp>0   s
    z.modelscope_list_repo_files.<locals>.<listcomp>T)Zmodel_idr   	recursive)Zmodelscope.hub.apir!   loginZget_model_files)r   r   r    r!   apifilesr   r   r   modelscope_list_repo_files&   s   
r*   pathc              	   C   sl   t | (}zt| W W  d    S  ty+   tttf   Y W  d    S w 1 s/w   Y  d S N)r   jsonloadsr   r   dictstr)r+   r   r   r   r   _maybe_json_dict8   s   
r1   c              
   C   sx   t ttf  }t| '}| D ]}z|  \}}|||< W q ty)   Y qw W d    |S 1 s5w   Y  |S r,   )r/   r0   r   	readlinesstripsplitr   )r+   Zparsed_dictr   lineZ
model_nameZredirect_namer   r   r   _maybe_space_split_dict@   s   

r6   c                 C   sP   t }|s| S t| s| S t|pt|}||  }r&td| | |S | S )z
    Use model_redirect to redirect the model name to a local folder.

    :param model: hf model name
    :return: maybe redirect to a local folder
    z model redirect: [ %s ] -> [ %s ])r   r   existsr1   r6   getr   info)r   Zmodel_redirect_pathredirect_dictZredirect_modelr   r   r   maybe_model_redirectL   s   	r;   )NN)r-   	functoolsr   osr   pathlibr   typingr   r   Z	vllm.envsr   Zvllm.loggerr   __name__r   r0   boolr   r   listr*   r/   r1   r6   r;   r   r   r   r   <module>   s2   
""