语音交互服务 SIS-一句话识别Http接口:代码示例

时间:2023-11-20 11:21:53

代码示例

如下示例仅供参考,最新代码请前往获取SDK章节获取并运行。

import com.huawei.sis.bean.SisConfig;
import com.huawei.sis.bean.SisConstant;
import com.huawei.sis.bean.request.AsrCustomShortRequest;
import com.huawei.sis.bean.response.AsrCustomShortResponse;
import com.huawei.sis.bean.AuthInfo;
import com.huawei.sis.client.AsrCustomizationClient;
import com.huawei.sis.exception.SisException;
import com.huawei.sis.util.IOUtils;
import java.util.List;
import com.huawei.sis.util.JsonUtils;


/**
 * 一句话识别
 *
 * Copyright 2021 Huawei Technologies Co.,Ltd.
 */
public class AsrCustomizationDemo {
  private static final int SLEEP_TIME = 500;
  private static final int MAX_POLLING_NUMS = 1000;

  // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
  // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
  private String ak = System.getenv("HUAWEICLOUD_SDK_AK");
  private String sk = System.getenv("HUAWEICLOUD_SDK_SK");
  

  private String region = "";    // 区域,如cn-north-1、cn-north-4
  private String projectId = ""; // 项目id。登录管理控制台,鼠标移动到右上角的用户名上,在下拉列表中选择我的凭证,在项目列表中查看项目id。多项目时,展开“所属区域”,从“项目ID”列获取子项目ID。
  // 一句话识别参数
  private String path = "";             // 音频文件路径,如D:/test.wav等,sdk会将音频文件转化为base64编码
  private String pathAudioFormat = "";  // 文件格式,如wav等
  private String pathProperty = "chinese_16k_general";     // 属性字符串,language_sampleRate_domain, 16k模型推荐使用chinese_16k_general


  /**
   * 设置一句话识别参数,所有参数均有默认值,不配置也可使用
   *
   * @param request 一句话识别请求
   */
  private void setShortParameter(AsrCustomShortRequest request) {

    // 设置是否添加标点,默认是no
    request.setAddPunc("yes");
    // 设置是否将语音中的数字转写为阿拉伯数字,yes或no,默认yes
    request.setDigitNorm("no");
  }

  /**
   * 定义config,所有参数可选,设置超时时间等。
   *
   * @return SisConfig
   */
  private SisConfig getConfig() {
    SisConfig config = new SisConfig();
    // 设置连接超时,默认10000ms
    config.setConnectionTimeout(SisConstant.DEFAULT_CONNECTION_TIMEOUT);
    // 设置读取超时,默认10000ms
    config.setReadTimeout(SisConstant.DEFAULT_READ_TIMEOUT);
    // 设置代理, 一定要确保代理可用才启动此设置。 代理初始化也可用不加密的代理,new ProxyHostInfo(host, port);
    // ProxyHostInfo proxy = new ProxyHostInfo(host, port, username, password);
    // config.setProxy(proxy);
    return config;
  }


  /**
   * 一句话识别demo
   */
  private void shortDemo() {
    try {
      // 1. 初始化AsrCustomizationClient
      // 定义authInfo,根据ak,sk,region,projectId
      AuthInfo authInfo = new AuthInfo(ak, sk, region, projectId);
      // 设置config,主要与超时有关
      SisConfig config = getConfig();
      // 根据authInfo和config,构造AsrCustomizationClient
      AsrCustomizationClient asr = new AsrCustomizationClient(authInfo, config);

      // 2. 配置请求
      String data = IOUtils.getEncodeDataByPath(path);
      AsrCustomShortRequest request = new AsrCustomShortRequest(data, pathAudioFormat, pathProperty);
      // 设置请求参数,所有参数均为可选
      setShortParameter(request);

      // 3. 发送请求,获取响应
      AsrCustomShortResponse response = asr.getAsrShortResponse(request);
      // 打印结果
      System.out.println(JsonUtils.obj2Str(response, true));

    } catch (SisException e) {
      e.printStackTrace();
      System.out.println("error_code:" + e.getErrorCode() + "\nerror_msg:" + e.getErrorMsg());
    }
  }

  public static void main(String[] args) {
    AsrCustomizationDemo demo = new AsrCustomizationDemo();
    demo.shortDemo();
  }

}
support.huaweicloud.com/sdkreference-sis/sis_05_0044.html