异步语音转写
平台提供异步语音转写 API,适用于长音频的语音转写,单个音频文件长度小于 5 小时。
- 支持普通话、粤语、英语等 10+ 语言与方言
- 支持智能断句与标点预测
- 支持按照声纹进行说话人分离
- 支持时间戳(字幕)信息返回
使用流程
- 先调用 文件上传 API 上传音频文件并获取 file_id
- 调用 创建异步语音转写任务 API,获取
task_id - 调用 查询语音转写任务状态 API,基于
task_id获取语音转写任务进度 - 当任务完成时,会返回转写结果
过程示例
1. 获取 file_id
Python
"""
本示例用于获取待转写音频的 file_id。注意:需要先将密钥信息写入环境变量 `API_KEY`。
"""
import requests
import os
api_key = os.environ.get("API_KEY")
url = "https://maas-api.hivoice.cn/v1/files/upload"
payload = {'purpose': 'a2t_async_input'}
files=[
('file',('test.mp3',open('path/to/test.mp3','rb'),'audio/mpeg'))
]
headers = {
'authority': 'maas-api.hivoice.cn',
'Authorization': f'Bearer {api_key}'
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
2. 创建语音转写任务
Python
"""
本示例用于创建语音转写任务。注意:需要先将密钥信息写入环境变量 `API_KEY`。
"""
import requests
import json
import os
api_key = os.environ.get("API_KEY")
url = "https://maas-api.hivoice.cn/v1/audio/asr/tasks"
payload = json.dumps({
"model": "u2-asr",
"file_id": <file_id>,
"format": "mp3"
})
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
3. 查询语音转写进度
Python
"""
本示例用于查询语音转写进度。注意:需要先将密钥信息写入环境变量 `API_KEY`,并将需要查询任务的 id 写入环境变量 `TASK_ID`。
"""
import requests
import json
import os
task_id = os.environ.get("TASK_ID")
api_key = os.environ.get("API_KEY")
url = f"https://maas-api.hivoice.cn/v1/audio/asr/tasks/{task_id}"
payload = {}
headers = {
'Authorization': f'Bearer {api_key}',
'content-type': 'application/json',
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)