o
    rqi                     @   s   d dl mZ d dlmZmZm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 e Zdd	 Zefd
eeef dee fddZd
eeef fddZdd ZdS )    N)ListOptionalUnion)HubApi)model_file_download)Config)DEFAULT_MODEL_REVISION	ModelFile)
get_loggerc              
   C   s^   zt | }t|dpt|dW S  ty. } ztd|  d|  W Y d }~dS d }~ww )NmodelZ
model_typezparse config file z	 failed: F)r   	from_filehasattr	Exceptionloggererror)cfg_filecfge r   `/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/modelscope/pipelines/util.pyis_config_has_model   s   
r   pathrevisionc                    sZ   fdd t | tr | S  fdd| D }t|}t|}|r+|s+td|  |S )z_ Whether path is an official hub name or a valid local
    path to official hub directory.
    c              
      s`   t | rt | tj}t |S zt j|  d}W dS  ty/ } ztd| d }~ww )Nr   Tzinvalid model repo path )	ospexistsjoinr	   CONFIGURATIONr   Z	get_modelr   
ValueError)r   r   _r   r   r   r   is_official_hub_impl   s   

z2is_official_hub_path.<locals>.is_official_hub_implc                       g | ]} |qS r   r   .0m)r    r   r   
<listcomp>+       z(is_official_hub_path.<locals>.<listcomp>z6some model are hub address, some are not, model list: 
isinstancestrallanyr   )r   r   resultsall_trueany_truer   )r    r   r   is_official_hub_path   s   
r/   c                    sV   dd  t | tr | S  fdd| D }t|}t|}|r)|s)td|  |S )zG whether path is a valid modelhub path and containing model config
    c                 S   s   t | r't | tj}t | tj}t |rt|S t |r%t|S dS zt| tj}t|r5W dS t| tj}t|W S  tyI   Y dS w )NFT)	r   r   r   r	   r   ZCONFIGr   r   r   )r   r   Zhf_cfg_filer   r   r   is_modelhub_path_impl:   s"   



z'is_model.<locals>.is_modelhub_path_implc                    r!   r   r   r"   r0   r   r   r%   R   r&   zis_model.<locals>.<listcomp>z7some models are hub address, some are not, model list: r'   )r   r,   r-   r.   r   r1   r   is_model6   s   
r2   c                    s   dd l }| jjdkr_t|tsJ t|dd |D i d}|d d  D ] | fdd|D |d  < q&d|d v rK|d	d |D |d< d
|d v r]|dd |D |d
< |S d S )Nr   ZOfaForAllTasksc                 S   s   g | ]}|d  d qS )samplesr   r   r#   dr   r   r   r%   d       z!batch_process.<locals>.<listcomp>)Z
nsentencesr3   	net_inputr7   c                    s   g | ]}|d    qS )r7   r   r4   kr   r   r%   i   r6   w_resize_ratiosc                 S      g | ]}|d  qS )r:   r   r4   r   r   r   r%   l   r&   h_resize_ratiosc                 S   r;   )r<   r   r4   r   r   r   r%   o   r&   )torch	__class____name__r(   listlenkeyscat)r   datar=   Z
batch_datar   r8   r   batch_process]   s*   rE   )Zos.pathr   r   typingr   r   r   Zmodelscope.hub.apir   Zmodelscope.hub.file_downloadr   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r	   Zmodelscope.utils.loggerr
   r   r   r)   r/   r2   rE   r   r   r   r   <module>   s   

'