All Products
Search
Document Center

Create a custom model through the POP API

Last Updated: Jun 18, 2020

You can train custom models on your client, rather than through the console settings, by calling the following pctowap open platform (POP) API operations:

  • Training corpus management
    • POP API operation for creating training corpora: CreateAsrLmData
    • POP API operation for querying training corpora: GetAsrLmData
    • POP API operation for deleting training corpora: DeleteAsrLmData
    • POP API operation for listing training corpora: ListAsrLmData
  • Custom model management
    • POP API operation for creating custom models: CreateAsrLmModel
    • POP API operation for querying custom models: GetAsrLmModel
    • POP API operation for deleting custom models: DeleteAsrLmModel
    • POP API operation for listing custom models: ListAsrLmModel
  • Custom model training and publishing
    • POP API operation for adding training corpora to custom models: AddDataToAsrLmModel
    • POP API operation for deleting training corpora from custom models: RemoveDataFromAsrLmModel
    • POP API operation for training custom models: TrainAsrLmModel
    • POP API operation for publishing custom models: DeployAsrLmModel
    • POP API operation for unpublishing custom models: UndeployAsrLmModel

Upgrade a speech recognition service to the business edition

To call the corresponding POP API operation of the customization platform to train custom models for a speech recognition service, you need to upgrade the speech recognition service to the business edition in the console. Click Service and Purchase in the left-side navigation pane, click the Speech Recognition tab, and then upgrade the corresponding service to the business edition. For more information, see Create custom models in the console.

Note:

  • After the speech recognition service is upgraded to the business edition, you need to purchase an appropriate number of models based on the business requirements. The number of custom models that can be created by using the corresponding POP API operation cannot exceed that of models purchased.
  • In the Models section on the Project Settings page in the console, change the model specified for a project to the basic model of the custom model. For more information about basic models, see the Create a custom models section.

The following table lists the custom model service parameters.

Item Value
Region ID ap-southeast-1
Domain name nls-slp.ap-southeast-1.aliyuncs.com
Protocol type HTTPS
POP API version 2019-08-19

Prepare a training corpus

Prepare a training corpus according to the calling limits and optimization suggestions. For more information, see the training corpus description in the introduction of custom models.

Metric Description
Number of custom models You need to pay for custom models by quantity.
Number of training corpora A maximum of 100 training corpora can be created.
Size of a single training corpus The maximum size is 10 MB.
Number of training corpora used by a single custom model The fixed value is 10.
Effective time A custom model takes effect in 5 minutes if the size of its training corpora does not exceed 10 MB. The custom model takes effect in half an hour if the size of its training corpora exceeds 10 MB.

Train a model

Follow these steps to train a model:

  1. Prepare and import a training corpus to the customization platform.
  2. Create a custom model and add the training corpus to this model.
  3. Train the model with the training corpus and wait until the training is completed.
  • Status transition diagram of the training corpus

1

The following table describes the different statuses of the training corpus.

Status Description
Fetching The training corpus is being imported from the URL into the customization platform.
FetchingFailed An error occurs while copying the training corpus. Check whether the URL of the training corpus is correct. Only HTTP or HTTPS URLs of Alibaba Cloud OSS are supported. You can check whether a URL is valid by accessing the URL in a browser.
Ready The training corpus is imported.
  • Status transition diagram of the custom model

2

The following table describes the different statuses of the custom model.

Status Description
Empty The new custom model is not trained.
Training The custom model is being trained.
TrainingFailed The custom model fails to be trained.
Ready The custom model is unpublished.
Deploying The custom model is being published or unpublished.
Deployed The custom model is published.

API description

Manage the training corpora

A training corpus is the similar to one uploaded in the console.

Create a training corpus

  1. Operation:

CreateAsrLmData

  1. Request parameters:

When you submit the request for creating a training corpus, you need to specify the request parameters in the request body.

Parameter Type Description Required
Name String The name of the training corpus to be created. Yes
Url String The URL of the training corpus. The customization platform can access and download a training corpus only by accessing an HTTP or HTTPS URL of Alibaba Cloud OSS. Yes
Description String The description of the training corpus to be created. No
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "RequestId": "C71B7CAA-18D6-4012-AC3D-425BA1CB670C",
  3. "DataId": "9934e10f19044282825508cbc7c86af8"
  4. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.
  • The following table describes the parameters returned in JSON format.
Parameter Type Description
RequestId String The ID of the request.
DataId String The ID of the training corpus that was created. The ID is required for training models with the training corpus later.

Note: You need to check the status of the training corpus until it enters the Ready status, which indicates the training corpus is created.

Query a dataset

  1. Operation:

GetAsrLmData

  1. Request parameters:

When you submit the request for querying a training corpus, you need to specify the request parameters in the request body.

Parameter Type Description Required
DataId String The ID of the training corpus to be queried. Yes
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "Data": {
  3. "Name": "Test training corpus",
  4. "Status": "Ready",
  5. "Md5": "38fc072ac60796a84ce1a0b13f783cc8",
  6. "Description": "Training corpus created by calling the corresponding POP API operation",
  7. "Url": "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/SLP/SLPTest.txt",
  8. "CreateTime": "2019-02-11 14:40:35",
  9. "UpdateTime": "2019-02-11 14:40:35",
  10. "Id": "9934e10f19044282825508cbc7c86af8",
  11. "Size": 5991
  12. },
  13. "RequestId": "C88130E6-F3B5-4F3E-9BF5-9C617DDD3321"
  14. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.
  • The following table describes the parameters returned in JSON format.
Parameter Type Description
RequestId String The ID of the request.
Data Data object A data object contains the parameters in the following table.

The following table describes the parameters in a Data object.

Parameter Type Description
Id String The unique ID of the training corpus that was obtained. The ID was generated when the corresponding training corpus was created.
Name String The name of the training corpus that was obtained.
Description String The description of the training corpus that was obtained.
Size Integer The size of the training corpus that was obtained.
Md5 String The MD5 value of the training corpus that was obtained.
Url String The URL of the training corpus that was obtained.
Status String The status of the training corpus that was obtained. Valid values: Fetching, FetchingFailed, and Ready.
CreateTime String The time when the training corpus was created.
UpdateTime String The last time when the training corpus was created.
ErrorMessage String The error message returned when the value of Status is TrainingFailed.

Delete a training corpus

  1. Operation:

DeleteAsrLmData

  1. Request parameters:

When you submit the request for deleting a training corpus, you need to specify the request parameters in the request body.

Parameter Type Description Required
DataId String The ID of the training corpus that you want to delete. Yes
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "RequestId": "7130914d32a3441db06747523675d9ff"
  3. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.

Note: You can delete a training corpus only when it is in the Ready status.

List the training corpora

  1. Operation:

ListAsrLmData

  1. Request parameters:

When you submit the request for listing the training corpora, you need to specify the request parameters in the request body.

