本文为您介绍如何快速调用灵积语音模型服务。
说明
若您有合作需求或技术咨询,请提交工单。
前提条件
已开通服务并获得API-KEY。具体操作,请参见开通DashScope并创建API-KEY。
已安装SDK。具体操作,请参见安装DashScope SDK。
示例代码
以下示例展示了调用Paraformer语音识别文件转写API,对一个通过URL给出的音频文件进行语音识别的代码。
说明
需要使用您的API-KEY替换示例中的
your-dashscope-api-key
,代码才能正常运行。通过URL指定进行语音转写的文件,其大小不超过2GB。
file_urls参数支持传入多个文件URL,示例中仅展示对一个文件URL进行转写。
# For prerequisites running the following sample, visit https://help.aliyun.com/document_detail/611472.html
import dashscope
from urllib import request
import json
dashscope.api_key='your-dashscope-api-key'
task_response=dashscope.audio.asr.Transcription.async_call(
model='paraformer-v1',
file_urls=['https://dashscope.oss-cn-beijing.aliyuncs.com/samples/audio/paraformer/hello_world.wav']
)
transcription_response=dashscope.audio.asr.Transcription.wait(task=task_response.output.task_id)
transcription_url=transcription_response.output['results'][0]['transcription_url']
transcription_results=json.loads(request.urlopen(transcription_url).read().decode('utf8'))
print(json.dumps(transcription_results, indent=4, ensure_ascii=False))
调用成功后,将会返回例如以下示例的文件转写结果。
{
"file_url": "https://dashscope.oss-cn-beijing.aliyuncs.com/samples/audio/paraformer/hello_world.wav",
"properties": {
"audio_format": "pcm_s16le",
"channels": [
0
],
"original_sampling_rate": 16000,
"original_duration": 4087
},
"transcripts": [
{
"channel_id": 0,
"content_duration": 3840,
"text": "Hello, world, 来自阿里巴巴达摩院语音实验室。",
"sentences": [
{
"begin_time": 0,
"end_time": 3840,
"text": "Hello, world, 来自阿里巴巴达摩院语音实验室。",
"words": [
{
"begin_time": 0,
"end_time": 540,
"text": "Hello",
"punctuation": ", "
},
{
"begin_time": 540,
"end_time": 1260,
"text": "world",
"punctuation": ", "
},
{
"begin_time": 1260,
"end_time": 1440,
"text": "来",
"punctuation": ""
},
{
"begin_time": 1440,
"end_time": 1660,
"text": "自",
"punctuation": ""
},
{
"begin_time": 1660,
"end_time": 1800,
"text": "阿",
"punctuation": ""
},
{
"begin_time": 1800,
"end_time": 2020,
"text": "里",
"punctuation": ""
},
{
"begin_time": 2020,
"end_time": 2200,
"text": "巴",
"punctuation": ""
},
{
"begin_time": 2200,
"end_time": 2400,
"text": "巴",
"punctuation": ""
},
{
"begin_time": 2400,
"end_time": 2580,
"text": "达",
"punctuation": ""
},
{
"begin_time": 2580,
"end_time": 2760,
"text": "摩",
"punctuation": ""
},
{
"begin_time": 2760,
"end_time": 3000,
"text": "院",
"punctuation": ""
},
{
"begin_time": 3000,
"end_time": 3180,
"text": "语",
"punctuation": ""
},
{
"begin_time": 3180,
"end_time": 3400,
"text": "音",
"punctuation": ""
},
{
"begin_time": 3400,
"end_time": 3580,
"text": "实",
"punctuation": ""
},
{
"begin_time": 3580,
"end_time": 3720,
"text": "验",
"punctuation": ""
},
{
"begin_time": 3720,
"end_time": 3840,
"text": "室",
"punctuation": "。"
}
]
}
]
}
]
}
了解更多
更多关于Paraformer语音识别的调用说明,请参见API详情。
文档内容是否对您有帮助?