All Products
Search
Document Center

Specify a custom model through the SDK 2.0

Last Updated: May 22, 2020

Specify a custom model trained by using POP API operations in the SDK

To create a custom model in the console, click Custom Models in the left-side navigation pane, and then click Create model. If you want to use a custom model for a project in the console, go to the Project Settings page of the project, click Switch Model, select the specified custom model, and then click Publish. You do not need to specify the custom model in the code because the model is bound to the appkey of the project. If you want to use a custom model trained by the corresponding pctowap open platform (POP) API operation, you need to call the corresponding SDK method to specify the ID of the custom model. This topic describes how to configure custom models for the short sentence recognition, real-time speech recognition, and recording file recognition services.

Short sentence recognition

In the short sentence recognition service, you need to specify the advanced parameter customization_id to specify the ID of the custom model.

Java SDK 2.0

For more information about how to use the Java SDK, see Java SDK 2.0 for short sentence recognition. The SDK does not provide a method to set the customization_id parameter. You can set the parameter through the addCustomedParam method of the SpeechRecognizer class.

  1. public void addCustomedParam(String key, Object value);

In this demo, call the addCustomedParam method to specify the custom model after creating the recognizer object of the SpeechRecognizer class and before calling the start method.

  1. SpeechRecognizer recognizer = new SpeechRecognizer(client, getRecognizerListener());
  2. ······· // Ignore other configurations.
  3. recognizer.addCustomedParam("customization_id", "The ID of your custom model");
  4. ·······

iOS SDK 2.0

For more information about how to use the iOS SDK, see iOS SDK 2.0 for short sentence recognition.You can call the setParams method of the RequestParam object to specify the ID of the custom model.

  1. ......
  2. NSMutableDictionary *userParams = [[NSMutableDictionary alloc]init];
  3. [userParams setValue:@"The ID of your custom model" forKey:@"customization_id"];
  4. [_recognizeRequestParam setParams:userParams];
  5. ......

Android SDK 2.0

For more information about how to use the Android SDK, see Android SDK 2.0 for short sentence recognition.You can call the setParams method of the SpeechRecognizer object to specify the ID of the custom model.

  1. ......
  2. String userParamString = "{\"customization_id\":\"The ID of your custom model\"}";
  3. speechRecognizer.setParams(userParamString);

RESTful API 2.0

For more information about how to use the RESTful API, see RESTful API 2.0 for short sentence recognition. You can add the customization_id parameter to other request parameters as an HTTP request parameter. The following Java demo shows how to set the request parameters:

  1. String url = "http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr";
  2. String request = url;
  3. request = request + "?appkey=" + appkey;
  4. request = request + "&customization_id=" + "The ID of your custom model";
  5. ·····

The procedure for setting the customization_id parameter in the demos of other programming languages is the same as that in the Java demo.

Real-time speech recognition

In the real-time speech recognition service, you need to specify the advanced parameter customization_id to specify the ID of the custom model.

Java SDK

For more information about how to use the Java SDK, see Java SDK 2.0 for real-time speech recognition. The SDK does not provide a method to set the customization_id parameter. You can set the parameter through the addCustomedParam method of the SpeechTranscriber class.

  1. public void addCustomedParam(String key, Object value);

In this demo, call the addCustomedParam method to specify the custom model after creating the transcriber object of the SpeechTranscriber class and before calling the start method.

  1. SpeechTranscriber transcriber = new SpeechTranscriber(client, getTranscriberListener());
  2. ······· // Ignore other configurations.
  3. transcriber.addCustomedParam("customization_id", "The ID of your custom model");
  4. ······

iOS SDK 2.0

For more information about how to use the iOS SDK, see iOS SDK 2.0 for real-time speech recognition.You can call the setParams method of the RequestParam object to specify the ID of the custom model.

  1. ......
  2. NSMutableDictionary *userParams = [[NSMutableDictionary alloc]init];
  3. [userParams setValue:@"The ID of your custom model" forKey:@"customization_id"];
  4. [_transRequestParam setParams:userParams];
  5. ......

Android SDK 2.0

For more information about how to use the Android SDK, see Android SDK 2.0 for real-time speech recognition.You can call the setParams method of the SpeechTranscriber object to specify the ID of the custom model.

  1. ......
  2. String userParamString = "{\"customization_id\":\"The ID of your custom model\"}";
  3. speechTranscriber.setParams(userParamString);

Recording file recognition

In the recording file recognition service, you need to specify the advanced parameter customization_id to specify the ID of the custom model.

For more information about how to use the recording file recognition service, see the API description of the recording file recognition service. Same as other request parameters, the customization_id parameter can be set in JSON format to the body of the HTTP request.

  1. {
  2. "app_key": "The appkey of your project",
  3. "customization_id": "The ID of your custom model",
  4. "file_link": "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav"
  5. }

The following Java demo shows how to set the request parameters:

  1. CommonRequest postRequest = new CommonRequest();
  2. ······· // Ignore other configurations.
  3. /**
  4. * Set parameters for the recording file recognition request and add request parameters to the request body as a JSON-formatted string.
  5. */
  6. JSONObject taskObject = new JSONObject();
  7. // The appkey of the project.
  8. taskObject.put(KEY_APP_KEY, appKey);
  9. // The URL of the recording file to be recognized.
  10. taskObject.put(KEY_FILE_LINK, "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav");
  11. taskObject.put("customization_id", "The ID of your custom model");
  12. String task = taskObject.toJSONString();
  13. System.out.println(task);
  14. // The request body, which includes request parameters in a JSON-formatted string.
  15. postRequest.putBodyParameter(KEY_TASK, task);
  16. ·······

The procedure for setting the customization_id parameter in the demos of other programming languages is the same as that in the Java demo.