
    6pj-                         S SK r S SKJr  S SKJrJr  S SKJrJ	r	J
r
JrJr  S SKJr  S SKJr  S SKJrJr  S SKJrJrJr  S SKJrJr  S S	KJrJrJr  S S
KJ r   S SK!J"r"  S SK#J$r$J%r%J&r&  \"" 5       r'\S   r( " S S\5      r)g)    N)ABCabstractmethod)AnyDictListOptionalUnion)snapshot_download)Tasks)build_backbonebuild_model)can_load_by_mscheck_model_from_owner_grouptry_to_load_hf_model)Config
ConfigDict)DEFAULT_MODEL_REVISIONInvoke	ModelFile)verify_device)
get_logger)filter_plugin_in_whitelistregister_modelhub_reporegister_plugins_repo)ztorch.Tensorz	tf.Tensorc                   F   \ rS rSrSrS rS\\\4   4S jr	  SS\
\   S\
\   4S	 jjr\S\\\4   4S
 j5       rS\\\4   S\\\4   4S jr\S 5       r\\SSS4S\S\
\   S\S\S\
\   4
S jj5       r  SS\\\R.                  4   S\\\\   4   S\
\   4S jjrSrg)Model   zBase model interface.
    c                     Xl         UR                  SS5      n[        U5        X@l        UR                  SS5      =(       d    [	        U5      U l        g )Ndevicegputrust_remote_codeF)	model_dirgetr   _device_namer   r!   )selfr"   argskwargsdevice_names        ]/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/modelscope/models/base/base_model.py__init__Model.__init__   sL    "jj51k"'!'" ">29= 	    returnc                 D    U R                  U R                  " U0 UD65      $ )N)postprocessforwardr%   r&   r'   s      r)   __call__Model.__call__'   s!    d =f =>>r,   Ninfo_strr"   c                 p    U=(       d    Sn[        US9(       d  U R                  (       d  [        U5      egg)a  Check trust_remote_code if the model needs to import extra libs

Args:
    info_str(str): The info showed to user if trust_remote_code is `False`.
    model_dir(`Optional[str]`): The local model directory. If is a trusted model, check remote code will pass.
zThis model requires `trust_remote_code` to be `True` because it needs to import extra libs or execute the code in the model repo, setting this to true means you trust the files in it.)r"   N)r   r!   RuntimeError)r%   r4   r"   s      r)   check_trust_remote_codeModel.check_trust_remote_code*   s?      0/ 	 ,i@))"8,, * Ar,   c                     g)zd
Run the forward pass for a model.

Returns:
    Dict[str, Any]: output from the model forward pass
N r1   s      r)   r0   Model.forward<   s     	r,   inputsc                     U$ )zModel specific postprocess and convert model output to
standard model outputs.

Args:
    inputs:  input data

Return:
    dict of results:  a dict containing outputs of model, each
        output should have the standard output name.
