This topic describes how to use the Python software development kit (SDK) for Alibaba Cloud Intelligent Speech Interaction and provides installation instructions and code examples.
Prerequisites
-
Before you use the SDK, review the API reference. For more information, see API reference.
-
You have activated Intelligent Speech Interaction and obtained an AccessKey ID and AccessKey secret. For more information, see Get started.
SDK description
-
The Python demo for audio file transcription uses the CommonRequest method of the Alibaba Cloud SDK for Python to submit transcription requests and retrieve the results. This method uses Remote Procedure Call (RPC)-style POP API calls.
-
For more information about using the Alibaba Cloud SDK for Python, see Use the Python SDK.
-
For more information about how to use the CommonRequest method of the Python SDK, see Generalized calls.
SDK installation
To run the Python demo for audio file transcription, you must install the core library of the Alibaba Cloud SDK for Python.
The Alibaba Cloud SDK for Python supports the following Python versions. You can install the SDK using pip or from GitHub.
-
Python 2.6 or later
-
Python 2.7 or later
-
Python 3 or later
We recommend that you install the SDK using pip:
Run the following command to install version 2.16.0 of the Python SDK:
pip install aliyun-python-sdk-core==2.16.0
Call procedure
-
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 transcription result query request and set the task ID as a query parameter.
-
Poll for the transcription result.
Sample code
-
Download nls-sample-16k.wav. The audio file used in the demo is in Pulse-Code Modulation (PCM) format with a 16,000 Hz sample rate. The model set in the console is the general-purpose model. If you use a different audio file, you must specify its 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 your access credentials as environment variables. The environment variables for the AccessKey ID, AccessKey secret, and AppKey of Intelligent Speech Interaction are ALIYUN_AK_ID, ALIYUN_AK_SECRET, and NLS_APP_KEY.
# -*- coding: utf8 -*-
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 = "nls-filetrans"
DOMAIN = "filetrans.cn-shanghai.aliyuncs.com"
API_VERSION = "2018-08-17"
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 an 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')
# If you are a new user, use version 4.0. If you are an existing user of version 2.0 (default) and want to continue using it, comment out this parameter setting.
# Specifies whether to return word-level information. The default value is false. To enable this feature, you must set the version to 4.0.
task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_VERSION : "4.0", 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_VERSION : "4.0", 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 was successfully responded to!")
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 object 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 a query request for the transcription result.
# Poll the transcription result until the status descriptor returned by the server is "SUCCESS" or "SUCCESS_WITH_NO_VALID_FRAGMENT",
# or an error descriptor. Then, stop polling.
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 ("The audio file was transcribed successfully!")
else :
print ("Failed to transcribe the audio file!")
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"
# Start the audio file transcription.
fileTrans(accessKeyId, accessKeySecret, appKey, fileLink)