Parameter Type Description Required
PageNumber Integer The number of the page that contains the required corpora. The value is an integer greater than 0. Default value: 1. No
PageSize Integer The number of training corpora on each page. Valid values: 1 to 100. Default value: 10. No
ModelId String The ID of the specified model. You can obtain the training corpora used by this model. By default, all training corpora used by this model are listed. No
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "RequestId": "7130914d32a3441db06747523675d9ff",
  3. "Page": {
  4. "Content": [{
  5. "Id": "1b64bee9994749f2a67eadac6379f80c",
  6. "Name": "Sample training corpus",
  7. "Description": "A sample training corpus",
  8. "Size": 7777404,
  9. "Md5": "39326cf690e384735355a385ec1e7a00",
  10. "Url": "slp/tmp/demo-data-lm.txt",
  11. "Status": "Ready",
  12. "CreateTime": "2018-10-31 17:20:39",
  13. "UpdateTime": "2018-10-31 17:20:39"
  14. }],
  15. "TotalPages": 1,
  16. "TotalItems": 1,
  17. "PageNumber": 1,
  18. "PageSize": 10
  19. }
  20. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.
  • The following table describes the parameters returned in JSON format.
Parameter Type Description
RequestId String The ID of the request.
Page Page object A Page object contains the parameters in the following table. If the request parameter PageNumber is specified, the information about the training corpora on the specified page is returned. If the request parameter PageNumber is not specified, the information about the training corpora on the first page is returned.

The following table describes the parameters in a Page object.

Parameter Type Description
Content List< Data > The list of training corpora. The parameters involved are the same as those contained in a Data object.
TotalPages Integer The total number of pages.
TotalItems Integer The total number of training corpora.
PageNumber Integer The number of the page that contains the required training corpora. The value is the same as that of the corresponding request parameter.
PageSize Integer The number of training corpora on each page. The value is the same as that of the corresponding request parameter.

Manage custom models

Create a custom model

  1. Operation:

CreateAsrLmModel

  1. Request parameters:

When you submit the request for creating a custom model, you need to specify request parameters in the request body.

Parameter Type Description Required
Name String The name of the model to be created. Yes
BaseId String The ID of the basic model, which cannot be modified after the custom model is created. You need to set the current model to this basic model on the Project Settings page in the console. Yes
Description String The description of the model to be created. No

The following table lists the IDs of the basic models that can be used to train custom models. For more information about basic models, see the project models in the console.

Model ID
Universal speech recognition model (Mandarin Chinese, 16 kHz) universal
Speech recognition model for telephone customer services and quality check for phone calls (Mandarin Chinese, 8 kHz) customer_service_8k
Speech recognition model for the e-commerce field (Mandarin Chinese, 16 kHz) e_commerce
Speech recognition model for administration and court scenarios (Mandarin Chinese, 16 kHz) law_politics
Speech recognition model for telephone customer services and quality check for phone calls (Chinese dialects, 8 kHz) dialect_customer_service_8k
Speech recognition model for telephone customer services and quality check for phone calls (Cantonese, 8 kHz) cantonese_customer_service_8k
Speech recognition model (English, 16 kHz) english
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "ModelId": "dbb6b71ff3e54b45a600ee5157a27647",
  3. "RequestId": "945C59DF-B3D9-4F22-808E-76752FF3872F"
  4. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.
  • The following table describes the parameters returned in JSON format.
Parameter Type Description
RequestId String The ID of the request.
ModelId String The ID of the custom model that was created. The ID is required for training the model later.

Note: When the custom model is created, the value of Status changes to Empty.

Query a custom model

  1. Operation:

GetAsrLmModel

  1. Request parameters:

When you submit the request for querying a custom model, you need to specify request parameters in the request body.

Parameter Type Description Required
ModelId String The ID of the custom model that you want to obtain. Yes
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "Model": {
  3. "Name": "Test custom model",
  4. "Status": "Empty",
  5. "Description":"Test custom model description",
  6. "CreateTime": "2019-02-12 10:11:57",
  7. "UpdateTime": "2019-02-12 10:11:57",
  8. "Id": "dbb6b71ff3e54b45a600ee5157a27647",
  9. "BaseId": "common",
  10. "Size": 0
  11. },
  12. "RequestId": "6CE24FF7-B7C8-4B9F-B0EB-FE4AF20B0CCC"
  13. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.
  • The following table describes the parameters returned in JSON format.
Parameter Type Description
RequestId String The ID of the request.
Model Model object A Model object contains the parameters in the following table.

The following table describes the parameters in a Model object.

Parameter Type Description
Id String The unique ID of the custom model that was obtained. The ID was generated when the corresponding custom model was created
Name String The name of the model that was obtained.
Description String The description of the model that was obtained.
BaseId String The ID of the basic model.
Size Integer The size of the model that was obtained.
Status String The status of the model that was obtained. Valid values: Empty, Training, TrainingFailed, Ready, Deploying, and Deployed.
CreateTime String The time when the model was created.
UpdateTime String The last time when the model was updated.
ErrorMessage String The error message returned when the value of Status is TrainingFailed.

Delete a custom model

Note: Before you delete a custom model, make sure that the model is not being used. Otherwise, the model is invalid for the object being recognized.

  1. Operation:

DeleteAsrLmModel

  1. Request parameters:

When you submit the request for deleting a custom model, you need to specify request parameters in the request body.

Parameter Type Description Required
ModelId String The ID of the custom model that you want to delete. Yes
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "RequestId": "7130914d32a3441db06747523675d9ff"
  3. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.

Note: A custom model cannot be deleted when it is in the Training or Deploying status.

List custom models

  1. Operation:

ListAsrLmModel

  1. Request parameters:

When you submit the request for listing the custom models, you need to specify request parameters in the request body.

Parameter Type Description Required
PageNumber Integer The number of the page that contains the required models. The value is an integer greater than 0. Default value: 1. No
PageSize Integer The number of custom models on each page. Valid values: 1 to 100. Default value: 10. No
DataId String The ID of the specified training corpus. You can obtain the custom models that use this training corpus. By default, all custom models that use this training corpus are listed. No
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "RequestId": "7130914d32a3441db06747523675d9ff",
  3. "Page": {
  4. "Content": [{
  5. "Id": "demo-model",
  6. "Name": "Sample custom model",
  7. "Description": "A sample custom model",
  8. "Size": 0,
  9. "Status": "Empty",
  10. "CreateTime": "2018-11-01 17:05:21",
  11. "UpdateTime": "2018-11-01 17:05:21",
  12. "BaseId": "common"
  13. }],
  14. "TotalPages": 1,
  15. "TotalItems": 1,
  16. "PageNumber": 1,
  17. "PageSize": 10
  18. }
  19. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.
  • The following table describes the parameters returned in JSON format.
Parameter Type Description
RequestId String The ID of the request.
Page Page object A Page object contains the parameters in the following table. If the request parameter PageNumber is specified, the information about the custom models on the specified page is returned. If the request parameter PageNumber is not specified, the information about the custom models on the first page is returned.

The following table describes the parameters in a Page object.

Parameter Type Description
Content List< Model > The list of models. The parameters involved are the same as those contained in a Model object.
TotalPages Integer The total number of pages.
TotalItems Integer The total number of custom models.
PageNumber Integer The number of the page that contains the required custom models. The value is the same as that of the corresponding request parameter.
PageSize Integer The number of custom models on each page. The value is the same as that of the corresponding request parameter.

Train and publish custom models

Add a training corpus to a custom model

Note: Do not add a training corpus to a custom model repeatedly.

  1. Operation:

AddDataToAsrLmModel

  1. Request parameters:

When you submit the request for adding a training corpus to a custom model, you need to specify request parameters in the request body.

