
    9iIH                         S SK r S SKrS SKJrJr  S SKJr  S SKrS SKJ	r	  S SK
JrJrJr  S SKJr  \" 5       rSr " S S	5      rg)
    N)ListOptional)urlparse)tqdm)MODELSCOPE_URL_SCHEMEencode_media_to_base64get_endpoint)
get_loggerzihttps://modelscope.cn/models/modelscope/modelscope_aigc_default_logo/resolve/master/aigc_default_logo.pngc                   Z   \ rS rSrSr1 Skr1 Skr1 Skr         S'S\S\S	\S
\S\	\   S\	\   S\	\
\      S\	\   S\	\
\      S\	\
\      S\	\   S\	\   4S jjrS rS rS rS rS(S\S\	\   4S jjrSSSSS.S\	\   S\S\	\   S \	\   S!S4
S" jjrS!\4S# jr\S$\4S% j5       rS&rg))	AigcModel   a  
Helper class to encapsulate AIGC-specific model creation parameters.

This class can be initialized directly with parameters, or loaded from a
JSON configuration file using the `from_json_file` classmethod.

Example of direct initialization:
    >>> aigc_model = AigcModel(
    ...     aigc_type='Checkpoint',
    ...     base_model_type='SD_XL',
    ...     model_path='/path/to/your/model.safetensors'
    ...     base_model_id='AI-ModelScope/FLUX.1-dev'
    ... )

Example of loading from a JSON file:
    `config.json`:
    {
        "model_path": "/path/to/your/model.safetensors",
        "aigc_type": "Checkpoint",
        "base_model_type": "SD_XL",
        "base_model_id": "AI-ModelScope/FLUX.1-dev"
    }

    >>> aigc_model = AigcModel.from_json_file('config.json')
>   VAELoRA
Checkpoint>   SD_2SD_3SD_XLFLUX_1SD_1_5SD_2_1UNKNOWNQWEN_IMAGE_20_BWAN_VIDEO_2_1_14_BWAN_VIDEO_2_1_I2V_14_BWAN_VIDEO_2_1_T2V_14_BWAN_VIDEO_2_2_TI2V_5_BWAN_VIDEO_2_1_T2V_1_3_BWAN_VIDEO_2_1_FLF2V_14_BWAN_VIDEO_2_2_I2V_A_14_BWAN_VIDEO_2_2_T2V_A_14_B>   3dmanfoodlogoplantwomananimalartwaresceneryclothing	commodity	dimensionphotographyconstruction
cg-fantasymain-strongother-stylessci-fi-mechatoy-figurinesother-functionspicture-controlart-style-strongcharacter-strongflat-abstractionhand-drawn-stylee-commerce-designautomobile-trafficillustration-designarchitectural-designcharacter-enhancementclassic-painting-styleN	aigc_typebase_model_type
model_pathbase_model_idtagdescriptioncover_imagespath_in_repotrigger_wordsofficial_tagsmodel_sourcebase_model_sub_typec                    X0l         Xl        X l        XPl        X`l        Xl        Xl        Ub  / nU H  n[        U[        5      (       a  UR                  S5      (       d~  UR                  S5      (       dh  UR                  S5      (       dR   [        U5      nUR                  U5        [        R                  S[        R                  R!                  U5      5        M  UR                  U5        M  UR                  U5        M     Xl        O[*        /U l        X@l        Xl        Xl        U R3                  5         U R5                  5         U
(       a  Xl        U R9                  5         OSU l        U R;                  5         g! ["        [$        4 a4  n[        R'                  SUU5        UR                  U5         SnAGM  SnAff = f)aN  
Initializes the AigcModel helper.

Args:
    model_path (str): The path of checkpoint/LoRA weight file or folder.
    aigc_type (str): AIGC model type. Recommended: 'Checkpoint', 'LoRA', 'VAE'.
    base_model_type (str): Vision foundation model type. Recommended values are in BASE_MODEL_TYPES.
    tag (str, optional): Tag for the AIGC model. Defaults to 'v1.0'.
    description (str, optional): Model description. Defaults to 'this is an aigc model'.
    cover_images (List[str], optional): List of cover image URLs.
    base_model_id (str, optional): Base model name. e.g., 'AI-ModelScope/FLUX.1-dev'.
    path_in_repo (str, optional): Path in the repository.
    trigger_words (List[str], optional): Trigger words for the AIGC Lora model.
    official_tags (List[str], optional): Official tags for the AIGC model. Defaults to None.
    model_source (str, optional): Source of the model.
        `USER_UPLOAD`, `TRAINED_FROM_MODELSCOPE` or `TRAINED_FROM_ALIYUN_FC`. Defaults to 'USER_UPLOAD'.
    base_model_sub_type (str, Optional): Sub vision foundation model type. Defaults to ''. e.g. `SD_1_5`
Nzhttp://zhttps://zdata:z#Converted local image to base64: %sz2Failed to process local image %s: %s. Using as-is.)rB   r@   rA   rD   rE   rJ   rK   
isinstancestr
startswithr   appendloggerinfoospathbasenameFileNotFoundError
ValueErrorwarningrF   DEFAULT_AIGC_COVER_IMAGErC   rG   rH   _validate_aigc_type_validate_base_model_typerI   _validate_official_tags_process_model_path)selfr@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   processed_cover_imagesimgprocessed_imges                    Y/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/hub/utils/aigc.py__init__AigcModel.__init__H   s   B %".&(#6 #%'"#c3''NN955"~~j99"~~g66
?,B3,GM299-H"KK(M(*(8(8(=? /55c:*11#6+ $, !7!9 :D*(* 	  "&&(!.((*!%D 	  "9 !2:> ?"NN T #Q( 399#>>	?s   AFG(G  Gc                     U R                   U R                  ;  aK  SR                  [        U R                  5      5      n[        R                  SU R                    SU S35        gg)z?Validate aigc_type and provide a warning for unsupported types., zUnsupported aigc_type: "z". Recommended values: z1. Custom values are allowed but may cause issues.N)r@   
AIGC_TYPESjoinsortedrQ   rX   r^   supported_typess     rc   rZ   AigcModel._validate_aigc_type   s`    >>0"iit(?@ONN5dnn5E F22A1B CMM N 1    c                     U R                   U R                  ;  aK  SR                  [        U R                  5      5      n[        R                  SU R                    SU S35        gg)zCValidate base_model_type and provide warning for unsupported types.rg   zYour base_model_type: "z," may not be supported. Recommended values: z2. Custom values are allowed but may cause issues. N)rA   BASE_MODEL_TYPESri   rj   rQ   rX   rk   s     rc   r[   #AigcModel._validate_base_model_type   sg    t'<'<<"iit/D/D(EFONN)$*>*>)? @''6&7 8CDE =rn   c                    U R                    Vs1 s H  oU R                  ;  d  M  UiM     nnU(       aP  SR                  U R                  5      nSR                  S U 5       5      n[        R	                  SU SU S35        ggs  snf )z@Validate official tags and provide warning for unsupported tags.rg   c              3   .   #    U  H  nS U S 3v   M     g7f)"N ).0rD   s     rc   	<genexpr>4AigcModel._validate_official_tags.<locals>.<genexpr>   s     (L|1SE|s   zYour tag(s): z+ may not be supported. Recommended values: z. N)rI   OFFICIAL_TAGSri   rQ   rX   )r^   rD   invalid_tagssupported_tagsinvalid_tags_strs        rc   r\   !AigcModel._validate_official_tags   s     ))
)8J8J-J ) 	 
 !YYt'9'9:N#yy(L|(LLNN 01 2''5&6b:; 	
