All Products
Search
Document Center

Python Demo

Last Updated: May 18, 2020

Note:

  • Read API reference before using the SDK.
  • The SDK in this topic applies only to Intelligent Speech Interaction V2.0 that Alibaba Cloud published on July 5, 2018. If you have activated an earlier version of Intelligent Speech Interaction, activate Intelligent Speech Interaction V2.0 before using the SDK.

SDK instructions

The Python demo of the recording file recognition service uses the CommonRequest method of the Alibaba Cloud SDK for Python to send pctowap open platform (POP) API requests in a remote procedure call (RPC) style. You can send a recording file recognition request and query the recording file recognition result. For more information about the Alibaba Cloud SDK for Python, see Use Python SDK in Python SDK Developer Guide. For more information about how to use the CommonRequest method of the Python SDK, see Use CommonRequest in Python SDK Developer Guide.

SDK installation

You only need to install the core library of the Alibaba Cloud SDK for Python. The Alibaba Cloud SDK for Python supports Python 2.6.x, 2.7.x, 3.x, and later. You can run a pip command or download an SDK package from GitHub to install the Python SDK.

Note: You can run the following pip command to install the Python SDK V2.13.3:

  1. pip install aliyun-python-sdk-core==2.13.3

Note: In this topic, the latest SDK version for the recording file recognition service may be subject to change from time to time. You can continue to use the SDK version that you have installed.

Call procedure

  1. Create and initialize an AcsClient instance. Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account to authenticate the client. For more information about how to obtain your AccessKey information, see Activate Intelligent Speech Interaction in Quick Start.
  2. Create a recording file recognition request and set request parameters.
  3. Send the recording file recognition request and process the response returned by the server to obtain the task ID.
  4. Create a query request for the recording file recognition result and specify the task ID.
  5. Poll the recognition result.

Demo instructions

Note: The recording file in the demo is a pulse-code modulation (PCM) file sampled at 16 kHz. The demo uses a universal model in the console. If you use your own recording file, specify its audio coding format and audio sampling rate and select an appropriate model in the console. For more information about how to select a model, see Manage projects in Console User Guide.

nls-sample-16k.wav

Sample code

  1. # -*- coding: utf8 -*-
  2. import json
  3. import time
  4. from aliyunsdkcore.acs_exception.exceptions import ClientException
  5. from aliyunsdkcore.acs_exception.exceptions import ServerException
  6. from aliyunsdkcore.client import AcsClient
  7. from aliyunsdkcore.request import CommonRequest
  8. def fileTrans(akId, akSecret, appKey, fileLink) :
  9. # The constant parameters, such as the region ID. Do not modify their values.
  10. REGION_ID = "ap-southeast-1"
  11. PRODUCT = "nls-filetrans"
  12. DOMAIN = "filetrans.ap-southeast-1.aliyuncs.com"
  13. API_VERSION = "2019-08-23"
  14. POST_REQUEST_ACTION = "SubmitTask"
  15. GET_REQUEST_ACTION = "GetTaskResult"
  16. # The keys of request parameters.
  17. KEY_APP_KEY = "appkey"
  18. KEY_FILE_LINK = "file_link"
  19. KEY_VERSION = "version"
  20. KEY_ENABLE_WORDS = "enable_words"
  21. # The key that specifies whether to enable automatic track splitting.
  22. KEY_AUTO_SPLIT = "auto_split"
  23. # The keys of response parameters.
  24. KEY_TASK = "Task"
  25. KEY_TASK_ID = "TaskId"
  26. KEY_STATUS_TEXT = "StatusText"
  27. KEY_RESULT = "Result"
  28. # The status values.
  29. STATUS_SUCCESS = "SUCCESS"
  30. STATUS_RUNNING = "RUNNING"
  31. STATUS_QUEUEING = "QUEUEING"
  32. # Create an AcsClient instance.
  33. client = AcsClient(akId, akSecret, REGION_ID)
  34. # Create and send a recording file recognition request.
  35. postRequest = CommonRequest()
  36. postRequest.set_domain(DOMAIN)
  37. postRequest.set_version(API_VERSION)
  38. postRequest.set_product(PRODUCT)
  39. postRequest.set_action_name(POST_REQUEST_ACTION)
  40. postRequest.set_method('POST')
  41. # Specify the version of the recording file recognition service. If you are a new user, set this parameter to 4.0. If you use the default version 2.0, comment out this parameter.
  42. # Specify whether to return the recognition results of words. Default value: false. This parameter takes effect only when the version of the recording file recognition service is 4.0.
  43. task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_VERSION : "4.0", KEY_ENABLE_WORDS : False}
  44. # Specify whether to enable automatic track splitting. You can set the KEY_AUTO_SPLIT parameter to True to enable automatic track splitting.
  45. # task = {KEY_APP_KEY : appKey, KEY_FILE_LINK : fileLink, KEY_VERSION : "4.0", KEY_ENABLE_WORDS : False, KEY_AUTO_SPLIT : True}
  46. task = json.dumps(task)
  47. print(task)
  48. postRequest.add_body_params(KEY_TASK, task)
  49. taskId = ""
  50. try :
  51. postResponse = client.do_action_with_exception(postRequest)
  52. postResponse = json.loads(postResponse)
  53. print (postResponse)
  54. statusText = postResponse[KEY_STATUS_TEXT]
  55. if statusText == STATUS_SUCCESS :
  56. print ("The recording file recognition request is successful.")
  57. taskId = postResponse[KEY_TASK_ID]
  58. else :
  59. print ("The recording file recognition request fails.")
  60. return
  61. except ServerException as e:
  62. print (e)
  63. except ClientException as e:
  64. print (e)
  65. # Create a CommonRequest object and specify the task ID.
  66. getRequest = CommonRequest()
  67. getRequest.set_domain(DOMAIN)
  68. getRequest.set_version(API_VERSION)
  69. getRequest.set_product(PRODUCT)
  70. getRequest.set_action_name(GET_REQUEST_ACTION)
  71. getRequest.set_method('GET')
  72. getRequest.add_query_param(KEY_TASK_ID, taskId)
  73. # Send the query request for the recording file recognition result.
  74. # Use the polling method to query the recognition result. The polling runs until the status message that the server returns is SUCCESS, SUCCESS_WITH_NO_VALID_FRAGMENT,
  75. # or an error message.
  76. statusText = ""
  77. while True :
  78. try :
  79. getResponse = client.do_action_with_exception(getRequest)
  80. getResponse = json.loads(getResponse)
  81. print (getResponse)
  82. statusText = getResponse[KEY_STATUS_TEXT]
  83. if statusText == STATUS_RUNNING or statusText == STATUS_QUEUEING :
  84. # Continue the polling.
  85. time.sleep(10)
  86. else :
  87. # End the polling.
  88. break
  89. except ServerException as e:
  90. print (e)
  91. except ClientException as e:
  92. print (e)
  93. if statusText == STATUS_SUCCESS :
  94. print ("The recording file is recognized.")
  95. else :
  96. print ("Failed to recognize the recording file.")
  97. return
  98. accessKeyId = "Your AccessKey ID"
  99. accessKeySecret = "Your AccessKey secret"
  100. appKey = "Your appkey"
  101. fileLink = "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav"
  102. # Start the recording file recognition task.
  103. fileTrans(accessKeyId, accessKeySecret, appKey, fileLink)