
    9i              
       8   S SK r S SKJr  S SKJrJrJr  S SKJ	r	  S SK
JrJr  S SKJr  S SKJr  S SKJr  S SKJrJrJr  S	S
KJr  \" 5       r\R2                  \R4                  4S\S\S\\   S\\   4S jjr\4S\S\\   4S jjrS\\\\   4   4S jrS r S r!g)    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                     U R                  U5      (       a  [        R                  SU S35        gU R                  UUUUS9  [        R                  SU S35        g)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        T/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/utils/hub.pycreate_model_if_not_existr      sf     x  fXJ&EFG!%	 	 	
 	fXJ&<=>    model_id_or_pathrevisionc                    [         R                  R                  U 5      (       d  [        U [        R
                  US9nO{[         R                  R                  U 5      (       a/  [         R                  R                  U [        R
                  5      nO([         R                  R                  U 5      (       a  U nOg[        R                  " U5      $ )zRead 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     77>>*++(i55J
	'	(	(WW\\"2I4K4KL
	(	)	)%
J''r   modelc                     [        U [        5      (       a(  [        R                  " U 5      (       d  [	        U 5      n U $ U  Vs/ s H+  n[        R                  " U5      (       d  [	        U5      OUPM-     n nU $ s  snf )N)
isinstancestrospr#   r
   )r+   ms     r   	auto_loadr1   ?   sr    %zz%  %e,E L GL
FK

1a 1<e 	 
 L	
s   2A8c                     [         R                  " U [        R                  5      n[         R                  " U S5      n[         R                  " U5      (       a  [
        R                  " U5      n[        UR                  S5      (       a   UR                  R                  R                  $ [        UR                  S5      (       a1  [        UR                  S5      (       d  UR                  R                  $ UR                  R                  $ [         R                  " U5      (       a5  [
        R                  " U5      n[        US5      (       a  UR                  $ S$ g! [         a"  n[        R                  SU 35         SnAgSnAff = f)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+   r4   r6   r5   	Exceptionr   error)	model_dirconfiguration_fileconfig_filecfges        r   get_model_typer?   K   s&   B XXi1H1HIhhy-8::())""#56Csyy*--yy))...%' '/6syy&/I/Iyy+++yy~~%ZZ$$"";/C%,S,%?%?3>>ITI %  B<QC@AABs2   B'E *AE 6E AE E 
F	'FF	c                 x   SSK nSSKnSnUR                  R                  U [        R
                  5      nUR                  R                  U5      (       aH  [        USS9 nUR                  U5      nSSS5        WR                  5        VVs0 s H  u  pxXx_M	     nnnUGc  UR                  R                  U [        R                  5      n	[        R                  " U	5      n
[        U
[        R                  5      (       aA  [        U
[        R                     S5      (       a  U
[        R                     R                   nGOd[        U
[        R                  5      (       ad  [        U
[        R                     S5      (       aB  U
[        R                     R"                  nUR                  5        VVs0 s H  u  pX_M	     nnnO[        U
[        R$                  5      (       a@  [        U
[        R$                     S5      (       a  U
[        R$                     R                   nO[        U
[        R$                  5      (       ac  [        U
[        R$                     S5      (       aA  U
[        R$                     R"                  nUR                  5        VVs0 s H  u  pX_M	     nnnUR                  R                  U S5      n	Uc  UR                  R                  U	5      (       au  [        R                  " U	5      n
[        U
S5      (       a  U
R                   nOA[        U
S5      (       a0  U
R"                  nUR                  5        VVs0 s H  u  pX_M	     nnnUb.  UR                  5        VVs0 s H  u  pU['        U5      _M     nnnU$ ! , (       d  f       GN"= fs  snnf s  snnf s  snnf s  snnf s  snnf )aL  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)encodinglabel2idid2labelr3   )jsonr!   r"   r&   r   LABEL_MAPPINGr#   openloaditemsr$   r   r(   r7   r   r+   rB   rC   preprocessorint)r:   rD   r!   rB   
label_pathflabel_mappingnameidxconfig_pathconfigrC   idlabels                 r   parse_label_mappingrT   j   s    Hi)@)@AJ	ww~~j!!*w/1 IIaLM 0/</B/B/DE/D)$DI/DEggll9i.E.EF!!+.6<--..7|))*J48 48l001::HV\//00W|))*J68 68l001::H3;>>3CD3Cib	3CHDHV\6677G|001:=? =?l778AAHV\6677G|001:=? =?l778AAH3;>>3CD3Cib	3CHD'',,y-8KBGGNN;77!!+.6:&&HVZ((H3;>>3CD3Cib	3CHD4<NN4DE4DyuE3r7N4DEOA 0/E E E EEs*   NN2N$N*N0/N6
N)"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_V2r.   rJ   r   r*   r1   r?   rT    r   r   <module>r`      s    
  ( (  > < > *2 2 	 %4$:$:!)!3!3  SM	
 #* +A(# ("3-(.	U3S	>* 	B>3r   