Parameter Type Description Required
ModelId String The ID of the custom model. Yes
DataId String The ID of the training corpus. Yes
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "RequestId": "9B232563-12C0-4242-AA27-C250E1BB45EB"
  3. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.

Note: The status of the added training corpus must be Ready. The custom model cannot be in the Training or Deploying status.

Delete a training corpus from a custom model

  1. Operation:

RemoveDataFromAsrLmModel

  1. Request parameters:

When you submit the request for deleting a training corpus from a custom model, you need to specify request parameters in the request body.

Parameter Type Description Required
ModelId String The ID of the custom model. Yes
DataId String The ID of the training corpus. Yes
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "RequestId": "7130914d32a3441db06747523675d9ff"
  3. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.

Note: A custom model cannot be deleted when it is in the Training or Deploying status.

Train a custom model

  1. Operation:

TrainAsrLmModel

  1. Request parameters:

When you submit the request for training a custom model, you need to specify request parameters in the request body.

Parameter Type Description Required
ModelId String The ID of the custom model. Yes
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "RequestId": "3D922A91-68AA-4260-AFE4-C429832F9389"
  3. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.

Note:

  • A custom model in the Deploying status cannot be trained.
  • After a custom model is trained, it will be automatically published and enters the Deployed status.

Publish a custom model

  1. Operation:

DeployAsrLmModel

  1. Request parameters:

When you submit the request for bringing a custom model online, you need to specify request parameters in the request body.

Parameter Type Description Required
ModelId String The ID of the custom model. Yes
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "RequestId": "D9DDA978-5D68-45A4-B840-E4BC45C7497B"
  3. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.

Note: Only custom models in the Ready status can be published.

Unpublish a custom model

Note: Before you unpublish a custom model, make sure that the model is not being used. Otherwise, the model is invalid for the object being recognized.

  1. Operation:

UndeployAsrLmModel

  1. Request parameters:

When you submit the request for bringing a custom model offline, you need to specify request parameters in the request body.

Parameter Type Description Required
ModelId String The ID of the custom model. Yes
  1. Response parameters:

The response body returned by the server is in JSON format.

  1. {
  2. "RequestId": "8417BA8E-2428-41D2-A849-396A08979990"
  3. }

Parameter description

  • The HTTP status code 200 indicates that the operation is successful. For more information about the error codes, see the Error codes section.

Note: Only custom models in the Deployed status can be unpublished.

Operations for different statuses

Note: In the following table, the value NO indicates that the operation is not allowed in the current status. The value - indicates that the operation is allowed in the current status.

Status/Operation Fetching FetchingFailed Ready Empty Training Training Failed Ready Deploying Deployed
CreateAsrLmData - - - - - - - - -
ListAsrLmData - - - - - - - - -
GetAsrLmData - - - - - - - - -
DeleteAsrLmData NO NO - - NO - - - -
CreateAsrLmModel - - - - - - - - -
ListAsrLmModel - - - - - - - - -
GetAsrLmModel - - - - - - - - -
DeleteAsrLmModel - - - - NO - - NO -
AddDataToAsrLmModel NO NO - - NO - - NO -
RemoveDataFromAsrLmModel - - - - NO - - - -
TrainAsrLmModel - - - - - - - NO -
DeployAsrLmModel - - - NO NO NO - NO NO
UndeployAsrLmModel - - - NO NO NO NO NO -

Error codes

An error message is contained in the body of the response from the server if an error occurs when one of the preceding operations is performed. You can locate the fault based on the error message.

Error code Description
SLP.ASR_MODEL_ERROR The error message returned because the custom model is faulty.
SLP.NOT_FOUND The error message returned because the ID is invalid and the specified resources cannot be found based on the ID.
SLP.PARAMETER_ERROR The error message returned because the parameters you set are invalid when you create resources.
SLP.EXCEED_LIMIT The error message returned because the number of the resources has reached the upper limit and a new resource cannot be created.

The following response takes the SLP.NOT_FOUND error as an example:

  1. {
  2. "RequestId": "E70F51F6-23E3-4681-B954-ABF32B899B41",
  3. "HostId": "nls-slp.ap-southeast-1.aliyuncs.com",
  4. "Code": "SLP.NOT_FOUND",
  5. "Message": "Model not found!"
  6. }
Parameter Type Description
RequestId String The ID of the request.
HostId String The domain name of the customization platform.
Code String The error code.
Message String The error message.

Demo code

Note:

  • A Java demo is provided. It uses the CommonRequest method of the Alibaba Cloud SDK for Java to send POP API requests in a remote procedure call (RPC) style. For more information about how to use the CommonRequest method of the Alibaba Cloud SDK for Java, see Use CommonRequest.
  • When using the SDK, you need to use your Alibaba Cloud account to authenticate the client for all API operations. The IAcsClient object is globally universal. You need to use the AccessKey ID and AccessKey secret of your Alibaba Cloud account to call the Alibaba Cloud SDK for Java and create a globally unique IAcsClient object. For more information about how to obtain your AccessKey information, see Activate Intelligent Speech Interaction in Quick Start.
  • The demo consists of the following classes:
    • AsrLmModelPopApiDemo: the POP API test demo including the main method. You can use this class to test the API operations and check the status during integration.
    • AsrLmData: the training corpus class, which encapsulates the object data and API operations relevant to training corpora.
    • AsrLmModel: the custom model class, which encapsulates the object data and API operations relevant to custom models.

Add Java dependencies: You only need to add dependencies on the core library of the Alibaba Cloud SDK for Java and the Alibaba open source library fastjson. The core library version of the Alibaba Cloud SDK for Java can be V3.5.0 to V3.7.X.

  1. <dependency>
  2. <groupId>com.aliyun</groupId>
  3. <artifactId>aliyun-java-sdk-core</artifactId>
  4. <version>3.7.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.alibaba</groupId>
  8. <artifactId>fastjson</artifactId>
  9. <version>1.2.49</version>
  10. </dependency>

