AI开发平台MODELARTS-部署在线服务:示例代码

时间:2024-11-08 17:12:25

示例代码

在ModelArts notebook平台,Session鉴权无需输入鉴权参数。其它平台的Session鉴权请参见Session鉴权

  • 方式1:已部署为在线服务predictor的初始化
    1
    2
    3
    4
    5
    from modelarts.session import Session
    from modelarts.model import Predictor
    
    session = Session()
    predictor_instance = Predictor(session, service_id="your_service_id")
    
  • 方式2:部署在线服务predictor
    • 部署服务到公共资源池
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      from modelarts.session import Session
      from modelarts.model import Model
      from modelarts.config.model_config import ServiceConfig, TransformerConfig, Schedule
      
      session = Session()
      model_instance = Model(session, model_id='your_model_id')
      vpc_id = None                                        # (可选)在线服务实例部署的虚拟私有云ID,默认为空
      subnet_network_id = None                             # (可选)子网的网络ID,默认为空
      security_group_id = None                             # (可选)安全组,默认为空
      configs = [ServiceConfig(model_id=model_instance.model_id,
                               weight="100",
                               instance_count=1,
                               specification="modelarts.vm.cpu.2u")]  # 参考表3中specification字段
      predictor_instance = model_instance.deploy_predictor(
                  service_name="service_predictor_name",
                  infer_type="real-time",
                  vpc_id=vpc_id,
                  subnet_network_id=subnet_network_id,
                  security_group_id=security_group_id,
                  configs=configs,                       # predictor配置参数, 参考下文configs参数格式说明
                  schedule = [Schedule(op_type='stop', time_unit='HOURS', duration=1)]       # (可选)设置在线服务运行时间
      )
      

      参数“model_id”代表将部署成在线服务的模型。“model_id”可以通过查询模型列表或者ModelArts管理控制台获取。

    • 部署服务到专属资源池
      from modelarts.config.model_config import ServiceConfig
      
      configs = [ServiceConfig(model_id=model_instance.model_id, weight="100", instance_count=1, 
      						 specification="modelarts.vm.cpu.2u")]
      predictor_instance = model_instance.deploy_predictor( 
                                                            service_name="your_service_name",
                                                            infer_type="real-time",
                                                            configs=configs,
                                                            cluster_id="your dedicated pool id"
                                                          )

    configs参数格式说明:SDK提供了ServiceConfig类对其定义,configs为list,list中的元组对象是ServiceConfig。定义代码如下:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    configs = []
    envs = {"model_name":"mxnet-model-1", "load_epoch":"0"}
    
    service_config1 = ServiceConfig(
            model_id="model_id1",                 # model_id1和model_id2必须是同一个模型的不同版本对应的model_id
            weight="70",
            specification="modelarts.vm.cpu.2u",  # 参考表3中specification字段
            instance_count=2,
            envs=envs)                            # (可选)设置环境变量的值,如:envs = {"model_name":"mxnet-model-1", "load_epoch":"0"}
    service_config2 = ServiceConfig(
            model_id='model_id2',
            weight="30",
            specification="modelarts.vm.cpu.2u",  # 参考表3中specification字段
            instance_count=2,
            envs=envs)                            # (可选)设置环境变量的值,如:envs = {"model_name":"mxnet-model-1", "load_epoch":"0"}
    configs.append(service_config1)
    configs.append(service_config2)
    
  • 方式3:部署批量服务transformer
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    from modelarts.session import Session
    from modelarts.model import Model
    from modelarts.config.model_config import TransformerConfig
    
    session = Session()
    model_instance = Model(session, model_id='your_model_id')
    vpc_id = None                                        # (可选)批量服务实例部署的虚拟私有云ID,默认为空
    subnet_network_id = None                             # (可选)子网的网络ID,默认为空
    security_group_id = None                             # (可选)安全组,默认为空
    
    transformer = model_instance.deploy_transformer(
            service_name="service_transformer_name",
            infer_type="batch",
            vpc_id=vpc_id,
            subnet_network_id=subnet_network_id,
            security_group_id=security_group_id,
            configs=configs                          # transformer配置参数, 参考下文configs参数格式说明 
    )
    

    configs参数格式说明:SDK提供了TransformerConfig类对其定义,configs都是list,list中的元组对象是TransformerConfig。定义代码如下:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    configs = []
    mapping_rule = None                               # (可选)输入参数与csv数据的映射关系
    mapping_type= "file"                              # file或者csv
    envs = {"model_name":"mxnet-model-1", "load_epoch":"0"}
    
    transformer_config1 = TransformerConfig(
                model_id="model_id",
                specification="modelarts.vm.cpu.2u",   # 参考表3中specification字段
                instance_count=2,
                src_path="/shp-cn4/sdk-demo/",         # 批量任务输入数据的OBS路径,如:"/your_obs_bucket/src_path"
                dest_path="/shp-cn4/data-out/",        # 批量任务输出结果的OBS路径,如:"/your_obs_bucket/dest_path"
                req_uri="/",
                mapping_type=mapping_type,
                mapping_rule=mapping_rule,
                envs=envs)                             # (可选)设置环境变量的值,如:envs = {"model_name":"mxnet-model-1", "load_epoch":"0"}
    configs.append(transformer_config1)
    
support.huaweicloud.com/sdkreference-modelarts/modelarts_04_0201.html