npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

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

v1.0.9

Published

[![npm version](https://badge.fury.io/js/@ailabs-yating%2Fasr-respeaker-client-sdk-javascript.svg)](https://badge.fury.io/js/@ailabs-yating%2Fasr-respeaker-client-sdk-javascript)

Downloads

38

Readme

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即可執行