r:   )r%   r<   r'   s      r)   r/   Model.postprocessF   s	     r,   c                     U " S0 UD6$ )zDefine the instantiation method of a model,default method is by
calling the constructor. Note that in the case of no loading model
process in constructor of a task model, a load_model method is
added, and thus this method is overloaded
r:   r:   )clsr'   s     r)   _instantiateModel._instantiateS   s     }V}r,   Fmodel_name_or_pathrevisioncfg_dictr   r!   c                    UR                  S5      nUb  UR                  S5        UR                  [        R                  5      nUb   UR                  [        R                  5        O[        R                  n[        U5      n	U=(       d    U	nUR                  SS5      n
[        R                  " U5      (       a  UnO3USL a  [        S5      e[        R                  < SU< 3n[        UUUU
S9n[        R                  SU 35        [        R                  " U[        R                  5      nSnUb  UnO:[        R                   R                  U5      (       a  ["        R$                  " U5      n['        US	S5      nS	U;   a  UR                  S	5      n['        US
[)        5       5      n[+        US5      (       a"  [+        US5      (       d  UR,                  Ul        ['        USS5      n[1        U[2        5      (       a  UR5                  S5      (       a  SUSS -   nUR                  SS5      nUc  [7        XU5      (       a  SnSnUS;   a  [9        XU40 UD6nUb+  UR                  SS5      nUc  Ub  UR;                  U5      nU$ Uc  [=        S[        R                   S35      eXl        URA                  S5      n[C        U5      (       a  U(       d  [        S5      eU(       a  U(       a  [        RE                  S5        [G        XS9  0 nU(       a  U	(       d  SU0n[I        U5        URK                  5        H  u  nnUUU'   M     Ub  XOl&        U[N        RP                  L a  SUl)        [U        U5      nO
[W        XUS9n[+        US5      (       a  URX                  Ul,        [+        US5      (       d  UUl-        UR                  SS5        UUl.        UUl        U$ )a  Instantiate a model from local directory or remote model repo. Note
that when loading from remote, the model revision can be specified.

Args:
    model_name_or_path(str): A model dir or a model id to be loaded
    revision(str, `optional`): The revision used when the model_name_or_path is
        a model id of the remote hub. default `master`.
    cfg_dict(Config, `optional`): An optional model config. If provided, it will replace
        the config read out of the `model_name_or_path`
    device(str, `optional`): The device to load the model.
    trust_remote_code(bool, `optional`): Whether to trust and allow execution of remote code. Default is False.
    **kwargs:
        task(str, `optional`): The `Tasks` enumeration value to replace the task value
            read out of config in the `model_name_or_path`. This is useful when the model to be loaded is not
            equal to the model saved.
            For example, load a `backbone` into a `text-classification` model.
            Other kwargs will be directly fed into the `model` key, to replace the default configs.
        use_hf(bool, `optional`):
            If set to True, it will initialize the model using AutoModel or AutoModelFor* from hf.
            If set to False, the model is loaded using the modelscope mode.
            If set to None, the loading mode will be automatically selected.
        ignore_file_pattern(List[str], `optional`):
            This parameter is passed to snapshot_download
        device_map(str | Dict[str, str], `optional`):
            This parameter is passed to AutoModel or AutoModelFor*
        torch_dtype(torch.dtype, `optional`):
            This parameter is passed to AutoModel or AutoModelFor*
        config(PretrainedConfig, `optional`):
            This parameter is passed to AutoModel or AutoModelFor*
Returns:
    A model instance.

Examples:
    >>> from modelscope.models import Model
    >>> Model.from_pretrained('damo/nlp_structbert_backbone_base_std', task='text-classification')
model_prefetchedNignore_file_patternTz9Expecting model is pre-fetched locally, but is not found./)
user_agentrH   zinitialize model from taskmodel
model_typetyper    cuda   use_hfF>   NT
device_map`z` file not found.pluginsa  Detected plugins field in the model configuration file, but trust_remote_code=True was not explicitly set.
To prevent potential execution of malicious code, loading has been refused.
If you trust this model repository, please pass trust_remote_code=True to from_pretrained.zUse trust_remote_code=True. Will invoke codes or install plugins from remote model repo. Please make sure that you can trust the external codes.)allow_remoter!   )	task_namedefault_argspipelinecfgr"   )/r#   popr   KEY
PRETRAINEDr   ospexistsr6   r
   loggerinfojoinr   CONFIGURATIONospathr   	from_filegetattrr   hasattrrM   rN   
isinstancestr
startswithr   r   toFileNotFoundErrorr"   safe_getr   warningr   r   itemsr   r   backboneinit_backboner   r   rX   rY   name)r@   rC   rD   rE   r   r!   r'   
prefetched
invoked_by_model_trustedrH   local_model_dirconfiguration_pathrY   rV   	model_cfgrM   rQ   rL   rR   rT   rW   kvs                           r)   from_pretrainedModel.from_pretrained\   s   X ZZ 23
!JJ)*ZZ

+
!JJvzz"**J56HI-?$jj)>E::())0OT!"O  %+JJ
;J/"%$7	9O
 	,_,=>? XXoy7N7NOCWW^^.//""#56CC.	V

6*IC*,7	9l++GIv4N4N&11INY5
fc""v'8'8'?'?fQRj(FHd+>n_-79 9F\!(V 3+13EL$7J!f&8(L;#I++,,=>@ @- ,,y)%g..7Hm  (NNJK 	O^/1BCLg&LLNDAqIaL #%&&*I#"9-E\KE 3
## \\ENue$$EIk4('
)r,   target_foldersave_checkpoint_namesconfigc                     [        S5      e)a  save the pretrained model, its configuration and other related files to a directory,
    so that it can be re-loaded

Args:
    target_folder (Union[str, os.PathLike]):
    Directory to which to save. Will be created if it doesn't exist.

    save_checkpoint_names (Union[str, List[str]]):
    The checkpoint names to be saved in the target_folder

    config (Optional[dict], optional):
    The config for the configuration.json, might not be identical with model.config
z>save_pretrained method need to be implemented by the subclass.)NotImplementedError)r%   r}   r~   r   r'   s        r)   save_pretrainedModel.save_pretrained   s    $ "LN 	Nr,   )r$   r"   r!   )NN)__name__
__module____qualname____firstlineno____doc__r*   r   ri   r   r2   r   r7   r   r0   r/   classmethodrA   r   r   boolr{   r	   rc   PathLiker   dictr   __static_attributes__r:   r,   r)   r   r      sR   >?4S> ? ;?;?-*23--+3C=-$ $sCx.  $sCx. tCH~     3I+/&*<AO,/O"*3-O #)O !$	O
 ,4D>O Of HL15N',S"++-='>N/4S$s)^/DN !)N Nr,   r   )*rc   os.pathrd   r]   abcr   r   typingr   r   r   r   r	    modelscope.hub.snapshot_downloadr
   modelscope.metainfor   modelscope.models.builderr   r    modelscope.utils.automodel_utilsr   r   r   modelscope.utils.configr   r   modelscope.utils.constantr   r   r   modelscope.utils.devicer   modelscope.utils.loggerr   modelscope.utils.pluginsr   r   r   r_   Tensorr   r:   r,   r)   <module>r      sh    	  # 3 3 > % AD D 7 O O 1 .= = 
	*	+gNC gNr,   