
    9i+                         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%  \"" 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)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      U l        g )Ndevicegputrust_remote_codeF)	model_dirgetr   _device_namer    )selfr!   argskwargsdevice_names        a/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/base/base_model.py__init__Model.__init__   s9    "jj51k"'!',?!G    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                 h    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    )r$   r3   r!   s      r(   check_trust_remote_codeModel.check_trust_remote_code'   s:      0/ 	 ,i@))383) Ar+   c                     g)zd
Run the forward pass for a model.

Returns:
    Dict[str, Any]: output from the model forward pass
N r0   s      r(   r/   Model.forward7   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.
r8   )r$   r:   r&   s      r(   r.   Model.postprocessA   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
r8   r8   )clsr&   s     r(   _instantiateModel._instantiateN   s     }V}r+   Fmodel_name_or_pathrevisioncfg_dictr   r    c                 r   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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[/        U[0        5      (       a  UR3                  S5      (       a  SUSS -   nUR                  SS5      nUc  [5        XU5      (       a  SnSnUS;   a  [7        XU40 UD6nUb+  UR                  SS5      nUc  Ub  UR9                  U5      nU$ Uc  [;        S[        R                   S35      eXl        UR?                  S5      nU(       a  U(       d  [        S5      eU(       a  U(       a  [        RA                  S5        [C        XS9  0 nU(       a  SU0n[E        U5        URG                  5        H  u  nnUUU'   M     Ub  XNl$        U[J        RL                  L a  SUl'        [Q        U5      nO
[S        XUS9n[)        US5      (       a  URT                  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_agentrF   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
PRETRAINEDospexistsRuntimeErrorr
   loggerinfojoinr   CONFIGURATIONospathr   	from_filegetattrr   hasattrrK   rL   
isinstancestr
startswithr   r   toFileNotFoundErrorr!   safe_getwarningr   r   itemsr   r   backboneinit_backboner   r   rV   rW   name)r>   rA   rB   rC   r   r    r&   
prefetched
invoked_byrF   local_model_dirconfiguration_pathrW   rT   	model_cfgrK   rO   rJ   rP   rR   rU   kvs                          r(   from_pretrainedModel.from_pretrainedW   sv   X ZZ 23
!JJ)*ZZ

+
!JJvzz"**J$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),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   rh   r   r1   r   r5   r   r/   r.   classmethodr?   r   r   boolry   r	   rb   PathLiker   dictr   __static_attributes__r8   r+   r(   r   r      sS   H?4S> ? ;?;?4*23-4+3C=4  $sCx.  $sCx. tCH~     3I+/&*<AN,/N"*3-N #)N !$	N
 ,4D>N Nd HL15N',S"++-='>N/4S$s)^/DN !)N Nr+   r   ))rb   os.pathrc   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^   Tensorr   r8   r+   r(   <module>r      sc    	  # 3 3 > % AD D 7 O O 1 .= 
	*	+bNC bNr+   