@ailabs-yating/asr-respeaker-client-sdk-javascript
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

Yating ASR SDK - JavaScript

npm version

AILabs Yating ASR Respeaker Javascript SDK - 幫助使用者在前端介接音檔上傳語音辨識

可參考文件

注意: 此SDK只適用於前端

使用方法

import { AILabsYatingASRRespeaker } from "@ailabs-yating/asr-respeaker-client-sdk-javascript"

const sdk = new AILabsYatingASRRespeaker()

sdk.on('direction', (direction) => {
  console.log(`got direction: ${direction}`)
})

sdk.on('directionAppConnected', (isConnected) => {
  if(isConnected) {
    console.log('direction app connected!')
  } else {
    console.log('direction app NOT connected!')
  }
})

sdk.setupAudioDevices()
  .then(devices => {
    // 這邊可以選擇欲使用的麥克風device Id, 若不傳入則預設使用第一裝置
    // 若有偵測到respeaker, 且沒有傳入device Id, 則會自動選取
    return sdk.start(devices[0].deviceId)
  })
  .then(() => {
    console.log("record started!")
  })

const onFinishClicked = async () => {
  // 錄音檔(audioBlob), 方向紀錄(direction)可以自行存檔
  const { audioBlob, direction } = await sdk.finish()

  // 上傳音檔取得網址
  // 若有現有audio blob 也可以直接使用
  // const audioUrl = await sdk.uploadAudioBlob(audioBlob)
  const audioUrl = await sdk.uploadAudioBlob()

  // 開始轉譯音檔
  // 若有現有audio url 也可以直接使用
  // const asrUuid = await sdk.sendAsrTranscription("https://example.com/example.wav")
	const asrUuid = await sdk.sendAsrTranscription()

  while (true) {
    // loop取得asr結果, 如果有尚未完成辨識的uuid也可以帶入function
    // const asrResult = await sdk.getAsrTranscriptionResult("uuid")
    const asrResult = await sdk.getAsrTranscriptionResult()
    if (asrResult.status == 'completed') {
      break
    }
    if (asrResult.status == 'error') {
      throw Error('asr failed')
    }

    // retry later
    await new Promise(r => setTimeout(r, 2000))
  }

  // ASR轉譯結果送往表單辨識
  // 若有現有asrResult 也可以直接使用
  // const formResponseJson = await sdk.getFormTranscription(asrResult)
  const formResponseJson = await sdk.getFormTranscription()

  console.log(formResponseJson)

  // ASR轉譯結果送往Light Form辨識
  // 定義表單
  // 可以定義選擇題(choice), 填充題(fill_in), 是非題(yes_no)
  const lightFormDef = [
    {
      "nodeId": "pat_occupation",
      "description": "職業",
      "actionType": "choice",
      "options": [
        "無",
        "工",
        "商",
        "軍",
        "公",
        "教",
        "服務業",
        "養殖畜牧",
        "其他"
      ]
    },
    {
      "nodeId": "pat_language_code",
      "description": "語言",
      "actionType": "choice",
      "options": [
        "國",
        "台",
        "客家",
        "原住民",
        "英",
        "日",
        "其他"
      ]
    },
    {
      "nodeId": "pat_marital",
      "description": "婚姻狀況",
      "actionType": "choice",
      "options": [
        "未婚",
        "已婚",
        "鰥",
        "寡",
        "離婚"
      ]
    },
    {
      "nodeId": "pat_literacy",
      "description": "讀寫能力",
      "actionType": "choice",
      "options": [
        "識字",
        "不識字"
      ]
    },
    {
      "nodeId": "pat_education",
      "description": "教育程度",
      "actionType": "choice",
      "options": [
        "小學",
        "國中",
        "高中",
        "專科",
        "其他",
        "大學",
        "碩士",
        "博士"
      ]
    },
    {
      "nodeId": "pat_religion",
      "description": "宗教",
      "actionType": "choice",
      "options": [
        "無",
        "佛教",
        "道教",
        "基督教",
        "天主教",
        "回教",
        "其他"
      ]
    },
    {
      "nodeId": "pat_menstrual",
      "description": "月經史",
      "actionType": "choice",
      "options": [
        "停經",
        "有",
        "無",
        "不詳"
      ]
    },
    {
      "nodeId": "pat_menstrual_LMP",
      "description": "LMP(日期)",
      "actionType": "fill_in"
    },
    {
      "nodeId": "pat_bloodType",
      "description": "自述血型",
      "actionType": "choice",
      "options": [
        "A",
        "AB",
        "B",
        "O",
        "不詳"
      ]
    },
    {
      "nodeId": "pat_bloodType_TransReact",
      "description": "輸血反應",
      "actionType": "choice",
      "options": [
        "無",
        "有",
        "不詳"
      ]
    }
  ]
  // 若有現有asrResult也可以直接使用
  // const lightFromUuid = await sdk.sendLightFormFilling(lightFormDef, null, asrResult)
  const lightFromFillingUuid = await sdk.sendLightFormFilling(lightFormDef)
  let lightFromFillingResult
  while (true) {
    // loop取得light form結果, 如果有尚未完成辨識的uuid也可以帶入function
    // const lightFromFillingResult = await sdk.getLightFromFillingResult("uuid")
    lightFromFillingResult = await sdk.getLightFromFillingResult()
    if (lightFromFillingResult.status == 'completed') {
      break
    }
    if (lightFromFillingResult.status == 'error') {
      throw Error('light form filling failed')
    }

    // retry later
    await new Promise(r => setTimeout(r, 2000))
  }

  console.log(lightFromFillingResult)
}

