如何通过Azure Python SDK v2部署自定义Hugging Face模型(如AudioGen)
部署自定义Hugging Face模型(facebook/audiogen-medium)到Azure在线端点的正确方法
问题背景
部分Hugging Face模型(如facebook/audiogen-medium)未收录在Azure模型目录,也无一键部署按钮。使用Azure Python SDK v2部署时,存在模型注册路径困惑、注册报错,以及端点部署步骤待确认的问题。
本地调用模型代码:
from audiocraft.models import AudioGen model = AudioGen.get_pretrained("facebook/audiogen-medium") model.set_generation_params(duration=5) # 生成5秒音频 descriptions = ['dog barking', 'sirene of an emergency vehicle', 'footsteps in a corridor'] wav = model.generate(descriptions) # 生成3个样本
注册模型时因path属性填写错误触发ValueError: No value for given attribute,报错堆栈见用户提供内容。已编写score.py推理脚本及端点部署代码。
解决方案
一、模型注册的正确方式
Azure ML注册模型时,path指向包含模型文件+推理脚本的完整目录,而非单独文件。针对audiocraft有两种可行方案:
方案1:提前下载模型并打包注册
- 本地下载模型并保存:
from audiocraft.models import AudioGen model = AudioGen.get_pretrained("facebook/audiogen-medium") model.save_pretrained("./audiogen-model") # 保存到本地目录
- 整理目录结构:
audiogen-model/ ├── 模型权重文件(由save_pretrained生成) └── score.py # 你的推理脚本
- 注册模型:
from azure.ai.ml.entities import Model from azure.ai.ml.constants import AssetTypes model = Model( name="audiogen-medium", version="1", path="./audiogen-model", type=AssetTypes.CUSTOM_MODEL, description="Facebook AudioGen Medium模型" ) ml_client.models.create_or_update(model)
方案2:依赖audiocraft自动下载(无需提前打包)
若不想提前下载模型,可仅注册score.py,但需在部署环境中配置所有依赖,确保audiocraft能自动拉取模型权重。
二、解决注册时的ValueError报错
从堆栈信息看,问题出在workspaceName序列化为空,检查ML客户端初始化代码,确保正确传入工作区名称:
from azure.ai.ml import MLClient from azure.identity import DefaultAzureCredential ml_client = MLClient( DefaultAzureCredential(), subscription_id="你的订阅ID", resource_group_name="你的资源组名", workspace_name="你的工作区名" # 必须非空且正确 )
三、score.py优化(增加鲁棒性)
import json import os import traceback from audiocraft.models import AudioGen from audiocraft.data.audio import audio_write from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient def init(): global model, blob_service_client, container_name try: model = AudioGen.get_pretrained('facebook/audiogen-medium') model.set_generation_params(duration=5) connect_str = os.getenv('AZURE_STORAGE_CONNECTION_STRING') if not connect_str: raise ValueError("未设置AZURE_STORAGE_CONNECTION_STRING环境变量") blob_service_client = BlobServiceClient.from_connection_string(connect_str) container_name = 'audiogen_generated_files' # 自动创建不存在的容器 if not blob_service_client.get_container_client(container_name).exists(): blob_service_client.create_container(container_name) except Exception as e: traceback.print_exc() raise e def run(payload: str): try: data = json.loads(payload) theme = data.get("theme") if not theme: return {"error": "缺少theme参数"} prompts = [f'{theme}, slow speed', f'{theme}, fast speed', f'{theme} door closing', f'{theme} starting'] wavs = model.generate(prompts) urls = [] os.makedirs('/tmp', exist_ok=True) # 确保临时目录存在 for idx, one_wav in enumerate(wavs): file_name = f'{theme}_{idx}.wav' local_file_path = f'/tmp/{file_name}' audio_write(local_file_path, one_wav.cpu(), model.sample_rate, strategy="loudness", loudness_compressor=True) blob_client = blob_service_client.get_blob_client(container=container_name, blob=file_name) with open(local_file_path, "rb") as f: blob_client.upload_blob(f, overwrite=True) urls.append(blob_client.url) os.remove(local_file_path) return {"urls": urls} except Exception as e: traceback.print_exc() return {"error": str(e)}
四、端点与部署步骤修正
原部署代码缺少环境配置(需安装audiocraft等依赖),且存在拼写错误(deployement改为deployment),完整代码如下:
- 准备conda依赖文件
conda.yml:
name: audiogen-env channels: - defaults - conda-forge dependencies: - python=3.10 - pip=23.0.1 - pip: - audiocraft==0.1.7 - azure-storage-blob==12.19.0 - torch==2.0.1 - torchaudio==2.0.2
- 完整部署代码:
import uuid from azure.ai.ml.entities import ManagedOnlineEndpoint, ManagedOnlineDeployment, Environment from azure.ai.ml.constants import EnvironmentType # 创建在线端点 endpoint = ManagedOnlineEndpoint( name=f"audiogen-endpoint-{str(uuid.uuid4())[:8]}", description="AudioGen模型推理端点", auth_mode="key" ) endpoint = ml_client.online_endpoints.begin_create_or_update(endpoint).result() # 创建自定义环境 env = Environment( name="audiogen-env", version="1", description="AudioGen模型运行环境", conda_file="./conda.yml", image="mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04:latest" ) ml_client.environments.create_or_update(env) # 创建部署 deployment = ManagedOnlineDeployment( name="audiogen-deployment-mo", endpoint_name=endpoint.name, model=ml_client.models.get(name="audiogen-medium", version="1"), environment=env, instance_count=1, instance_type="Standard_NC6s_v3", # 必须选择GPU实例,AudioGen依赖CUDA environment_variables={"AZURE_STORAGE_CONNECTION_STRING": "你的Blob存储连接字符串"} ) deployment = ml_client.begin_create_or_update(deployment).result() # 设置流量全部分配到该部署 ml_client.online_endpoints.begin_create_or_update( ManagedOnlineEndpoint( name=endpoint.name, traffic={"audiogen-deployment-mo": 100} ) ).result()
关键注意事项
- AudioGen依赖GPU加速,必须选择支持GPU的实例类型(如Standard_NC6s_v3、Standard_NV6等)
- 确保Azure工作区有足够的GPU实例配额
- 若不需要持久化音频,可修改
run函数直接返回音频字节流,无需上传Blob存储
内容的提问来源于stack exchange,提问作者Momo
相关产品推荐
相关产品推荐

