This topic describes how to use the Python SDK from Alibaba Cloud Intelligent Speech Interaction and provides installation instructions and code examples.
Prerequisites
Before you use the SDK, read the API reference. For more information, see API reference.
You have activated Intelligent Speech Interaction and obtained an AccessKey ID and an AccessKey secret. For more information, see Getting started.
SDK description
The Python example for audio file transcription uses the CommonRequest feature of the Alibaba Cloud Python SDK to submit transcription requests and query results. This example uses the RPC style for POP API calls.
For more information about how to use the Alibaba Cloud Python SDK, see Use the Python SDK.
For more information about how to use CommonRequest in the Python SDK, see Make calls using CommonRequest.
Install the SDK
To run the Python example for audio file transcription, you need to install only the core library of the Alibaba Cloud Python SDK.
The Alibaba Cloud Python SDK supports the following Python versions and can be installed using pip or from GitHub.
Python 2.6 or later
Python 2.7 or later
Python 3 or later
Run the following command to install version 2.13.3 of the Python SDK using pip. This is the recommended installation method.
pip install aliyun-python-sdk-core==2.13.3Procedure
Create and initialize an AcsClient instance.
Create an audio file transcription request and set the request parameters.
Submit the audio file transcription request, process the server-side response, and retrieve the task ID.
Create a request to query the transcription result. Set the query parameter to the task ID.
Poll for the transcription result.
Sample code
Download nls-sample-16k.wav. The audio file used in the example is in the PCM format with a sample rate of 16000 Hz. The model is set to the general-purpose model in the console. If you use a different audio file, you must specify the correct encoding format and sample rate, and set the corresponding model in the console. For more information about model settings, see Manage projects.
Before you call the API, configure environment variables for your access credentials. The SDK reads your AccessKey ID, AccessKey secret, and AppKey from the ALIYUN_AK_ID, ALIYUN_AK_SECRET, and NLS_APP_KEY environment variables, respectively.
# -*- coding: utf-8 -*-
import json
import time
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
def fileTrans(akId, akSecret, appKey, fileLink) :
# The region ID. This is a static field.
REGION_ID = "cn-shanghai"
PRODUCT = "SpeechFileTranscriberLite"
DOMAIN = "speechfiletranscriberlite.cn-shanghai.aliyuncs.com"
API_VERSION = "2021-12-21"
POST_REQUEST_ACTION = "SubmitTask"
GET_REQUEST_ACTION = "GetTaskResult"
# Request parameters
KEY_APP_KEY = "appkey"
KEY_FILE_LINK = "file_link"
KEY_VERSION = "version"
KEY_ENABLE_WORDS = "enable_words"
# Specifies whether to enable intelligent channel splitting.
KEY_AUTO_SPLIT = "auto_split"
# Response parameters
KEY_TASK = "Task"
KEY_TASK_ID = "TaskId"
KEY_STATUS_TEXT = "StatusText"
KEY_RESULT = "Result"
# Status values
STATUS_SUCCESS = "SUCCESS"
STATUS_RUNNING = "RUNNING"
STATUS_QUEUEING = "QUEUEING"
# Create an AcsClient instance.
client = AcsClient(akId, akSecret, REGION_ID)
# Submit the audio file transcription request.
postRequest = CommonRequest()
postRequest.set_domain(DOMAIN)
postRequest.set_version(API_VERSION)
postRequest.set_product(PRODUCT)
postRequest.set_action_name(POST_REQUEST_ACTION)
postRequest.set_method('POST')
task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_ENABLE_WORDS : False}
# To enable intelligent channel splitting, set KEY_AUTO_SPLIT to True in the task.
# task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_ENABLE_WORDS : False, KEY_AUTO_SPLIT : True}
task = json.dumps(task)
print(task)
postRequest.add_body_params(KEY_TASK, task)
taskId = ""
try :
postResponse = client.do_action_with_exception(postRequest)
postResponse = json.loads(postResponse)
print (postResponse)
statusText = postResponse[KEY_STATUS_TEXT]
if statusText == STATUS_SUCCESS :
print ("The audio file transcription request received a successful response.")
taskId = postResponse[KEY_TASK_ID]
else :
print ("The audio file transcription request failed.")
return
except ServerException as e:
print (e)
except ClientException as e:
print (e)
# Create a CommonRequest and set the task ID.
getRequest = CommonRequest()
getRequest.set_domain(DOMAIN)
getRequest.set_version(API_VERSION)
getRequest.set_product(PRODUCT)
getRequest.set_action_name(GET_REQUEST_ACTION)
getRequest.set_method('GET')
getRequest.add_query_param(KEY_TASK_ID, taskId)
# Submit the request to query the audio file transcription result.
# Poll for the transcription result. Stop polling when the server returns a status descriptor of "SUCCESS", "SUCCESS_WITH_NO_VALID_FRAGMENT",
# or an error.
statusText = ""
while True :
try :
getResponse = client.do_action_with_exception(getRequest)
getResponse = json.loads(getResponse)
print (getResponse)
statusText = getResponse[KEY_STATUS_TEXT]
if statusText == STATUS_RUNNING or statusText == STATUS_QUEUEING :
# Continue polling.
time.sleep(10)
else :
# Stop polling.
break
except ServerException as e:
print (e)
except ClientException as e:
print (e)
if statusText == STATUS_SUCCESS :
print ("Audio file transcription succeeded.")
else :
print ("Audio file transcription failed.")
return
accessKeyId = os.getenv('ALIYUN_AK_ID')
accessKeySecret = os.getenv('ALIYUN_AK_SECRET')
appKey = os.getenv('NLS_APP_KEY')
fileLink = "https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav"
# Run the audio file transcription.
fileTrans(accessKeyId, accessKeySecret, appKey, fileLink)