s
   BBc           
      	   SSK Jn  [        R                  R	                  U R
                  5      U l        [        R                  R                  U R
                  5      (       d  [        SU R
                   35      e[        R                  R                  U R
                  5      (       aA  U R
                  n[        R                  S[        R                  R                  U5      5        GO[        R                  R                  U R
                  5      (       Ga8  [        R                  " U R
                  5      n[        U5      S:X  a  [        SU R
                   S35      eU Vs/ s HS  n[        R                  R                  [        R                  R                  U R
                  U5      5      (       d  MQ  UPMU     nn1 SknU(       a@  U Vs1 s H  oDR!                  5       iM     nnUR#                  U5      (       a  [        S5      e/ S	QnS
nU GH  n	[$        R$                  " [        R                  R                  U R
                  SU	 35      5      n
U
(       d  MN  U
S   n[        R                  SU	 S3[        R                  R                  U5      5        [        U
5      S:  a  [        R'                  SU	 S3[        R                  R                  U5      5        [        R                  SU	 S3U
SS
  Vs/ s H"  n[        R                  R                  U5      PM$     sn5          O   U(       Gd  [        R                  " U R
                  5       Vs/ s HS  n[        R                  R                  [        R                  R                  U R
                  U5      5      (       d  MQ  UPMU     nnU(       aw  [        R                  R                  U R
                  US   5      n[        R'                  S[        R                  R                  U5      5        [        R                  SU5        O2[        SU R
                   S35      eO[        SU R
                   35      eU(       a]  [        R                  SU5        U" U5      n[        R                  R                  U5      U l        US   U l        US   U l        X l        g
g
s  snf s  snf s  snf s  snf )z0Process model_path to extract weight informationr   )get_file_hashzModel path does not exist: zUsing file: %szDirectory is empty: zW. Please place at least one model file at the top level (e.g., .safetensors/.pth/.bin).>   	readme.md.gitattributesconfiguration.jsonzTop-level directory contains only [.gitattributes, configuration.json, README.md]. Please place additional model files at the top level (e.g., .safetensors/.pth/.bin).)z.safetensorsz.pthz.binN*zFound z	 file: %s   z	Multiple z$ files found, using: %s for metadatazOther z
 files: %sz:No safetensors/pth/bin files found, using: %s for metadatazAvailable files: %szNo files found in directory: zI. AIGC models require at least one model file (.safetensors recommended).z(Model path must be a file or directory: z!Computing hash and size for %s...	file_hash	file_size)modelscope.utils.file_utilsr   rS   rT   
expanduserrB   existsrW   isfilerQ   rR   rU   isdirlistdirlenri   lowerissubsetglobrX   weight_filenameweight_sha256weight_sizetarget_file)r^   r   r   top_entriesname	top_filesplaceholder_names
normalizedfile_extensionsextfilesf	all_files	hash_infos                 rc   r]   AigcModel._process_model_path   s   = '',,T__=ww~~doo..:4??:KLMM77>>$//**//KKK("''*:*:;*GHWW]]4??++ **T__5K;1$ *4??*; <l m  "-!,77>>"'',,t"EF   ! 7@Aytjjly
A&&'899$o  ?OK&		"'',,t!C5	"JK5"'(KKK&Y 7 " 0 0 =?5zA~'u,PQGG,,[9; fSE$<BG)$L)QRWW%5%5a%8)$LN ' !zz$//::!ww~~bggll4??A&FG :  
 "$'',,t	!"MKNNT((57 KK 5yA$77H Ib c  & :4??:KLN N KK;[I%k2I $&77#3#3K#@D !*;!7D(5D* q B, %M
s&   %AS9SS?)SAS1Smodel_idtokenc           	      .   [         R                  SU5         [        R                  R	                  U R
                  5      (       aH  [         R                  SU R
                  5        UR                  UU R
                  U R                  USS9  O[        R                  R                  U R
                  5      (       a  [         R                  SU R                  5        UR                  U R                  U R                  (       a  U R                  S-   U R                  -   OU R                  UUSU R                   S3S	9  [         R                  S
U5        g! [         a5  n[         R                  SU5        [         R                  S5         SnAgSnAff = f)z!Upload model files to repository.zUploading model to %s...zUploading directory: %sz"Upload model folder for AIGC model)repo_idfolder_pathrG   r   commit_messagezUploading file: %s/zUpload z for AIGC model)path_or_fileobjrG   r   r   r   z!Successfully uploaded model to %sTz#Warning: Failed to upload model: %sz9You may need to upload the model manually after creation.NF)rQ   rR   rS   rT   r   rB   upload_folderrG   r   r   upload_filer   	ExceptionrX   )r^   apir   r   rb   s        rc   upload_to_repoAigcModel.upload_to_repo  sG   .9	ww}}T__--5tG!!$ $!%!2!2#G " I 000$2B2BC$($4$4(( "&!2!2S!84;O;O!O.2.B.B$%,T-A-A,B C% $%   & KK;XF 	NN@!DNNKM		s   D<E 
F+FFi,  )cookiestimeoutheadersendpointr   r   r   r   returnc          
         U=(       d
    [        5       n[        UR                  5       5      R                  R	                  S5      nUR                  S5      (       d  [         SU 3O[         SUR	                  S5       3nU S3n[        U SS5      =(       d    U R                  n[        R                  R                  [        R                  R                  U5      5      n[        R                  R                  U5      (       d  [        S	U 35      eU(       a  [        U5      OSnUc  [        S
5      eUR!                  SSUS    305        [        R                  R#                  U5      n	 SS[$        4S jjn
['        U	SSSSS9 n[)        US5       n[*        R,                  " UUU
" X5      US9nSSS5        SSS5         WR/                  5       n[7        U[        5      (       a5  UR9                  S5      SL a   UR9                  SS5      n[;        SU 35      egg! , (       d  f       Nr= f! , (       d  f       N{= f! [*        R0                  R2                   a    WR5                  5          gf = f)a`  Pre-upload aigc model weights to the LFS server.

Server may require the sha256 of weights to be registered before creation.
This method streams the weight file so the sha gets registered.

Args:
    cookies: Optional requests-style cookies (CookieJar/dict). If provided, preferred.
    timeout: Request timeout seconds.
    headers: Optional headers.
zwww.prezlfs.zpre-lfs.zpre.z/api/v1/models/aigc/weightsr   Nz Pre-upload expects a file, got: z)Token does not exist, please login first.Cookiezm_session_id=m_session_id
chunk_sizec              3   ~   #     U R                  U5      nU(       d  g UR                  [        U5      5        Uv   M9  7fN)readupdater   )file_objectpbarr   cks       rc   read_in_chunks3AigcModel.preupload_weights.<locals>.read_in_chunks\  s:       %%j1CG$ s   ;=BTz[Pre-uploading] )totalunit
unit_scaledynamic_ncolsdescrb)r   datar   SuccessFMessagezunknown errorzPre-upload failed: )i   )r	   r   striphostnamelstriprO   r   getattrrB   rS   rT   abspathr   r   rW   dictr   getsizeintr   openrequestsputjson
exceptionsJSONDecodeErrorraise_for_statusrM   getRuntimeError)r^   r   r   r   r   endpoint_hostbase_urlurl	file_pathr   r   r   r   rrespmsgs                   rc   preupload_weightsAigcModel.preupload_weights6  s1     -|~%hnn&67@@GGO N[MeMefkMlMl01m_E)*(=3G3G3O2PQ 	 Z:;D-6I$//	GGOOBGG$6$6y$AB	ww~~i((?	{KLL#*$w-?HIIM'.2I1J"KLMGGOOI.	 .=	'*	 "')
 -1i&!LL#'0#	 ')	668D
 dD!!dhhy&9U&B((9o6C!4SE:;; 'C! '&) ) ""22 	 	s6   	H1H 4H1I  
H.	*H11
H?.I32I3c                 <   U R                   U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S.$ )zDConverts the AIGC parameters to a dictionary suitable for API calls.r@   rA   rD   rE   rF   rC   rB   r   r   r   rH   rI   rJ   rK   r   )r^   s    rc   to_dictAigcModel.to_dict}  s     #3388++ --!////#33!//++!//!// --#'#;#;
 	
rn   	json_pathc                 l   SSK n[        R                  R                  U5      n[        R                  R	                  U5      (       d  [        SU 35      e[        USSS9 nUR                  U5      nSSS5        / SQnU H  nUW;  d  M  [        SU S	35      e   U " S
0 WD6$ ! , (       d  f       N8= f)z
Creates an AigcModel instance from a JSON configuration file.

Args:
    json_path (str): The path to the JSON configuration file.

Returns:
    AigcModel: An instance of the AigcModel.
r   NzJSON config file not found at: r   zutf-8)encoding)rB   r@   rA   rC   z(Missing required field in JSON config: ''ru   )	r   rS   rT   r   r   rV   r   loadrW   )clsr   r   r   configrequired_fieldsfields          rc   from_json_fileAigcModel.from_json_file  s     	GG&&y1	ww~~i((#1)=? ? )S73qYYq\F 4
 %EF" >ugQGI I %
 }V} 43s   !B%%
B3)r@   rC   rK   rA   rF   rE   rB   rJ   rI   rG   rD   r   rH   r   r   r   )	 zv1.0zthis is an aigc modelNr   NNUSER_UPLOADr   r   )__name__
__module____qualname____firstlineno____doc__rh   rp   ry   rN   r   r   rd   rZ   r[   r\   r]   r   objectr   r   r   r   classmethodr   __static_attributes__ru   rn   rc   r   r      s   4 /J	M   #%<,0&(-1-1&3-/S#S# S# 	S#
 S# c]S# c]S# tCy)S# smS#  S	*S#  S	*S# smS# &c]S#jNE;X+tC  F 7;),4848E<#+F#3E< $'E< $,D>	E<
 %-SME<
 >BE<N
 
& s  rn   r   )r   rS   typingr   r   urllib.parser   r   	tqdm.autor   modelscope.hub.utils.utilsr   r   r	   modelscope.utils.loggerr
   rQ   rY   r   ru   rn   rc   <module>r     sC     	 ! !  N N .	 
W Wrn   