语音交互服务 SIS-提交录音文件识别任务:请求示例

时间:2024-05-26 05:52:40

请求示例

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

  • 提交录音文件识别任务
    POST https://{endpoint}/v1/1609d8170cd86660a81281e5a3a03f8b/asr/transcriber/jobs
    
    Request Header:
    Content-Type: application/json
    X-Auth-Token: MIINRwYJKoZIhvcNAQcCoIINODCCDTQCAQExDTALBglghkgBZQMEAgEwgguVBgkqhkiG...
    
    Request Body:
    {
      "config":
      {
        "audio_format": "auto",
        "property": "chinese_8k_general",
        "add_punc": "yes",
        "need_analysis_info":
        {
          "diarization": true,
          "channel": "LEFT_AGENT",
          "emotion": true
        },
        "need_word_info": "yes"
      },
      "data_url":"https://****_voip.obs.myxxxcloud.com/VOIP/***.wav"
    }
  • 使用Python3语言提交录音 文字识别 任务
    # -*- coding: utf-8 -*-
    # 此demo仅供测试使用,强烈建议使用sdk。需提前安装requests,执行pip install requests
    import requests
    import time
    import json
    
    def lasr_demo():
        url = 'https://{{endpoint}}/v1/{{project_id}}/asr/transcriber/jobs'  # endpoint和project_id需替换
        token = '用户对应region的token'
        obs_url = 'obs路径'
        header = {
            'Content-Type': 'application/json',
            'X-Auth-Token': token
        }
        body = {
            'data_url': obs_url,
            'config': {
                'property': 'chinese_8k_general',
                'audio_format': 'auto'
            }
        }
        resp = requests.post(url, data=json.dumps(body), headers=header)
        job_id = json.loads(resp.text, encoding='utf-8')['job_id']
        query_task(header, job_id)
    def query_task(header, job_id):
        # endpoint和project_id需替换
        url = 'https://{{endpoint}}/v1/{{project_id}}/asr/transcriber/jobs/' + job_id
        for _ in range(1000):
            resp = requests.get(url, headers=header)
            print(resp.text)
            if resp.text.find('segments') != -1:
                break
            time.sleep(1)
    if __name__ == '__main__':
        lasr_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 LasrDemo {
      public void lasrDemo() {
        try {
          // endpoint和projectId需要替换成实际信息。
          URL url = new URL("https://{{endpoint}}/v1/{{project_id}}/asr/transcriber/jobs");
          String token = "对应region的token";
          String obsUrl = "音频在obs的路径";
          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_url\":\"" +  obsUrl + "\", \"config\": { \"audio_format\": \"auto\", "
            + "\"property\":\"chinese_8k_general\"}}";
          osw.append(body);
          osw.flush();
          InputStream is = connection.getInputStream();
          BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
          String jobId = "";
          while (br.ready()) {
            String line = br.readLine().trim();
            jobId = line.replace("{\"job_id\":\"", "").replace("\"}", "");
          }
          queryTask(token, jobId);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      private void queryTask(String token, String jobId) {
        try {
          // endpoint和projectId需要替换成实际信息。
          URL url = new URL("https://{{endpoint}}/v1/{{project_id}}/asr/transcriber/jobs/" + jobId);
          while (true) {
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.addRequestProperty("Content-Type", "application/json");
            connection.addRequestProperty("X-Auth-Token", token);
    
            InputStream is = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            if (br.ready()) {
              String line = br.readLine();
              System.out.println(line);
              if (line.contains("segments")) {
                break;
              }
            }
            Thread.sleep(2000);
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        LasrDemo lasrDemo = new LasrDemo();
        lasrDemo.lasrDemo();
      }
    }
support.huaweicloud.com/api-sis/sis_03_0092.html