setTimeout(onFinishClicked, 5 * 60 * 1000)

後端設定

由於CORS安全性規則以及確保API key安全, 您需要為sdk建立一個後端server將sdk request轉送至Yating ASR以及Agent, 所有欄位皆是選填, 預設設定如下:

new AILabsYatingASRRespeaker({
  host: "http://localhost",
  path: {
    transcription: "/transcriptions",
    transcriptionResult: "/transcriptions/:uid",
    upload: "/uploads",
    form: "/form",
    lightFormFilling: "/form/light",
    lightFormFillingResult: "/form/light/:uid",
  },
  headers: {},
  secrets: {
    asrAPIKey: "YOUR ASR API KEY",
    agentAPIKey: "YOUR Agent API KEY"
  },
  directionAppUrl: "ws://localhost:8989/ws",
  asrModelName: 'asr-zh-tw-std'
})

選項說明如下:

  • host: 後端server網址
  • path: 每個api對應後端path, SDK會送出正確格式的request body, 後端直接轉送即可

    若path以http://https://開頭, 則不會再加上host設定, 每個path都是獨立判斷

    • transcription
      • Method: POST
      • 對應: POST https://asr.api.yating.tw/v1/transcriptions
      • 需要ASR api key
        • (header) key=<asr api key>
        • 或是可以設定headers.asrAPIKey
    • transcriptionResult
      • Method: GET
      • 對應: GET https://asr.api.yating.tw/v1/transcriptions/:uid
      • SDK會將:uid自動替換為當前uid
      • 需要ASR api key (同上略)
    • upload
      • Method: POST
      • 對應: GET https://asr.api.yating.tw/v1/uploads
      • 需要ASR api key (同上略)
    • form
      • Method: POST
      • 對應: POST https://agent.api.yating.tw/v1/form/audio
      • 需要Agent api key
        • (header) Authorization=Bearer <agent api key>
        • 或是可以設定headers.agentAPIKey
    • lightFormFilling
      • Method: POST
      • 對應: POST https://agent.api.yating.tw/v1/form/light
      • 需要Agent api key (同上略)
    • transcriptionResult
      • Method: GET
      • 對應: GET https://agent.api.yating.tw/v1/form/light/:uid
      • SDK會將:uid自動替換為當前uid
      • 需要Agent api key (同上略)
  • headers: 可在此設定帶往後端request的headers
  • secrets: 可在此設定供後端使用的asr/agent api key
    • asrAPIKey: asr api key
    • agentAPIKey: agent api key
  • directionAppUrl: direction小程式websocket網址, 若無特別設定不需更改
    • 若要使用respeaker功能, 請執行direction小程式後插入respeaker裝置, 執行start()時就會有位置資訊
  • asrModelName: 預設為中台model, 可以使用的值請參考文件

Direction App 設定

由於瀏覽器無法直接存取ReSpeaker USB裝置, 我們需要在本機執行一個小程式(direction app)與sdk溝通

direction app使用時需要常駐啟動, 否則無法取得位置資訊

Mac OS 版本

  • 下載連結
  • 設定步驟
    1. 點擊上方連結下載direction_app
    2. 打開終端機(Terminal)並進入下載資料夾 cd ~/Downloads
    3. 執行chmod +x direction_app 讓系統知道這是一個可執行檔
    4. 點擊兩下direction_app執行, 這時系統會提示來自未識別的開發者
    5. 打開設定->安全性會看到"direction_app"遭到阻檔無法使用等字樣, 選取強制打開
    6. 點擊兩下direction_app即可執行 (或是在終端機中輸入./direction_app執行)

Windows 版本

  • 下載連結
  • 設定步驟
    1. 點擊上方連結下載direction_app
    2. 參考此處安裝ReSpeaker USB驅動程式
    3. 點兩下direction_app.exe即可執行

Readme

Keywords

none

Package Sidebar

Install

npm i @ailabs-yating/asr-respeaker-client-sdk-javascript

Weekly Downloads

3

Version

1.0.9

License

ISC

Unpacked Size

98 kB

Total Files

7

Last publish

Collaborators

  • gmin_chen