o
    ॵ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model
model_typezparse config file z	 failed: F)r   	from_filehasattr	Exceptionloggererror)cfg_filecfge r   M/home/ubuntu/.local/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   	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   
r1   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   CONFIGr   r   r   )r   r   hf_cfg_filer   r   r   is_modelhub_path_impl:   s"   



z'is_model.<locals>.is_modelhub_path_implc                    r#   r   r   r$   r4   r   r   r'   R   r(   zis_model.<locals>.<listcomp>z7some models are hub address, some are not, model list: r)   )r   r.   r/   r0   r   r5   r   is_model6   s   
r6   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   OfaForAllTasksc                 S   s   g | ]}|d  d qS )samplesr   r   r%   dr   r   r   r'   d       z!batch_process.<locals>.<listcomp>)
nsentencesr8   	net_inputr=   c                    s   g | ]}|d    qS )r=   r   r9   kr   r   r'   i   r;   w_resize_ratiosc                 S      g | ]}|d  qS )r@   r   r9   r   r   r   r'   l   r(   h_resize_ratiosc                 S   rA   )rB   r   r9   r   r   r   r'   o   r(   )torch	__class____name__r*   listlenkeyscat)r   datarC   
batch_datar   r>   r   batch_process]   s*   rL   )os.pathr   r   typingr   r   r   modelscope.hub.apir   modelscope.hub.file_downloadr   modelscope.utils.configr   modelscope.utils.constantr   r	   modelscope.utils.loggerr
   r   r   r+   r1   r6   rL   r   r   r   r   <module>   s   

'