Use an SDK 2.0 to set a custom model

更新时间:
复制 MD 格式

This topic describes how to use a POP (Platform-as-a-Service Open Platform) API to set a custom model in an SDK.

Train a custom model using a POP API

To use a custom model that you trained using a POP API, you must set its model ID in the SDK. This section describes how to set the custom model ID for short sentence recognition, real-time speech recognition, and audio file recognition.

Short sentence recognition

For short sentence recognition, set the customization_id advanced parameter to specify the custom model ID.

  • Java SDK

    Note

    For information about the basic usage, see Java SDK.

    The SDK does not provide a set method for the customization_id parameter. Instead, you can call the addCustomedParam method of the SpeechRecognizer class to set the parameter:

    public void addCustomedParam(String key, Object value);

    After you create the recognizer object of the SpeechRecognizer class and before you call the start method, call the addCustomedParam method to specify the ID of your custom linguistic model. For more information, see the following sample code:

    SpeechRecognizer recognizer = new SpeechRecognizer(client, getRecognizerListener());
    ...   // Other settings are omitted.
    recognizer.addCustomedParam("customization_id", "The ID of your custom linguistic model");

  • C++ SDK

    Note

    For information about the basic usage, see C++ SDK.

    The SDK does not provide a set method for the customization_id parameter. Instead, you can call the setPayloadParam method of the SpeechRecognizerRequest class to set the parameter:

    /**
     * @brief Set a parameter.
     * @param value A string in JSON map format.
     * @return A value of 0 is returned if the parameter is set. Otherwise, a value of -1 is returned.
     */
    int setPayloadParam(const char value);

    After you create the request object of the SpeechRecognizerRequest class and before you call the start method, call the setPayloadParam method to specify the ID of your custom linguistic model. For more information, see the following sample code:

    SpeechRecognizerRequest *request = NlsClient::getInstance()->createRecognizerRequest(callback);
    ...  // Other settings are omitted.
    request->setPayloadParam("{\"customization_id\":\"Your custom model ID\"}");

  • iOS SDK

    Note

    To get started, read the iOS SDK to understand its basic usage.

    You can call the setParams method of the RequestParam class to specify the ID of your custom linguistic model.

    NSMutableDictionary *userParams = [[NSMutableDictionary alloc]init];
    [userParams setValue:@"The ID of your custom linguistic model" forKey:@"customization_id"];
    [_recognizeRequestParam setParams:userParams];

  • Android SDK

    Note

    For information about the basic usage, see Android SDK.

    You can call the setParams method of the SpeechRecognizer class to specify the ID of your custom linguistic model.

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

  • RESTful API

    Note

    For information about the basic usage, see RESTful API.

    Append the customization_id parameter to the other HTTP request parameters. The following Java example shows how to do this. The method for setting the customization_id parameter is similar in other languages.

    String url = "http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr";
    String request = url;
    request = request + "?appkey=" + appkey;
    request = request + "&customization_id=" + "your custom model ID";

Real-time speech recognition

For real-time speech recognition, set the customization_id advanced parameter to specify the custom model ID.

  • Java SDK

    Note

    For information about the basic usage, see Java SDK.

    The SDK does not provide a set method for the customization_id parameter. Instead, you can call the addCustomedParam method of the SpeechTranscriber class to set the parameter:

    public void addCustomedParam(String key, Object value);

    After you create the transcriber object of the SpeechTranscriber class and before you call the start method, call the addCustomedParam method to specify the ID of your custom linguistic model. For more information, see the following sample code:

    SpeechTranscriber transcriber = new SpeechTranscriber(client, getTranscriberListener());
    ... // Other settings are omitted.
    transcriber.addCustomedParam("customization_id", "Your custom model ID");

  • C++ SDK

    Note

    For information about the basic usage, see C++ SDK.

    The SDK does not provide a set method for the customization_id parameter. Instead, you can call the setPayloadParam method of the SpeechTranscriberRequest class to set the parameter:

    /**
     * @brief Set a parameter.
     * @param value A string in JSON map format.
     * @return A value of 0 is returned if the parameter is set. Otherwise, a value of -1 is returned.
     */
    int setPayloadParam(const char value);

    After you create the request object of the SpeechTranscriberRequest class and before you call the start method, call the setPayloadParam method to specify the ID of your custom linguistic model. For more information, see the following sample code:

    SpeechTranscriberRequest* request = NlsClient::getInstance()->createTranscriberRequest(callback);
    ... // Other settings are omitted.
    request->setPayloadParam("{\"customization_id\":\"The ID of your custom linguistic model\"}");

  • iOS SDK

    Note

    For information about the basic usage of the iOS SDK, see iOS SDK.

    You can call the setParams method of the RequestParam class to specify the ID of your custom linguistic model.

    NSMutableDictionary *userParams = [[NSMutableDictionary alloc]init];
    [userParams setValue:@"The ID of your custom linguistic model" forKey:@"customization_id"];
    [_transRequestParam setParams:userParams];

  • Android SDK

    Note

    For information about the basic usage, see Android SDK.

    You can call the setParams method of the SpeechTranscriber class to specify the ID of your custom linguistic model.

    String userParamString = "{\"customization_id\":\"Your custom model ID\"}";
    speechTranscriber.setParams(userParamString);

Recording file recognition

For audio file recognition, set the customization_id advanced parameter to specify the custom model ID.

Note

Before you begin, make sure that you know how to use the recording file recognition API. For more information, see Overview.

Set the customization_id parameter in the HTTP request body as a JSON-formatted string, similar to other input parameters.

{
    "app_key": "yourAppkey",      // To obtain an AppKey, go to the console: https://nls-portal.console.aliyun.com/applist
    "customization_id": "Your custom model ID",
    "file_link": "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav"
}

The following Java SDK example shows how to set the customization_id parameter. The method is similar for other SDKs.

CommonRequest postRequest = new CommonRequest();
...  // Other settings are omitted.
/**
 * Set the file transcription request parameters in the request body as a JSON-formatted string.
 */
JSONObject taskObject = new JSONObject();
// Set the AppKey. To obtain an AppKey, go to the console: https://nls-portal.console.aliyun.com/applist
taskObject.put(KEY_APP_KEY, appKey);
// Set the URL of the audio file.
taskObject.put(KEY_FILE_LINK, "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav");
taskObject.put("customization_id", "Your custom model ID");
String task = taskObject.toJSONString();
System.out.println(task);
// Set the preceding JSON string as the Body parameter.
postRequest.putBodyParameter(KEY_TASK, task);