o
    *j                  
   @   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fded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licensec                 C   sH   |  |rtd| d dS | j||||d td| d dS )Nzmodel z already exists, skip creation.F)r   r   r   r   z successfully created.T)Zrepo_existsloggerinfoZcreate_model)apir   r   r   r    r   U/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/utils/hub.pycreate_model_if_not_exist   s   
r   model_id_or_pathrevisionc                 C   s\   t j| st| tj|d}nt j| rt j| tj}nt j| r'| }ndS 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   N)ospathexistsr   r   CONFIGURATIONisdirjoinisfiler
   	from_file)r   r   Z
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>D   s    zauto_load.<locals>.<listcomp>)
isinstancestrr&   r   r	   )r%   r   r   r   	auto_load?   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%   r.   r0   r/   	Exceptionr   error)	model_dirZconfiguration_fileconfig_filecfger   r   r   get_model_typeK   s,   






r8   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<      r=   c                 S   r@   r   r   rA   r   r   r   r<      r=   r-   c                 S   r@   r   r   rA   r   r   r   r<      r=   c                 S   s   i | ]	\}}|t |qS r   )int)r'   rC   rB   r   r   r   r<      s    )jsonr   r   r!   r   ZLABEL_MAPPINGr   openloaditemsr   r
   r#   r1   r   r%   r>   r?   Zpreprocessor)
r4   rE   r   r>   Z
label_pathfZlabel_mappingZconfig_pathconfigr?   r   r   r   parse_label_mappingj   sR   







rK   )"r   Zos.pathr   r&   typingr   r   r   requestsr   Zmodelscope.hub.constantsr   r   Zmodelscope.hub.file_downloadr   Z modelscope.hub.snapshot_downloadr	   Zmodelscope.utils.configr
   Zmodelscope.utils.constantr   r   r   r   r   ZPUBLICZ	APACHE_V2r+   rD   r   r$   r,   r8   rK   r   r   r   r   <module>   s:   