AsrLmModelPopApiDemo:

  1. import com.alibaba.fastjson.JSONObject;
  2. import com.aliyuncs.DefaultAcsClient;
  3. import com.aliyuncs.IAcsClient;
  4. import com.aliyuncs.profile.DefaultProfile;
  5. public class AsrLmModelPopApiDemo {
  6. private static String REGION = "ap-southeast-1";
  7. private static final String STATUS_FETCHING = "Fetching";
  8. private static final String STATUS_FETCHINGFAILED = "FetchingFailed";
  9. private static final String STATUS_READY = "Ready";
  10. private static final String STATUS_EMPTY = "Empty";
  11. private static final String STATUS_TRAINING = "Training";
  12. private static final String STATUS_TRAININGFAILED = "TrainingFailed";
  13. private static final String STATUS_DEPLOYING = "Deploying";
  14. private static final String STATUS_DEPLOYED = "Deployed";
  15. private static IAcsClient client;
  16. private AsrLmData asrLmData;
  17. private AsrLmModel asrLmModel;
  18. public AsrLmModelPopApiDemo(String akId, String akSecret) {
  19. DefaultProfile profile = DefaultProfile.getProfile(REGION, akId, akSecret);
  20. client = new DefaultAcsClient(profile);
  21. asrLmData = new AsrLmData(client);
  22. asrLmModel = new AsrLmModel(client);
  23. }
  24. /******************************* Manage training corpora *******************************/
  25. // Create a training corpus.
  26. public String createAsrLmData(String name, String fileUrl, String description) {
  27. String dataId = asrLmData.createAsrLmData(name, fileUrl, description);
  28. if (null == dataId) {
  29. return dataId;
  30. }
  31. // Check the status of the training corpus until it enters the Ready status, which indicates that the training corpus is imported.
  32. while (true) {
  33. AsrLmData.LmData data = asrLmData.getAsrLmData(dataId);
  34. if (null == data) {
  35. dataId = null;
  36. break;
  37. }
  38. if (data.Status.equals(STATUS_FETCHING)) {
  39. System.out.println("The training corpus is being imported to the custom model. dataId: " + dataId);
  40. try {
  41. Thread.sleep(100);
  42. } catch (InterruptedException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. else if (data.Status.equals(STATUS_FETCHINGFAILED)) {
  47. System.out.println(" An error occurs while copying the training corpus. dataId: " + dataId);
  48. asrLmData.deleteAsrLmData(dataId);
  49. dataId = null;
  50. break;
  51. }
  52. else if (data.Status.equals(STATUS_READY)) {
  53. System.out.println("The training corpus is imported. dataId: " + dataId);
  54. break;
  55. }
  56. }
  57. return dataId;
  58. }
  59. // Query a training corpus.
  60. public AsrLmData.LmData getAsrLmData(String dataId) {
  61. return asrLmData.getAsrLmData(dataId);
  62. }
  63. // Delete a training corpus.
  64. public boolean deleteAsrLmData(String dataId) {
  65. AsrLmData.LmData data = asrLmData.getAsrLmData(dataId);
  66. if (null == data) {
  67. return false;
  68. }
  69. if (!data.Status.equals(STATUS_READY)) {
  70. System.out.println("The training corpus cannot be deleted in the current status. status: " + data.Status);
  71. return false;
  72. }
  73. return asrLmData.deleteAsrLmData(dataId);
  74. }
  75. // List training corpora.
  76. public AsrLmData.LmDataPage listAsrLmData() {
  77. return asrLmData.listAsrLmData();
  78. }
  79. /******************************* Manage custom models *******************************/
  80. // Create a custom model.
  81. public String createAsrLmModel(String name, String baseId, String description) {
  82. String modelId = asrLmModel.createAsrLmModel(name, baseId, description);
  83. if (null == modelId) {
  84. return modelId;
  85. }
  86. // Check the status of the custom model until it enters the Empty status, which indicates that the custom model is created.
  87. while (true) {
  88. AsrLmModel.LmModel model = asrLmModel.getAsrLmModel(modelId);
  89. if (null == model) {
  90. modelId = null;
  91. break;
  92. }
  93. if (model.Status.equals(STATUS_EMPTY)) {
  94. break;
  95. }
  96. else {
  97. System.out.println("The custom model fails to be created. modelId: " + modelId);
  98. asrLmModel.deleteAsrLmModel(modelId);
  99. modelId = null;
  100. break;
  101. }
  102. }
  103. return modelId;
  104. }
  105. // Obtain a custom model.
  106. public AsrLmModel.LmModel getAsrLmModel(String modelId) {
  107. return asrLmModel.getAsrLmModel(modelId);
  108. }
  109. // Delete a custom model.
  110. public boolean deleteAsrLmModel(String modelId) {
  111. AsrLmModel.LmModel model = asrLmModel.getAsrLmModel(modelId);
  112. if (null == model) {
  113. return false;
  114. }
  115. if (model.Status.equals(STATUS_TRAINING) || model.Status.equals(STATUS_DEPLOYING)) {
  116. System.out.println("The custom model cannot be deleted in the current status. status: " + model.Status);
  117. return false;
  118. }
  119. return asrLmModel.deleteAsrLmModel(modelId);
  120. }
  121. // List custom models.
  122. public AsrLmModel.LmModelPage listAsrLmModel() {
  123. return asrLmModel.listAsrLmModel();
  124. }
  125. /**************************** Train and publish custom models ***************************/
  126. // Add a training corpus to a custom model.
  127. public boolean addDataToAsrLmModel(String dataId, String modelId) {
  128. AsrLmData.LmData data = asrLmData.getAsrLmData(dataId);
  129. if (null == data) {
  130. return false;
  131. }
  132. if (!data.Status.equals(STATUS_READY)) {
  133. System.out.println("The training corpus cannot be added to the custom model when the training corpus is in the current status. status: " + data.Status);
  134. return false;
  135. }
  136. AsrLmModel.LmModel model = asrLmModel.getAsrLmModel(modelId);
  137. if (null == model) {
  138. return false;
  139. }
  140. if (model.Status.equals(STATUS_TRAINING) || model.Status.equals(STATUS_DEPLOYING)) {
  141. System.out.println("The training corpus cannot be added to the custom model when the custom model is in the current status. status: " + model.Status);
  142. return false;
  143. }
  144. return asrLmModel.addDataToAsrLmModel(dataId, modelId);
  145. }
  146. // Delete a training corpus from a custom model.
  147. public boolean removeDataFromAsrLmModel(String dataId, String modelId) {
  148. // List the training corpora used by a specified custom model and determine whether the training corpora to be deleted have been added to this model.
  149. boolean isAdded = false;
  150. AsrLmData.LmDataPage page = asrLmData.listAsrLmData(1, 10, modelId);
  151. if (page != null && page.Content.size() > 0) {
  152. for (int i = 0; i < page.Content.size(); i++) {
  153. if (dataId.equals(page.Content.get(i).Id)) {
  154. isAdded = true;
  155. break;
  156. }
  157. }
  158. }
  159. if (!isAdded) {
  160. System.out.println("The training corpora to be deleted do not exist in the custom model.") ;
  161. return false;
  162. }
  163. // Check whether the training corpus can be deleted when the custom model is in the current status.
  164. AsrLmModel.LmModel model = asrLmModel.getAsrLmModel(modelId);
  165. if (null == model) {
  166. return false;
  167. }
  168. if (model.Status.equals(STATUS_TRAINING)) {
  169. System.out.println("The training corpus cannot be deleted when the custom model is in the current status. status: " + model.Status);
  170. return false;
  171. }
  172. return asrLmModel.removeDataFromAsrLmModel(dataId, modelId);
  173. }
  174. // Train a custom model.
  175. public boolean trainAsrLmModel(String modelId) {
  176. AsrLmModel.LmModel model = asrLmModel.getAsrLmModel(modelId);
  177. if (null == model) {
  178. return false;
  179. }
  180. if (model.Status.equals(STATUS_DEPLOYING)) {
  181. System.out.println("The custom model cannot be trained in the current status. status: " + model.Status);
  182. return false;
  183. }
  184. boolean isTrain = asrLmModel.trainAsrLmModel(modelId);
  185. if (!isTrain) {
  186. return isTrain;
  187. }
  188. // Check the status of the models until it enters the Deployed status, which indicates that the custom model is trained and published.
  189. while (true) {
  190. model = asrLmModel.getAsrLmModel(modelId);
  191. if (null == model) {
  192. isTrain = false;
  193. break;
  194. }
  195. if (model.Status.equals(STATUS_TRAINING) || model.Status.equals(STATUS_DEPLOYING)) {
  196. if (model.Status.equals(STATUS_TRAINING)) {
  197. System.out.println("The custom model is being trained. modelId: " + modelId);
  198. }
  199. else {
  200. System.out.println("The custom model is being published. modelId: " + modelId);
  201. }
  202. try {
  203. Thread.sleep(5000);
  204. } catch (InterruptedException e) {
  205. e.printStackTrace();
  206. }
  207. }
  208. else if (model.Status.equals(STATUS_TRAININGFAILED)) {
  209. System.out.println("The custom model fails to be trained. modelId: " + modelId);
  210. isTrain = false;
  211. break;
  212. }
  213. else if (model.Status.equals(STATUS_DEPLOYED)) {
  214. System.out.println("The custom model is trained and published. modeId: " + modelId);
  215. isTrain = true;
  216. break;
  217. }
  218. else {
  219. System.out.println("The custom model cannot be trained in the current status. Status: " + model.Status);
  220. isTrain = false;
  221. break;
  222. }
  223. }
  224. return isTrain;
  225. }
  226. // Publish a custom model.
  227. public boolean deployAsrLmModel(String modelId) {
  228. AsrLmModel.LmModel model = asrLmModel.getAsrLmModel(modelId);
  229. if (null == model) {
  230. return false;
  231. }
  232. if (!model.Status.equals(STATUS_READY)) {
  233. System.out.println("The custom model cannot be published in the current status. status: " + model.Status);
  234. return false;
  235. }
  236. boolean isDeployed = asrLmModel.deployAsrLmModel(modelId);
  237. if (!isDeployed) {
  238. return isDeployed;
  239. }
  240. // Check the status of the models until it enters the Deployed status, which indicates that the custom model is published.
  241. while (true) {
  242. model = asrLmModel.getAsrLmModel(modelId);
  243. if (null == model) {
  244. isDeployed = false;
  245. break;
  246. }
  247. if (model.Status.equals(STATUS_DEPLOYING)) {
  248. System.out.println("The custom model is being published. modelId: " + modelId);
  249. try {
  250. Thread.sleep(100);
  251. } catch (InterruptedException e) {
  252. e.printStackTrace();
  253. }
  254. }
  255. else if (model.Status.equals(STATUS_DEPLOYED)) {
  256. System.out.println("The custom model is published. modelId: " + modelId);
  257. isDeployed = true;
  258. break;
  259. }
  260. else {
  261. System.out.println("The custom model cannot be published in the current status. Status: " + model.Status);
  262. isDeployed = false;
  263. break;
  264. }
  265. }
  266. return isDeployed;
  267. }
  268. // Unpublish a custom model.
  269. public boolean undeployAsrLmModel(String modelId) {
  270. AsrLmModel.LmModel model = asrLmModel.getAsrLmModel(modelId);
  271. if (null == model) {
  272. return false;
  273. }
  274. if (!model.Status.equals(STATUS_DEPLOYED)) {
  275. System.out.println("The custom model cannot be unpublished in the current status. status: " + model.Status);
  276. return false;
  277. }
  278. boolean isUnDeployed = asrLmModel.undeployAsrLmModel(modelId);
  279. if (!isUnDeployed) {
  280. return isUnDeployed;
  281. }
  282. // Check the status of the custom model until it enters the Ready status, which indicates that the custom model is unpublished.
  283. while (true) {
  284. model = asrLmModel.getAsrLmModel(modelId);
  285. if (null == model) {
  286. isUnDeployed = false;
  287. break;
  288. }
  289. if (model.Status.equals(STATUS_DEPLOYING)) {
  290. System.out.println("The custom model is being unpublished. modelId: " + modelId);
  291. try {
  292. Thread.sleep(100);
  293. } catch (InterruptedException e) {
  294. e.printStackTrace();
  295. }
  296. }
  297. else if (model.Status.equals(STATUS_READY)) {
  298. System.out.println("The custom model is unpublished. modelId: " + modelId);
  299. isUnDeployed = true;
  300. break;
  301. }
  302. else {
  303. System.out.println("The custom model cannot be unpublished in the current status. Status: " + model.Status);
  304. isUnDeployed = false;
  305. break;
  306. }
  307. }
  308. return isUnDeployed;
  309. }
  310. public static void main(String[] args) {
  311. if (args.length < 2) {
  312. System.err.println("AsrLmModelPopApiDemo need params: <AccessKey Id> <AccessKey Secret>");
  313. return;
  314. }
  315. String accessKeyId = args[0];
  316. String accessKeySecret = args[1];
  317. AsrLmModelPopApiDemo demo = new AsrLmModelPopApiDemo(accessKeyId, accessKeySecret);
  318. /******************************* Manage training corpora *******************************/
  319. // The ID of the training corpus.
  320. String dataId;
  321. // Create a training corpus.
  322. String name = "Test training corpus";
  323. String fileUrl = "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/SLP/SLPTest.txt";
  324. String description = "Training corpus created by calling the corresponding POP API operation";
  325. dataId = demo.createAsrLmData(name, fileUrl, description);
  326. if (dataId != null) {
  327. System.out.println("The training corpus is created. dataId:" + dataId);
  328. }
  329. else {
  330. System.out.println("The training corpus fails to be created.") ;
  331. }
  332. // Query a training corpus.
  333. AsrLmData.LmData data = demo.getAsrLmData(dataId);
  334. if (data != null) {
  335. System.out.println("The training corpus is obtained:" + JSONObject.toJSONString(data));
  336. }
  337. else {
  338. System.out.println("The training corpus fails to be obtained.") ;
  339. }
  340. // List training corpora.
  341. AsrLmData.LmDataPage page = demo.listAsrLmData();
  342. if (page != null) {
  343. System.out.println("The training corpora are listed: " + JSONObject.toJSONString(page));
  344. }
  345. else {
  346. System.out.println("The training corpora fail to be listed.") ;
  347. return;
  348. }
  349. /************************** Manage custom models *********************************/
  350. // The ID of the custom model.
  351. String modelId;
  352. // Create a custom model.
  353. String modelName = "Test custom model";
  354. // Replace the universal model with a basic model based on business requirements.
  355. String baseId = "universal";
  356. String modelDescription = "Test custom model description";
  357. modelId = demo.createAsrLmModel(modelName, baseId, modelDescription);
  358. if (modelId != null) {
  359. System.out.println("The custom model is created. modelId:" + modelId);
  360. }
  361. else {
  362. System.out.println("The custom model fails to be created.") ;
  363. }
  364. // Query a custom model.
  365. AsrLmModel.LmModel model = demo.getAsrLmModel(modelId);
  366. if (model != null) {
  367. System.out.println("The custom model is obtained:" + JSONObject.toJSONString(model));
  368. }
  369. else {
  370. System.out.println("The custom model fails to be obtained.") ;
  371. }
  372. // List custom models.
  373. AsrLmModel.LmModelPage modelPage = demo.listAsrLmModel();
  374. if (modelPage != null) {
  375. System.out.println("The custom models are listed: " + JSONObject.toJSONString(modelPage));
  376. }
  377. else {
  378. System.out.println("The custom models fail to be listed.") ;
  379. }
  380. /******************************* Train and publish custom models *******************************/
  381. // Add a training corpus to a custom model.
  382. boolean isAdded = demo.addDataToAsrLmModel(dataId, modelId);
  383. if (isAdded) {
  384. System.out.println("The training corpus is added to the custom model.") ;
  385. }
  386. else {
  387. System.out.println("The training corpus fails to be added to the custom model.") ;
  388. }
  389. // Train a custom model.
  390. boolean isTrian = demo.trainAsrLmModel(modelId);
  391. if (isTrian) {
  392. System.out.println("The custom model is trained.") ;
  393. }
  394. else {
  395. System.out.println("The custom model fails to be trained.") ;
  396. }
  397. // Unpublish a custom model.
  398. boolean isUnDeployed = demo.undeployAsrLmModel(modelId);
  399. if (isUnDeployed) {
  400. System.out.println("The custom model is unpublished.") ;
  401. }
  402. else {
  403. System.out.println("The custom model fails to be unpublished.") ;
  404. }
  405. // Publish a custom model.
  406. boolean isDeployed = demo.deployAsrLmModel(modelId);
  407. if (isDeployed) {
  408. System.out.println("The custom model is published.") ;
  409. }
  410. else {
  411. System.out.println("The custom model fails to be published.") ;
  412. }
  413. /***************************** Clear training corpora and custom models based on your needs *****************************/
  414. // 1. Unpublish custom models. You can perform the operation based on your needs.
  415. isUnDeployed = demo.undeployAsrLmModel(modelId);
  416. if (isUnDeployed) {
  417. System.out.println("The custom model is unpublished.") ;
  418. }
  419. else {
  420. System.out.println("The custom model fails to be unpublished.") ;
  421. }
  422. //2. Delete a training corpus from a custom model. You can perform the operation based on your needs.
  423. boolean isDeleted = demo.removeDataFromAsrLmModel(dataId, modelId);
  424. if (isDeleted) {
  425. System.out.println("The training corpus is deleted from the custom model.") ;
  426. }
  427. else {
  428. System.out.println("The training corpus fails to be deleted from the custom model.") ;
  429. }
  430. // 3. Delete a training corpus. You can perform the operation based on your needs.
  431. boolean isDeletedData = demo.deleteAsrLmData(dataId);
  432. if (isDeletedData) {
  433. System.out.println("The training corpus is deleted.") ;
  434. }
  435. else {
  436. System.out.println("The training corpus fails to be deleted.") ;
  437. }
  438. // 4. Delete a custom model. You can perform the operation based on your needs.
  439. boolean isDeletedModel = demo.deleteAsrLmModel(modelId);
  440. if (isDeletedModel) {
  441. System.out.println("The custom model is deleted.") ;
  442. }
  443. else {
  444. System.out.println("The custom model fails to be deleted.") ;
  445. }
  446. }
  447. }

AsrLmData:

  1. import com.alibaba.fastjson.JSONObject;
  2. import com.aliyuncs.CommonRequest;
  3. import com.aliyuncs.CommonResponse;
  4. import com.aliyuncs.IAcsClient;
  5. import com.aliyuncs.exceptions.ClientException;
  6. import com.aliyuncs.http.MethodType;
  7. import com.aliyuncs.http.ProtocolType;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class AsrLmData {
  11. public static class LmData {
  12. public String Name;
  13. public String Status;
  14. public String Description;
  15. public String Url;
  16. public String CreateTime;
  17. public String UpdateTime;
  18. public String Id;
  19. public String ErrorMessage; // The error message returned only when the training corpus is in the FetchingFailed status.
  20. public int Size;
  21. }
  22. public static class LmDataPage {
  23. public int PageNumber;
  24. public int PageSize;
  25. public int TotalItems;
  26. public int TotalPages;
  27. public List<LmData> Content = new ArrayList<LmData>();
  28. }
  29. private static final String VERSION = "2018-11-20";
  30. private static final String DOMAIN = "nls-slp.ap-southeast-1.aliyuncs.com";
  31. private static ProtocolType PROTOCOL_TYPE = ProtocolType.HTTPS;
  32. private static final String KEY_NAME = "Name";
  33. private static final String KEY_URL = "Url";
  34. private static final String KEY_DESCRIPTION = "Description";
  35. private static final String KEY_DATA_ID = "DataId";
  36. private static final String KEY_DATA = "Data";
  37. private static final String KEY_PAGE = "Page";
  38. private static final String KEY_PAGE_NUMBER = "PageNumber";
  39. private static final String KEY_PAGE_SIZE = "PageSize";
  40. private static final String KEY_MODEL_ID = "ModelId";
  41. private IAcsClient client;
  42. private CommonRequest newRequest(String action) {
  43. CommonRequest request = new CommonRequest();
  44. request.setDomain(DOMAIN);
  45. request.setProtocol(PROTOCOL_TYPE);
  46. request.setVersion(VERSION);
  47. request.setMethod(MethodType.POST);
  48. request.setAction(action);
  49. return request;
  50. }
  51. public AsrLmData(IAcsClient client) {
  52. this.client = client;
  53. }
  54. /**
  55. * Create a training corpus.
  56. * @param name Required. The name of the training corpus to be created.
  57. * @param fileUrl Required. The download URL of the training corpus file. Ensure that the file can be downloaded on the server side.
  58. * @param description Optional. The description of the training corpus to be created.
  59. * @return String The ID of the created training corpus.
  60. */
  61. public String createAsrLmData(String name, String fileUrl, String description) {
  62. CommonRequest request = newRequest("CreateAsrLmData");
  63. request.putBodyParameter(KEY_NAME, name);
  64. request.putBodyParameter(KEY_URL, fileUrl);
  65. request.putBodyParameter(KEY_DESCRIPTION, description);
  66. CommonResponse response = null;
  67. try {
  68. response = client.getCommonResponse(request);
  69. } catch (ClientException e) {
  70. e.printStackTrace();
  71. }
  72. System.out.println("CreateAsrLmData: " + response.getData());
  73. if (response == null || response.getHttpStatus() != 200) {
  74. System.out.println(response.getData());
  75. System.out.println("The training corpus fails to be created. HTTP error code:" + response.getHttpStatus());
  76. return null;
  77. }
  78. JSONObject result = JSONObject.parseObject(response.getData());
  79. String dataId = result.getString(KEY_DATA_ID);
  80. return dataId;
  81. }
  82. /**
  83. * Query a training corpus.
  84. * @param dataId The ID of the training corpus that you want to obtain.
  85. * @return LmData The Data object of the obtained training corpus.
  86. */
  87. public LmData getAsrLmData(String dataId) {
  88. CommonRequest request = newRequest("GetAsrLmData");
  89. request.putBodyParameter(KEY_DATA_ID, dataId);
  90. CommonResponse response = null;
  91. try {
  92. response = client.getCommonResponse(request);
  93. } catch (ClientException e) {
  94. e.printStackTrace();
  95. }
  96. System.out.println("GetAsrLmData: " + response.getData());
  97. if (response == null || response.getHttpStatus() != 200) {
  98. System.out.println(response.getData());
  99. System.out.println("The training corpus fails to be obtained. HTTP error code:" + response.getHttpStatus());
  100. return null;
  101. }
  102. JSONObject result = JSONObject.parseObject(response.getData());
  103. String dataJson = result.getString(KEY_DATA);
  104. LmData data = JSONObject.parseObject(dataJson, LmData.class);
  105. return data;
  106. }
  107. /**
  108. * Delete a training corpus.
  109. * @param dataId Required. The ID of the training corpus to be deleted.
  110. * @return Indicates whether the training corpus is deleted.
  111. */
  112. public boolean deleteAsrLmData(String dataId) {
  113. CommonRequest request = newRequest("DeleteAsrLmData");
  114. request.putBodyParameter(KEY_DATA_ID, dataId);
  115. CommonResponse response = null;
  116. try {
  117. response = client.getCommonResponse(request);
  118. } catch (ClientException e) {
  119. e.printStackTrace();
  120. }
  121. System.out.println("DeleteAsrLmData: " + response.getData());
  122. if (response == null || response.getHttpStatus() != 200) {
  123. System.out.println(response.getData());
  124. System.out.println("The training corpus fails to be deleted. HTTP error code:" + response.getHttpStatus());
  125. return false;
  126. }
  127. return true;
  128. }
  129. /**
  130. * List training corpora.
  131. * @param pageNumber Optional. The number of the page that contains the required training corpora. The value is an integer greater than 0. Default value: 1.
  132. * @param pageSize Optional. The number of training corpora on each page. Valid values: 1 to 100. Default value: 10.
  133. * @param modelId Optional. The ID of the specified model. You can obtain the training corpora used by this model. By default, all training corpora used by this model are listed.
  134. * @return The information about the training corpora returned.
  135. */
  136. public LmDataPage listAsrLmData(int pageNumber, int pageSize, String modelId) {
  137. CommonRequest request = newRequest("ListAsrLmData");
  138. request.putBodyParameter(KEY_PAGE_NUMBER, pageNumber);
  139. request.putBodyParameter(KEY_PAGE_SIZE, pageSize);
  140. request.putBodyParameter(KEY_MODEL_ID, modelId);
  141. CommonResponse response = null;
  142. try {
  143. response = client.getCommonResponse(request);
  144. } catch (ClientException e) {
  145. e.printStackTrace();
  146. }
  147. System.out.println("ListAsrLmData: " + response.getData());
  148. if (response == null || response.getHttpStatus() != 200) {
  149. System.out.println(response.getData());
  150. System.out.println("The training corpora fail to be listed. HTTP error code: " + response.getHttpStatus());
  151. return null;
  152. }
  153. JSONObject result = JSONObject.parseObject(response.getData());
  154. String pageJson = result.getString(KEY_PAGE);
  155. LmDataPage page = JSONObject.parseObject(pageJson, LmDataPage.class);
  156. return page;
  157. }
  158. public LmDataPage listAsrLmData() {
  159. return listAsrLmData(1, 10, null);
  160. }
  161. }

AsrLmModel:

  1. import com.alibaba.fastjson.JSONObject;
  2. import com.aliyuncs.CommonRequest;
  3. import com.aliyuncs.CommonResponse;
  4. import com.aliyuncs.IAcsClient;
  5. import com.aliyuncs.exceptions.ClientException;
  6. import com.aliyuncs.http.MethodType;
  7. import com.aliyuncs.http.ProtocolType;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class AsrLmModel {
  11. public static class LmModel {
  12. public String Name;
  13. public String Status;
  14. public String Description;
  15. public String CreateTime;
  16. public String UpdateTime;
  17. public String Id;
  18. public String BaseId;
  19. public String ErrorMessage; // The error message returned only when the custom model is in the TrainingFailed status.
  20. public int Size;
  21. }
  22. public static class LmModelPage {
  23. public int PageNumber;
  24. public int PageSize;
  25. public int TotalItems;
  26. public int TotalPages;
  27. public List<LmModel> Content = new ArrayList<LmModel>();
  28. }
  29. private static final String VERSION = "2019-08-19";
  30. private static final String DOMAIN = "nls-slp.ap-southeast-1.aliyuncs.com";
  31. private static ProtocolType PROTOCOL_TYPE = ProtocolType.HTTPS;
  32. private static final String KEY_NAME = "Name";
  33. private static final String KEY_BASE_ID = "BaseId";
  34. private static final String KEY_DESCRIPTION = "Description";
  35. private static final String KEY_MODEL_ID = "ModelId";
  36. private static final String KEY_MODEL = "Model";
  37. private static final String KEY_PAGE = "Page";
  38. private static final String KEY_PAGE_NUMBER = "PageNumber";
  39. private static final String KEY_PAGE_SIZE = "PageSize";
  40. private static final String KEY_DATA_ID = "DataId";
  41. private IAcsClient client;
  42. private CommonRequest newRequest(String action) {
  43. CommonRequest request = new CommonRequest();
  44. request.setDomain(DOMAIN);
  45. request.setProtocol(PROTOCOL_TYPE);
  46. request.setVersion(VERSION);
  47. request.setMethod(MethodType.POST);
  48. request.setAction(action);
  49. return request;
  50. }
  51. public AsrLmModel(IAcsClient client) {
  52. this.client = client;
  53. }
  54. /**
  55. * Create a custom model.
  56. * @param name Required.The name of the custom model to be created.
  57. * @param baseId Required. The ID of the basic model. The ID cannot be modified after the custom model is created.
  58. * @param description Optional. The description of the custom model to be created.
  59. * @return The ID of the created custom model.
  60. */
  61. public String createAsrLmModel(String name, String baseId, String description) {
  62. CommonRequest request = newRequest("CreateAsrLmModel");
  63. request.putBodyParameter(KEY_NAME, name);
  64. request.putBodyParameter(KEY_BASE_ID, baseId);
  65. request.putBodyParameter(KEY_DESCRIPTION, description);
  66. CommonResponse response = null;
  67. try {
  68. response = client.getCommonResponse(request);
  69. } catch (ClientException e) {
  70. e.printStackTrace();
  71. }
  72. System.out.println("CreateAsrLmModel: " + response.getData());
  73. if (response == null || response.getHttpStatus() != 200) {
  74. System.out.println(response.getData());
  75. System.out.println("The custom model fails to be created. HTTP error code:" + response.getHttpStatus());
  76. return null;
  77. }
  78. JSONObject result = JSONObject.parseObject(response.getData());
  79. String modelId = result.getString(KEY_MODEL_ID);
  80. return modelId;
  81. }
  82. /**
  83. * Query a custom model.
  84. * @param modelId The ID of the custom model that you want to obtain.
  85. * @return The Model object of the custom model returned.
  86. */
  87. public LmModel getAsrLmModel(String modelId) {
  88. CommonRequest request = newRequest("GetAsrLmModel");
  89. request.putBodyParameter(KEY_MODEL_ID, modelId);
  90. CommonResponse response = null;
  91. try {
  92. response = client.getCommonResponse(request);
  93. } catch (ClientException e) {
  94. e.printStackTrace();
  95. }
  96. System.out.println("GetAsrLmModel: " + response.getData());
  97. if (response == null || response.getHttpStatus() != 200) {
  98. System.out.println(response.getData());
  99. System.out.println("The custom model fails to be obtained. HTTP error code: " + response.getHttpStatus());
  100. return null;
  101. }
  102. JSONObject result = JSONObject.parseObject(response.getData());
  103. String modelJson = result.getString(KEY_MODEL);
  104. LmModel model = JSONObject.parseObject(modelJson, LmModel.class);
  105. return model;
  106. }
  107. /**
  108. * Delete a custom model.
  109. * @param modelId The ID of the custom model to be deleted.
  110. * @return Indicates whether the custom model is deleted.
  111. */
  112. public boolean deleteAsrLmModel(String modelId) {
  113. CommonRequest request = newRequest("DeleteAsrLmModel");
  114. request.putBodyParameter(KEY_MODEL_ID, modelId);
  115. CommonResponse response = null;
  116. try {
  117. response = client.getCommonResponse(request);
  118. } catch (ClientException e) {
  119. e.printStackTrace();
  120. }
  121. System.out.println("DeleteAsrLmModel: " + response.getData());
  122. if (response == null || response.getHttpStatus() != 200) {
  123. System.out.println(response.getData());
  124. System.out.println("The custom model fails to be deleted. HTTP error code:" + response.getHttpStatus());
  125. return false;
  126. }
  127. return true;
  128. }
  129. /**
  130. * List custom models.
  131. * @param pageNumber Optional. The number of the page that contains the required custom models. The value is an integer greater than 0. Default value: 1.
  132. * @param pageSize Optional. The number of custom models on each page. Valid values: 1 to 100. Default value: 10.
  133. * @param dataId Optional. The ID of the specified training corpus. You can obtain the custom models that use this training corpus. By default, all custom models that use this training corpus are listed.
  134. * @return The description about the custom models returned.
  135. */
  136. public LmModelPage listAsrLmModel(int pageNumber, int pageSize, String dataId) {
  137. CommonRequest request = newRequest("ListAsrLmModel");
  138. request.putBodyParameter(KEY_PAGE_NUMBER, pageNumber);
  139. request.putBodyParameter(KEY_PAGE_SIZE, pageSize);
  140. request.putBodyParameter(KEY_DATA_ID, dataId);
  141. CommonResponse response = null;
  142. try {
  143. response = client.getCommonResponse(request);
  144. } catch (ClientException e) {
  145. e.printStackTrace();
  146. }
  147. System.out.println("ListAsrLmModel: " + response.getData());
  148. if (response == null || response.getHttpStatus() != 200) {
  149. System.out.println(response.getData());
  150. System.out.println("The custom models fail to be listed. HTTP error code: " + response.getHttpStatus());
  151. return null;
  152. }
  153. JSONObject result = JSONObject.parseObject(response.getData());
  154. String pageJson = result.getString(KEY_PAGE);
  155. LmModelPage page = JSONObject.parseObject(pageJson, LmModelPage.class);
  156. return page;
  157. }
  158. public LmModelPage listAsrLmModel() {
  159. return listAsrLmModel(1, 10, null);
  160. }
  161. /**
  162. * Add a training corpus to a custom model.
  163. * @param dataId The ID of the training corpus to be added to the custom model.
  164. * @param modelId The ID of the custom model.
  165. * @return Indicates whether the training corpus is added to the custom model.
  166. */
  167. public boolean addDataToAsrLmModel(String dataId, String modelId) {
  168. CommonRequest request = newRequest("AddDataToAsrLmModel");
  169. request.putBodyParameter(KEY_DATA_ID, dataId);
  170. request.putBodyParameter(KEY_MODEL_ID, modelId);
  171. CommonResponse response = null;
  172. try {
  173. response = client.getCommonResponse(request);
  174. } catch (ClientException e) {
  175. e.printStackTrace();
  176. }
  177. System.out.println("AddDataToAsrLmModel: " + response.getData());
  178. if (response == null || response.getHttpStatus() != 200) {
  179. System.out.println(response.getData());
  180. System.out.println("The training corpus fails to be added to the custom model. HTTP error code:" + response.getHttpStatus());
  181. return false;
  182. }
  183. return true;
  184. }
  185. /**
  186. * Delete a training corpus from a custom model.
  187. * @param dataId The ID of the training corpus to be deleted.
  188. * @param modelId The ID of the custom model.
  189. * @return Indicates whether the training corpus is deleted.
  190. */
  191. public boolean removeDataFromAsrLmModel(String dataId, String modelId) {
  192. CommonRequest request = newRequest("RemoveDataFromAsrLmModel");
  193. request.putBodyParameter(KEY_DATA_ID, dataId);
  194. request.putBodyParameter(KEY_MODEL_ID, modelId);
  195. CommonResponse response = null;
  196. try {
  197. response = client.getCommonResponse(request);
  198. } catch (ClientException e) {
  199. e.printStackTrace();
  200. }
  201. System.out.println("RemoveDataFromAsrLmModel: " + response.getData());
  202. if (response == null || response.getHttpStatus() != 200) {
  203. System.out.println(response.getData());
  204. System.out.println("The training corpus fails to be deleted from the custom model. HTTP error code:" + response.getHttpStatus());
  205. return false;
  206. }
  207. return true;
  208. }
  209. /**
  210. * Train a custom model.
  211. * @param modelId The ID of the custom model to be trained.
  212. * @return Indicates whether the custom model is trained.
  213. * */
  214. public boolean trainAsrLmModel(String modelId) {
  215. CommonRequest request = newRequest("TrainAsrLmModel");
  216. request.putBodyParameter(KEY_MODEL_ID, modelId);
  217. CommonResponse response = null;
  218. try {
  219. response = client.getCommonResponse(request);
  220. } catch (ClientException e) {
  221. e.printStackTrace();
  222. }
  223. System.out.println("TrainAsrLmModel: " + response.getData());
  224. if (response == null || response.getHttpStatus() != 200) {
  225. System.out.println(response.getData());
  226. System.out.println("The custom model fails to be trained. HTTP error code:" + response.getHttpStatus());
  227. return false;
  228. }
  229. return true;
  230. }
  231. /**
  232. * Publish a custom model.
  233. * @param modelId The ID of the custom model to be published.
  234. * @return Indicates whether the custom model is published.
  235. */
  236. public boolean deployAsrLmModel(String modelId) {
  237. CommonRequest request = newRequest("DeployAsrLmModel");
  238. request.putBodyParameter(KEY_MODEL_ID, modelId);
  239. CommonResponse response = null;
  240. try {
  241. response = client.getCommonResponse(request);
  242. } catch (ClientException e) {
  243. e.printStackTrace();
  244. }
  245. System.out.println("DeployAsrLmModel: " + response.getData());
  246. if (response == null || response.getHttpStatus() != 200) {
  247. System.out.println(response.getData());
  248. System.out.println("The custom model fails to be published. HTTP error code: " + response.getHttpStatus());
  249. return false;
  250. }
  251. return true;
  252. }
  253. /**
  254. * Unpublish a custom model.
  255. * @param modelId The ID of the custom model to be unpublished.
  256. * @return Indicates whether the custom model is unpublished.
  257. */
  258. public boolean undeployAsrLmModel(String modelId) {
  259. CommonRequest request = newRequest("UndeployAsrLmModel");
  260. request.putBodyParameter(KEY_MODEL_ID, modelId);
  261. CommonResponse response = null;
  262. try {
  263. response = client.getCommonResponse(request);
  264. } catch (ClientException e) {
  265. e.printStackTrace();
  266. }
  267. System.out.println("UndeployAsrLmModel: " + response.getData());
  268. if (response == null || response.getHttpStatus() != 200) {
  269. System.out.println(response.getData());
  270. System.out.println("The custom model fails to be unpublished. HTTP error code: " + response.getHttpStatus());
  271. return false;
  272. }
  273. return true;
  274. }
  275. }