@@ -42,7 +42,7 @@ def simple_hash(input_string: str) -> str:
4242 return sha256_hash [:8 ] + sha256_hash [- 8 :]
4343
4444
45- def deploy_model (model_id : str , region : str = "cn-hangzhou" ):
45+ def deploy_model (model_id : str , region : str = "cn-hangzhou" , storage : str = "nas" ):
4646 """
4747 部署模型到函数计算
4848
@@ -71,9 +71,7 @@ def deploy_model(model_id: str, region: str = "cn-hangzhou"):
7171 encoded_model_id = urllib .parse .quote (model_id , safe = "" )
7272
7373 # 调用部署接口
74- model_registry_url = os .getenv (
75- "MODEL_REGISTRY_URL" , "model-registry.devsapp.cn"
76- )
74+ model_registry_url = os .getenv ("MODEL_REGISTRY_URL" , "model-registry.devsapp.cn" )
7775 deploy_url = (
7876 f"http://{ model_registry_url } /api/v1/models/{ encoded_model_id } /deploy-info"
7977 )
@@ -108,6 +106,28 @@ def deploy_model(model_id: str, region: str = "cn-hangzhou"):
108106 # 更新resources中的props
109107 s_yaml ["resources" ]["test_func" ]["props" ] = deploy_info
110108
109+ # 下载到 oss
110+ if storage == "oss" :
111+ s_yaml ["resources" ]["test_func" ]["props" ].pop ("nasConfig" , None )
112+ s_yaml ["resources" ]["test_func" ]["props" ].pop ("vpcConfig" , None )
113+ s_yaml ["resources" ]["test_func" ]["props" ]["ossMountConfig" ] = "auto"
114+ s_yaml ["resources" ]["test_func" ]["props" ][
115+ "role"
116+ ] = "acs:ram::${config('AccountID')}:role/aliyunfcdefaultrole"
117+ s_yaml ["resources" ]["test_func" ]["props" ]["annotations" ]["modelConfig" ][
118+ "storage"
119+ ] = storage
120+ # 修改 customContainerConfig.entrypoint 中的路径
121+ custom_container_config = s_yaml ["resources" ]["test_func" ]["props" ].get ("customContainerConfig" , {})
122+ if "entrypoint" in custom_container_config :
123+ entrypoint = custom_container_config ["entrypoint" ]
124+ if isinstance (entrypoint , list ):
125+ # 遍历 entrypoint 数组,替换包含 /mnt/ 的路径
126+ for i , item in enumerate (entrypoint ):
127+ if isinstance (item , str ) and "/mnt/" in item and not item .startswith ("vllm" ) and not item .isdigit () and item not in ["--port" , "--served-model-name" , "--trust-remote-code" ]:
128+ entrypoint [i ] = f"/mnt/serverless-{ region } -d5d4cd07-616a-5428-91b7-ec2d0257b3a2"
129+ custom_container_config ["entrypoint" ] = entrypoint
130+
111131 # 保存配置到临时文件
112132 s_yaml_file = f"s.yaml"
113133 with open (s_yaml_file , "w" , encoding = "utf-8" ) as f :
@@ -326,14 +346,9 @@ def cleanup_deployment(s_yaml_file: str):
326346 try :
327347 print (f"正在清除部署资源: { s_yaml_file } " )
328348 # 清除模型
329- subprocess .check_call (
330- f"echo 123456 | sudo -S s model remove -y -t { s_yaml_file } " , shell = True
331- )
349+ subprocess .check_call (f"s model remove -y -t { s_yaml_file } " , shell = True )
332350 # 清除函数
333- subprocess .check_call (
334- f"echo 123456 | sudo -S s remove -y -t { s_yaml_file } --skip-push" ,
335- shell = True ,
336- )
351+ subprocess .check_call (f"s remove -y -t { s_yaml_file } --skip-push" , shell = True )
337352 print ("部署资源清除完成!" )
338353 except subprocess .CalledProcessError as e :
339354 print (f"清除部署资源失败: { e } " )
@@ -354,6 +369,7 @@ def main():
354369 parser .add_argument (
355370 "--auto-cleanup" , action = "store_true" , help = "部署和测试完成后自动执行清理操作"
356371 )
372+ parser .add_argument ("--storage" , help = "存储区域" , default = "nas" )
357373
358374 args = parser .parse_args ()
359375
@@ -364,7 +380,9 @@ def main():
364380 return 0
365381 elif args .model_id :
366382 # 部署和测试模型
367- deploy_url , s_yaml_file = deploy_model (args .model_id , args .region )
383+ deploy_url , s_yaml_file = deploy_model (
384+ args .model_id , args .region , args .storage
385+ )
368386
369387 # 测试模型
370388 test_model (args .model_id , deploy_url , s_yaml_file )
0 commit comments