语音交互服务 SIS-http接口:请求示例

时间:2024-08-31 11:10:50

请求示例

“endpoint”即调用API的请求地址,不同服务不同区域的“endpoint”不同,具体请参见终端节点

  • 上传短音频,快速返回识别结果
    POST https://{endpoint}/v1/{project_id}/asr/short-audio
    
    Request Header:
    Content-Type: application/json
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...   
    
    Request body: 
    {
      "config":
      {
        "audio_format": "wav",
        "property": "chinese_8k_common",
        "add_punc": "yes",
        "need_word_info": "yes"
      },
      "data": "/+MgxAAUeHpMAUkQAANhuRAC..."
    }
  • 使用Python3语言上传短音频,快速返回识别结果
    # -*- coding: utf-8 -*-
    # 此demo仅供测试使用,强烈建议使用sdk。需提前安装requests,执行pip install requests
    import requests
    import base64
    import json
    
    def sasr_demo():
        url = 'https://{{endpoint}}/v1/{{project_id}}/asr/short-audio'  # endpoint和project_id需替换
        token = '用户对应region的token'
        file_path = '将要识别音频的路径'
        with open(file_path, 'rb') as f:
            data = f.read()
            base64_data = str(base64.b64encode(data), 'utf-8')
        header = {
            'Content-Type': 'application/json',
            'X-Auth-Token': token
        }
        body = {
            'data': base64_data,
            'config': {
                'property': 'chinese_8k_common',
                'audio_format': 'pcm8k16bit'
            }
        }
        resp = requests.post(url, data=json.dumps(body), headers=header)
        print(resp.text)
    
    if __name__ == '__main__':
        sasr_demo()
    
  • 使用Java语言上传短音频,快速返回识别结果
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    /**
     * 此demo仅供测试使用,强烈建议使用SDK
     */
    public class SasrDemo {
      public void sasrDemo() {
        try {
          // endpoint和projectId需要替换成实际信息。
          URL url = new URL("https://{{endpoint}}/v1/{{project_id}}/asr/short-audio");
          String token = "对应region的token";
          String audioBody = "8k wav格式audio对应base64编码";
          HttpURLConnection connection = (HttpURLConnection)url.openConnection();
          connection.setRequestMethod("POST");
          connection.setDoInput(true);
          connection.setDoOutput(true);
          connection.addRequestProperty("Content-Type", "application/json");
          connection.addRequestProperty("X-Auth-Token", token);
    
    
          OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
          String body = "{\"data\":\"" +  audioBody + "\", \"config\": { \"audio_format\": \"wav\", "
            + "\"property\":\"chinese_8k_common\"}}";
          osw.append(body);
          osw.flush();
          InputStream is = connection.getInputStream();
          BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
          while (br.ready()) {
            System.out.println(br.readLine());
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        SasrDemo sasrDemo = new SasrDemo();
        sasrDemo.sasrDemo();
      }
    }
support.huaweicloud.com/api-sis/sis_03_0094.html