
    vj              
       @   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Zefdedee         fdZdeeee         f         fdZd Z 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                     |                      |          r t                              d| d           dS |                     ||||           t                              d| d           dS )Nzmodel z already exists, skip creation.F)r   r   r   r   z successfully created.T)repo_existsloggerinfocreate_model)apir   r   r   r   s        d/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/modelscope/utils/hub.pycreate_model_if_not_existr      s     x   FXFFFGGGu!%	 	 	
 	
 	
 	=X===>>>t    model_id_or_pathrevisionc                 ~   t           j                            |           st          | t          j        |          }nnt           j                            |           r+t           j                            | t          j                  }n$t           j                            |           r| }ndS t          j
        |          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   
local_paths      r   read_configr*   (   s     7>>*++ (i5J J J

	'	(	( W\\"2I4KLL

	(	)	) %

tJ'''r   modelc                     t          | t                    r$t          j        |           st	          |           } nd | D             } | S )Nc                 X    g | ]'}t          j        |          st          |          n|(S  )ospr#   r
   ).0ms     r   
<listcomp>zauto_load.<locals>.<listcomp>D   s@     
 
 
AB
1<a   1
 
 
r   )
isinstancestrr/   r#   r
   )r+   s    r   	auto_loadr5   ?   sY    % 
z%   	-%e,,E
 
FK
 
 
 Lr   c                 |   	 t          j        | t          j                  }t          j        | d          }t          j        |          r|t          j        |          }t          |j        d          r|j        j	        j
        S t          |j        d          r!t          |j        d          s|j        j        S |j        j
        S t          j        |          r-t          j        |          }t          |d          r|j        ndS dS # t          $ r(}t                              d|            Y d}~dS d}~ww xY 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+   r8   r:   r9   	Exceptionr   error)	model_dirconfiguration_fileconfig_filecfges        r   get_model_typerC   K   sS   B Xi1HIIhy-88:()) 	J"#566Csy*-- &y)..%' ' &/6sy&/I/I&y++y~%Z$$ 	J";//C%,S,%?%?I3>>TI	J 	J  B B B@Q@@AAAAAAAAABs+   BD	 5D	 :D	 A D	 	
D;D66D;c                 @   ddl }ddl}d}|j                            | t          j                  }|j                            |          r\t          |d          5 }|                    |          }ddd           n# 1 swxY w Y   d |	                                D             }||j                            | t          j
                  }t          j        |          }t          |t          j                  r9t          |t          j                 d          r|t          j                 j        }n1t          |t          j                  rVt          |t          j                 d          r6|t          j                 j        }	d |		                                D             }nt          |t          j                  r8t          |t          j                 d          r|t          j                 j        }not          |t          j                  rUt          |t          j                 d          r5|t          j                 j        }	d	 |		                                D             }|j                            | d
          }|{|j                            |          rat          j        |          }t          |d          r|j        }n5t          |d          r%|j        }	d |		                                D             }|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                     i | ]\  }}||	S r.   r.   )r0   nameidxs      r   
<dictcomp>z'parse_label_mapping.<locals>.<dictcomp>   s    EEE)$D#EEEr   label2idid2labelc                     i | ]\  }}||	S r.   r.   r0   idlabels      r   rI   z'parse_label_mapping.<locals>.<dictcomp>       DDDib%rDDDr   c                     i | ]\  }}||	S r.   r.   rM   s      r   rI   z'parse_label_mapping.<locals>.<dictcomp>   rP   r   r7   c                     i | ]\  }}||	S r.   r.   rM   s      r   rI   z'parse_label_mapping.<locals>.<dictcomp>   rP   r   c                 4    i | ]\  }}|t          |          S r.   )int)r0   rO   rN   s      r   rI   z'parse_label_mapping.<locals>.<dictcomp>   s$    EEEyubE3r77EEEr   )jsonr!   r"   r&   r   LABEL_MAPPINGr#   openloaditemsr$   r   r(   r;   r   r+   rJ   rK   preprocessor)
r>   rU   r!   rJ   
label_pathflabel_mappingconfig_pathconfigrK   s
             r   parse_label_mappingr`   j   s    KKKIIIHi)@AAJ	w~~j!! F*w/// 	)1 IIaLLM	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)EE}/B/B/D/DEEEgll9i.EFF!+..6<-.. 	E7|)*J48 48 	El01:HHV\/00 
	EW|)*J68 68 
	El01:HDD8>>3C3CDDDHHV\677 	EG|01:=? =? 	El78AHHV\677 	EG|01:=? =? 	El78AHDD8>>3C3CDDDH',,y-88KBGNN;77!+..6:&& 	EHHVZ(( 	EHDD8>>3C3CDDDHEEHNN4D4DEEEOs   A==BB)"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   r   r   PUBLIC	APACHE_V2r4   rT   r   r*   r5   rC   r`   r.   r   r   <module>rk      s   
			       ( ( ( ( ( ( ( ( ( (       > > > > > > > > < < < < < < > > > > > > * * * * * *2 2 2 2 2 2 2 2 2 2      	 %4$:!)!3   SM	
 #   * +A( (# ("3-( ( ( (.	U3S	>* 	 	 	 	B B B>3 3 3 3 3r   