o
    ॵi3                     @   s  d dl Z d dlmZ d dlmZmZmZ d dlm	Z	 d dl
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mZ d	d
lmZ e Zejejefdededee dee dee f
ddZefdedee fddZdeeee f fddZdd Z dd Z!dS )    N)ListOptionalUnion)	HTTPError)LicensesModelVisibility)model_file_download)snapshot_download)Config)DEFAULT_MODEL_REVISIONConfigFields	ModelFile   )
get_loggermodel_idchinese_name
visibilitylicenserevisionc                 C   sl   d}z	| j ||d W n ty   d}Y nw |r#td| d dS | j||||d td| d dS )NT)r   r   Fzmodel z already exists, skip creation.)r   r   r   r   z successfully created.)	get_modelr   printcreate_model)apir   r   r   r   r   exists r   H/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/utils/hub.pycreate_model_if_not_exist   s"   r   model_id_or_pathc                 C   sV   t j| st| tj|d}nt j| rt j| tj}nt j| r&| }t	
|S )z Read config from hub or local path

    Args:
        model_id_or_path (str): Model repo name or local directory path.
        revision: revision of the model when getting from the hub
    Return:
        config (:obj:`Config`): config object
    )r   )ospathr   r   r   CONFIGURATIONisdirjoinisfiler
   	from_file)r   r   
local_pathr   r   r   read_config.   s   

r&   modelc                 C   s2   t | trt| st| } | S dd | D } | S )Nc                 S   s"   g | ]}t |st|n|qS r   )ospr   r	   ).0mr   r   r   
<listcomp>H   s    zauto_load.<locals>.<listcomp>)
isinstancestrr(   r   r	   )r'   r   r   r   	auto_loadC   s   

r.   c              
   C   s   zRt | tj}t | d}t |r:t|}t|jdr$|jj	j
W S t|jdr5t|jds5|jjW S |jj
W S t |rPt|}t|drM|jW S dW S W dS  tym } ztd|  W Y d}~dS d}~ww )a  Get the model type from the configuration.

    This method will try to get the model type from 'model.backbone.type',
    'model.type' or 'model.model_type' field in the configuration.json file. If
    this file does not exist, the method will try to get the 'model_type' field
    from the config.json.

    Args:
        model_dir: The local model dir to use. @return: The model type
    string, returns None if nothing is found.
    config.jsonbackbone
model_typetypeNz%parse config file failed with error: )r(   r"   r   r    r#   r
   r$   hasattrr'   r0   r2   r1   	Exceptionloggererror)	model_dirconfiguration_fileconfig_filecfger   r   r   get_model_typeO   s,   






r<   c           
      C   s  ddl }ddl}d}|j| tj}|j|r<t|dd}||}W d   n1 s.w   Y  dd |	 D }|du r|j| tj
}t|}t|tjrbt|tj drb|tj j}nPt|tjrt|tj dr|tj j}	d	d |		 D }n2t|tjrt|tj dr|tj j}nt|tjrt|tj dr|tj j}	d
d |		 D }|j| d}|du r|j|rt|}t|dr|j}nt|dr|j}	dd |		 D }|durdd |	 D }|S )ap  Get the label mapping from the model dir.

    This method will do:
    1. Try to read label-id mapping from the label_mapping.json
    2. Try to read label-id mapping from the configuration.json
    3. Try to read label-id mapping from the config.json

    Args:
        model_dir: The local model dir to use.

    Returns:
        The label2id mapping if found.
    r   Nzutf-8)encodingc                 S   s   i | ]\}}||qS r   r   )r)   nameidxr   r   r   
<dictcomp>       z'parse_label_mapping.<locals>.<dictcomp>label2idid2labelc                 S      i | ]\}}||qS r   r   r)   idlabelr   r   r   r@      rA   c                 S   rD   r   r   rE   r   r   r   r@      rA   r/   c                 S   rD   r   r   rE   r   r   r   r@      rA   c                 S   s   i | ]	\}}|t |qS r   )int)r)   rG   rF   r   r   r   r@      s    )jsonr   r   r"   r   LABEL_MAPPINGr   openloaditemsr    r
   r$   r3   r   r'   rB   rC   preprocessor)
r7   rI   r   rB   
label_pathflabel_mappingconfig_pathconfigrC   r   r   r   parse_label_mappingn   sR   







rT   )"r   os.pathr   r(   typingr   r   r   requestsr   modelscope.hub.constantsr   r   modelscope.hub.file_downloadr    modelscope.hub.snapshot_downloadr	   modelscope.utils.configr
   modelscope.utils.constantr   r   r   r5   r   PUBLIC	APACHE_V2r-   rH   r   r&   r.   r<   rT   r   r   r   r   <module>   s@   

