This topic describes how to quickly use the Alibaba Cloud Model Studio speech model service.
Prerequisites
You have activated the service and obtained an API key. For more information, see Obtain and configure an API key and Configure an API key as an environment variable.
You have installed the software development kit (SDK). For more information, see Install the SDK.
Sample code
The following example shows how to call the Paraformer speech recognition file transcription API to transcribe an audio file from a URL.
Replace
your-dashscope-api-keyin the example with your API key to run the code.The file specified by the URL cannot be larger than 2 GB.
The file_urls parameter supports multiple file URLs. This example demonstrates transcription for only a single file 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))A successful call returns a transcription result, as shown in the following example.
{
"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, from the Alibaba DAMO Academy Speech Lab.",
"sentences": [
{
"begin_time": 0,
"end_time": 3840,
"text": "Hello, world, from the Alibaba DAMO Academy Speech Lab.",
"words": [
{
"begin_time": 0,
"end_time": 540,
"text": "Hello",
"punctuation": ", "
},
{
"begin_time": 540,
"end_time": 1260,
"text": "world",
"punctuation": ", "
},
{
"begin_time": 1260,
"end_time": 1600,
"text": "from",
"punctuation": " "
},
{
"begin_time": 1600,
"end_time": 1800,
"text": "the",
"punctuation": " "
},
{
"begin_time": 1800,
"end_time": 2300,
"text": "Alibaba",
"punctuation": " "
},
{
"begin_time": 2300,
"end_time": 2700,
"text": "DAMO",
"punctuation": " "
},
{
"begin_time": 2700,
"end_time": 3200,
"text": "Academy",
"punctuation": " "
},
{
"begin_time": 3200,
"end_time": 3600,
"text": "Speech",
"punctuation": " "
},
{
"begin_time": 3600,
"end_time": 3840,
"text": "Lab",
"punctuation": "."
}
]
}
]
}
]
}Learn more
For more information about how to call the Paraformer speech recognition API, see API details.