def from_pretrained(model: Union[nn.Module, SwiftModel, PeftModel],
                        model_id: str = None,
                        adapter_name: Union[str, List[str], Dict[str, str]] = None,
                        revision: str = None,
                        **kwargs):
        """Prepare a model by a model_id in the ModelScope hub or a local dir.

        Args:
            model(`Union[nn.Module, 'SwiftModel']`): The model to be tuned.
            model_id(`str`): The model id of the modelhub or a local dir containing the configs/weights.
            adapter_name(`str`, `optional`): The adapter_name to use.
            revision(`str`, `optional`): The model revision if the model_id is a model id of the modelhub.
            **kwargs:
                Extra kwargs needed by ``SwiftModel.from_pretrained`` or ``PeftModel.from_pretrained``.
        Returns:
            The model wrapped by SwiftModel or PeftModel.
        """
        if not os.path.exists(model_id):
            model_id = snapshot_download(model_id, revision=revision)
        is_peft_model = False
        if os.path.exists(os.path.join(model_id, CONFIG_NAME)):
            with open(os.path.join(model_id, CONFIG_NAME), 'r', encoding='utf-8') as f:
                _json = json.load(f)
            is_peft_model = SWIFT_TYPE_KEY not in _json

        _name = adapter_name if isinstance(
            adapter_name, str) or adapter_name is None else adapter_name[0] \
            if isinstance(adapter_name, list) else list(adapter_name.keys())[0]
        _name = _name or ''
        if os.path.exists(os.path.join(model_id, _name, CONFIG_NAME)):
            with open(os.path.join(model_id, _name, CONFIG_NAME), 'r', encoding='utf-8') as f:
                _json = json.load(f)
            is_peft_model = SWIFT_TYPE_KEY not in _json and 'extra_state_keys' not in _json
        if is_peft_model:

            def load_peft_model(_model, _adapter_name, _new_name=None):
                if not _new_name:
                    _new_name = _adapter_name
                import peft
                if not isinstance(_model, peft.PeftModel):
                    return PeftModel.from_pretrained(
                        _model,
                        os.path.join(model_id, _adapter_name) if _adapter_name != 'default'
                        and os.path.exists(os.path.join(model_id, _adapter_name)) else model_id,
                        revision=revision,
                        adapter_name=_new_name,
                        **kwargs)
                else:
                    _model.load_adapter(
                        os.path.join(model_id, _adapter_name) if _adapter_name != 'default'
                        and os.path.exists(os.path.join(model_id, _adapter_name)) else model_id, _new_name)
                    return _model

            if not adapter_name:
                peft_model = load_peft_model(model, 'default')
                for _dir in os.listdir(model_id):
                    if os.path.isdir(os.path.join(model_id, _dir)) and \
                            os.path.exists(os.path.join(model_id, _dir, CONFIG_NAME)):
                        peft_model = load_peft_model(peft_model, _dir)
            elif isinstance(adapter_name, str):
                return load_peft_model(model, adapter_name)
            elif isinstance(adapter_name, list):
                peft_model = model
                for name in adapter_name:
                    peft_model = load_peft_model(peft_model, name)
            else:
                peft_model = model
                for key, value in adapter_name.items():
                    peft_model = load_peft_model(peft_model, key, value)
            return peft_model
        else:
            return SwiftModel.from_pretrained(model, model_id, revision=revision, adapter_name=adapter_name, **kwargs)
Prepare a model by a model_id in the ModelScope hub or a local dir.

        Args:
            model(`Union[nn.Module, 'SwiftModel']`): The model to be tuned.
            model_id(`str`): The model id of the modelhub or a local dir containing the configs/weights.
            adapter_name(`str`, `optional`): The adapter_name to use.
            revision(`str`, `optional`): The model revision if the model_id is a model id of the modelhub.
            **kwargs:
                Extra kwargs needed by ``SwiftModel.from_pretrained`` or ``PeftModel.from_pretrained``.
        Returns:
            The model wrapped by SwiftModel or PeftModel.
        
swift/tuners/base.py